How to use visit method of Capybara Package

Best Capybara code snippet using Capybara.visit

visit_spec.rb

Source:visit_spec.rb Github

copy

Full Screen

1# frozen_string_literal: true2Capybara::SpecHelper.spec '#visit' do3 it 'should fetch a response from the driver with a relative url' do4 @session.visit('/')5 expect(@session).to have_content('Hello world!')6 @session.visit('/foo')7 expect(@session).to have_content('Another World')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'145 expect(@session).to have_content 'No referer'146 end147 it 'should send no referer when visiting a second page' do148 @session.visit '/get_referer'149 @session.visit '/get_referer'150 expect(@session).to have_content 'No referer'151 end152 it 'should send a referer when following a link' do153 @session.visit '/referer_base'154 @session.find('//a[@href="/get_referer"]').click155 expect(@session).to have_content %r{http://.*/referer_base}156 end157 it 'should preserve the original referer URL when following a redirect' do158 @session.visit('/referer_base')159 @session.find('//a[@href="/redirect_to_get_referer"]').click160 expect(@session).to have_content %r{http://.*/referer_base}161 end162 it 'should send a referer when submitting a form' do163 @session.visit '/referer_base'164 @session.find('//input').click165 expect(@session).to have_content %r{http://.*/referer_base}166 end167 it 'can set cookie if a blank path is specified' do168 @session.visit('')169 @session.visit('/get_cookie')170 expect(@session).to have_content('root cookie')171 end172end...

Full Screen

Full Screen

rack_test_spec.rb

Source:rack_test_spec.rb Github

copy

Full Screen

...30 end31 describe '#click_link' do32 it "should use data-method if option is true" do33 @session.driver.options[:respect_data_method] = true34 @session.visit "/with_html"35 @session.click_link "A link with data-method"36 expect(@session.html).to include('The requested object was deleted')37 end38 it "should not use data-method if option is false" do39 @session.driver.options[:respect_data_method] = false40 @session.visit "/with_html"41 @session.click_link "A link with data-method"42 expect(@session.html).to include('Not deleted')43 end44 it "should use data-method if available even if it's capitalized" do45 @session.driver.options[:respect_data_method] = true46 @session.visit "/with_html"47 @session.click_link "A link with capitalized data-method"48 expect(@session.html).to include('The requested object was deleted')49 end50 after do51 @session.driver.options[:respect_data_method] = false52 end53 end54 describe "#fill_in" do55 it "should warn that :fill_options are not supported" do56 expect_any_instance_of(Capybara::Node::Element).to receive(:warn)57 .with("Options passed to Capybara::Node#set but the driver doesn't support them")58 @session.visit "/with_html"59 @session.fill_in 'test_field', with: 'not_moneky', fill_options: { random: true }60 end61 end62 describe "#attach_file" do63 context "with multipart form" do64 it "should submit an empty form-data section if no file is submitted" do65 @session.visit("/form")66 @session.click_button("Upload Empty")67 expect(@session.html).to include('Successfully ignored empty file field.')68 end69 end70 end71 end72end73RSpec.describe Capybara::RackTest::Driver do74 before do75 @driver = TestSessions::RackTest.driver76 end77 describe ':headers option' do78 it 'should always set headers' do79 @driver = Capybara::RackTest::Driver.new(TestApp, :headers => {'HTTP_FOO' => 'foobar'})80 @driver.visit('/get_header')81 expect(@driver.html).to include('foobar')82 end83 it 'should keep headers on link clicks' do84 @driver = Capybara::RackTest::Driver.new(TestApp, :headers => {'HTTP_FOO' => 'foobar'})85 @driver.visit('/header_links')86 @driver.find_xpath('.//a').first.click87 expect(@driver.html).to include('foobar')88 end89 it 'should keep headers on form submit' do90 @driver = Capybara::RackTest::Driver.new(TestApp, :headers => {'HTTP_FOO' => 'foobar'})91 @driver.visit('/header_links')92 @driver.find_xpath('.//input').first.click93 expect(@driver.html).to include('foobar')94 end95 it 'should keep headers on redirects' do96 @driver = Capybara::RackTest::Driver.new(TestApp, :headers => {'HTTP_FOO' => 'foobar'})97 @driver.visit('/get_header_via_redirect')98 expect(@driver.html).to include('foobar')99 end100 end101 describe ':follow_redirects option' do102 it "defaults to following redirects" do103 @driver = Capybara::RackTest::Driver.new(TestApp)104 @driver.visit('/redirect')105 expect(@driver.response.header['Location']).to be_nil106 expect(@driver.current_url).to match %r{/landed$}107 end108 it "is possible to not follow redirects" do109 @driver = Capybara::RackTest::Driver.new(TestApp, :follow_redirects => false)110 @driver.visit('/redirect')111 expect(@driver.response.header['Location']).to match %r{/redirect_again$}112 expect(@driver.current_url).to match %r{/redirect$}113 end114 end115 describe ':redirect_limit option' do116 context "with default redirect limit" do117 before do118 @driver = Capybara::RackTest::Driver.new(TestApp)119 end120 it "should follow 5 redirects" do121 @driver.visit("/redirect/5/times")122 expect(@driver.html).to include('redirection complete')123 end124 it "should not follow more than 6 redirects" do125 expect do126 @driver.visit("/redirect/6/times")127 end.to raise_error(Capybara::InfiniteRedirectError)128 end129 end130 context "with 21 redirect limit" do131 before do132 @driver = Capybara::RackTest::Driver.new(TestApp, :redirect_limit => 21)133 end134 it "should follow 21 redirects" do135 @driver.visit("/redirect/21/times")136 expect(@driver.html).to include('redirection complete')137 end138 it "should not follow more than 21 redirects" do139 expect do140 @driver.visit("/redirect/22/times")141 end.to raise_error(Capybara::InfiniteRedirectError)142 end143 end144 end145end146module CSSHandlerIncludeTester147 def dont_extend_css_handler148 raise 'should never be called'149 end150end151include CSSHandlerIncludeTester152RSpec.describe Capybara::RackTest::CSSHandlers do153 it "should not be extended by global includes" do154 expect(Capybara::RackTest::CSSHandlers.new).not_to respond_to(:dont_extend_css_handler)...

