How to use visible method of Capybara.Node Package

Best Capybara code snippet using Capybara.Node.visible

actions.rb

Source:actions.rb Github

copy

Full Screen

...80 find_options[:with] = currently_with if currently_with81 find(:fillable_field, locator, find_options).set(with, fill_options)82 end83 # @!macro label_click84 # @option options [Boolean] allow_label_click (Capybara.automatic_label_click) Attempt to click the label to toggle state if element is non-visible.85 ##86 #87 # Find a radio button and mark it as checked. The radio button can be found88 # via name, id or label text.89 #90 # page.choose('Male')91 #92 # @overload choose([locator], **options)93 # @param [String] locator Which radio button to choose94 #95 # @option options [String] option Value of the radio_button to choose96 # @option options [String] id Match fields that match the id attribute97 # @option options [String] name Match fields that match the name attribute98 # @option options [String, Array<String>] class Match fields that match the class(es) provided99 # @macro waiting_behavior100 # @macro label_click101 #102 # @return [Capybara::Node::Element] The element chosen or the label clicked103 def choose(locator = nil, **options)104 _check_with_label(:radio_button, true, locator, options)105 end106 ##107 #108 # Find a check box and mark it as checked. The check box can be found109 # via name, id or label text.110 #111 # page.check('German')112 #113 #114 # @overload check([locator], **options)115 # @param [String] locator Which check box to check116 #117 # @option options [String] option Value of the checkbox to select118 # @option options [String] id Match fields that match the id attribute119 # @option options [String] name Match fields that match the name attribute120 # @option options [String, Array<String>] class Match fields that match the class(es) provided121 # @macro label_click122 # @macro waiting_behavior123 #124 # @return [Capybara::Node::Element] The element checked or the label clicked125 def check(locator = nil, **options)126 _check_with_label(:checkbox, true, locator, options)127 end128 ##129 #130 # Find a check box and mark uncheck it. The check box can be found131 # via name, id or label text.132 #133 # page.uncheck('German')134 #135 #136 # @overload uncheck([locator], **options)137 # @param [String] locator Which check box to uncheck138 #139 # @option options [String] option Value of the checkbox to deselect140 # @option options [String] id Match fields that match the id attribute141 # @option options [String] name Match fields that match the name attribute142 # @option options [String, Array<String>] class Match fields that match the class(es) provided143 # @macro label_click144 # @macro waiting_behavior145 #146 # @return [Capybara::Node::Element] The element unchecked or the label clicked147 def uncheck(locator = nil, **options)148 _check_with_label(:checkbox, false, locator, options)149 end150 ##151 #152 # If `:from` option is present, `select` finds a select box, or text input with associated datalist,153 # on the page and selects a particular option from it.154 # Otherwise it finds an option inside current scope and selects it.155 # If the select box is a multiple select, +select+ can be called multiple times to select more than156 # one option.157 # The select box can be found via its name, id or label text. The option can be found by its text.158 #159 # page.select 'March', from: 'Month'160 #161 # @macro waiting_behavior162 #163 # @param value [String] Which option to select164 # @param from: [String] The id, Capybara.test_id atrtribute, name or label of the select box165 #166 # @return [Capybara::Node::Element] The option element selected167 def select(value = nil, from: nil, **options)168 el = from ? find_select_or_datalist_input(from, options) : self169 if el.respond_to?(:tag_name) && (el.tag_name == 'input')170 select_datalist_option(el, value)171 else172 el.find(:option, value, options).select_option173 end174 end175 ##176 #177 # Find a select box on the page and unselect a particular option from it. If the select178 # box is a multiple select, +unselect+ can be called multiple times to unselect more than179 # one option. The select box can be found via its name, id or label text.180 #181 # page.unselect 'March', from: 'Month'182 #183 # @macro waiting_behavior184 #185 # @param value [String] Which option to unselect186 # @param from: [String] The id, Capybara.test_id attribute, name or label of the select box187 #188 # @return [Capybara::Node::Element] The option element unselected189 def unselect(value = nil, from: nil, **options)190 scope = from ? find(:select, from, options) : self191 scope.find(:option, value, options).unselect_option192 end193 ##194 #195 # Find a file field on the page and attach a file given its path. The file field can196 # be found via its name, id or label text. In the case of the file field being hidden for197 # styling reasons the `make_visible` option can be used to temporarily change the CSS of198 # the file field, attach the file, and then revert the CSS back to original.199 #200 # page.attach_file(locator, '/path/to/file.png')201 #202 # @overload attach_file([locator], path, **options)203 # @macro waiting_behavior204 #205 # @param [String] locator Which field to attach the file to206 # @param [String] path The path of the file that will be attached, or an array of paths207 #208 # @option options [Symbol] match (Capybara.match) The matching strategy to use (:one, :first, :prefer_exact, :smart).209 # @option options [Boolean] exact (Capybara.exact) Match the exact label name/contents or accept a partial match.210 # @option options [Boolean] multiple Match field which allows multiple file selection211 # @option options [String] id Match fields that match the id attribute212 # @option options [String] name Match fields that match the name attribute213 # @option options [String, Array<String>] class Match fields that match the class(es) provided214 # @option options [true, Hash] make_visible A Hash of CSS styles to change before attempting to attach the file, if `true` { opacity: 1, display: 'block', visibility: 'visible' } is used (may not be supported by all drivers)215 #216 # @return [Capybara::Node::Element] The file field element217 def attach_file(locator = nil, path, make_visible: nil, **options) # rubocop:disable Style/OptionalArguments218 Array(path).each do |p|219 raise Capybara::FileNotFound, "cannot attach file, #{p} does not exist" unless File.exist?(p.to_s)220 end221 # Allow user to update the CSS style of the file input since they are so often hidden on a page222 if make_visible223 ff = find(:file_field, locator, options.merge(visible: :all))224 while_visible(ff, make_visible) { |el| el.set(path) }225 else226 find(:file_field, locator, options).set(path)227 end228 end229 private230 def find_select_or_datalist_input(from, options)231 synchronize(Capybara::Queries::BaseQuery.wait(options, session_options.default_max_wait_time)) do232 begin233 find(:select, from, options)234 rescue Capybara::ElementNotFound => select_error235 raise if %i[selected with_selected multiple].any? { |option| options.key?(option) }236 begin237 find(:datalist_input, from, options)238 rescue Capybara::ElementNotFound => dlinput_error239 raise Capybara::ElementNotFound, "#{select_error.message} and #{dlinput_error.message}"240 end241 end242 end243 end244 def select_datalist_option(input, value)245 datalist_options = input.evaluate_script(DATALIST_OPTIONS_SCRIPT)246 option = datalist_options.find { |o| o.values_at('value', 'label').include?(value) }247 raise ::Capybara::ElementNotFound, %(Unable to find datalist option "#{value}") unless option248 input.set(option['value'])249 rescue ::Capybara::NotSupportedByDriverError250 # Implement for drivers that don't support JS251 datalist = find(:xpath, XPath.descendant(:datalist)[XPath.attr(:id) == input[:list]], visible: false)252 option = datalist.find(:datalist_option, value, disabled: false)253 input.set(option.value)254 end255 def while_visible(element, visible_css)256 visible_css = { opacity: 1, display: 'block', visibility: 'visible' } if visible_css == true257 _update_style(element, visible_css)258 raise ExpectationNotMet, 'The style changes in :make_visible did not make the file input visible' unless element.visible?259 begin260 yield element261 ensure262 _reset_style(element)263 end264 end265 def _update_style(element, style)266 element.execute_script(UPDATE_STYLE_SCRIPT, style)267 rescue Capybara::NotSupportedByDriverError268 warn 'The :make_visible option is not supported by the current driver - ignoring'269 end270 def _reset_style(element)271 element.execute_script(RESET_STYLE_SCRIPT)272 rescue StandardError # rubocop:disable Lint/HandleExceptions swallow extra errors273 end274 def _check_with_label(selector, checked, locator, allow_label_click: session_options.automatic_label_click, **options)275 synchronize(Capybara::Queries::BaseQuery.wait(options, session_options.default_max_wait_time)) do276 begin277 el = find(selector, locator, options)278 el.set(checked)279 rescue StandardError => e280 raise unless allow_label_click && catch_error?(e)281 begin282 el ||= find(selector, locator, options.merge(visible: :all))283 find(:label, for: el, visible: true).click unless el.checked? == checked284 rescue StandardError # swallow extra errors - raise original285 raise e286 end287 end288 end289 end290 UPDATE_STYLE_SCRIPT = <<~'JS'291 this.capybara_style_cache = this.style.cssText;292 var css = arguments[0];293 for (var prop in css){294 if (css.hasOwnProperty(prop)) {295 this.style[prop] = css[prop]296 }297 }...

