How to use extract_selector method of Capybara.Node.Matchers Package

Best Capybara code snippet using Capybara.Node.Matchers.extract_selector

matchers.rb

Source:matchers.rb Github

copy

Full Screen

...140 # @overload assert_all_of_selectors([kind = Capybara.default_selector], *locators, **options)141 #142 def assert_all_of_selectors(*args, wait: nil, **options, &optional_filter_block)143 wait = session_options.default_max_wait_time if wait.nil?144 selector = extract_selector(args)145 synchronize(wait) do146 args.each do |locator|147 assert_selector(selector, locator, options, &optional_filter_block)148 end149 end150 end151 # Asserts that none of the provided selectors are present on the given page152 # or descendants of the current node. If options are provided, the assertion153 # will check that each locator is present with those options as well (other than :wait).154 #155 # page.assert_none_of_selectors(:custom, 'Tom', 'Joe', visible: all)156 # page.assert_none_of_selectors(:css, '#my_div', 'a.not_clicked')157 #158 # It accepts all options that {Capybara::Node::Finders#all} accepts,159 # such as :text and :visible.160 #161 # The :wait option applies to all of the selectors as a group, so none of the locators must be present162 # within :wait (Defaults to Capybara.default_max_wait_time) seconds.163 #164 # @overload assert_none_of_selectors([kind = Capybara.default_selector], *locators, **options)165 #166 def assert_none_of_selectors(*args, wait: nil, **options, &optional_filter_block)167 wait = session_options.default_max_wait_time if wait.nil?168 selector = extract_selector(args)169 synchronize(wait) do170 args.each do |locator|171 assert_no_selector(selector, locator, options, &optional_filter_block)172 end173 end174 end175 ##176 #177 # Asserts that a given selector is not on the page or a descendant of the current node.178 # Usage is identical to Capybara::Node::Matchers#assert_selector179 #180 # Query options such as :count, :minimum, :maximum, and :between are181 # considered to be an integral part of the selector. This will return182 # true, for example, if a page contains 4 anchors but the query expects 5:183 #184 # page.assert_no_selector('a', minimum: 1) # Found, raises Capybara::ExpectationNotMet185 # page.assert_no_selector('a', count: 4) # Found, raises Capybara::ExpectationNotMet186 # page.assert_no_selector('a', count: 5) # Not Found, returns true187 #188 # @param (see Capybara::Node::Finders#assert_selector)189 # @raise [Capybara::ExpectationNotMet] If the selector exists190 #191 def assert_no_selector(*args, &optional_filter_block)192 _verify_selector_result(args, optional_filter_block) do |result, query|193 if result.matches_count? && (!result.empty? || query.expects_none?)194 raise Capybara::ExpectationNotMet, result.negative_failure_message195 end196 end197 end198 alias_method :refute_selector, :assert_no_selector199 ##200 #201 # Checks if a given XPath expression is on the page or a descendant of the current node.202 #203 # page.has_xpath?('.//p[@id="foo"]')204 #205 # By default it will check if the expression occurs at least once,206 # but a different number can be specified.207 #208 # page.has_xpath?('.//p[@id="foo"]', count: 4)209 #210 # This will check if the expression occurs exactly 4 times.211 #212 # It also accepts all options that {Capybara::Node::Finders#all} accepts,213 # such as :text and :visible.214 #215 # page.has_xpath?('.//li', text: 'Horse', visible: true)216 #217 # has_xpath? can also accept XPath expressions generate by the218 # XPath gem:219 #220 # xpath = XPath.generate { |x| x.descendant(:p) }221 # page.has_xpath?(xpath)222 #223 # @param [String] path An XPath expression224 # @param options (see Capybara::Node::Finders#all)225 # @option options [Integer] :count (nil) Number of times the expression should occur226 # @return [Boolean] If the expression exists227 #228 def has_xpath?(path, **options, &optional_filter_block)229 has_selector?(:xpath, path, options, &optional_filter_block)230 end231 ##232 #233 # Checks if a given XPath expression is not on the page or a descendant of the current node.234 # Usage is identical to Capybara::Node::Matchers#has_xpath?235 #236 # @param (see Capybara::Node::Finders#has_xpath?)237 # @return [Boolean]238 #239 def has_no_xpath?(path, **options, &optional_filter_block)240 has_no_selector?(:xpath, path, options, &optional_filter_block)241 end242 ##243 #244 # Checks if a given CSS selector is on the page or a descendant of the current node.245 #246 # page.has_css?('p#foo')247 #248 # By default it will check if the selector occurs at least once,249 # but a different number can be specified.250 #251 # page.has_css?('p#foo', count: 4)252 #253 # This will check if the selector occurs exactly 4 times.254 #255 # It also accepts all options that {Capybara::Node::Finders#all} accepts,256 # such as :text and :visible.257 #258 # page.has_css?('li', text: 'Horse', visible: true)259 #260 # @param [String] path A CSS selector261 # @param options (see Capybara::Node::Finders#all)262 # @option options [Integer] :count (nil) Number of times the selector should occur263 # @return [Boolean] If the selector exists264 #265 def has_css?(path, **options, &optional_filter_block)266 has_selector?(:css, path, options, &optional_filter_block)267 end268 ##269 #270 # Checks if a given CSS selector is not on the page or a descendant of the current node.271 # Usage is identical to Capybara::Node::Matchers#has_css?272 #273 # @param (see Capybara::Node::Finders#has_css?)274 # @return [Boolean]275 #276 def has_no_css?(path, **options, &optional_filter_block)277 has_no_selector?(:css, path, options, &optional_filter_block)278 end279 ##280 #281 # Checks if the page or current node has a link with the given282 # text or id.283 #284 # @param [String] locator The text or id of a link to check for285 # @param options286 # @option options [String, Regexp] :href The value the href attribute must be287 # @return [Boolean] Whether it exists288 #289 def has_link?(locator = nil, **options, &optional_filter_block)290 has_selector?(:link, locator, options, &optional_filter_block)291 end292 ##293 #294 # Checks if the page or current node has no link with the given295 # text or id.296 #297 # @param (see Capybara::Node::Finders#has_link?)298 # @return [Boolean] Whether it doesn't exist299 #300 def has_no_link?(locator = nil, **options, &optional_filter_block)301 has_no_selector?(:link, locator, options, &optional_filter_block)302 end303 ##304 #305 # Checks if the page or current node has a button with the given306 # text, value or id.307 #308 # @param [String] locator The text, value or id of a button to check for309 # @return [Boolean] Whether it exists310 #311 def has_button?(locator = nil, **options, &optional_filter_block)312 has_selector?(:button, locator, options, &optional_filter_block)313 end314 ##315 #316 # Checks if the page or current node has no button with the given317 # text, value or id.318 #319 # @param [String] locator The text, value or id of a button to check for320 # @return [Boolean] Whether it doesn't exist321 #322 def has_no_button?(locator = nil, **options, &optional_filter_block)323 has_no_selector?(:button, locator, options, &optional_filter_block)324 end325 ##326 #327 # Checks if the page or current node has a form field with the given328 # label, name or id.329 #330 # For text fields and other textual fields, such as textareas and331 # HTML5 email/url/etc. fields, it's possible to specify a :with332 # option to specify the text the field should contain:333 #334 # page.has_field?('Name', with: 'Jonas')335 #336 # It is also possible to filter by the field type attribute:337 #338 # page.has_field?('Email', type: 'email')339 #340 # Note: 'textarea' and 'select' are valid type values, matching the associated tag names.341 #342 # @param [String] locator The label, name or id of a field to check for343 # @option options [String, Regexp] :with The text content of the field or a Regexp to match344 # @option options [String] :type The type attribute of the field345 # @return [Boolean] Whether it exists346 #347 def has_field?(locator = nil, **options, &optional_filter_block)348 has_selector?(:field, locator, options, &optional_filter_block)349 end350 ##351 #352 # Checks if the page or current node has no form field with the given353 # label, name or id. See {Capybara::Node::Matchers#has_field?}.354 #355 # @param [String] locator The label, name or id of a field to check for356 # @option options [String, Regexp] :with The text content of the field or a Regexp to match357 # @option options [String] :type The type attribute of the field358 # @return [Boolean] Whether it doesn't exist359 #360 def has_no_field?(locator = nil, **options, &optional_filter_block)361 has_no_selector?(:field, locator, options, &optional_filter_block)362 end363 ##364 #365 # Checks if the page or current node has a radio button or366 # checkbox with the given label, value, id, or Capybara.test_id attribute that is currently367 # checked.368 #369 # @param [String] locator The label, name or id of a checked field370 # @return [Boolean] Whether it exists371 #372 def has_checked_field?(locator = nil, **options, &optional_filter_block)373 has_selector?(:field, locator, options.merge(checked: true), &optional_filter_block)374 end375 ##376 #377 # Checks if the page or current node has no radio button or378 # checkbox with the given label, value or id, or Capybara.test_id attribute that is currently379 # checked.380 #381 # @param [String] locator The label, name or id of a checked field382 # @return [Boolean] Whether it doesn't exist383 #384 def has_no_checked_field?(locator = nil, **options, &optional_filter_block)385 has_no_selector?(:field, locator, options.merge(checked: true), &optional_filter_block)386 end387 ##388 #389 # Checks if the page or current node has a radio button or390 # checkbox with the given label, value or id, or Capybara.test_id attribute that is currently391 # unchecked.392 #393 # @param [String] locator The label, name or id of an unchecked field394 # @return [Boolean] Whether it exists395 #396 def has_unchecked_field?(locator = nil, **options, &optional_filter_block)397 has_selector?(:field, locator, options.merge(unchecked: true), &optional_filter_block)398 end399 ##400 #401 # Checks if the page or current node has no radio button or402 # checkbox with the given label, value or id, or Capybara.test_id attribute that is currently403 # unchecked.404 #405 # @param [String] locator The label, name or id of an unchecked field406 # @return [Boolean] Whether it doesn't exist407 #408 def has_no_unchecked_field?(locator = nil, **options, &optional_filter_block)409 has_no_selector?(:field, locator, options.merge(unchecked: true), &optional_filter_block)410 end411 ##412 #413 # Checks if the page or current node has a select field with the414 # given label, name or id.415 #416 # It can be specified which option should currently be selected:417 #418 # page.has_select?('Language', selected: 'German')419 #420 # For multiple select boxes, several options may be specified:421 #422 # page.has_select?('Language', selected: ['English', 'German'])423 #424 # It's also possible to check if the exact set of options exists for425 # this select box:426 #427 # page.has_select?('Language', options: ['English', 'German', 'Spanish'])428 #429 # You can also check for a partial set of options:430 #431 # page.has_select?('Language', with_options: ['English', 'German'])432 #433 # @param [String] locator The label, name or id of a select box434 # @option options [Array] :options Options which should be contained in this select box435 # @option options [Array] :with_options Partial set of options which should be contained in this select box436 # @option options [String, Array] :selected Options which should be selected437 # @option options [String, Array] :with_selected Partial set of options which should minimally be selected438 # @return [Boolean] Whether it exists439 #440 def has_select?(locator = nil, **options, &optional_filter_block)441 has_selector?(:select, locator, options, &optional_filter_block)442 end443 ##444 #445 # Checks if the page or current node has no select field with the446 # given label, name or id. See {Capybara::Node::Matchers#has_select?}.447 #448 # @param (see Capybara::Node::Matchers#has_select?)449 # @return [Boolean] Whether it doesn't exist450 #451 def has_no_select?(locator = nil, **options, &optional_filter_block)452 has_no_selector?(:select, locator, options, &optional_filter_block)453 end454 ##455 #456 # Checks if the page or current node has a table with the given id457 # or caption:458 #459 # page.has_table?('People')460 #461 # @param [String] locator The id or caption of a table462 # @return [Boolean] Whether it exist463 #464 def has_table?(locator = nil, **options, &optional_filter_block)465 has_selector?(:table, locator, options, &optional_filter_block)466 end467 ##468 #469 # Checks if the page or current node has no table with the given id470 # or caption. See {Capybara::Node::Matchers#has_table?}.471 #472 # @param (see Capybara::Node::Matchers#has_table?)473 # @return [Boolean] Whether it doesn't exist474 #475 def has_no_table?(locator = nil, **options, &optional_filter_block)476 has_no_selector?(:table, locator, options, &optional_filter_block)477 end478 ##479 #480 # Asserts that the current_node matches a given selector481 #482 # node.assert_matches_selector('p#foo')483 # node.assert_matches_selector(:xpath, '//p[@id="foo"]')484 # node.assert_matches_selector(:foo)485 #486 # It also accepts all options that {Capybara::Node::Finders#all} accepts,487 # such as :text and :visible.488 #489 # node.assert_matches_selector('li', text: 'Horse', visible: true)490 #491 # @param (see Capybara::Node::Finders#all)492 # @raise [Capybara::ExpectationNotMet] If the selector does not match493 #494 def assert_matches_selector(*args, &optional_filter_block)495 _verify_match_result(args, optional_filter_block) do |result|496 raise Capybara::ExpectationNotMet, 'Item does not match the provided selector' unless result.include? self497 end498 end499 def assert_not_matches_selector(*args, &optional_filter_block)500 _verify_match_result(args, optional_filter_block) do |result|501 raise Capybara::ExpectationNotMet, 'Item matched the provided selector' if result.include? self502 end503 end504 alias_method :refute_matches_selector, :assert_not_matches_selector505 ##506 #507 # Checks if the current node matches given selector508 #509 # @param (see Capybara::Node::Finders#has_selector?)510 # @return [Boolean]511 #512 def matches_selector?(*args, &optional_filter_block)513 assert_matches_selector(*args, &optional_filter_block)514 rescue Capybara::ExpectationNotMet515 false516 end517 ##518 #519 # Checks if the current node matches given XPath expression520 #521 # @param [String, XPath::Expression] xpath The XPath expression to match against the current code522 # @return [Boolean]523 #524 def matches_xpath?(xpath, **options, &optional_filter_block)525 matches_selector?(:xpath, xpath, options, &optional_filter_block)526 end527 ##528 #529 # Checks if the current node matches given CSS selector530 #531 # @param [String] css The CSS selector to match against the current code532 # @return [Boolean]533 #534 def matches_css?(css, **options, &optional_filter_block)535 matches_selector?(:css, css, options, &optional_filter_block)536 end537 ##538 #539 # Checks if the current node does not match given selector540 # Usage is identical to Capybara::Node::Matchers#has_selector?541 #542 # @param (see Capybara::Node::Finders#has_selector?)543 # @return [Boolean]544 #545 def not_matches_selector?(*args, &optional_filter_block)546 assert_not_matches_selector(*args, &optional_filter_block)547 rescue Capybara::ExpectationNotMet548 false549 end550 ##551 #552 # Checks if the current node does not match given XPath expression553 #554 # @param [String, XPath::Expression] xpath The XPath expression to match against the current code555 # @return [Boolean]556 #557 def not_matches_xpath?(xpath, **options, &optional_filter_block)558 not_matches_selector?(:xpath, xpath, options, &optional_filter_block)559 end560 ##561 #562 # Checks if the current node does not match given CSS selector563 #564 # @param [String] css The CSS selector to match against the current code565 # @return [Boolean]566 #567 def not_matches_css?(css, **options, &optional_filter_block)568 not_matches_selector?(:css, css, options, &optional_filter_block)569 end570 ##571 # Asserts that the page or current node has the given text content,572 # ignoring any HTML tags.573 #574 # @!macro text_query_params575 # @overload $0(type, text, **options)576 # @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`.577 # @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.578 # @option options [Integer] :count (nil) Number of times the text is expected to occur579 # @option options [Integer] :minimum (nil) Minimum number of times the text is expected to occur580 # @option options [Integer] :maximum (nil) Maximum number of times the text is expected to occur581 # @option options [Range] :between (nil) Range of times that is expected to contain number of times text occurs582 # @option options [Numeric] :wait (Capybara.default_max_wait_time) Maximum time that Capybara will wait for text to eq/match given string/regexp argument583 # @option options [Boolean] :exact (Capybara.exact_text) Whether text must be an exact match or just substring584 # @overload $0(text, **options)585 # @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.586 # @option options [Integer] :count (nil) Number of times the text is expected to occur587 # @option options [Integer] :minimum (nil) Minimum number of times the text is expected to occur588 # @option options [Integer] :maximum (nil) Maximum number of times the text is expected to occur589 # @option options [Range] :between (nil) Range of times that is expected to contain number of times text occurs590 # @option options [Numeric] :wait (Capybara.default_max_wait_time) Maximum time that Capybara will wait for text to eq/match given string/regexp argument591 # @option options [Boolean] :exact (Capybara.exact_text) Whether text must be an exact match or just substring592 # @raise [Capybara::ExpectationNotMet] if the assertion hasn't succeeded during wait time593 # @return [true]594 #595 def assert_text(*args)596 _verify_text(args) do |count, query|597 unless query.matches_count?(count) && (count.positive? || query.expects_none?)598 raise Capybara::ExpectationNotMet, query.failure_message599 end600 end601 end602 ##603 # Asserts that the page or current node doesn't have the given text content,604 # ignoring any HTML tags.605 #606 # @macro text_query_params607 # @raise [Capybara::ExpectationNotMet] if the assertion hasn't succeeded during wait time608 # @return [true]609 #610 def assert_no_text(*args)611 _verify_text(args) do |count, query|612 if query.matches_count?(count) && (count.positive? || query.expects_none?)613 raise Capybara::ExpectationNotMet, query.negative_failure_message614 end615 end616 end617 ##618 # Checks if the page or current node has the given text content,619 # ignoring any HTML tags.620 #621 # By default it will check if the text occurs at least once,622 # but a different number can be specified.623 #624 # page.has_text?('lorem ipsum', between: 2..4)625 #626 # This will check if the text occurs from 2 to 4 times.627 #628 # @macro text_query_params629 # @return [Boolean] Whether it exists630 #631 def has_text?(*args)632 assert_text(*args)633 rescue Capybara::ExpectationNotMet634 false635 end636 alias_method :has_content?, :has_text?637 ##638 # Checks if the page or current node does not have the given text639 # content, ignoring any HTML tags and normalizing whitespace.640 #641 # @macro text_query_params642 # @return [Boolean] Whether it doesn't exist643 #644 def has_no_text?(*args)645 assert_no_text(*args)646 rescue Capybara::ExpectationNotMet647 false648 end649 alias_method :has_no_content?, :has_no_text?650 def ==(other)651 eql?(other) || (other.respond_to?(:base) && base == other.base)652 end653 private654 def extract_selector(args)655 args.first.is_a?(Symbol) ? args.shift : session_options.default_selector656 end657 def _verify_selector_result(query_args, optional_filter_block)658 query_args = _set_query_session_options(*query_args)659 query = Capybara::Queries::SelectorQuery.new(*query_args, &optional_filter_block)660 synchronize(query.wait) do661 yield query.resolve_for(self), query662 end663 true664 end665 def _verify_match_result(query_args, optional_filter_block)666 query_args = _set_query_session_options(*query_args)667 query = Capybara::Queries::MatchQuery.new(*query_args, &optional_filter_block)668 synchronize(query.wait) do...