Full Screen

Full Screen

rspec_spec.rb

Source:rspec_spec.rb Github

copy

Full Screen

2require 'spec_helper'3RSpec.describe 'capybara/rspec' do4 context 'Feature', type: :feature do5 it 'should include Capybara in rspec' do6 visit('/foo')7 expect(page.body).to include('Another World')8 end9 it 'should include RSpec matcher proxies' do10 expect(self.class.ancestors).to include Capybara::RSpecMatcherProxies11 end12 context 'resetting session' do13 it 'sets a cookie in one example...' do14 visit('/set_cookie')15 expect(page.body).to include('Cookie set to test_cookie')16 end17 it '...then it is not available in the next' do18 visit('/get_cookie')19 expect(page.body).not_to include('test_cookie')20 end21 end22 context 'setting the current driver' do23 it 'sets the current driver in one example...' do24 Capybara.current_driver = :selenium25 end26 it '...then it has returned to the default in the next example' do27 expect(Capybara.current_driver).to eq(:rack_test)28 end29 end30 it 'switches to the javascript driver when giving it as metadata', js: true do31 expect(Capybara.current_driver).to eq(Capybara.javascript_driver)32 end33 it 'switches to the given driver when giving it as metadata', driver: :culerity do34 expect(Capybara.current_driver).to eq(:culerity)35 end36 describe '#all' do37 it 'allows access to the Capybara finder' do38 visit('/with_html')39 found = all(:css, 'h2') { |element| element[:class] == 'head' }40 expect(found.size).to eq(5)41 end42 it 'allows access to the RSpec matcher' do43 visit('/with_html')44 strings = %w[test1 test2]45 expect(strings).to all(be_a(String))46 end47 end48 describe '#within' do49 it 'allows access to the Capybara scoper' do50 visit('/with_html')51 expect do52 within(:css, '#does_not_exist') { click_link 'Go to simple' }53 end.to raise_error(Capybara::ElementNotFound)54 end55 it 'allows access to the RSpec matcher' do56 visit('/with_html')57 # This reads terribly, but must call #within58 expect(find(:css, 'span.number').text.to_i).to within(1).of(41)59 end60 end61 end62 context 'Type: Other', type: :other do63 context 'when RSpec::Matchers is included after Capybara::DSL' do64 let(:test_class_instance) do65 Class.new do66 include Capybara::DSL67 include RSpec::Matchers68 end.new69 end70 describe '#all' do71 it 'allows access to the Capybara finder' do72 test_class_instance.visit('/with_html')73 expect(test_class_instance.all(:css, 'h2.head').size).to eq(5)74 end75 it 'allows access to the RSpec matcher' do76 test_class_instance.visit('/with_html')77 strings = %w[test1 test2]78 expect(strings).to test_class_instance.all(be_a(String))79 end80 end81 describe '#within' do82 it 'allows access to the Capybara scoper' do83 test_class_instance.visit('/with_html')84 expect do85 test_class_instance.within(:css, '#does_not_exist') { test_class_instance.click_link 'Go to simple' }86 end.to raise_error(Capybara::ElementNotFound)87 end88 it 'allows access to the RSpec matcher' do89 test_class_instance.visit('/with_html')90 # This reads terribly, but must call #within91 expect(test_class_instance.find(:css, 'span.number').text.to_i).to test_class_instance.within(1).of(41)92 end93 end94 context 'when `match_when_negated` is not defined in a matcher' do95 before do96 RSpec::Matchers.define :only_match_matcher do |expected|97 match do |actual|98 !(actual ^ expected)99 end100 end101 end102 it 'can be called with `not_to`' do103 # This test is for a bug in jruby where `super` isn't defined correctly - https://github.com/jruby/jruby/issues/4678104 # Reported in https://github.com/teamcapybara/capybara/issues/2115105 test_class_instance.instance_eval do106 expect do107 expect(true).not_to only_match_matcher(false) # rubocop:disable RSpec/ExpectActual108 end.not_to raise_error109 end110 end111 end112 end113 it 'should not include Capybara' do114 expect { visit('/') }.to raise_error(NoMethodError)115 end116 end117end118feature 'Feature DSL' do119 scenario 'is pulled in' do120 visit('/foo')121 expect(page.body).to include('Another World')122 end123end...

