How to use above method of Capybara.Queries Package

Best Capybara code snippet using Capybara.Queries.above

selector_query.rb

Source:selector_query.rb Github

copy

Full Screen

...3module Capybara4 module Queries5 class SelectorQuery < Queries::BaseQuery6 attr_reader :expression, :selector, :locator, :options7 SPATIAL_KEYS = %i[above below left_of right_of near].freeze8 VALID_KEYS = SPATIAL_KEYS + COUNT_KEYS +9 %i[text id class style visible obscured exact exact_text normalize_ws match wait filter_set focused]10 VALID_MATCH = %i[first smart prefer_exact one].freeze11 def initialize(*args,12 session_options:,13 enable_aria_label: session_options.enable_aria_label,14 enable_aria_role: session_options.enable_aria_role,15 test_id: session_options.test_id,16 selector_format: nil,17 order: nil,18 **options,19 &filter_block)20 @resolved_node = nil21 @resolved_count = 022 @options = options.dup23 @order = order24 @filter_cache = Hash.new { |hsh, key| hsh[key] = {} }25 super(@options)26 self.session_options = session_options27 @selector = Selector.new(28 find_selector(args[0].is_a?(Symbol) ? args.shift : args[0]),29 config: {30 enable_aria_label: enable_aria_label,31 enable_aria_role: enable_aria_role,32 test_id: test_id33 },34 format: selector_format35 )36 @locator = args.shift37 @filter_block = filter_block38 raise ArgumentError, "Unused parameters passed to #{self.class.name} : #{args}" unless args.empty?39 @expression = selector.call(@locator, **@options)40 warn_exact_usage41 assert_valid_keys42 end43 def name; selector.name; end44 def label; selector.label || selector.name; end45 def description(only_applied = false) # rubocop:disable Style/OptionalBooleanParameter46 desc = +''47 show_for = show_for_stage(only_applied)48 if show_for[:any]49 desc << 'visible ' if visible == :visible50 desc << 'non-visible ' if visible == :hidden51 end52 desc << "#{label} #{locator.inspect}"53 if show_for[:any]54 desc << " with#{' exact' if exact_text == true} text #{options[:text].inspect}" if options[:text]55 desc << " with exact text #{exact_text}" if exact_text.is_a?(String)56 end57 desc << " with id #{options[:id]}" if options[:id]58 desc << " with classes [#{Array(options[:class]).join(',')}]" if options[:class]59 desc << ' that is focused' if options[:focused]60 desc << ' that is not focused' if options[:focused] == false61 desc << case options[:style]62 when String63 " with style attribute #{options[:style].inspect}"64 when Regexp65 " with style attribute matching #{options[:style].inspect}"66 when Hash67 " with styles #{options[:style].inspect}"68 else ''69 end70 %i[above below left_of right_of near].each do |spatial_filter|71 if options[spatial_filter] && show_for[:spatial]72 desc << " #{spatial_filter} #{options[spatial_filter] rescue '<ERROR>'}" # rubocop:disable Style/RescueModifier73 end74 end75 desc << selector.description(node_filters: show_for[:node], **options)76 desc << ' that also matches the custom filter block' if @filter_block && show_for[:node]77 desc << " within #{@resolved_node.inspect}" if describe_within?78 if locator.is_a?(String) && locator.start_with?('#', './/', '//') && !selector.raw_locator?79 desc << "\nNote: It appears you may be passing a CSS selector or XPath expression rather than a locator. " \80 "Please see the documentation for acceptable locator values.\n\n"81 end82 desc83 end84 def applied_description85 description(true)86 end87 def matches_filters?(node, node_filter_errors = [])88 return true if (@resolved_node&.== node) && options[:allow_self]89 matches_locator_filter?(node) &&90 matches_system_filters?(node) &&91 matches_spatial_filters?(node) &&92 matches_node_filters?(node, node_filter_errors) &&93 matches_filter_block?(node)94 rescue *(node.respond_to?(:session) ? node.session.driver.invalid_element_errors : [])95 false96 end97 def visible98 case (vis = options.fetch(:visible) { default_visibility })99 when true then :visible100 when false then :all101 else vis102 end103 end104 def exact?105 supports_exact? ? options.fetch(:exact, session_options.exact) : false106 end107 def match108 options.fetch(:match, session_options.match)109 end110 def xpath(exact = nil)111 exact = exact? if exact.nil?112 expr = apply_expression_filters(@expression)113 expr = exact ? expr.to_xpath(:exact) : expr.to_s if expr.respond_to?(:to_xpath)114 expr = filtered_expression(expr)115 expr = "(#{expr})[#{xpath_text_conditions}]" if try_text_match_in_expression?116 expr117 end118 def css119 filtered_expression(apply_expression_filters(@expression))120 end121 # @api private122 def resolve_for(node, exact = nil)123 applied_filters.clear124 @filter_cache.clear125 @resolved_node = node126 @resolved_count += 1127 node.synchronize do128 children = find_nodes_by_selector_format(node, exact).map(&method(:to_element))129 Capybara::Result.new(ordered_results(children), self)130 end131 end132 # @api private133 def supports_exact?134 return @expression.respond_to? :to_xpath if @selector.supports_exact?.nil?135 @selector.supports_exact?136 end137 def failure_message138 +"expected to find #{applied_description}" << count_message139 end140 def negative_failure_message141 +"expected not to find #{applied_description}" << count_message142 end143 private144 def selector_format145 @selector.format146 end147 def matching_text148 options[:text] || options[:exact_text]149 end150 def text_fragments151 (text = matching_text).is_a?(String) ? text.split : []152 end153 def xpath_text_conditions154 case (text = matching_text)155 when String156 text.split.map { |txt| XPath.contains(txt) }.reduce(&:&)157 when Regexp158 condition = XPath.current159 condition = condition.uppercase if text.casefold?160 Selector::RegexpDisassembler.new(text).alternated_substrings.map do |strs|161 strs.flat_map(&:split).map { |str| condition.contains(str) }.reduce(:&)162 end.reduce(:|)163 end164 end165 def try_text_match_in_expression?166 first_try? &&167 matching_text &&168 @resolved_node.is_a?(Capybara::Node::Base) &&169 @resolved_node.session&.driver&.wait?170 end171 def first_try?172 @resolved_count == 1173 end174 def show_for_stage(only_applied)175 lambda do |stage = :any|176 !only_applied || (stage == :any ? applied_filters.any? : applied_filters.include?(stage))177 end178 end179 def applied_filters180 @applied_filters ||= []181 end182 def find_selector(locator)183 case locator184 when Symbol then Selector[locator]185 else Selector.for(locator)186 end || Selector[session_options.default_selector]187 end188 def find_nodes_by_selector_format(node, exact)189 hints = {}190 hints[:uses_visibility] = true unless visible == :all191 hints[:texts] = text_fragments unless selector_format == :xpath192 hints[:styles] = options[:style] if use_default_style_filter?193 hints[:position] = true if use_spatial_filter?194 case selector_format195 when :css196 if node.method(:find_css).arity == 1197 node.find_css(css)198 else199 node.find_css(css, **hints)200 end201 when :xpath202 if node.method(:find_xpath).arity == 1203 node.find_xpath(xpath(exact))204 else205 node.find_xpath(xpath(exact), **hints)206 end207 else208 raise ArgumentError, "Unknown format: #{selector_format}"209 end210 end211 def to_element(node)212 if @resolved_node.is_a?(Capybara::Node::Base)213 Capybara::Node::Element.new(@resolved_node.session, node, @resolved_node, self)214 else215 Capybara::Node::Simple.new(node)216 end217 end218 def valid_keys219 VALID_KEYS + custom_keys220 end221 def matches_node_filters?(node, errors)222 applied_filters << :node223 unapplied_options = options.keys - valid_keys224 @selector.with_filter_errors(errors) do225 node_filters.all? do |filter_name, filter|226 next true unless apply_filter?(filter)227 if filter.matcher?228 unapplied_options.select { |option_name| filter.handles_option?(option_name) }.all? do |option_name|229 unapplied_options.delete(option_name)230 filter.matches?(node, option_name, options[option_name], @selector)231 end232 elsif options.key?(filter_name)233 unapplied_options.delete(filter_name)234 filter.matches?(node, filter_name, options[filter_name], @selector)235 elsif filter.default?236 filter.matches?(node, filter_name, filter.default, @selector)237 else238 true239 end240 end241 end242 end243 def matches_filter_block?(node)244 return true unless @filter_block245 if node.respond_to?(:session)246 node.session.using_wait_time(0) { @filter_block.call(node) }247 else248 @filter_block.call(node)249 end250 end251 def filter_set(name)252 ::Capybara::Selector::FilterSet[name]253 end254 def node_filters255 if options.key?(:filter_set)256 filter_set(options[:filter_set])257 else258 @selector259 end.node_filters260 end261 def expression_filters262 filters = @selector.expression_filters263 filters.merge filter_set(options[:filter_set]).expression_filters if options.key?(:filter_set)264 filters265 end266 def ordered_results(results)267 case @order268 when :reverse269 results.reverse270 else271 results272 end273 end274 def custom_keys275 @custom_keys ||= node_filters.keys + expression_filters.keys276 end277 def assert_valid_keys278 unless VALID_MATCH.include?(match)279 raise ArgumentError, "Invalid option #{match.inspect} for :match, should be one of #{VALID_MATCH.map(&:inspect).join(', ')}"280 end281 unhandled_options = @options.keys.reject do |option_name|282 valid_keys.include?(option_name) ||283 expression_filters.any? { |_name, ef| ef.handles_option? option_name } ||284 node_filters.any? { |_name, nf| nf.handles_option? option_name }285 end286 return if unhandled_options.empty?287 invalid_names = unhandled_options.map(&:inspect).join(', ')288 valid_names = (valid_keys - [:allow_self]).map(&:inspect).join(', ')289 raise ArgumentError, "Invalid option(s) #{invalid_names}, should be one of #{valid_names}"290 end291 def filtered_expression(expr)292 conditions = {}293 conditions[:id] = options[:id] if use_default_id_filter?294 conditions[:class] = options[:class] if use_default_class_filter?295 conditions[:style] = options[:style] if use_default_style_filter? && !options[:style].is_a?(Hash)296 builder(expr).add_attribute_conditions(**conditions)297 end298 def use_default_id_filter?299 options.key?(:id) && !custom_keys.include?(:id)300 end301 def use_default_class_filter?302 options.key?(:class) && !custom_keys.include?(:class)303 end304 def use_default_style_filter?305 options.key?(:style) && !custom_keys.include?(:style)306 end307 def use_default_focused_filter?308 options.key?(:focused) && !custom_keys.include?(:focused)309 end310 def use_spatial_filter?311 options.values_at(*SPATIAL_KEYS).compact.any?312 end313 def apply_expression_filters(expression)314 unapplied_options = options.keys - valid_keys315 expression_filters.inject(expression) do |expr, (name, ef)|316 next expr unless apply_filter?(ef)317 if ef.matcher?318 unapplied_options.select(&ef.method(:handles_option?)).inject(expr) do |memo, option_name|319 unapplied_options.delete(option_name)320 ef.apply_filter(memo, option_name, options[option_name], @selector)321 end322 elsif options.key?(name)323 unapplied_options.delete(name)324 ef.apply_filter(expr, name, options[name], @selector)325 elsif ef.default?326 ef.apply_filter(expr, name, ef.default, @selector)327 else328 expr329 end330 end331 end332 def warn_exact_usage333 return unless options.key?(:exact) && !supports_exact?334 warn "The :exact option only has an effect on queries using the XPath#is method. Using it with the query \"#{expression}\" has no effect."335 end336 def exact_text337 options.fetch(:exact_text, session_options.exact_text)338 end339 def describe_within?340 @resolved_node && !document?(@resolved_node) && !simple_root?(@resolved_node)341 end342 def document?(node)343 node.is_a?(::Capybara::Node::Document)344 end345 def simple_root?(node)346 node.is_a?(::Capybara::Node::Simple) && node.path == '/'347 end348 def apply_filter?(filter)349 filter.format.nil? || (filter.format == selector_format)350 end351 def matches_locator_filter?(node)352 return true unless @selector.locator_filter && apply_filter?(@selector.locator_filter)353 @selector.locator_filter.matches?(node, @locator, @selector, exact: exact?)354 end355 def matches_system_filters?(node)356 applied_filters << :system357 matches_visibility_filters?(node) &&358 matches_id_filter?(node) &&359 matches_class_filter?(node) &&360 matches_style_filter?(node) &&361 matches_focused_filter?(node) &&362 matches_text_filter?(node) &&363 matches_exact_text_filter?(node)364 end365 def matches_spatial_filters?(node)366 applied_filters << :spatial367 return true unless use_spatial_filter?368 node_rect = Rectangle.new(node.initial_cache[:position] || node.rect)369 if options[:above]370 el_rect = rect_cache(options[:above])371 return false unless node_rect.above? el_rect372 end373 if options[:below]374 el_rect = rect_cache(options[:below])375 return false unless node_rect.below? el_rect376 end377 if options[:left_of]378 el_rect = rect_cache(options[:left_of])379 return false unless node_rect.left_of? el_rect380 end381 if options[:right_of]382 el_rect = rect_cache(options[:right_of])383 return false unless node_rect.right_of? el_rect384 end385 if options[:near]386 return false if node == options[:near]387 el_rect = rect_cache(options[:near])388 return false unless node_rect.near? el_rect389 end390 true391 end392 def matches_id_filter?(node)393 return true unless use_default_id_filter? && options[:id].is_a?(Regexp)394 options[:id].match? node[:id]395 end396 def matches_class_filter?(node)397 return true unless use_default_class_filter? && need_to_process_classes?398 if options[:class].is_a? Regexp399 options[:class].match? node[:class]400 else401 classes = (node[:class] || '').split402 options[:class].select { |c| c.is_a? Regexp }.all? do |r|403 classes.any? { |cls| r.match? cls }404 end405 end406 end407 def matches_focused_filter?(node)408 return true unless use_default_focused_filter?409 (node == node.session.active_element) == options[:focused]410 end411 def need_to_process_classes?412 case options[:class]413 when Regexp then true414 when Array then options[:class].any?(Regexp)415 else416 false417 end418 end419 def matches_style_filter?(node)420 case options[:style]421 when String, nil422 true423 when Regexp424 options[:style].match? node[:style]425 when Hash426 matches_style?(node, options[:style])427 end428 end429 def matches_style?(node, styles)430 @actual_styles = node.initial_cache[:style] || node.style(*styles.keys)431 styles.all? do |style, value|432 if value.is_a? Regexp433 value.match? @actual_styles[style.to_s]434 else435 @actual_styles[style.to_s] == value436 end437 end438 end439 def matches_text_filter?(node)440 value = options[:text]441 return true unless value442 return matches_text_exactly?(node, value) if exact_text == true443 regexp = value.is_a?(Regexp) ? value : Regexp.escape(value.to_s)444 matches_text_regexp?(node, regexp)445 end446 def matches_exact_text_filter?(node)447 return true unless exact_text.is_a?(String)448 matches_text_exactly?(node, exact_text)449 end450 def matches_visibility_filters?(node)451 obscured = options[:obscured]452 return (visible != :hidden) && (node.initial_cache[:visible] != false) && !node.obscured? if obscured == false453 vis = case visible454 when :visible455 node.initial_cache[:visible] || (node.initial_cache[:visible].nil? && node.visible?)456 when :hidden457 (node.initial_cache[:visible] == false) || (node.initial_cache[:visbile].nil? && !node.visible?)458 else459 true460 end461 vis && case obscured462 when true463 node.obscured?464 when false465 !node.obscured?466 else467 true468 end469 end470 def matches_text_exactly?(node, value)471 regexp = value.is_a?(Regexp) ? value : /\A#{Regexp.escape(value.to_s)}\z/472 matches_text_regexp?(node, regexp)473 end474 def normalize_ws475 options.fetch(:normalize_ws, session_options.default_normalize_ws)476 end477 def matches_text_regexp?(node, regexp)478 text_visible = visible479 text_visible = :all if text_visible == :hidden480 node.text(text_visible, normalize_ws: normalize_ws).match?(regexp)481 end482 def default_visibility483 @selector.default_visibility(session_options.ignore_hidden_elements, options)484 end485 def builder(expr)486 selector.builder(expr)487 end488 def position_cache(key)489 @filter_cache[key][:position] ||= key.rect490 end491 def rect_cache(key)492 @filter_cache[key][:rect] ||= Rectangle.new(position_cache(key))493 end494 class Rectangle495 attr_reader :top, :bottom, :left, :right496 def initialize(position)497 # rubocop:disable Style/RescueModifier498 @top = position['top'] rescue position['y']499 @bottom = position['bottom'] rescue (@top + position['height'])500 @left = position['left'] rescue position['x']501 @right = position['right'] rescue (@left + position['width'])502 # rubocop:enable Style/RescueModifier503 end504 def distance(other)505 distance = Float::INFINITY506 line_segments.each do |ls1|507 other.line_segments.each do |ls2|508 distance = [509 distance,510 distance_segment_segment(*ls1, *ls2)511 ].min512 end513 end514 distance515 end516 def above?(other)517 bottom <= other.top518 end519 def below?(other)520 top >= other.bottom521 end522 def left_of?(other)523 right <= other.left524 end525 def right_of?(other)526 left >= other.right527 end528 def near?(other)529 distance(other) <= 50530 end...