Full Screen

Full Screen

query.rb

Source:query.rb Github

copy

Full Screen

2 # @deprecated This class and its methods are not supposed to be used by users of Capybara's public API.3 # It may be removed in future versions of Capybara.4 class Query < Queries::BaseQuery5 attr_accessor :selector, :locator, :options, :expression, :find, :negative6 VALID_KEYS = [:text, :visible, :between, :count, :maximum, :minimum, :exact, :match, :wait]7 VALID_MATCH = [:first, :smart, :prefer_exact, :one]8 def initialize(*args)9 @options = if args.last.is_a?(Hash) then args.pop.dup else {} end10 if args[0].is_a?(Symbol)11 @selector = Selector.all[args[0]]12 @locator = args[1]13 else14 @selector = Selector.all.values.find { |s| s.match?(args[0]) }15 @locator = args[0]16 end17 @selector ||= Selector.all[Capybara.default_selector]18 # for compatibility with Capybara 2.019 if Capybara.exact_options and @selector == Selector.all[:option]20 @options[:exact] = true21 end22 @expression = @selector.call(@locator)23 assert_valid_keys24 end25 def name; selector.name; end26 def label; selector.label or selector.name; end27 def description28 @description = "#{label} #{locator.inspect}"29 @description << " with text #{options[:text].inspect}" if options[:text]30 @description << selector.description(options)31 @description32 end33 def matches_filters?(node)34 if options[:text]35 regexp = options[:text].is_a?(Regexp) ? options[:text] : Regexp.escape(options[:text].to_s)36 return false if not node.text(visible).match(regexp)37 end38 case visible39 when :visible then return false unless node.visible?40 when :hidden then return false if node.visible?41 end42 selector.custom_filters.each do |name, filter|43 if options.has_key?(name)44 return false unless filter.matches?(node, options[name])45 elsif filter.default?46 return false unless filter.matches?(node, filter.default)47 end48 end49 end50 def visible51 if options.has_key?(:visible)52 case @options[:visible]53 when true then :visible54 when false then :all55 else @options[:visible]56 end57 else58 if Capybara.ignore_hidden_elements59 :visible60 else61 :all62 end63 end64 end65 def exact?66 if options.has_key?(:exact)67 @options[:exact]68 else69 Capybara.exact70 end71 end72 def match73 if options.has_key?(:match)74 @options[:match]75 else76 Capybara.match77 end78 end79 def xpath(exact=nil)80 exact = self.exact? if exact == nil81 if @expression.respond_to?(:to_xpath) and exact82 @expression.to_xpath(:exact)83 else84 @expression.to_s85 end86 end87 def css88 @expression89 end90 # @api private91 def resolve_for(node, exact = nil)92 node.synchronize do93 children = if selector.format == :css94 node.find_css(self.css)95 else96 node.find_xpath(self.xpath(exact))97 end.map do |child|98 if node.is_a?(Capybara::Node::Base)99 Capybara::Node::Element.new(node.session, child, node, self)100 else101 Capybara::Node::Simple.new(child)102 end103 end104 Capybara::Result.new(children, self)105 end106 end107 private108 def valid_keys109 COUNT_KEYS + [:text, :visible, :exact, :match, :wait] + @selector.custom_filters.keys110 end111 def assert_valid_keys112 super113 unless VALID_MATCH.include?(match)114 raise ArgumentError, "invalid option #{match.inspect} for :match, should be one of #{VALID_MATCH.map(&:inspect).join(", ")}"115 end116 end117 end118end...

