Best Capybara code snippet using Capybara.xpath
find_spec.rb
Source:find_spec.rb
...83 expect(e.message).not_to match(/you may be passing a CSS selector or XPath expression/)84 end85 end86 end87 context 'with xpath selectors' do88 it 'should find the first element using the given locator' do89 expect(@session.find(:xpath, '//h1').text).to eq('This is a test')90 expect(@session.find(:xpath, "//input[@id='test_field']").value).to eq('monkey')91 end92 it 'should warn if passed a non-valid locator type' do93 expect_any_instance_of(Kernel).to receive(:warn).with(/must respond to to_xpath or be an instance of String/)94 expect { @session.find(:xpath, 123) }.to raise_error # rubocop:disable RSpec/UnspecifiedException95 end96 end97 context 'with custom selector' do98 it 'should use the custom selector' do99 Capybara.add_selector(:beatle) do100 xpath { |name| ".//*[@id='#{name}']" }101 end102 expect(@session.find(:beatle, 'john').text).to eq('John')103 expect(@session.find(:beatle, 'paul').text).to eq('Paul')104 end105 end106 context 'with custom selector with custom `match` block' do107 it 'should use the custom selector when locator matches the block' do108 Capybara.add_selector(:beatle) do109 xpath { |num| ".//*[contains(@class, 'beatle')][#{num}]" }110 match { |value| value.is_a?(Integer) }111 end112 expect(@session.find(:beatle, '2').text).to eq('Paul')113 expect(@session.find(1).text).to eq('John')114 expect(@session.find(2).text).to eq('Paul')115 expect(@session.find('//h1').text).to eq('This is a test')116 end117 end118 context 'with custom selector with custom filter' do119 before do120 Capybara.add_selector(:beatle) do121 xpath { |name| ".//li[contains(@class, 'beatle')][contains(text(), '#{name}')]" }122 node_filter(:type) { |node, type| node[:class].split(/\s+/).include?(type) }123 node_filter(:fail) { |_node, _val| raise Capybara::ElementNotFound, 'fail' }124 end125 end126 it 'should find elements that match the filter' do127 expect(@session.find(:beatle, 'Paul', type: 'drummer').text).to eq('Paul')128 expect(@session.find(:beatle, 'Ringo', type: 'drummer').text).to eq('Ringo')129 end130 it 'ignores filter when it is not given' do131 expect(@session.find(:beatle, 'Paul').text).to eq('Paul')132 expect(@session.find(:beatle, 'John').text).to eq('John')133 end134 it "should not find elements that don't match the filter" do135 expect { @session.find(:beatle, 'John', type: 'drummer') }.to raise_error(Capybara::ElementNotFound)136 expect { @session.find(:beatle, 'George', type: 'drummer') }.to raise_error(Capybara::ElementNotFound)137 end138 it 'should not raise an ElementNotFound error from in a filter' do139 expect { @session.find(:beatle, 'John', fail: 'something') }.to raise_error(Capybara::ElementNotFound, /beatle "John"/)140 end141 end142 context 'with custom selector with custom filter and default' do143 before do144 Capybara.add_selector(:beatle) do145 xpath { |name| ".//li[contains(@class, 'beatle')][contains(text(), '#{name}')]" }146 node_filter(:type, default: 'drummer') { |node, type| node[:class].split(/\s+/).include?(type) }147 end148 end149 it 'should find elements that match the filter' do150 expect(@session.find(:beatle, 'Paul', type: 'drummer').text).to eq('Paul')151 expect(@session.find(:beatle, 'Ringo', type: 'drummer').text).to eq('Ringo')152 end153 it 'should use default value when filter is not given' do154 expect(@session.find(:beatle, 'Paul').text).to eq('Paul')155 expect { @session.find(:beatle, 'John') }.to raise_error(Capybara::ElementNotFound)156 end157 it "should not find elements that don't match the filter" do158 expect { @session.find(:beatle, 'John', type: 'drummer') }.to raise_error(Capybara::ElementNotFound)159 expect { @session.find(:beatle, 'George', type: 'drummer') }.to raise_error(Capybara::ElementNotFound)160 end161 end162 context 'with alternate filter set' do163 before do164 Capybara::Selector::FilterSet.add(:value) do165 node_filter(:with) { |node, with| node.value == with.to_s }166 end167 Capybara.add_selector(:id_with_field_filters) do168 xpath { |id| XPath.descendant[XPath.attr(:id) == id.to_s] }169 filter_set(:field)170 end171 end172 it 'should allow use of filters from custom filter set' do173 expect(@session.find(:id, 'test_field', filter_set: :value, with: 'monkey').value).to eq('monkey')174 expect { @session.find(:id, 'test_field', filter_set: :value, with: 'not_monkey') }.to raise_error(Capybara::ElementNotFound)175 end176 it 'should allow use of filter set from a different selector' do177 expect(@session.find(:id, 'test_field', filter_set: :field, with: 'monkey').value).to eq('monkey')178 expect { @session.find(:id, 'test_field', filter_set: :field, with: 'not_monkey') }.to raise_error(Capybara::ElementNotFound)179 end180 it 'should allow importing of filter set into selector' do181 expect(@session.find(:id_with_field_filters, 'test_field', with: 'monkey').value).to eq('monkey')182 expect { @session.find(:id_with_field_filters, 'test_field', with: 'not_monkey') }.to raise_error(Capybara::ElementNotFound)183 end184 end185 context 'with css as default selector' do186 before { Capybara.default_selector = :css }187 after { Capybara.default_selector = :xpath }188 it 'should find the first element using the given locator' do189 expect(@session.find('h1').text).to eq('This is a test')190 expect(@session.find("input[id='test_field']").value).to eq('monkey')191 end192 end193 it 'should raise ElementNotFound with a useful default message if nothing was found' do194 expect do195 @session.find(:xpath, '//div[@id="nosuchthing"]').to be_nil196 end.to raise_error(Capybara::ElementNotFound, 'Unable to find xpath "//div[@id=\\"nosuchthing\\"]"')197 end198 it 'should accept an XPath instance' do199 @session.visit('/form')200 @xpath = Capybara::Selector[:fillable_field].call('First Name')201 expect(@xpath).to be_a(::XPath::Union)202 expect(@session.find(@xpath).value).to eq('John')203 end204 context 'with :exact option' do205 it 'matches exactly when true' do206 expect(@session.find(:xpath, XPath.descendant(:input)[XPath.attr(:id).is('test_field')], exact: true).value).to eq('monkey')207 expect do208 @session.find(:xpath, XPath.descendant(:input)[XPath.attr(:id).is('est_fiel')], exact: true)209 end.to raise_error(Capybara::ElementNotFound)210 end211 it 'matches loosely when false' do212 expect(@session.find(:xpath, XPath.descendant(:input)[XPath.attr(:id).is('test_field')], exact: false).value).to eq('monkey')213 expect(@session.find(:xpath, XPath.descendant(:input)[XPath.attr(:id).is('est_fiel')], exact: false).value).to eq('monkey')214 end215 it 'defaults to `Capybara.exact`' do216 Capybara.exact = true217 expect do218 @session.find(:xpath, XPath.descendant(:input)[XPath.attr(:id).is('est_fiel')])219 end.to raise_error(Capybara::ElementNotFound)220 Capybara.exact = false221 @session.find(:xpath, XPath.descendant(:input)[XPath.attr(:id).is('est_fiel')])222 end223 it 'warns when the option has no effect' do224 expect_any_instance_of(Kernel).to receive(:warn)225 .with('The :exact option only has an effect on queries using the XPath#is method. Using it with the query "#test_field" has no effect.')226 @session.find(:css, '#test_field', exact: true)227 end228 end229 context 'with :match option' do230 context 'when set to `one`' do231 it 'raises an error when multiple matches exist' do232 expect do233 @session.find(:css, '.multiple', match: :one)234 end.to raise_error(Capybara::Ambiguous)235 end236 it 'raises an error even if there the match is exact and the others are inexact' do237 expect do238 @session.find(:xpath, XPath.descendant[XPath.attr(:class).is('almost_singular')], exact: false, match: :one)239 end.to raise_error(Capybara::Ambiguous)240 end241 it 'returns the element if there is only one' do242 expect(@session.find(:css, '.singular', match: :one).text).to eq('singular')243 end244 it 'raises an error if there is no match' do245 expect do246 @session.find(:css, '.does-not-exist', match: :one)247 end.to raise_error(Capybara::ElementNotFound)248 end249 end250 context 'when set to `first`' do251 it 'returns the first matched element' do252 expect(@session.find(:css, '.multiple', match: :first).text).to eq('multiple one')253 end254 it 'raises an error if there is no match' do255 expect do256 @session.find(:css, '.does-not-exist', match: :first)257 end.to raise_error(Capybara::ElementNotFound)258 end259 end260 context 'when set to `smart`' do261 context 'and `exact` set to `false`' do262 it 'raises an error when there are multiple exact matches' do263 expect do264 @session.find(:xpath, XPath.descendant[XPath.attr(:class).is('multiple')], match: :smart, exact: false)265 end.to raise_error(Capybara::Ambiguous)266 end267 it 'finds a single exact match when there also are inexact matches' do268 result = @session.find(:xpath, XPath.descendant[XPath.attr(:class).is('almost_singular')], match: :smart, exact: false)269 expect(result.text).to eq('almost singular')270 end271 it 'raises an error when there are multiple inexact matches' do272 expect do273 @session.find(:xpath, XPath.descendant[XPath.attr(:class).is('almost_singul')], match: :smart, exact: false)274 end.to raise_error(Capybara::Ambiguous)275 end276 it 'finds a single inexact match' do277 result = @session.find(:xpath, XPath.descendant[XPath.attr(:class).is('almost_singular but')], match: :smart, exact: false)278 expect(result.text).to eq('almost singular but not quite')279 end280 it 'raises an error if there is no match' do281 expect do282 @session.find(:xpath, XPath.descendant[XPath.attr(:class).is('does-not-exist')], match: :smart, exact: false)283 end.to raise_error(Capybara::ElementNotFound)284 end285 end286 context 'with `exact` set to `true`' do287 it 'raises an error when there are multiple exact matches' do288 expect do289 @session.find(:xpath, XPath.descendant[XPath.attr(:class).is('multiple')], match: :smart, exact: true)290 end.to raise_error(Capybara::Ambiguous)291 end292 it 'finds a single exact match when there also are inexact matches' do293 result = @session.find(:xpath, XPath.descendant[XPath.attr(:class).is('almost_singular')], match: :smart, exact: true)294 expect(result.text).to eq('almost singular')295 end296 it 'raises an error when there are multiple inexact matches' do297 expect do298 @session.find(:xpath, XPath.descendant[XPath.attr(:class).is('almost_singul')], match: :smart, exact: true)299 end.to raise_error(Capybara::ElementNotFound)300 end301 it 'raises an error when there is a single inexact matches' do302 expect do303 @session.find(:xpath, XPath.descendant[XPath.attr(:class).is('almost_singular but')], match: :smart, exact: true)304 end.to raise_error(Capybara::ElementNotFound)305 end306 it 'raises an error if there is no match' do307 expect do308 @session.find(:xpath, XPath.descendant[XPath.attr(:class).is('does-not-exist')], match: :smart, exact: true)309 end.to raise_error(Capybara::ElementNotFound)310 end311 end312 end313 context 'when set to `prefer_exact`' do314 context 'and `exact` set to `false`' do315 it 'picks the first one when there are multiple exact matches' do316 result = @session.find(:xpath, XPath.descendant[XPath.attr(:class).is('multiple')], match: :prefer_exact, exact: false)317 expect(result.text).to eq('multiple one')318 end319 it 'finds a single exact match when there also are inexact matches' do320 result = @session.find(:xpath, XPath.descendant[XPath.attr(:class).is('almost_singular')], match: :prefer_exact, exact: false)321 expect(result.text).to eq('almost singular')322 end323 it 'picks the first one when there are multiple inexact matches' do324 result = @session.find(:xpath, XPath.descendant[XPath.attr(:class).is('almost_singul')], match: :prefer_exact, exact: false)325 expect(result.text).to eq('almost singular but not quite')326 end327 it 'finds a single inexact match' do328 result = @session.find(:xpath, XPath.descendant[XPath.attr(:class).is('almost_singular but')], match: :prefer_exact, exact: false)329 expect(result.text).to eq('almost singular but not quite')330 end331 it 'raises an error if there is no match' do332 expect do333 @session.find(:xpath, XPath.descendant[XPath.attr(:class).is('does-not-exist')], match: :prefer_exact, exact: false)334 end.to raise_error(Capybara::ElementNotFound)335 end336 end337 context 'with `exact` set to `true`' do338 it 'picks the first one when there are multiple exact matches' do339 result = @session.find(:xpath, XPath.descendant[XPath.attr(:class).is('multiple')], match: :prefer_exact, exact: true)340 expect(result.text).to eq('multiple one')341 end342 it 'finds a single exact match when there also are inexact matches' do343 result = @session.find(:xpath, XPath.descendant[XPath.attr(:class).is('almost_singular')], match: :prefer_exact, exact: true)344 expect(result.text).to eq('almost singular')345 end346 it 'raises an error if there are multiple inexact matches' do347 expect do348 @session.find(:xpath, XPath.descendant[XPath.attr(:class).is('almost_singul')], match: :prefer_exact, exact: true)349 end.to raise_error(Capybara::ElementNotFound)350 end351 it 'raises an error if there is a single inexact match' do352 expect do353 @session.find(:xpath, XPath.descendant[XPath.attr(:class).is('almost_singular but')], match: :prefer_exact, exact: true)354 end.to raise_error(Capybara::ElementNotFound)355 end356 it 'raises an error if there is no match' do357 expect do358 @session.find(:xpath, XPath.descendant[XPath.attr(:class).is('does-not-exist')], match: :prefer_exact, exact: true)359 end.to raise_error(Capybara::ElementNotFound)360 end361 end362 end363 it 'defaults to `Capybara.match`' do364 Capybara.match = :one365 expect do366 @session.find(:css, '.multiple')367 end.to raise_error(Capybara::Ambiguous)368 Capybara.match = :first369 expect(@session.find(:css, '.multiple').text).to eq('multiple one')370 end371 it 'raises an error when unknown option given' do372 expect do373 @session.find(:css, '.singular', match: :schmoo)374 end.to raise_error(ArgumentError)375 end376 end377 it 'supports a custom filter block' do378 expect(@session.find(:css, 'input', &:disabled?)[:name]).to eq('disabled_text')379 end380 context 'within a scope' do381 before do382 @session.visit('/with_scope')383 end384 it 'should find the an element using the given locator' do385 @session.within(:xpath, "//div[@id='for_bar']") do386 expect(@session.find('.//li[1]').text).to match(/With Simple HTML/)387 end388 end389 it 'should support pseudo selectors' do390 @session.within(:xpath, "//div[@id='for_bar']") do391 expect(@session.find(:css, 'input:disabled').value).to eq('James')392 end393 end394 end395 it 'should raise if selector type is unknown' do396 expect do397 @session.find(:unknown, '//h1')398 end.to raise_error(ArgumentError)399 end400 context 'with Capybara.test_id' do401 it 'should not match on it when nil' do402 Capybara.test_id = nil403 expect(@session).not_to have_field('test_id')404 end...
xpath
Using AI Code Generation
1 visit('/')2 all(:xpath, '//h3/a').map(&:text)3 visit('/')4 all(:css, 'h3.r a').map(&:text)5 visit('/')6 all(:css, 'h3.r a').map(&:text)
xpath
Using AI Code Generation
1 def xpath(*args)2 find(:xpath, *args)3xpath('//input[@name="q"]')4 def xpath(*args)5 find(:xpath, *args)6xpath('//input[@name="q"]')7xpath('//input[@name="q"]')
xpath
Using AI Code Generation
1visit('/')2fill_in('q', :with => 'ruby')3click_button('btnG')4Capybara.current_session.driver.browser.save_screenshot("google.png")5puts page.xpath('//h3/a')6puts page.xpath('//h3/a').text7puts page.xpath('//h3/a').first.text8puts page.xpath('//h3/a').last.text9puts page.xpath('//h3/a').count10puts page.xpath('//h3/a').first['href']11puts page.xpath('//h3/a').last['href']12puts page.xpath('//h3/a').last['href'].class13puts page.xpath('//h3/a').last['href'].split('/')14puts page.xpath('//h3/a').last['href'].split('/').last15puts page.xpath('//h3/a').last['href'].split('/').last.class16puts page.xpath('//h3/a').last['href'].split('/').last.split('?')17puts page.xpath('//h3/a').last['href'].split('/').last.split('?').first18puts page.xpath('//h3/a').last['href'].split('/').last.split('?').first.class19puts page.xpath('//h3/a').last['href'].split('/').last.split('?').first.split('.')20puts page.xpath('//h3/a').last['href'].split('/').last.split('?').first.split('.').last21puts page.xpath('//
xpath
Using AI Code Generation
1page.save_screenshot('google.png', :full => true)2page.save_page('google.html')3page.xpath('//input').each do |input|4doc = Nokogiri::HTML(open('google.html'))5doc.xpath('//input').each do |input|6page.xpath('//input').each do |input|7doc = Nokogiri::HTML(open('google.html'))8doc.xpath('//input').each do |input|9page.xpath('//input').each do |input|10doc = Nokogiri::HTML(open('google.html'))11doc.xpath('//input').each do |input|12page.xpath('//input').each do |input|13doc = Nokogiri::HTML(open('google.html'))14doc.xpath('//input').each do |input|15page.xpath('//input').each do |input|16doc = Nokogiri::HTML(open('google.html'))17doc.xpath('//input').each do |input|18page.xpath('//input').each do |input|19doc = Nokogiri::HTML(open('google.html'))20doc.xpath('//input').each do |input|21page.xpath('//input').each do |input|
xpath
Using AI Code Generation
1page.find(:xpath, '//div[@id="my_div"]')2page.find(:xpath, '//div[@id="my_div"]')3page.find('div', :id => 'my_div')4page.find('div', :id => 'my_div', :class => 'my_class')5page.find(:css, 'div.my_class', :id => 'my_div')6page.find(:css, 'div.my_class', :id => 'my_div', :class => 'my_class')7page.find(:css, 'div.my_class', :id => 'my_div', :class => 'my_class', :name => 'my_name')8page.find(:css, 'div.my_class', :id => 'my_div', :class => 'my_class', :name => 'my_name', :text => 'my_text')
xpath
Using AI Code Generation
1 find(:xpath, "//input[@name='q']").set("Hello World")2doc = Nokogiri::HTML(open('google.html'))3doc.xpath('//input').each do |input|4page.xpath('//input').each do |input|5doc = Nokogiri::HTML(open('google.html'))6doc.xpath('//input').each do |input|7page.xpath('//input').each do |input|
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!!