Best Capybara code snippet using Capybara.expression
selector.rb
Source:selector.rb
...4 node_filter(:checked, :boolean) { |node, value| !(value ^ node.checked?) }5 node_filter(:unchecked, :boolean) { |node, value| (value ^ node.checked?) }6 node_filter(:disabled, :boolean, default: false, skip_if: :all) { |node, value| !(value ^ node.disabled?) }7 node_filter(:multiple, :boolean) { |node, value| !(value ^ node.multiple?) }8 expression_filter(:name) { |xpath, val| xpath[XPath.attr(:name) == val] }9 expression_filter(:placeholder) { |xpath, val| xpath[XPath.attr(:placeholder) == val] }10 describe(:node_filters) do |checked: nil, unchecked: nil, disabled: nil, multiple: nil, **|11 desc, states = +'', []12 states << 'checked' if checked || (unchecked == false)13 states << 'not checked' if unchecked || (checked == false)14 states << 'disabled' if disabled == true15 states << 'not disabled' if disabled == false16 desc << " that is #{states.join(' and ')}" unless states.empty?17 desc << ' with the multiple attribute' if multiple == true18 desc << ' without the multiple attribute' if multiple == false19 desc20 end21end22# rubocop:disable Metrics/BlockLength23Capybara.add_selector(:xpath) do24 xpath { |xpath| xpath }25end26Capybara.add_selector(:css) do27 css { |css| css }28end29Capybara.add_selector(:id) do30 xpath { |id| XPath.descendant[XPath.attr(:id) == id.to_s] }31end32Capybara.add_selector(:field) do33 xpath do |locator, **options|34 xpath = XPath.descendant(:input, :textarea, :select)[!XPath.attr(:type).one_of('submit', 'image', 'hidden')]35 locate_field(xpath, locator, options)36 end37 expression_filter(:type) do |expr, type|38 type = type.to_s39 if %w[textarea select].include?(type)40 expr.self(type.to_sym)41 else42 expr[XPath.attr(:type) == type]43 end44 end45 filter_set(:_field) # checked/unchecked/disabled/multiple/name/placeholder46 node_filter(:readonly, :boolean) { |node, value| !(value ^ node.readonly?) }47 node_filter(:with) do |node, with|48 with.is_a?(Regexp) ? node.value =~ with : node.value == with.to_s49 end50 describe_expression_filters do |type: nil, **options|51 desc = +''52 (expression_filters.keys - [:type]).each { |ef| desc << " with #{ef} #{options[ef]}" if options.key?(ef) }53 desc << " of type #{type.inspect}" if type54 desc55 end56 describe_node_filters do |**options|57 " with value #{options[:with].to_s.inspect}" if options.key?(:with)58 end59end60Capybara.add_selector(:fieldset) do61 xpath(:legend) do |locator, legend: nil, **|62 locator_matchers = (XPath.attr(:id) == locator.to_s) | XPath.child(:legend)[XPath.string.n.is(locator.to_s)]63 locator_matchers |= XPath.attr(test_id) == locator if test_id64 xpath = XPath.descendant(:fieldset)65 xpath = xpath[locator_matchers] unless locator.nil?66 xpath = xpath[XPath.child(:legend)[XPath.string.n.is(legend)]] if legend67 xpath68 end69 node_filter(:disabled, :boolean) { |node, value| !(value ^ node.disabled?) }70end71Capybara.add_selector(:link) do72 xpath(:title, :alt) do |locator, href: true, alt: nil, title: nil, **|73 xpath = XPath.descendant(:a)74 xpath = xpath[75 case href76 when nil, false77 !XPath.attr(:href)78 when true79 XPath.attr(:href)80 when Regexp81 nil # needs to be handled in filter82 else83 XPath.attr(:href) == href.to_s84 end85 ]86 unless locator.nil?87 locator = locator.to_s88 matchers = [XPath.attr(:id) == locator,89 XPath.string.n.is(locator),90 XPath.attr(:title).is(locator),91 XPath.descendant(:img)[XPath.attr(:alt).is(locator)]]92 matchers << XPath.attr(:'aria-label').is(locator) if enable_aria_label93 matchers << XPath.attr(test_id) == locator if test_id94 xpath = xpath[matchers.reduce(:|)]95 end96 xpath = xpath[find_by_attr(:title, title)]97 xpath = xpath[XPath.descendant(:img)[XPath.attr(:alt) == alt]] if alt98 xpath99 end100 node_filter(:href) do |node, href|101 # If not a Regexp it's been handled in the main XPath102 href.is_a?(Regexp) ? node[:href].match(href) : true103 end104 expression_filter(:download, valid_values: [true, false, String]) do |expr, download|105 mod = case download106 when true then XPath.attr(:download)107 when false then !XPath.attr(:download)108 when String then XPath.attr(:download) == download109 end110 expr[mod]111 end112 describe_expression_filters do |**options|113 desc = +''114 desc << " with href #{options[:href].inspect}" if options[:href] && !options[:href].is_a?(Regexp)115 desc << ' with no href attribute' if options.fetch(:href, true).nil?116 desc117 end118 describe_node_filters do |href: nil, **|119 " with href matching #{href.inspect}" if href.is_a? Regexp120 end121end122Capybara.add_selector(:button) do123 xpath(:value, :title, :type) do |locator, **options|124 input_btn_xpath = XPath.descendant(:input)[XPath.attr(:type).one_of('submit', 'reset', 'image', 'button')]125 btn_xpath = XPath.descendant(:button)126 image_btn_xpath = XPath.descendant(:input)[XPath.attr(:type) == 'image']127 unless locator.nil?128 locator = locator.to_s129 locator_matchers = XPath.attr(:id).equals(locator) | XPath.attr(:value).is(locator) | XPath.attr(:title).is(locator)130 locator_matchers |= XPath.attr(:'aria-label').is(locator) if enable_aria_label131 locator_matchers |= XPath.attr(test_id) == locator if test_id132 input_btn_xpath = input_btn_xpath[locator_matchers]133 btn_xpath = btn_xpath[locator_matchers | XPath.string.n.is(locator) | XPath.descendant(:img)[XPath.attr(:alt).is(locator)]]134 alt_matches = XPath.attr(:alt).is(locator)135 alt_matches |= XPath.attr(:'aria-label').is(locator) if enable_aria_label136 image_btn_xpath = image_btn_xpath[alt_matches]137 end138 res_xpath = input_btn_xpath.union(btn_xpath).union(image_btn_xpath)139 res_xpath = expression_filters.keys.inject(res_xpath) { |memo, ef| memo[find_by_attr(ef, options[ef])] }140 res_xpath141 end142 node_filter(:disabled, :boolean, default: false, skip_if: :all) { |node, value| !(value ^ node.disabled?) }143 describe_expression_filters144 describe_node_filters do |disabled: nil, **|145 ' that is disabled' if disabled == true146 end147end148Capybara.add_selector(:link_or_button) do149 label 'link or button'150 xpath do |locator, **options|151 self.class.all.values_at(:link, :button).map { |selector| selector.xpath.call(locator, options) }.reduce(:union)152 end153 node_filter(:disabled, :boolean, default: false, skip_if: :all) { |node, value| node.tag_name == 'a' || !(value ^ node.disabled?) }154 describe_node_filters do |disabled: nil, **|155 ' that is disabled' if disabled == true156 end157end158Capybara.add_selector(:fillable_field) do159 label 'field'160 xpath do |locator, **options|161 xpath = XPath.descendant(:input, :textarea)[!XPath.attr(:type).one_of('submit', 'image', 'radio', 'checkbox', 'hidden', 'file')]162 locate_field(xpath, locator, options)163 end164 expression_filter(:type) do |expr, type|165 type = type.to_s166 if type == 'textarea'167 expr.self(type.to_sym)168 else169 expr[XPath.attr(:type) == type]170 end171 end172 filter_set(:_field, %i[disabled multiple name placeholder])173 node_filter(:with) do |node, with|174 with.is_a?(Regexp) ? node.value =~ with : node.value == with.to_s175 end176 describe_expression_filters177 describe_node_filters do |**options|178 " with value #{options[:with].to_s.inspect}" if options.key?(:with)179 end180end181Capybara.add_selector(:radio_button) do182 label 'radio button'183 xpath do |locator, **options|184 xpath = XPath.descendant(:input)[XPath.attr(:type) == 'radio']185 locate_field(xpath, locator, options)186 end187 filter_set(:_field, %i[checked unchecked disabled name])188 node_filter(:option) { |node, value| node.value == value.to_s }189 describe_expression_filters190 describe_node_filters do |option: nil, **|191 " with value #{option.inspect}" if option192 end193end194Capybara.add_selector(:checkbox) do195 xpath do |locator, **options|196 xpath = XPath.descendant(:input)[XPath.attr(:type) == 'checkbox']197 locate_field(xpath, locator, options)198 end199 filter_set(:_field, %i[checked unchecked disabled name])200 node_filter(:option) { |node, value| node.value == value.to_s }201 describe_expression_filters202 describe_node_filters do |option: nil, **|203 " with value #{option.inspect}" if option204 end205end206Capybara.add_selector(:select) do207 label 'select box'208 xpath do |locator, **options|209 xpath = XPath.descendant(:select)210 locate_field(xpath, locator, options)211 end212 filter_set(:_field, %i[disabled multiple name placeholder])213 node_filter(:options) do |node, options|214 actual = if node.visible?215 node.all(:xpath, './/option', wait: false).map(&:text)216 else217 node.all(:xpath, './/option', visible: false, wait: false).map { |option| option.text(:all) }218 end219 options.sort == actual.sort220 end221 expression_filter(:with_options) do |expr, options|222 options.inject(expr) do |xpath, option|223 xpath[Capybara::Selector.all[:option].call(option)]224 end225 end226 node_filter(:selected) do |node, selected|227 actual = node.all(:xpath, './/option', visible: false, wait: false).select(&:selected?).map { |option| option.text(:all) }228 Array(selected).sort == actual.sort229 end230 node_filter(:with_selected) do |node, selected|231 actual = node.all(:xpath, './/option', visible: false, wait: false).select(&:selected?).map { |option| option.text(:all) }232 (Array(selected) - actual).empty?233 end234 describe_expression_filters do |with_options: nil, **opts|235 desc = +''236 desc << " with at least options #{with_options.inspect}" if with_options237 desc << describe_all_expression_filters(opts)238 desc239 end240 describe_node_filters do |options: nil, selected: nil, with_selected: nil, **|241 desc = +''242 desc << " with options #{options.inspect}" if options243 desc << " with #{selected.inspect} selected" if selected244 desc << " with at least #{with_selected.inspect} selected" if with_selected245 desc246 end247end248Capybara.add_selector(:datalist_input) do249 label 'input box with datalist completion'250 xpath do |locator, **options|251 xpath = XPath.descendant(:input)[XPath.attr(:list)]252 locate_field(xpath, locator, options)253 end254 filter_set(:_field, %i[disabled name placeholder])255 node_filter(:options) do |node, options|256 actual = node.find("//datalist[@id=#{node[:list]}]", visible: :all).all(:datalist_option, wait: false).map(&:value)257 options.sort == actual.sort258 end259 expression_filter(:with_options) do |expr, options|260 options.inject(expr) do |xpath, option|261 xpath[XPath.attr(:list) == XPath.anywhere(:datalist)[Capybara::Selector.all[:datalist_option].call(option)].attr(:id)]262 end263 end264 describe_expression_filters do |with_options: nil, **opts|265 desc = +''266 desc << " with at least options #{with_options.inspect}" if with_options267 desc << describe_all_expression_filters(opts)268 desc269 end270 describe_node_filters do |options: nil, **|271 " with options #{options.inspect}" if options272 end273end274Capybara.add_selector(:option) do275 xpath do |locator|276 xpath = XPath.descendant(:option)277 xpath = xpath[XPath.string.n.is(locator.to_s)] unless locator.nil?278 xpath279 end280 node_filter(:disabled, :boolean) { |node, value| !(value ^ node.disabled?) }281 node_filter(:selected, :boolean) { |node, value| !(value ^ node.selected?) }282 describe_node_filters do |**options|283 desc = +''284 desc << " that is#{' not' unless options[:disabled]} disabled" if options.key?(:disabled)285 desc << " that is#{' not' unless options[:selected]} selected" if options.key?(:selected)286 desc287 end288end289Capybara.add_selector(:datalist_option) do290 label 'datalist option'291 visible(:all)292 xpath do |locator|293 xpath = XPath.descendant(:option)294 xpath = xpath[XPath.string.n.is(locator.to_s) | (XPath.attr(:value) == locator.to_s)] unless locator.nil?295 xpath296 end297 node_filter(:disabled, :boolean) { |node, value| !(value ^ node.disabled?) }298 describe_node_filters do |**options|299 " that is#{' not' unless options[:disabled]} disabled" if options.key?(:disabled)300 end301end302Capybara.add_selector(:file_field) do303 label 'file field'304 xpath do |locator, options|305 xpath = XPath.descendant(:input)[XPath.attr(:type) == 'file']306 locate_field(xpath, locator, options)307 end308 filter_set(:_field, %i[disabled multiple name])309 describe_expression_filters310end311Capybara.add_selector(:label) do312 label 'label'313 xpath(:for) do |locator, options|314 xpath = XPath.descendant(:label)315 unless locator.nil?316 locator_matchers = XPath.string.n.is(locator.to_s) | (XPath.attr(:id) == locator.to_s)317 locator_matchers |= XPath.attr(test_id) == locator if test_id318 xpath = xpath[locator_matchers]319 end320 if options.key?(:for) && !options[:for].is_a?(Capybara::Node::Element)321 with_attr = XPath.attr(:for) == options[:for].to_s322 labelable_elements = %i[button input keygen meter output progress select textarea]323 wrapped = !XPath.attr(:for) &324 XPath.descendant(*labelable_elements)[XPath.attr(:id) == options[:for].to_s]325 xpath = xpath[with_attr | wrapped]326 end327 xpath328 end329 node_filter(:for) do |node, field_or_value|330 if field_or_value.is_a? Capybara::Node::Element331 if node[:for]332 field_or_value[:id] == node[:for]333 else334 field_or_value.find_xpath('./ancestor::label[1]').include? node.base335 end336 else337 true # Non element values were handled through the expression filter338 end339 end340 describe_expression_filters do |**options|341 " for element with id of \"#{options[:for]}\"" if options.key?(:for) && !options[:for].is_a?(Capybara::Node::Element)342 end343 describe_node_filters do |**options|344 " for element #{options[:for]}" if options[:for]&.is_a?(Capybara::Node::Element)345 end346end347Capybara.add_selector(:table) do348 xpath(:caption) do |locator, caption: nil, **|349 xpath = XPath.descendant(:table)350 unless locator.nil?351 locator_matchers = (XPath.attr(:id) == locator.to_s) | XPath.descendant(:caption).is(locator.to_s)352 locator_matchers |= XPath.attr(test_id) == locator if test_id353 xpath = xpath[locator_matchers]354 end355 xpath = xpath[XPath.descendant(:caption) == caption] if caption356 xpath357 end358 describe_expression_filters do |caption: nil, **|359 " with caption \"#{caption}\"" if caption360 end361end362Capybara.add_selector(:frame) do363 xpath(:name) do |locator, **options|364 xpath = XPath.descendant(:iframe).union(XPath.descendant(:frame))365 unless locator.nil?366 locator_matchers = (XPath.attr(:id) == locator.to_s) | (XPath.attr(:name) == locator.to_s)367 locator_matchers |= XPath.attr(test_id) == locator if test_id368 xpath = xpath[locator_matchers]369 end370 xpath = expression_filters.keys.inject(xpath) { |memo, ef| memo[find_by_attr(ef, options[ef])] }371 xpath372 end373 describe_expression_filters do |name: nil, **|374 " with name #{name}" if name375 end376end377Capybara.add_selector(:element) do378 xpath do |locator, **|379 XPath.descendant((locator || '@').to_sym)380 end381 expression_filter(:attributes, matcher: /.+/) do |xpath, name, val|382 case val383 when Regexp384 xpath385 when XPath::Expression386 xpath[XPath.attr(name)[val]]387 else388 xpath[XPath.attr(name.to_sym) == val]389 end390 end391 node_filter(:attributes, matcher: /.+/) do |node, name, val|392 val.is_a?(Regexp) ? node[name] =~ val : true393 end394 describe_expression_filters395end396# rubocop:enable Metrics/BlockLength...
filter.rb
Source:filter.rb
1# frozen_string_literal: true2require 'capybara/selector/filters/node_filter'3require 'capybara/selector/filters/expression_filter'4module Capybara5 class Selector6 def self.const_missing(const_name)7 case const_name8 when :Filter9 warn "DEPRECATED: Capybara::Selector::Filter is deprecated, please use Capybara::Selector::Filters::NodeFilter instead"10 Filters::NodeFilter11 when :ExpressionFilter12 warn "DEPRECATED: Capybara::Selector::ExpressionFilter is deprecated, please use Capybara::Selector::Filters::ExpressionFilter instead"13 Filters::ExpressionFilter14 else15 super16 end17 end...
expression
Using AI Code Generation
1 def search_for(search_term)2 visit('/')3 fill_in('q', :with => search_term)4 click_button('Google Search')5search.search_for('Capybara')6 def search_for(search_term)7 visit('/')8 fill_in('q', :with => search_term)9 click_button('Google Search')10search.search_for('Capybara')11 def search_for(search_term)12 visit('/')13 fill_in('q', :with => search_term)14 click_button('Google Search')
expression
Using AI Code Generation
1 Capybara.visit('/')2 def search_for(term)3session.search_for('Capybara')4 Capybara.visit('/')5 def search_for(term)6session.search_for('Capybara')
expression
Using AI Code Generation
1visit('http://www.google.com')2visit('http://www.google.com')3visit('http://www.google.com')4visit('http://www.google.com')5visit('http://www.google.com')6visit('http://www.google.com')
expression
Using AI Code Generation
1page.has_selector?(:xpath, '//*[@id="hplogo"]')2page.has_selector?(:xpath, '//*[@id="hplogo1"]')3expect(page).to have_selector(:xpath, '//*[@id="hplogo"]')4expect(page).to have_selector(:xpath, '//*[@id="hplogo1"]')
expression
Using AI Code Generation
1Capybara.current_session.find(:xpath, "//a[contains(text(),'Click Me')]").click2Capybara.current_session.find(:xpath, "//a[contains(text(),'Click Me')]").click3Capybara.current_session.find(:xpath, "//a[contains(text(),'Click Me')]").click4Capybara.current_session.find(:xpath, "//a[contains(text(),'Click Me')]").click5Capybara.current_session.find(:xpath, "//a[contains(text(),'Click Me')]").click6Capybara.current_session.find(:xpath, "//a[contains(text(),'Click Me')]").click
expression
Using AI Code Generation
1Capybara::Session.new(:selenium).visit('http://www.google.com')2Capybara::Session.new(:selenium).find(:css, 'input[name="q"]').click3Capybara::Session.new(:selenium).visit('http://www.google.com')4Capybara::Session.new(:selenium).find(:css, 'input[name="q"]').click5Capybara::Session.new(:selenium).visit('http://www.google.com')6Capybara::Session.new(:selenium).find(:css, 'input[name="q"]').click7Capybara::Session.new(:selenium).visit('http://www.google.com')8Capybara::Session.new(:selenium).find(:css, 'input[name="q"]').click9Capybara::Session.new(:selenium).visit('http://www.google.com')10Capybara::Session.new(:selenium).find(:css, 'input[name="q"]').click11 def search_for(search_term)12 visit('/')13 fill_in('q', :with => search_term)14 click_button('Google Search')
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!!