Full Screen

Full Screen

extract_selector

Using AI Code Generation

copy

Full Screen

1 config.allow_url("google.com")2 config.allow_url("google.co.in")3 current = current.find(:xpath, '..')4 path.join(' > ')5 def has_selector?(locator, options = {})6 locator = Capybara::Selector.new(locator).normalize(options)7 locator.match?(locator)8 if locator.include?('*')9 locator = Regexp.new(Regexp.escape(locator).gsub('\*', '.*'))10 any? { |node| node.extract_selector =~ locator }11 any? { |node| node.extract_selector == locator }12 def has_selector?(locator, options = {})13 locator = Capybara::Selector.new(locator).normalize(options)14 locator.match?(locator)15 if locator.include?('*')16 locator = Regexp.new(Regexp.escape(locator).gsub('\*', '.*'))17 any? { |node| node.extract_selector =~ locator }18 any? { |node| node.extract_selector == locator }

Full Screen

Full Screen

extract_selector

Using AI Code Generation

copy

Full Screen

1 def extract_selector(value)2 if value.is_a?(Hash)3 if value.has_key?(:xpath)4 elsif value.has_key?(:css)5 def find(*args)6 selector = extract_selector(args.first)

Full Screen

Full Screen

extract_selector

Using AI Code Generation