Full Screen

Full Screen

above

Using AI Code Generation

copy

Full Screen

1 def initialize(*args)2 def resolve_for(node)3 node.all(@selector)4 visit('/')5 fill_in('q', :with => 'ruby')6 find('input[type="submit"]').click7 find('a', :text => 'Ruby (programming language) - Wikipedia').click8Capybara::Queries::SelectorQuery.new('a', :text => 'Ruby (programming language) - Wikipedia').resolve_for(page)9Capybara::Queries::SelectorQuery.new('a', :text => 'Ruby (programming language) - Wikipedia').resolve_for(page).first.click10Capybara::Queries::SelectorQuery.new('a', :text => 'Ruby (programming language) - Wikipedia').resolve_for(page).each do |link|11 visit('/')12 fill_in('q', :with => 'ruby')13 find('input[type="submit"]').click14Capybara::Queries::SelectorQuery.new('a', :text => 'Ruby (programming language) - Wikipedia').resolve_for(page).each do |link|15 visit('/')16 fill_in('q', :with => 'ruby')17 find('input[type="submit"]').click18Capybara::Queries::SelectorQuery.new('a', :text => 'Ruby (programming language) - Wikipedia').resolve_for(page).each do |link|19 visit('/')20 fill_in('q', :with => 'ruby')21 find('input[type="submit"]').click22Capybara::Queries::SelectorQuery.new('a', :text => 'Ruby (programming language) - Wikipedia').resolve_for(page).each do |link|23 visit('/')24 fill_in('q', :with => 'ruby')25 find('input[type="submit"]').click26Capybara::Queries::SelectorQuery.new('a', :text => 'Ruby (

Full Screen

Full Screen

above

Using AI Code Generation

copy

Full Screen

1Capybara.Queries.new.check_if_element_exists("div", "id", "mydiv")2Capybara.Queries.new.check_if_element_exists("div", "class", "mydiv")3Capybara.Queries.new.check_if_element_exists("div", "text", "mydiv")4Capybara.Queries.new.check_if_element_exists("div", "xpath", "mydiv")5Capybara::Queries.new.check_if_element_exists("div", "id", "mydiv")6Capybara::Queries.new.check_if_element_exists("div", "class", "mydiv")7Capybara::Queries.new.check_if_element_exists("div", "text", "mydiv")8Capybara::Queries.new.check_if_element_exists("div", "xpath", "mydiv")9Capybara::Queries.check_if_element_exists("div", "id", "mydiv")10Capybara::Queries.check_if_element_exists("div", "class", "mydiv")11Capybara::Queries.check_if_element_exists("div", "text", "mydiv")12Capybara::Queries.check_if_element_exists("div", "xpath", "mydiv")13Capybara.Queries.check_if_element_exists("div", "id", "mydiv")14Capybara.Queries.check_if_element_exists("div", "class", "mydiv")15Capybara.Queries.check_if_element_exists("div", "text", "mydiv")16Capybara.Queries.check_if_element_exists("div", "xpath", "mydiv")17Capybara.Queries.check_if_element_exists("div", "id", "mydiv")18Capybara.Queries.check_if_element_exists("div", "class", "mydiv")19Capybara.Queries.check_if_element_exists("div", "text", "mydiv")20Capybara.Queries.check_if_element_exists("div", "xpath", "mydiv")

Full Screen

Full Screen

above

Using AI Code Generation

copy

Full Screen

1page.find(:xpath, "//div[@class='foo']")2page.find(:css, "div.foo")3page.find(:xpath, "//div[@class='foo']")4page.find(:css, "div.foo")5page.find(:xpath, "//div[@class='foo']")6page.find(:css, "div.foo")7page.find(:xpath, "//div[@class='foo']")8page.find(:css, "div.foo")9page.find(:xpath, "//div[@class='foo']")10page.find(:css, "div.foo")11page.find(:xpath, "//div[@class='foo']")12page.find(:css, "div.foo")13page.find(:xpath, "//div[@class='foo']")14page.find(:css, "div.foo")15page.find(:xpath, "//div[@class='foo']")16page.find(:css, "div.foo")17page.find(:xpath, "//div[@class='foo']")18page.find(:css, "div.foo")19page.find(:xpath, "//div[@class='foo']")20page.find(:css, "div.foo")21page.find(:xpath, "//div[@class='foo']")22page.find(:css, "div.foo")23page.find(:xpath, "//div[@class='foo']")24page.find(:css, "div.foo")25page.find(:xpath, "//div[@class='foo']")26page.find(:css, "div.foo")27page.find(:xpath, "//div[@class

Full Screen

Full Screen

above

Using AI Code Generation

copy

Full Screen

1page.find(:xpath, "//div[@class='foo']")2page.find(:css, "div.foo")3page.find(:xpath, "//div[@class='foo']")4page.find(:css, "div.foo")5page.find(:xpath, "//div[@class='foo']")6page.find(:css, "div.foo")7page.find(:xpath, "//div[@class='foo']")8page.find(:css, "div.foo")9page.find(:xpath, "//div[@class='foo']")10page.find(:css, "div.foo")11page.find(:xpath, "//div[@class='foo']")12page.find(:css, "div.foo")13page.find(:xpath, "//div[@class='foo']")14page.find(:css, "div.foo")15page.find(:xpath, "//div[@class='foo']")16page.find(:css, "div.foo")17page.find(:xpath, "//div[@class='foo']")18page.find(:css, "div.foo")19page.find(:xpath, "//div[@class='foo']")20page.find(:css, "div.foo")21page.find(:xpath, "//div[@class='foo']")22page.find(:css, "div.foo")23page.find(:xpath, "//div[@class='foo']")24page.find(:css, "div.foo")25page.find(:xpath, "//div[@class='foo']")26page.find(:css, "div.foo")27page.find(:xpath, "//div[@class

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