Best Capybara code snippet using Capybara.current_url
visit_spec.rb
Source:visit_spec.rb
...8 end9 it 'should fetch a response from the driver with an absolute url with a port' do10 # Preparation11 @session.visit('/')12 root_uri = URI.parse(@session.current_url)13 @session.visit("http://#{root_uri.host}:#{root_uri.port}/")14 expect(@session).to have_content('Hello world!')15 @session.visit("http://#{root_uri.host}:#{root_uri.port}/foo")16 expect(@session).to have_content('Another World')17 end18 it "should fetch a response when absolute URI doesn't have a trailing slash" do19 # Preparation20 @session.visit('/foo/bar')21 root_uri = URI.parse(@session.current_url)22 @session.visit("http://#{root_uri.host}:#{root_uri.port}")23 expect(@session).to have_content('Hello world!')24 end25 it 'should fetch a response when sequentially visiting same destination with a target' do26 @session.visit('/form')27 expect(@session).to have_css('#form_title')28 @session.visit('/form#form_title')29 expect(@session).to have_css('#form_title')30 end31 it 'raises any errors caught inside the server', requires: [:server] do32 quietly { @session.visit('/error') }33 expect do34 @session.visit('/')35 end.to raise_error(TestApp::TestAppError)36 end37 it 'should be able to open non-http url', requires: [:about_scheme] do38 @session.visit('about:blank')39 @session.assert_no_selector :xpath, '/html/body/*'40 end41 context 'when Capybara.always_include_port is true' do42 let(:root_uri) do43 @session.visit('/')44 URI.parse(@session.current_url)45 end46 before do47 Capybara.always_include_port = true48 end49 after do50 Capybara.always_include_port = false51 end52 it 'should fetch a response from the driver with an absolute url without a port' do53 @session.visit("http://#{root_uri.host}/")54 expect(URI.parse(@session.current_url).port).to eq(root_uri.port)55 expect(@session).to have_content('Hello world!')56 @session.visit("http://#{root_uri.host}/foo")57 expect(URI.parse(@session.current_url).port).to eq(root_uri.port)58 expect(@session).to have_content('Another World')59 end60 it 'should add the server port to a visited url if no port specified', requires: [:server] do61 allow(@session.driver).to receive(:visit)62 @session.visit('http://www.example.com')63 expect(@session.driver).to have_received(:visit).with("http://www.example.com:#{@session.server.port}")64 end65 it 'should not override the visit specified port even if default for scheme', requires: [:server] do66 allow(@session.driver).to receive(:visit)67 @session.visit('http://www.example.com:80')68 expect(@session.driver).to have_received(:visit).with('http://www.example.com:80')69 end70 it 'should give preference to app_host port if specified', requires: [:server] do71 allow(@session.driver).to receive(:visit)72 Capybara.app_host = 'http://www.example.com:6666'73 @session.visit('/random')74 expect(@session.driver).to have_received(:visit).with('http://www.example.com:6666/random')75 end76 it "shouldn't override port if no server", requires: [:server] do77 session = Capybara::Session.new(@session.mode, nil)78 allow(session.driver).to receive(:visit)79 session.visit('http://www.google.com')80 expect(session.driver).to have_received(:visit).with('http://www.google.com')81 end82 it "shouldn't override port if no server but app_host is set", requires: [:server] do83 session = Capybara::Session.new(@session.mode, nil)84 Capybara.app_host = 'http://www.example.com:6666'85 allow(session.driver).to receive(:visit)86 session.visit('http://www.google.com')87 expect(session.driver).to have_received(:visit).with('http://www.google.com')88 end89 end90 context 'when Capybara.always_include_port is false' do91 before do92 Capybara.always_include_port = false93 end94 it "shouldn't overwrite port if app_host is set", requires: [:server] do95 session = Capybara::Session.new(@session.mode, nil)96 Capybara.app_host = 'http://www.example.com:6666'97 allow(session.driver).to receive(:visit)98 session.visit('http://www.google.com')99 expect(session.driver).to have_received(:visit).with('http://www.google.com')100 end101 it "shouldn't overwrite port if port specfified", requires: [:server] do102 session = Capybara::Session.new(@session.mode, nil)103 Capybara.app_host = 'http://www.example.com:6666'104 allow(session.driver).to receive(:visit)105 session.visit('http://www.google.com:99')106 expect(session.driver).to have_received(:visit).with('http://www.google.com:99')107 end108 end109 context 'without a server', requires: [:server] do110 it 'should respect `app_host`' do111 serverless_session = Capybara::Session.new(@session.mode, nil)112 Capybara.app_host = "http://#{@session.server.host}:#{@session.server.port}"113 serverless_session.visit('/foo')114 expect(serverless_session).to have_content('Another World')115 end116 it 'should visit a fully qualified URL' do117 serverless_session = Capybara::Session.new(@session.mode, nil)118 serverless_session.visit("http://#{@session.server.host}:#{@session.server.port}/foo")119 expect(serverless_session).to have_content('Another World')120 end121 end122 context 'with Capybara.app_host set' do123 it 'should override server', requires: [:server] do124 another_session = Capybara::Session.new(@session.mode, @session.app.dup)125 Capybara.app_host = "http://#{@session.server.host}:#{@session.server.port}"126 another_session.visit('/foo')127 expect(another_session).to have_content('Another World')128 expect(another_session.current_url).to start_with(Capybara.app_host)129 expect(URI.parse(another_session.current_url).port).not_to eq another_session.server.port130 expect(URI.parse(another_session.current_url).port).to eq @session.server.port131 end132 it 'should append relative path', requires: [:server] do133 Capybara.app_host = "http://#{@session.server.host}:#{@session.server.port}/redirect/0"134 @session.visit('/times')135 expect(@session).to have_content('redirection complete')136 end137 it 'should work if `app_host` has a trailing /', requires: [:server] do138 Capybara.app_host = "http://#{@session.server.host}:#{@session.server.port}/"139 @session.visit('/')140 expect(@session).to have_content('Hello world!')141 end142 end143 it 'should send no referer when visiting a page' do144 @session.visit '/get_referer'...
current_url_spec.rb
Source:current_url_spec.rb
1# frozen_string_literal: true2require 'capybara/spec/test_app'3Capybara::SpecHelper.spec '#current_url, #current_path, #current_host' do4 before :all do # rubocop:disable RSpec/BeforeAfterAll5 @servers = Array.new(2) { Capybara::Server.new(TestApp.new).boot }6 # sanity check7 expect(@servers[0].port).not_to eq(@servers[1].port) # rubocop:disable RSpec/ExpectInHook8 expect(@servers.map(&:port)).not_to include 80 # rubocop:disable RSpec/ExpectInHook9 end10 def bases11 @servers.map { |s| "http://#{s.host}:#{s.port}" }12 end13 def should_be_on(server_index, path = '/host', scheme = 'http')14 # Check that we are on /host on the given server15 s = @servers[server_index]16 expect(@session).to have_current_path("#{scheme}://#{s.host}:#{s.port}#{path}", url: true)17 expect(@session.current_url.chomp('?')).to eq("#{scheme}://#{s.host}:#{s.port}#{path}")18 expect(@session.current_host).to eq("#{scheme}://#{s.host}") # no port19 expect(@session.current_path).to eq(path)20 # Server should agree with us21 expect(@session).to have_content("Current host is #{scheme}://#{s.host}:#{s.port}") if path == '/host'22 end23 def visit_host_links24 @session.visit("#{bases[0]}/host_links?absolute_host=#{bases[1]}")25 end26 it 'is affected by visiting a page directly' do27 @session.visit("#{bases[0]}/host")28 should_be_on 029 end30 it 'returns to the app host when visiting a relative url' do31 Capybara.app_host = bases[1]32 @session.visit("#{bases[0]}/host")33 should_be_on 034 @session.visit('/host')35 should_be_on 136 Capybara.app_host = nil37 end38 it 'is affected by setting Capybara.app_host' do39 Capybara.app_host = bases[0]40 @session.visit('/host')41 should_be_on 042 Capybara.app_host = bases[1]43 @session.visit('/host')44 should_be_on 145 Capybara.app_host = nil46 end47 it 'is unaffected by following a relative link' do48 visit_host_links49 @session.click_link('Relative Host')50 should_be_on 051 end52 it 'is affected by following an absolute link' do53 visit_host_links54 @session.click_link('Absolute Host')55 should_be_on 156 end57 it 'is unaffected by posting through a relative form' do58 visit_host_links59 @session.click_button('Relative Host')60 should_be_on 061 end62 it 'is affected by posting through an absolute form' do63 visit_host_links64 @session.click_button('Absolute Host')65 should_be_on 166 end67 it 'is affected by following a redirect' do68 @session.visit("#{bases[0]}/redirect")69 should_be_on 0, '/landed'70 end71 it 'is affected by pushState', requires: [:js] do72 @session.visit('/with_js')73 @session.execute_script("window.history.pushState({}, '', '/pushed')")74 expect(@session.current_path).to eq('/pushed')75 end76 it 'is affected by replaceState', requires: [:js] do77 @session.visit('/with_js')78 @session.execute_script("window.history.replaceState({}, '', '/replaced')")79 expect(@session.current_path).to eq('/replaced')80 end81 it "doesn't raise exception on a nil current_url", requires: [:driver] do82 allow(@session.driver).to receive(:current_url).and_return(nil)83 @session.visit('/')84 expect { @session.current_url }.not_to raise_exception85 expect { @session.current_path }.not_to raise_exception86 end87 context 'within iframe', requires: [:frames] do88 it 'should get the url of the top level browsing context' do89 @session.visit('/within_frames')90 expect(@session.current_url).to match(/within_frames\z/)91 @session.within_frame('frameOne') do92 expect(@session.current_url).to match(/within_frames\z/)93 end94 end95 end96end...
current_url
Using AI Code Generation
1 Capybara::Selenium::Driver.new(app, :browser => :firefox)2session = Capybara::Session.new(:selenium)3session.visit('/')4 Capybara::Selenium::Driver.new(app, :browser => :firefox)5session = Capybara::Session.new(:selenium)6session.visit('/')7 Capybara::Selenium::Driver.new(app, :browser => :firefox)8session = Capybara::Session.new(:selenium)9session.visit('/')10 Capybara::Selenium::Driver.new(app, :browser => :firefox)11session = Capybara::Session.new(:selenium)12session.visit('/')
current_url
Using AI Code Generation
1visit('/')2session = Capybara::Session.new(:selenium)3session.visit('/')4visit('/')5puts find(:xpath, "//a[text()='Gmail']").current_url6visit('/')7puts all(:xpath, "//a").first.current_url8visit('/')9puts find(:xpath, "//a[text()='Gmail']").find(:xpath, ".//img").current_url
current_url
Using AI Code Generation
1 Capybara::Selenium::Driver.new(app, :browser => :firefox)2session = Capybara::Session.new(:selenium)3session.visit('/')4 Capybara::Selenium::Driver.new(app, :browser => :firefox)5session = Capybara::Session.new(:selenium)6session.visit('/')7 Capybara::Selenium::Driver.new(app, :browser => :firefox)8session = Capybara::Session.new(:selenium)9session.visit('/')
current_url
Using AI Code Generation
1 Capybara::Selenium::Driver.new(app, :browser => :firefox)2session = Capybara::Session.new(:selenium)3session.visit('/')
current_url
Using AI Code Generation
1visit('http://google.com')2visit('http://google.com')3visit('http://google.com')4visit('http://google.com')5puts find(:xpath, "//body").current_url6Your name to display (optional):7Your name to display (optional):8Your name to display (optional):
current_url
Using AI Code Generation
1session = Capybara::Session.new(:selenium)2session.visit('/')3session = Capybara::Session.new(:selenium)4session.visit('/')5session = Capybara::Session.new(:selenium)6session.visit('/')7session = Capybara::Session.new(:selenium)8session.visit('/')9session = Capybara::Session.new(:selenium)10session.visit('/')
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!!