How to use warn_exact_usage method of Capybara.Queries Package

Best Capybara code snippet using Capybara.Queries.warn_exact_usage

selector_query.rb

Source:selector_query.rb Github

copy

Full Screen

...24 if Capybara.exact_options and @selector == Selector.all[:option]25 @options[:exact] = true26 end27 @expression = @selector.call(@locator, @options)28 warn_exact_usage29 assert_valid_keys30 end31 def name; selector.name; end32 def label; selector.label or selector.name; end33 def description34 @description = String.new("#{label} #{locator.inspect}")35 @description << " with#{" exact" if exact_text == true} text #{options[:text].inspect}" if options[:text]36 @description << " with exact text #{options[:exact_text]}" if options[:exact_text].is_a?(String)37 @description << " with id #{options[:id]}" if options[:id]38 @description << " with classes #{Array(options[:class]).join(',')}]" if options[:class]39 @description << selector.description(options)40 @description << " that also matches the custom filter block" if @filter_block41 @description42 end43 def matches_filters?(node)44 if options[:text]45 regexp = if options[:text].is_a?(Regexp)46 options[:text]47 else48 if exact_text == true49 /\A#{Regexp.escape(options[:text].to_s)}\z/50 else51 Regexp.escape(options[:text].to_s)52 end53 end54 text_visible = visible55 text_visible = :all if text_visible == :hidden56 return false if not node.text(text_visible).match(regexp)57 end58 if exact_text.is_a?(String)59 regexp = /\A#{Regexp.escape(options[:exact_text])}\z/60 text_visible = visible61 text_visible = :all if text_visible == :hidden62 return false if not node.text(text_visible).match(regexp)63 end64 case visible65 when :visible then return false unless node.visible?66 when :hidden then return false if node.visible?67 end68 res = query_filters.all? do |name, filter|69 if options.has_key?(name)70 filter.matches?(node, options[name])71 elsif filter.default?72 filter.matches?(node, filter.default)73 else74 true75 end76 end77 res &&= Capybara.using_wait_time(0){ @filter_block.call(node)} unless @filter_block.nil?78 res79 end80 def visible81 case (vis = options.fetch(:visible){ @selector.default_visibility })82 when true then :visible83 when false then :all84 else vis85 end86 end87 def exact?88 return false if !supports_exact?89 options.fetch(:exact, Capybara.exact)90 end91 def match92 options.fetch(:match, Capybara.match)93 end94 def xpath(exact=nil)95 exact = self.exact? if exact.nil?96 expr = if @expression.respond_to?(:to_xpath) and exact97 @expression.to_xpath(:exact)98 else99 @expression.to_s100 end101 filtered_xpath(expr)102 end103 def css104 filtered_css(@expression)105 end106 # @api private107 def resolve_for(node, exact = nil)108 node.synchronize do109 children = if selector.format == :css110 node.find_css(self.css)111 else112 node.find_xpath(self.xpath(exact))113 end.map do |child|114 if node.is_a?(Capybara::Node::Base)115 Capybara::Node::Element.new(node.session, child, node, self)116 else117 Capybara::Node::Simple.new(child)118 end119 end120 Capybara::Result.new(children, self)121 end122 end123 # @api private124 def supports_exact?125 @expression.respond_to? :to_xpath126 end127 private128 def valid_keys129 VALID_KEYS + custom_keys130 end131 def query_filters132 if options.has_key?(:filter_set)133 Capybara::Selector::FilterSet.all[options[:filter_set]].filters134 else135 @selector.custom_filters136 end137 end138 def custom_keys139 @custom_keys ||= query_filters.keys + @selector.expression_filters140 end141 def assert_valid_keys142 super143 unless VALID_MATCH.include?(match)144 raise ArgumentError, "invalid option #{match.inspect} for :match, should be one of #{VALID_MATCH.map(&:inspect).join(", ")}"145 end146 end147 def filtered_xpath(expr)148 if options.has_key?(:id) || options.has_key?(:class)149 expr = "(#{expr})"150 expr = "#{expr}[#{XPath.attr(:id) == options[:id]}]" if options.has_key?(:id) && !custom_keys.include?(:id)151 if options.has_key?(:class) && !custom_keys.include?(:class)152 class_xpath = Array(options[:class]).map do |klass|153 "contains(concat(' ',normalize-space(@class),' '),' #{klass} ')"154 end.join(" and ")155 expr = "#{expr}[#{class_xpath}]"156 end157 end158 expr159 end160 def filtered_css(expr)161 if options.has_key?(:id) || options.has_key?(:class)162 css_selectors = expr.split(',').map(&:rstrip)163 expr = css_selectors.map do |sel|164 sel += "##{Capybara::Selector::CSS.escape(options[:id])}" if options.has_key?(:id) && !custom_keys.include?(:id)165 sel += Array(options[:class]).map { |k| ".#{Capybara::Selector::CSS.escape(k)}"}.join if options.has_key?(:class) && !custom_keys.include?(:class)166 sel167 end.join(", ")168 end169 expr170 end171 def warn_exact_usage172 if options.has_key?(:exact) && !supports_exact?173 warn "The :exact option only has an effect on queries using the XPath#is method. Using it with the query \"#{expression.to_s}\" has no effect."174 end175 end176 def exact_text177 options.fetch(:exact_text, Capybara.exact_text)178 end179 end180 end181end...

Full Screen

Full Screen

warn_exact_usage

Using AI Code Generation

copy

Full Screen

1visit('/')2fill_in('q', with: 'Capybara')3click_button('Google Search')

Full Screen

Full Screen

warn_exact_usage

Using AI Code Generation

copy

Full Screen

1session = Capybara::Session.new(:selenium)2session.visit('https://www.google.com')3session.find(:css, 'div')

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