Best Capybara code snippet using Capybara.Node.Matchers._verify_selector_result
matchers.rb
Source:matchers.rb
...86 # @option options [Integer] :count (nil) Number of times the expression should occur87 # @raise [Capybara::ExpectationNotMet] If the selector does not exist88 #89 def assert_selector(*args, &optional_filter_block)90 _verify_selector_result(args, optional_filter_block) do |result, query|91 unless result.matches_count? && ((!result.empty?) || query.expects_none?)92 raise Capybara::ExpectationNotMet, result.failure_message93 end94 end95 end96 # Asserts that all of the provided selectors are present on the given page97 # or descendants of the current node. If options are provided, the assertion98 # will check that each locator is present with those options as well (other than :wait).99 #100 # page.assert_all_of_selectors(:custom, 'Tom', 'Joe', visible: all)101 # page.assert_all_of_selectors(:css, '#my_div', 'a.not_clicked')102 #103 # It accepts all options that {Capybara::Node::Finders#all} accepts,104 # such as :text and :visible.105 #106 # The :wait option applies to all of the selectors as a group, so all of the locators must be present107 # within :wait (Defaults to Capybara.default_max_wait_time) seconds.108 #109 # @overload assert_all_of_selectors([kind = Capybara.default_selector], *locators, options = {})110 #111 def assert_all_of_selectors(*args, &optional_filter_block)112 options = if args.last.is_a?(Hash) then args.pop.dup else {} end113 selector = if args.first.is_a?(Symbol) then args.shift else session_options.default_selector end114 wait = options.fetch(:wait, session_options.default_max_wait_time)115 synchronize(wait) do116 args.each do |locator|117 assert_selector(selector, locator, options, &optional_filter_block)118 end119 end120 end121 # Asserts that none of the provided selectors are present on the given page122 # or descendants of the current node. If options are provided, the assertion123 # will check that each locator is present with those options as well (other than :wait).124 #125 # page.assert_none_of_selectors(:custom, 'Tom', 'Joe', visible: all)126 # page.assert_none_of_selectors(:css, '#my_div', 'a.not_clicked')127 #128 # It accepts all options that {Capybara::Node::Finders#all} accepts,129 # such as :text and :visible.130 #131 # The :wait option applies to all of the selectors as a group, so none of the locators must be present132 # within :wait (Defaults to Capybara.default_max_wait_time) seconds.133 #134 # @overload assert_none_of_selectors([kind = Capybara.default_selector], *locators, options = {})135 #136 def assert_none_of_selectors(*args, &optional_filter_block)137 options = if args.last.is_a?(Hash) then args.pop.dup else {} end138 selector = if args.first.is_a?(Symbol) then args.shift else session_options.default_selector end139 wait = options.fetch(:wait, session_options.default_max_wait_time)140 synchronize(wait) do141 args.each do |locator|142 assert_no_selector(selector, locator, options, &optional_filter_block)143 end144 end145 end146 ##147 #148 # Asserts that a given selector is not on the page or a descendant of the current node.149 # Usage is identical to Capybara::Node::Matchers#assert_selector150 #151 # Query options such as :count, :minimum, :maximum, and :between are152 # considered to be an integral part of the selector. This will return153 # true, for example, if a page contains 4 anchors but the query expects 5:154 #155 # page.assert_no_selector('a', minimum: 1) # Found, raises Capybara::ExpectationNotMet156 # page.assert_no_selector('a', count: 4) # Found, raises Capybara::ExpectationNotMet157 # page.assert_no_selector('a', count: 5) # Not Found, returns true158 #159 # @param (see Capybara::Node::Finders#assert_selector)160 # @raise [Capybara::ExpectationNotMet] If the selector exists161 #162 def assert_no_selector(*args, &optional_filter_block)163 _verify_selector_result(args, optional_filter_block) do |result, query|164 if result.matches_count? && ((!result.empty?) || query.expects_none?)165 raise Capybara::ExpectationNotMet, result.negative_failure_message166 end167 end168 end169 alias_method :refute_selector, :assert_no_selector170 ##171 #172 # Checks if a given XPath expression is on the page or a descendant of the current node.173 #174 # page.has_xpath?('.//p[@id="foo"]')175 #176 # By default it will check if the expression occurs at least once,177 # but a different number can be specified.178 #179 # page.has_xpath?('.//p[@id="foo"]', count: 4)180 #181 # This will check if the expression occurs exactly 4 times.182 #183 # It also accepts all options that {Capybara::Node::Finders#all} accepts,184 # such as :text and :visible.185 #186 # page.has_xpath?('.//li', text: 'Horse', visible: true)187 #188 # has_xpath? can also accept XPath expressions generate by the189 # XPath gem:190 #191 # xpath = XPath.generate { |x| x.descendant(:p) }192 # page.has_xpath?(xpath)193 #194 # @param [String] path An XPath expression195 # @param options (see Capybara::Node::Finders#all)196 # @option options [Integer] :count (nil) Number of times the expression should occur197 # @return [Boolean] If the expression exists198 #199 def has_xpath?(path, options={}, &optional_filter_block)200 has_selector?(:xpath, path, options, &optional_filter_block)201 end202 ##203 #204 # Checks if a given XPath expression is not on the page or a descendant of the current node.205 # Usage is identical to Capybara::Node::Matchers#has_xpath?206 #207 # @param (see Capybara::Node::Finders#has_xpath?)208 # @return [Boolean]209 #210 def has_no_xpath?(path, options={}, &optional_filter_block)211 has_no_selector?(:xpath, path, options, &optional_filter_block)212 end213 ##214 #215 # Checks if a given CSS selector is on the page or a descendant of the current node.216 #217 # page.has_css?('p#foo')218 #219 # By default it will check if the selector occurs at least once,220 # but a different number can be specified.221 #222 # page.has_css?('p#foo', count: 4)223 #224 # This will check if the selector occurs exactly 4 times.225 #226 # It also accepts all options that {Capybara::Node::Finders#all} accepts,227 # such as :text and :visible.228 #229 # page.has_css?('li', text: 'Horse', visible: true)230 #231 # @param [String] path A CSS selector232 # @param options (see Capybara::Node::Finders#all)233 # @option options [Integer] :count (nil) Number of times the selector should occur234 # @return [Boolean] If the selector exists235 #236 def has_css?(path, options={}, &optional_filter_block)237 has_selector?(:css, path, options, &optional_filter_block)238 end239 ##240 #241 # Checks if a given CSS selector is not on the page or a descendant of the current node.242 # Usage is identical to Capybara::Node::Matchers#has_css?243 #244 # @param (see Capybara::Node::Finders#has_css?)245 # @return [Boolean]246 #247 def has_no_css?(path, options={}, &optional_filter_block)248 has_no_selector?(:css, path, options, &optional_filter_block)249 end250 ##251 #252 # Checks if the page or current node has a link with the given253 # text or id.254 #255 # @param [String] locator The text or id of a link to check for256 # @param options257 # @option options [String, Regexp] :href The value the href attribute must be258 # @return [Boolean] Whether it exists259 #260 def has_link?(locator=nil, options={}, &optional_filter_block)261 locator, options = nil, locator if locator.is_a? Hash262 has_selector?(:link, locator, options, &optional_filter_block)263 end264 ##265 #266 # Checks if the page or current node has no link with the given267 # text or id.268 #269 # @param (see Capybara::Node::Finders#has_link?)270 # @return [Boolean] Whether it doesn't exist271 #272 def has_no_link?(locator=nil, options={}, &optional_filter_block)273 locator, options = nil, locator if locator.is_a? Hash274 has_no_selector?(:link, locator, options, &optional_filter_block)275 end276 ##277 #278 # Checks if the page or current node has a button with the given279 # text, value or id.280 #281 # @param [String] locator The text, value or id of a button to check for282 # @return [Boolean] Whether it exists283 #284 def has_button?(locator=nil, options={}, &optional_filter_block)285 locator, options = nil, locator if locator.is_a? Hash286 has_selector?(:button, locator, options, &optional_filter_block)287 end288 ##289 #290 # Checks if the page or current node has no button with the given291 # text, value or id.292 #293 # @param [String] locator The text, value or id of a button to check for294 # @return [Boolean] Whether it doesn't exist295 #296 def has_no_button?(locator=nil, options={}, &optional_filter_block)297 locator, options = nil, locator if locator.is_a? Hash298 has_no_selector?(:button, locator, options, &optional_filter_block)299 end300 ##301 #302 # Checks if the page or current node has a form field with the given303 # label, name or id.304 #305 # For text fields and other textual fields, such as textareas and306 # HTML5 email/url/etc. fields, it's possible to specify a :with307 # option to specify the text the field should contain:308 #309 # page.has_field?('Name', with: 'Jonas')310 #311 # It is also possible to filter by the field type attribute:312 #313 # page.has_field?('Email', type: 'email')314 #315 # Note: 'textarea' and 'select' are valid type values, matching the associated tag names.316 #317 # @param [String] locator The label, name or id of a field to check for318 # @option options [String, Regexp] :with The text content of the field or a Regexp to match319 # @option options [String] :type The type attribute of the field320 # @return [Boolean] Whether it exists321 #322 def has_field?(locator=nil, options={}, &optional_filter_block)323 locator, options = nil, locator if locator.is_a? Hash324 has_selector?(:field, locator, options, &optional_filter_block)325 end326 ##327 #328 # Checks if the page or current node has no form field with the given329 # label, name or id. See {Capybara::Node::Matchers#has_field?}.330 #331 # @param [String] locator The label, name or id of a field to check for332 # @option options [String, Regexp] :with The text content of the field or a Regexp to match333 # @option options [String] :type The type attribute of the field334 # @return [Boolean] Whether it doesn't exist335 #336 def has_no_field?(locator=nil, options={}, &optional_filter_block)337 locator, options = nil, locator if locator.is_a? Hash338 has_no_selector?(:field, locator, options, &optional_filter_block)339 end340 ##341 #342 # Checks if the page or current node has a radio button or343 # checkbox with the given label, value or id, that is currently344 # checked.345 #346 # @param [String] locator The label, name or id of a checked field347 # @return [Boolean] Whether it exists348 #349 def has_checked_field?(locator=nil, options={}, &optional_filter_block)350 locator, options = nil, locator if locator.is_a? Hash351 has_selector?(:field, locator, options.merge(checked: true), &optional_filter_block)352 end353 ##354 #355 # Checks if the page or current node has no radio button or356 # checkbox with the given label, value or id, that is currently357 # checked.358 #359 # @param [String] locator The label, name or id of a checked field360 # @return [Boolean] Whether it doesn't exist361 #362 def has_no_checked_field?(locator=nil, options={}, &optional_filter_block)363 locator, options = nil, locator if locator.is_a? Hash364 has_no_selector?(:field, locator, options.merge(checked: true), &optional_filter_block)365 end366 ##367 #368 # Checks if the page or current node has a radio button or369 # checkbox with the given label, value or id, that is currently370 # unchecked.371 #372 # @param [String] locator The label, name or id of an unchecked field373 # @return [Boolean] Whether it exists374 #375 def has_unchecked_field?(locator=nil, options={}, &optional_filter_block)376 locator, options = nil, locator if locator.is_a? Hash377 has_selector?(:field, locator, options.merge(unchecked: true), &optional_filter_block)378 end379 ##380 #381 # Checks if the page or current node has no radio button or382 # checkbox with the given label, value or id, that is currently383 # unchecked.384 #385 # @param [String] locator The label, name or id of an unchecked field386 # @return [Boolean] Whether it doesn't exist387 #388 def has_no_unchecked_field?(locator=nil, options={}, &optional_filter_block)389 locator, options = nil, locator if locator.is_a? Hash390 has_no_selector?(:field, locator, options.merge(unchecked: true), &optional_filter_block)391 end392 ##393 #394 # Checks if the page or current node has a select field with the395 # given label, name or id.396 #397 # It can be specified which option should currently be selected:398 #399 # page.has_select?('Language', selected: 'German')400 #401 # For multiple select boxes, several options may be specified:402 #403 # page.has_select?('Language', selected: ['English', 'German'])404 #405 # It's also possible to check if the exact set of options exists for406 # this select box:407 #408 # page.has_select?('Language', options: ['English', 'German', 'Spanish'])409 #410 # You can also check for a partial set of options:411 #412 # page.has_select?('Language', with_options: ['English', 'German'])413 #414 # @param [String] locator The label, name or id of a select box415 # @option options [Array] :options Options which should be contained in this select box416 # @option options [Array] :with_options Partial set of options which should be contained in this select box417 # @option options [String, Array] :selected Options which should be selected418 # @option options [String, Array] :with_selected Partial set of options which should minimally be selected419 # @return [Boolean] Whether it exists420 #421 def has_select?(locator=nil, options={}, &optional_filter_block)422 locator, options = nil, locator if locator.is_a? Hash423 has_selector?(:select, locator, options, &optional_filter_block)424 end425 ##426 #427 # Checks if the page or current node has no select field with the428 # given label, name or id. See {Capybara::Node::Matchers#has_select?}.429 #430 # @param (see Capybara::Node::Matchers#has_select?)431 # @return [Boolean] Whether it doesn't exist432 #433 def has_no_select?(locator=nil, options={}, &optional_filter_block)434 locator, options = nil, locator if locator.is_a? Hash435 has_no_selector?(:select, locator, options, &optional_filter_block)436 end437 ##438 #439 # Checks if the page or current node has a table with the given id440 # or caption:441 #442 # page.has_table?('People')443 #444 # @param [String] locator The id or caption of a table445 # @return [Boolean] Whether it exist446 #447 def has_table?(locator=nil, options={}, &optional_filter_block)448 locator, options = nil, locator if locator.is_a? Hash449 has_selector?(:table, locator, options, &optional_filter_block)450 end451 ##452 #453 # Checks if the page or current node has no table with the given id454 # or caption. See {Capybara::Node::Matchers#has_table?}.455 #456 # @param (see Capybara::Node::Matchers#has_table?)457 # @return [Boolean] Whether it doesn't exist458 #459 def has_no_table?(locator=nil, options={}, &optional_filter_block)460 locator, options = nil, locator if locator.is_a? Hash461 has_no_selector?(:table, locator, options, &optional_filter_block)462 end463 ##464 #465 # Asserts that the current_node matches a given selector466 #467 # node.assert_matches_selector('p#foo')468 # node.assert_matches_selector(:xpath, '//p[@id="foo"]')469 # node.assert_matches_selector(:foo)470 #471 # It also accepts all options that {Capybara::Node::Finders#all} accepts,472 # such as :text and :visible.473 #474 # node.assert_matches_selector('li', text: 'Horse', visible: true)475 #476 # @param (see Capybara::Node::Finders#all)477 # @raise [Capybara::ExpectationNotMet] If the selector does not match478 #479 def assert_matches_selector(*args, &optional_filter_block)480 _verify_match_result(args, optional_filter_block) do |result|481 raise Capybara::ExpectationNotMet, "Item does not match the provided selector" unless result.include? self482 end483 end484 def assert_not_matches_selector(*args, &optional_filter_block)485 _verify_match_result(args, optional_filter_block) do |result|486 raise Capybara::ExpectationNotMet, 'Item matched the provided selector' if result.include? self487 end488 end489 alias_method :refute_matches_selector, :assert_not_matches_selector490 ##491 #492 # Checks if the current node matches given selector493 #494 # @param (see Capybara::Node::Finders#has_selector?)495 # @return [Boolean]496 #497 def matches_selector?(*args, &optional_filter_block)498 assert_matches_selector(*args, &optional_filter_block)499 rescue Capybara::ExpectationNotMet500 return false501 end502 ##503 #504 # Checks if the current node matches given XPath expression505 #506 # @param [String, XPath::Expression] xpath The XPath expression to match against the current code507 # @return [Boolean]508 #509 def matches_xpath?(xpath, options={}, &optional_filter_block)510 matches_selector?(:xpath, xpath, options, &optional_filter_block)511 end512 ##513 #514 # Checks if the current node matches given CSS selector515 #516 # @param [String] css The CSS selector to match against the current code517 # @return [Boolean]518 #519 def matches_css?(css, options={}, &optional_filter_block)520 matches_selector?(:css, css, options, &optional_filter_block)521 end522 ##523 #524 # Checks if the current node does not match given selector525 # Usage is identical to Capybara::Node::Matchers#has_selector?526 #527 # @param (see Capybara::Node::Finders#has_selector?)528 # @return [Boolean]529 #530 def not_matches_selector?(*args, &optional_filter_block)531 assert_not_matches_selector(*args, &optional_filter_block)532 rescue Capybara::ExpectationNotMet533 return false534 end535 ##536 #537 # Checks if the current node does not match given XPath expression538 #539 # @param [String, XPath::Expression] xpath The XPath expression to match against the current code540 # @return [Boolean]541 #542 def not_matches_xpath?(xpath, options={}, &optional_filter_block)543 not_matches_selector?(:xpath, xpath, options, &optional_filter_block)544 end545 ##546 #547 # Checks if the current node does not match given CSS selector548 #549 # @param [String] css The CSS selector to match against the current code550 # @return [Boolean]551 #552 def not_matches_css?(css, options={}, &optional_filter_block)553 not_matches_selector?(:css, css, options, &optional_filter_block)554 end555 ##556 # Asserts that the page or current node has the given text content,557 # ignoring any HTML tags.558 #559 # @!macro text_query_params560 # @overload $0(type, text, options = {})561 # @param [:all, :visible] type Whether to check for only visible or all text. If this parameter is missing or nil then we use the value of `Capybara.ignore_hidden_elements`, which defaults to `true`, corresponding to `:visible`.562 # @param [String, Regexp] text The string/regexp to check for. If it's a string, text is expected to include it. If it's a regexp, text is expected to match it.563 # @option options [Integer] :count (nil) Number of times the text is expected to occur564 # @option options [Integer] :minimum (nil) Minimum number of times the text is expected to occur565 # @option options [Integer] :maximum (nil) Maximum number of times the text is expected to occur566 # @option options [Range] :between (nil) Range of times that is expected to contain number of times text occurs567 # @option options [Numeric] :wait (Capybara.default_max_wait_time) Maximum time that Capybara will wait for text to eq/match given string/regexp argument568 # @option options [Boolean] :exact (Capybara.exact_text) Whether text must be an exact match or just substring569 # @overload $0(text, options = {})570 # @param [String, Regexp] text The string/regexp to check for. If it's a string, text is expected to include it. If it's a regexp, text is expected to match it.571 # @option options [Integer] :count (nil) Number of times the text is expected to occur572 # @option options [Integer] :minimum (nil) Minimum number of times the text is expected to occur573 # @option options [Integer] :maximum (nil) Maximum number of times the text is expected to occur574 # @option options [Range] :between (nil) Range of times that is expected to contain number of times text occurs575 # @option options [Numeric] :wait (Capybara.default_max_wait_time) Maximum time that Capybara will wait for text to eq/match given string/regexp argument576 # @option options [Boolean] :exact (Capybara.exact_text) Whether text must be an exact match or just substring577 # @raise [Capybara::ExpectationNotMet] if the assertion hasn't succeeded during wait time578 # @return [true]579 #580 def assert_text(*args)581 _verify_text(args) do |count, query|582 unless query.matches_count?(count) && ((count > 0) || query.expects_none?)583 raise Capybara::ExpectationNotMet, query.failure_message584 end585 end586 end587 ##588 # Asserts that the page or current node doesn't have the given text content,589 # ignoring any HTML tags.590 #591 # @macro text_query_params592 # @raise [Capybara::ExpectationNotMet] if the assertion hasn't succeeded during wait time593 # @return [true]594 #595 def assert_no_text(*args)596 _verify_text(args) do |count, query|597 if query.matches_count?(count) && ((count > 0) || query.expects_none?)598 raise Capybara::ExpectationNotMet, query.negative_failure_message599 end600 end601 end602 ##603 # Checks if the page or current node has the given text content,604 # ignoring any HTML tags.605 #606 # Whitespaces are normalized in both node's text and passed text parameter.607 # Note that whitespace isn't normalized in passed regexp as normalizing whitespace608 # in regexp isn't easy and doesn't seem to be worth it.609 #610 # By default it will check if the text occurs at least once,611 # but a different number can be specified.612 #613 # page.has_text?('lorem ipsum', between: 2..4)614 #615 # This will check if the text occurs from 2 to 4 times.616 #617 # @macro text_query_params618 # @return [Boolean] Whether it exists619 #620 def has_text?(*args)621 assert_text(*args)622 rescue Capybara::ExpectationNotMet623 return false624 end625 alias_method :has_content?, :has_text?626 ##627 # Checks if the page or current node does not have the given text628 # content, ignoring any HTML tags and normalizing whitespace.629 #630 # @macro text_query_params631 # @return [Boolean] Whether it doesn't exist632 #633 def has_no_text?(*args)634 assert_no_text(*args)635 rescue Capybara::ExpectationNotMet636 return false637 end638 alias_method :has_no_content?, :has_no_text?639 def ==(other)640 self.eql?(other) || (other.respond_to?(:base) && base == other.base)641 end642 private643 def _verify_selector_result(query_args, optional_filter_block, &result_block)644 _set_query_session_options(query_args)645 query = Capybara::Queries::SelectorQuery.new(*query_args, &optional_filter_block)646 synchronize(query.wait) do647 result = query.resolve_for(self)648 result_block.call(result, query)649 end650 return true651 end652 def _verify_match_result(query_args, optional_filter_block, &result_block)653 _set_query_session_options(query_args)654 query = Capybara::Queries::MatchQuery.new(*query_args, &optional_filter_block)655 synchronize(query.wait) do656 result = query.resolve_for(self.query_scope)657 result_block.call(result)...
_verify_selector_result
Using AI Code Generation
1 config.allow_url("google.com")2 config.allow_url("googleusercontent.com")3 config.allow_url("gstatic.com")4 config.allow_url("googleapis.com")5 config.allow_url("google.com")6 config.allow_url("googleusercontent.com")7 config.allow_url("gstatic.com")8 config.allow_url("googleapis.com")9 config.allow_url("google.com")10 config.allow_url("googleusercontent.com")11 config.allow_url("gstatic.com")12 config.allow_url("googleapis.com")13 config.allow_url("google.com")14 config.allow_url("googleusercontent.com")15 config.allow_url("gstatic.com")16 config.allow_url("googleapis.com")17 config.allow_url("google.com")18 config.allow_url("googleusercontent.com")19 config.allow_url("gstatic.com")20 config.allow_url("googleapis.com")21 config.allow_url("google
_verify_selector_result
Using AI Code Generation
1 Capybara::Poltergeist::Driver.new(app, {js_errors: false})2 Capybara::Webkit::Driver.new(app, {js_errors: false})3 Capybara::Selenium::Driver.new(app, {js_errors: false})4 let(:test_page) { TestPage.new }
_verify_selector_result
Using AI Code Generation
1def verify_selector(selector, message)2 _verify_selector_result(selector, message) do |result|3def verify_selector(selector, message)4 _verify_selector_result(selector, message) do |result|5def verify_selector(selector, message)6 _verify_selector_result(selector, message) do |result|7def verify_selector(selector, message)8 _verify_selector_result(selector, message) do |result|9def verify_selector(selector, message)10 _verify_selector_result(selector, message) do |result|11def verify_selector(selector, message)12 _verify_selector_result(selector, message) do |result|
_verify_selector_result
Using AI Code Generation
1 Capybara::Poltergeist::Driver.new(app, {js_errors: false})2 Capybara::Webkit::Driver.new(app, {js_errors: false})3 Capybara::Selenium::Driver.new(app, {js_errors: false})4 let(:test_page) { TestPage.new }
_verify_selector_result
Using AI Code Generation
1def verify_selector(selector, message)2 _verify_selector_result(selector, message) do |result|3def verify_selector(selector, message)4 _verify_selector_result(selector, message) do |result|5def verify_selector(selector, message)6 _verify_selector_result(selector, message) do |result|7def verify_selector(selector, message)8 _verify_selector_result(selector, message) do |result|9def verify_selector(selector, message)10 _verify_selector_result(selector, message) do |result|11def verify_selector(selector, message)12 _verify_selector_result(selector, message) do |result|
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!