Full Screen

Full Screen

visit

Using AI Code Generation

copy

Full Screen

1 visit('/')2 fill_in('q', :with => 'Hello World')3 click_button('Google Search')4 visit('/')5 fill_in('q', :with => 'Hello World')6 click_button('Google Search')

Full Screen

Full Screen

visit

Using AI Code Generation

copy

Full Screen

1visit('/')2visit('/')3fill_in('q', :with => 'Capybara')4click_button('Google Search')5visit('/')6fill_in('q', :with => 'Capybara')7click_button('Google Search')8puts page.has_content?('Capybara')9visit('/')10fill_in('q', :with => 'Capybara')11click_button('Google Search')12puts page.has_content?('Capybara')13puts page.has_content?('Ruby')14puts page.has_content?('Python')15visit('/')16fill_in('q',

Full Screen

Full Screen

visit

Using AI Code Generation

copy

Full Screen

1page.save_screenshot('google.png')2Capybara.page.save_screenshot('google.png')3session = Capybara::Session.new(:selenium)4session.page.save_screenshot('google.png')5session = Capybara::Session.new(:selenium)6session.page.save_screenshot('google.png')7The code in file 4.rb will not work as expected. The reason is that Capybara::Session.new(:selenium) will return a new instance of Capybara::Session. This instance will not have any methods defined in Capybara::DSL. So, session.visit will not work as expected. The following code will work as

Full Screen

Full Screen

visit

Using AI Code Generation

copy

Full Screen

1page.save_screenshot('google.png')2page.save_screenshot('google.png')3page.save_screenshot('google1.png')4page.save_screenshot('google.png')5page.save_screenshot('google1.png')6page.save_screenshot('google2.png')7page.save_screenshot('google.png')8page.save_screenshot('google1.png')9page.save_screenshot('google2.png')10page.save_screenshot('google3.png')11page.save_screenshot('google.png')12page.save_screenshot('google1.png')13page.save_screenshot('google2.png')14page.save_screenshot('google3.png')15page.save_screenshot('google4.png')

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.

Run Capybara automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful