How to use valid_keys method of Capybara.Queries Package

Best Capybara code snippet using Capybara.Queries.valid_keys

selector_query.rb

Source:selector_query.rb Github

copy

Full Screen

...15 @filter_block = filter_block16 raise ArgumentError, "Unused parameters passed to #{self.class.name} : #{args}" unless args.empty?17 @expression = @selector.call(@locator, @options.merge(enable_aria_label: session_options.enable_aria_label))18 warn_exact_usage19 assert_valid_keys20 end21 def name; selector.name; end22 def label; selector.label || selector.name; end23 def description24 @description = +""25 @description << "visible " if visible == :visible26 @description << "non-visible " if visible == :hidden27 @description << "#{label} #{locator.inspect}"28 @description << " with#{' exact' if exact_text == true} text #{options[:text].inspect}" if options[:text]29 @description << " with exact text #{exact_text}" if exact_text.is_a?(String)30 @description << " with id #{options[:id]}" if options[:id]31 @description << " with classes [#{Array(options[:class]).join(',')}]" if options[:class]32 @description << selector.description(options)33 @description << " that also matches the custom filter block" if @filter_block34 @description << " within #{@resolved_node.inspect}" if describe_within?35 @description36 end37 def matches_filters?(node)38 return false if options[:text] && !matches_text_filter(node, options[:text])39 return false if exact_text.is_a?(String) && !matches_exact_text_filter(node, exact_text)40 case visible41 when :visible then return false unless node.visible?42 when :hidden then return false if node.visible?43 end44 matches_node_filters?(node) && matches_filter_block?(node)45 rescue *(node.respond_to?(:session) ? node.session.driver.invalid_element_errors : [])46 false47 end48 def visible49 case (vis = options.fetch(:visible) { @selector.default_visibility(session_options.ignore_hidden_elements) })50 when true then :visible51 when false then :all52 else vis53 end54 end55 def exact?56 supports_exact? ? options.fetch(:exact, session_options.exact) : false57 end58 def match59 options.fetch(:match, session_options.match)60 end61 def xpath(exact = nil)62 exact = exact? if exact.nil?63 expr = apply_expression_filters(@expression)64 expr = exact ? expr.to_xpath(:exact) : expr.to_s if expr.respond_to?(:to_xpath)65 filtered_xpath(expr)66 end67 def css68 filtered_css(apply_expression_filters(@expression))69 end70 # @api private71 def resolve_for(node, exact = nil)72 @resolved_node = node73 node.synchronize do74 children = if selector.format == :css75 node.find_css(css)76 else77 node.find_xpath(xpath(exact))78 end.map do |child|79 if node.is_a?(Capybara::Node::Base)80 Capybara::Node::Element.new(node.session, child, node, self)81 else82 Capybara::Node::Simple.new(child)83 end84 end85 Capybara::Result.new(children, self)86 end87 end88 # @api private89 def supports_exact?90 @expression.respond_to? :to_xpath91 end92 private93 def find_selector(locator)94 selector = if locator.is_a?(Symbol)95 Selector.all.fetch(locator) { |sel_type| raise ArgumentError, "Unknown selector type (:#{sel_type})" }96 else97 Selector.all.values.find { |s| s.match?(locator) }98 end99 selector || Selector.all[session_options.default_selector]100 end101 def valid_keys102 VALID_KEYS + custom_keys103 end104 def matches_node_filters?(node)105 unapplied_options = options.keys - valid_keys106 node_filters.all? do |filter_name, filter|107 if filter.matcher?108 unapplied_options.select { |option_name| filter.handles_option?(option_name) }.all? do |option_name|109 unapplied_options.delete(option_name)110 filter.matches?(node, option_name, options[option_name])111 end112 elsif options.key?(filter_name)113 unapplied_options.delete(filter_name)114 filter.matches?(node, filter_name, options[filter_name])115 elsif filter.default?116 filter.matches?(node, filter_name, filter.default)117 else118 true119 end120 end121 end122 def matches_filter_block?(node)123 return true unless @filter_block124 if node.respond_to?(:session)125 node.session.using_wait_time(0) { @filter_block.call(node) }126 else127 @filter_block.call(node)128 end129 end130 def node_filters131 if options.key?(:filter_set)132 ::Capybara::Selector::FilterSet.all[options[:filter_set]].node_filters133 else134 @selector.node_filters135 end136 end137 def expression_filters138 filters = @selector.expression_filters139 filters.merge ::Capybara::Selector::FilterSet.all[options[:filter_set]].expression_filters if options.key?(:filter_set)140 filters141 end142 def custom_keys143 @custom_keys ||= node_filters.keys + expression_filters.keys144 end145 def assert_valid_keys146 unless VALID_MATCH.include?(match)147 raise ArgumentError, "invalid option #{match.inspect} for :match, should be one of #{VALID_MATCH.map(&:inspect).join(', ')}"148 end149 unhandled_options = @options.keys - valid_keys150 unhandled_options -= @options.keys.select do |option_name|151 expression_filters.any? { |_nmae, ef| ef.handles_option? option_name } ||152 node_filters.any? { |_name, nf| nf.handles_option? option_name }153 end154 return if unhandled_options.empty?155 invalid_names = unhandled_options.map(&:inspect).join(", ")156 valid_names = valid_keys.map(&:inspect).join(", ")157 raise ArgumentError, "invalid keys #{invalid_names}, should be one of #{valid_names}"158 end159 def filtered_xpath(expr)160 if options.key?(:id) && !custom_keys.include?(:id)161 expr = if options[:id].is_a? XPath::Expression162 "(#{expr})[#{XPath.attr(:id)[options[:id]]}]"163 else164 "(#{expr})[#{XPath.attr(:id) == options[:id]}]"165 end166 end167 if options.key?(:class) && !custom_keys.include?(:class)168 class_xpath = if options[:class].is_a?(XPath::Expression)169 XPath.attr(:class)[options[:class]]170 else171 Array(options[:class]).map do |klass|172 if klass.start_with?('!')173 !XPath.attr(:class).contains_word(klass.slice(1))174 else175 XPath.attr(:class).contains_word(klass)176 end177 end.reduce(:&)178 end179 expr = "(#{expr})[#{class_xpath}]"180 end181 expr182 end183 def filtered_css(expr)184 process_id = options.key?(:id) && !custom_keys.include?(:id)185 process_class = options.key?(:class) && !custom_keys.include?(:class)186 if process_id && options[:id].is_a?(XPath::Expression)187 raise ArgumentError, "XPath expressions are not supported for the :id filter with CSS based selectors"188 end189 if process_class && options[:class].is_a?(XPath::Expression)190 raise ArgumentError, "XPath expressions are not supported for the :class filter with CSS based selectors"191 end192 if process_class || process_id193 css_selectors = expr.split(',').map(&:rstrip)194 expr = css_selectors.map do |sel|195 sel += "##{Capybara::Selector::CSS.escape(options[:id])}" if process_id196 sel += css_from_classes(Array(options[:class])) if process_class197 sel198 end.join(", ")199 end200 expr201 end202 def css_from_classes(classes)203 classes = classes.group_by { |c| c.start_with? '!' }204 (classes[false].to_a.map { |c| ".#{Capybara::Selector::CSS.escape(c)}" } +205 classes[true].to_a.map { |c| ":not(.#{Capybara::Selector::CSS.escape(c.slice(1))})" }).join206 end207 def apply_expression_filters(expr)208 unapplied_options = options.keys - valid_keys209 expression_filters.inject(expr) do |memo, (name, ef)|210 if ef.matcher?211 unapplied_options.select { |option_name| ef.handles_option?(option_name) }.each do |option_name|212 unapplied_options.delete(option_name)213 memo = ef.apply_filter(memo, option_name, options[option_name])214 end215 memo216 elsif options.key?(name)217 unapplied_options.delete(name)218 ef.apply_filter(memo, name, options[name])219 elsif ef.default?220 ef.apply_filter(memo, name, ef.default)221 else222 memo...

Full Screen

Full Screen

match_query.rb

Source:match_query.rb Github

copy

Full Screen

...9 :all10 end11 end12 private13 def valid_keys14 VALID_KEYS + @selector.custom_filters.keys15 end16 end17 end18end...

Full Screen

Full Screen

valid_keys

Using AI Code Generation

copy

Full Screen

1def find_element(selector)2 if Capybara::Queries::SelectorQuery.new(selector).valid_keys.empty?3 find(selector)

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