Best Capybara code snippet using Capybara.all
finders.rb
Source:finders.rb
...12 # expires. The length of time +find+ will wait is controlled through {Capybara.default_max_wait_time}13 # and defaults to 2 seconds.14 # @option options [false, Numeric] wait (Capybara.default_max_wait_time) Maximum time to wait for matching element to appear.15 #16 # +find+ takes the same options as +all+.17 #18 # page.find('#foo').find('.bar')19 # page.find(:xpath, '//div[contains(., "bar")]')20 # page.find('li', :text => 'Quox').click_link('Delete')21 #22 # @param (see Capybara::Node::Finders#all)23 #24 # @option options [Boolean] match The matching strategy to use.25 #26 # @return [Capybara::Node::Element] The found element27 # @raise [Capybara::ElementNotFound] If the element can't be found before time expires28 #29 def find(*args)30 query = Capybara::Query.new(*args)31 synchronize(query.wait) do32 if query.match == :smart or query.match == :prefer_exact33 result = query.resolve_for(self, true)34 result = query.resolve_for(self, false) if result.size == 0 && !query.exact?35 else36 result = query.resolve_for(self)37 end38 if query.match == :one or query.match == :smart and result.size > 139 raise Capybara::Ambiguous.new("Ambiguous match, found #{result.size} elements matching #{query.description}")40 end41 if result.size == 042 raise Capybara::ElementNotFound.new("Unable to find #{query.description}")43 end44 result.first45 end.tap(&:allow_reload!)46 end47 ##48 #49 # Find a form field on the page. The field can be found by its name, id or label text.50 #51 # @macro waiting_behavior52 #53 # @param [String] locator Which field to find54 #55 # @option options [Boolean] checked Match checked field?56 # @option options [Boolean] unchecked Match unchecked field?57 # @option options [Boolean] disabled (false) Match disabled field?58 # @option options [Boolean] readonly Match readonly field?59 # @option options [String] with Value of field to match on60 # @option options [String] type Type of field to match on61 # @return [Capybara::Node::Element] The found element62 #63 def find_field(locator, options={})64 find(:field, locator, options)65 end66 alias_method :field_labeled, :find_field67 ##68 #69 # Find a link on the page. The link can be found by its id or text.70 #71 # @macro waiting_behavior72 #73 # @param [String] locator Which link to find74 # @option options [String,Regexp] href Value to match against the links href75 # @return [Capybara::Node::Element] The found element76 #77 def find_link(locator, options={})78 find(:link, locator, options)79 end80 ##81 #82 # Find a button on the page.83 # This can be any \<input> element of type submit, reset, image, button or it can be a84 # \<button> element. All buttons can be found by their id, value, or title. \<button> elements can also be found85 # by their text content, and image \<input> elements by their alt attribute86 # @macro waiting_behavior87 #88 # @param [String] locator Which button to find89 # @option options [Boolean] disabled (false) Match disabled button?90 # @return [Capybara::Node::Element] The found element91 #92 def find_button(locator, options={})93 find(:button, locator, options)94 end95 ##96 #97 # Find a element on the page, given its id.98 #99 # @macro waiting_behavior100 #101 # @param [String] id Which element to find102 #103 # @return [Capybara::Node::Element] The found element104 #105 def find_by_id(id, options={})106 find(:id, id, options)107 end108 ##109 #110 # Find all elements on the page matching the given selector111 # and options.112 #113 # Both XPath and CSS expressions are supported, but Capybara114 # does not try to automatically distinguish between them. The115 # following statements are equivalent:116 #117 # page.all(:css, 'a#person_123')118 # page.all(:xpath, '//a[@id="person_123"]')119 #120 #121 # If the type of selector is left out, Capybara uses122 # {Capybara.default_selector}. It's set to :css by default.123 #124 # page.all("a#person_123")125 #126 # Capybara.default_selector = :xpath127 # page.all('//a[@id="person_123"]')128 #129 # The set of found elements can further be restricted by specifying130 # options. It's possible to select elements by their text or visibility:131 #132 # page.all('a', :text => 'Home')133 # page.all('#menu li', :visible => true)134 #135 # By default if no elements are found, an empty array is returned;136 # however, expectations can be set on the number of elements to be found which137 # will trigger Capybara's waiting behavior for the expectations to match.The138 # expectations can be set using139 #140 # page.assert_selector('p#foo', :count => 4)141 # page.assert_selector('p#foo', :maximum => 10)142 # page.assert_selector('p#foo', :minimum => 1)143 # page.assert_selector('p#foo', :between => 1..10)144 #145 # See {Capybara::Helpers#matches_count?} for additional information about146 # count matching.147 #148 # @overload all([kind], locator, options)149 # @param [:css, :xpath] kind The type of selector150 # @param [String] locator The selector151 # @option options [String, Regexp] text Only find elements which contain this text or match this regexp152 # @option options [Boolean, Symbol] visible Only find elements with the specified visibility:153 # * true - only finds visible elements.154 # * false - finds invisible _and_ visible elements.155 # * :all - same as false; finds visible and invisible elements.156 # * :hidden - only finds invisible elements.157 # * :visible - same as true; only finds visible elements.158 # @option options [Integer] count Exact number of matches that are expected to be found159 # @option options [Integer] maximum Maximum number of matches that are expected to be found160 # @option options [Integer] minimum Minimum number of matches that are expected to be found161 # @option options [Range] between Number of matches found must be within the given range162 # @option options [Boolean] exact Control whether `is` expressions in the given XPath match exactly or partially163 # @option options [Integer] wait (Capybara.default_max_wait_time) The time to wait for element count expectations to become true164 # @return [Capybara::Result] A collection of found elements165 #166 def all(*args)167 query = Capybara::Query.new(*args)168 synchronize(query.wait) do169 result = query.resolve_for(self)170 raise Capybara::ExpectationNotMet, result.failure_message unless result.matches_count?171 result172 end173 end174 alias_method :find_all, :all175 ##176 #177 # Find the first element on the page matching the given selector178 # and options, or nil if no element matches. By default no waiting179 # behavior occurs, however if {Capybara.wait_on_first_by_default} is set to true180 # it will trigger Capybara's waiting behavior for a minimum of 1 matching element to be found and181 # return the first. Waiting behavior can also be triggered by passing in any of the count182 # expectation options.183 #184 # @overload first([kind], locator, options)185 # @param [:css, :xpath] kind The type of selector186 # @param [String] locator The selector187 # @param [Hash] options Additional options; see {#all}188 # @return [Capybara::Node::Element] The found element or nil189 #190 def first(*args)191 if Capybara.wait_on_first_by_default192 options = if args.last.is_a?(Hash) then args.pop.dup else {} end193 args.push({minimum: 1}.merge(options))194 end195 all(*args).first196 rescue Capybara::ExpectationNotMet197 nil198 end199 end200 end201end...
assert_all_of_selectors_spec.rb
Source:assert_all_of_selectors_spec.rb
1# frozen_string_literal: true2Capybara::SpecHelper.spec '#assert_all_of_selectors' do3 before do4 @session.visit('/with_html')5 end6 it 'should be true if the given selectors are on the page' do7 @session.assert_all_of_selectors(:css, 'p a#foo', 'h2#h2one', 'h2#h2two')8 end9 it 'should be false if any of the given selectors are not on the page' do10 expect { @session.assert_all_of_selectors(:css, 'p a#foo', 'h2#h2three', 'h2#h2one') }.to raise_error(Capybara::ElementNotFound)11 end12 it 'should use default selector' do13 Capybara.default_selector = :css14 expect { @session.assert_all_of_selectors('p a#foo', 'h2#h2three', 'h2#h2one') }.to raise_error(Capybara::ElementNotFound)15 @session.assert_all_of_selectors('p a#foo', 'h2#h2two', 'h2#h2one')16 end17 it 'should support filter block' do18 expect { @session.assert_all_of_selectors(:css, 'h2#h2one', 'h2#h2two') { |n| n[:id] == 'h2one' } }.to raise_error(Capybara::ElementNotFound, /custom filter block/)19 end20 context 'should respect scopes' do21 it 'when used with `within`' do22 @session.within "//p[@id='first']" do23 @session.assert_all_of_selectors(".//a[@id='foo']")24 expect { @session.assert_all_of_selectors(".//a[@id='red']") }.to raise_error(Capybara::ElementNotFound)25 end26 end27 it 'when called on elements' do28 el = @session.find "//p[@id='first']"29 el.assert_all_of_selectors(".//a[@id='foo']")30 expect { el.assert_all_of_selectors(".//a[@id='red']") }.to raise_error(Capybara::ElementNotFound)31 end32 end33 context 'with options' do34 it 'should apply options to all locators' do35 @session.assert_all_of_selectors(:field, 'normal', 'additional_newline', type: :textarea)36 expect { @session.assert_all_of_selectors(:field, 'normal', 'test_field', 'additional_newline', type: :textarea) }.to raise_error(Capybara::ElementNotFound)37 end38 end39 context 'with wait', requires: [:js] do40 it 'should not raise error if all the elements appear before given wait duration' do41 Capybara.using_wait_time(0.1) do42 @session.visit('/with_js')43 @session.click_link('Click me')44 @session.assert_all_of_selectors(:css, 'a#clickable', 'a#has-been-clicked', '#drag', wait: 1.5)45 end46 end47 end48end49Capybara::SpecHelper.spec '#assert_none_of_selectors' do50 before do51 @session.visit('/with_html')52 end53 it 'should be false if any of the given locators are on the page' do54 expect { @session.assert_none_of_selectors(:xpath, '//p', '//a') }.to raise_error(Capybara::ElementNotFound)55 expect { @session.assert_none_of_selectors(:xpath, '//abbr', '//a') }.to raise_error(Capybara::ElementNotFound)56 expect { @session.assert_none_of_selectors(:css, 'p a#foo') }.to raise_error(Capybara::ElementNotFound)57 end58 it 'should be true if none of the given locators are on the page' do59 @session.assert_none_of_selectors(:xpath, '//abbr', '//td')60 @session.assert_none_of_selectors(:css, 'p a#doesnotexist', 'abbr')61 end62 it 'should use default selector' do63 Capybara.default_selector = :css64 @session.assert_none_of_selectors('p a#doesnotexist', 'abbr')65 expect { @session.assert_none_of_selectors('abbr', 'p a#foo') }.to raise_error(Capybara::ElementNotFound)66 end67 context 'should respect scopes' do68 it 'when used with `within`' do69 @session.within "//p[@id='first']" do70 expect { @session.assert_none_of_selectors(".//a[@id='foo']") }.to raise_error(Capybara::ElementNotFound)71 @session.assert_none_of_selectors(".//a[@id='red']")72 end73 end74 it 'when called on an element' do75 el = @session.find "//p[@id='first']"76 expect { el.assert_none_of_selectors(".//a[@id='foo']") }.to raise_error(Capybara::ElementNotFound)77 el.assert_none_of_selectors(".//a[@id='red']")78 end79 end80 context 'with options' do81 it 'should apply the options to all locators' do82 expect { @session.assert_none_of_selectors('//p//a', text: 'Redirect') }.to raise_error(Capybara::ElementNotFound)83 @session.assert_none_of_selectors('//p', text: 'Doesnotexist')84 end85 it 'should discard all matches where the given regexp is matched' do86 expect { @session.assert_none_of_selectors('//p//a', text: /re[dab]i/i, count: 1) }.to raise_error(Capybara::ElementNotFound)87 @session.assert_none_of_selectors('//p//a', text: /Red$/)88 end89 end90 context 'with wait', requires: [:js] do91 it 'should not find elements if they appear after given wait duration' do92 @session.visit('/with_js')93 @session.click_link('Click me')94 @session.assert_none_of_selectors(:css, '#new_field', 'a#has-been-clicked', wait: 0.1)95 end96 end97end98Capybara::SpecHelper.spec '#assert_any_of_selectors' do99 before do...
all
Using AI Code Generation
1page.save_screenshot('google.png')2session = Capybara::Session.new(:selenium)3session.save_screenshot('google.png')4session = Capybara::Session.new(:selenium)5session.save_screenshot('google.png')6session = Capybara::Session.new(:selenium)7session.save_screenshot('google.png')
all
Using AI Code Generation
1 def search_for(keyword)2 visit('/')3 fill_in('q', :with => keyword)4 click_button('Google Search')5google.search_for('capybara')
all
Using AI Code Generation
1visit('/')2fill_in('q', :with => 'Selenum')3clic_button('Google Search')4page.save_screenshot('google.png')5visit('/')6fill_in('q':wi => 'Slenium')7click_button('GoogleSearch')8page.save_screenshot('google.png')9visit('/')10fill_in('q', :with => 'Selenium')11clck_button('Google Serch12save_screenot('gogle.png')13visit('/')14fill_in('q', :with => 'Seleium')15click_buton('Google Search')
all
Using AI Code Generation
1page.save_screenshot('capybara.png'2session = Capybara::Session.new(:selenium)3session.save_screenshot('google.png')4session = Capybara::Session.new(:selenium)5session.save_screenshot('google.png')6session = Capybara::Session.new(:selenium)7session.save_screenshot('google.png')
all
Using AI Code Generation
1visit('/')2fill_in('q', :with => 'capybara')3click_button('btnG')4click_link('Capybara - Wikipedia, the free encyclopedia')5page.should have_content('Capybara')
all
Using AI Code Generation
1visit('/')2fill_in('q', :with => 'Selenium')3click_button('Google Search')4page.save_screenshot('google.png')5visit('/')6fill_in('q', :with => 'Selenium')7click_button('Google Search')8page.save_screenshot('google.png')9visit('/')10fill_in('q', :with => 'Selenium')11click_button('Google Search')12page.save_screenshot('google.png')13visit('/')14fill_in('q', :with => 'Selenium')15click_button('Google Search')16page.save_screenshot('google.png')
all
Using AI Code Generation
1visit('/')2fill_in('q', :with => 'Selenium')3click_button('Google Search')4page.save_screenshot('google.png')5visit('/')6fill_in('q', :with => 'Selenium')7click_button('Google Search')8page.save_screenshot('google.png')9visit('/')10fill_in('q', :with => 'Selenium')11click_button('Google Search')12page.save_screenshot('google.png')13visit('/')14fill_in('q', :with => 'Selenium')15click_button('Google Search')16page.save_screenshot('google.png')
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!!