copy

Full Screen

1visit('/')2fill_in('q', :with => 'capybara')3click_button('Google Search')4click_link('Images')5puts extract_selector(:css, 'a.l')6puts extract_selector(:xpath, '//a[@class="l"]')7puts extract_selector(:xpath, '//a[contains(@class, "l")]')8puts extract_selector(:css, 'a.l', :xpath)9puts extract_selector(:xpath, '//a[@class="l"]', :xpath)10puts extract_selector(:xpath, '//a[contains(@class, "l")]', :xpath)

Full Screen

Full Screen

extract_selector

Using AI Code Generation

copy

Full Screen

1visit('/')2fill_in('q', with: search_term)3page.find(extract_selector(:button, 'Google Search')).click

Full Screen

Full Screen

extract_selector

Using AI Code Generation

copy

Full Screen

1 config.allow_url("google.com")2 config.allow_url("google.co.in")3 current = current.find(:xpath, '..')4 path.join(' > ')5 def has_selector?(locator, options = {})6 locator = Capybara::Selector.new(locator).normalize(options)7 locator.match?(locator)8 if locator.include?('*')9 locator = Regexp.new(Regexp.escape(locator).gsub('\*', '.*'))10 any? { |node| node.extract_selector =~ locator }11 any? { |node| node.extract_selector == locator }12 def has_selector?(locator, options = {})13 locator = Capybara::Selector.new(locator).normalize(options)14 locator.match?(locator)15 if locator.include?('*')16 locator = Regexp.new(Regexp.escape(locator).gsub('\*', '.*'))17 any? { |node| node.extract_selector =~ locator }18 any? { |node| node.extract_selector == locator }

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