Full Screen

Full Screen

visible

Using AI Code Generation

copy

Full Screen

1 !self['style'].match(/display:\s*none/)2Capybara.all('input').each do |i|3Capybara.all('input').each do |i|4Capybara.all('input').each do |i|5Capybara.all('input').each do |i|

Full Screen

Full Screen

visible

Using AI Code Generation

copy

Full Screen

1 !self[:style].match(/display:\s*none/)2Capybara::Session.new(:poltergeist).visit('/').tap do |page|3 puts page.find(:css, 'body').visible?

Full Screen

Full Screen

visible

Using AI Code Generation

copy

Full Screen

1 puts page.find(:css, 'input[type="submit"]').visible?2<SELECT NAME="x" ONCHANGE="javascript:submit();">3page.select('2', :from => 'x')4page.select('2', :from => 'x', :match => :first)5page.select('2', :from => 'x', :match => :prefer_exact)6page.select('2', :from => 'x', :match => :first, :exact => true)7page.select('2', :from => 'x', :match => :prefer

Full Screen

Full Screen

visible

Using AI Code Generation

copy

Full Screen

1 visit('/')2 fill_in('q', :with => 'capybara')3 click_button('Google Search')4 if page.has_content?('Capybara')5 visit('/')6 fill_in('q', :with => 'capybara')7 click_button('Google Search')

Full Screen

Full Screen

visible

Using AI Code Generation

copy

Full Screen

1 def find(*args)2 self.find(*args)3 def visit(path)4 self.visit(path)5 def find(*args)6 self.find(*args)7visit('/')8find(:xpath, "//a[text()='link']").click9visit('/')10click_link('link')11visit('/')12click_link('link')

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful