Best Capybara code snippet using Capybara.Driver.set
capybara_spec.rb
Source:capybara_spec.rb
...3require 'sauce/connect'4#require 'capybara/server'5describe Sauce::Capybara do6 after :each do7 Sauce::Utilities::Connect.instance_variable_set(:@tunnel, false)8 end9 describe Sauce::Capybara::Driver do10 before :each do11 ::Capybara::Server.any_instance.stub(:boot).and_return(true)12 end13 let(:app) { proc { |env| [200, {}, ["Hello Sauce!"]]} }14 let(:driver) { Sauce::Capybara::Driver.new(app) }15 after :each do 16 Capybara.reset_sessions!17 end18 describe "#body" do19 context "With Capybara 1.x", :capybara_version => [1, "1.9.9"] do20 it "should not exist in version 2" do21 driver.should respond_to :base_body22 end23 end24 context "With Capybara 2.x", :capybara_version => [2, "2.9.9"] do25 it "should not exist" do26 driver.should_not respond_to :base_body27 end28 end29 end30 describe "#source" do31 context "With Capybara 1", :capybara_version => [1, "1.9.9"] do32 it "should exist" do33 driver.should respond_to :base_source34 end35 end36 context "with Capybara 2.x", :capybara_version => [2, "2.9.9"] do37 it "should not exist" do38 driver.should_not respond_to :base_source39 end40 end41 end42 describe "#html" do43 context "With Capybara 1.x", :capybara_version => [1, "1.9.9"] do44 it "should not exist" do45 driver.should_not respond_to :base_html46 end47 end48 context "With Capybara 2.x", :capybara_version => [2, "2.9.9"] do49 it "should exist" do50 driver.should respond_to :base_html51 end52 end53 end54 describe '#finish' do55 let(:browser) { double('Sauce::Selenium2 mock') }56 before :each do57 driver.instance_variable_set(:@browser, browser)58 end59 it 'should quit the @browser' do60 browser.should_receive(:quit)61 driver.finish!62 end63 it 'should nil out @browser' do64 browser.stub(:quit)65 driver.finish!66 expect(driver.instance_variable_get(:@browser)).to be_nil67 end68 end69 describe '#rspec_browser' do70 let(:driver) { Sauce::Capybara::Driver.new(app) }71 before :each do72 Sauce::Selenium2.stub(:new).and_return(nil)73 end74 context "with no rspec driver" do75 before :each do76 Sauce.stub(:driver_pool).and_return({})77 end78 it "should return nil" do79 driver.rspec_browser.should be nil80 end81 it "should set the rspec_driver flag to false" do82 driver.rspec_browser83 driver.instance_variable_get(:@using_rspec_browser).should be_false84 end85 end86 context "with an rspec driver" do87 let(:mock_driver) {Object.new}88 before :each do89 Sauce.stub(:driver_pool).and_return({Thread.current.object_id => mock_driver})90 end91 it "should return the driver" do92 driver.rspec_browser.should be mock_driver93 end94 it "should set the rspec_driver flag to true" do95 driver.rspec_browser96 driver.instance_variable_get(:@using_rspec_browser).should be_true97 end98 end99 context "called after a driver_pool change" do100 context "with no driver present" do101 let(:mock_driver) {Object.new}102 before (:each) do103 Sauce.stub(:driver_pool).and_return(104 {Thread.current.object_id => mock_driver},105 {Thread.current.object_id => nil}106 )107 end108 it "should return nil" do109 driver.rspec_browser.should eq mock_driver110 driver.rspec_browser.should be nil111 end112 it "should set rspec_browser flag false" do113 driver.rspec_browser114 driver.rspec_browser115 driver.instance_variable_get(:@using_rspec_browser).should be_false116 end117 end118 end119 end120 describe '#browser' do121 let(:driver) { Sauce::Capybara::Driver.new(app) }122 before :each do123 # Stub out the selenium driver startup124 Sauce::Selenium2.stub(:new).and_return(nil)125 end126 context "when there is a driver in the driver pool" do127 let(:mock_browser) {Object.new}128 before :each do129 Sauce.driver_pool[Thread.current.object_id] = mock_browser130 end131 it "should use the driver_pools browser" do132 driver.browser.should eq mock_browser133 end134 end135 context 'when tunneling is disabled' do136 it 'should not call #connect_tunnel' do137 Sauce::Capybara.should_receive(:connect_tunnel).never138 Sauce.stub(:get_config) {{:start_tunnel => false}}139 driver.browser140 end141 end142 end143 describe '#find' do144 let(:selector) { '#lol' }145 context "with Capybara < 2.1", :capybara_version => [2, "2.0.9"] do146 it "should exist" do147 driver.respond_to?(:find).should be_true148 end149 context 'with an environment override' do150 before :each do151 ENV['SAUCE_DISABLE_RETRY'] = '1'152 end153 it 'should not retry and raise the error' do154 driver.should_receive(:base_find).with(selector).and_raise(Selenium::WebDriver::Error::UnknownError)155 expect {156 driver.find(selector)157 }.to raise_error(Selenium::WebDriver::Error::UnknownError)158 end159 after :each do160 ENV['SAUCE_DISABLE_RETRY'] = nil161 end162 end163 it 'should route through handle_retry' do164 driver.should_receive(:base_find).with(selector) # BLECH165 driver.find(selector)166 end167 it 'should retry 3 times and then raise' do168 driver.should_receive(:base_find).with(selector).exactly(4).times.and_raise(Selenium::WebDriver::Error::UnknownError)169 expect {170 driver.find(selector)171 }.to raise_error(Selenium::WebDriver::Error::UnknownError)172 end173 end174 context "with Capybara => 2.1", :capybara_version => ["2.1", "2.9.9"] do175 it "should not be aliased" do176 driver.respond_to?(:base_find).should be_false177 end178 it "should not be retried" do179 Sauce::Capybara::Driver.instance_variable_get(:@methods_to_retry).should_not include :find180 end181 end182 end183 describe "#find_css" do184 context "with Capybara < 2.1", :capybara_version => [0, "2.0.9"] do185 it "should not be aliased" do186 driver.respond_to?(:base_find_css).should be_false187 end188 it "should not be retried" do189 Sauce::Capybara::Driver.instance_variable_get(:@methods_to_retry).should_not include :find_css190 end191 end192 context "with Capybara >= 2.1", :capybara_version => ["2.1", "2.9.9"] do193 it "should be aliased" do194 driver.respond_to?(:base_find_css).should be_true195 end196 it "should be retried" do197 Sauce::Capybara::Driver.instance_variable_get(:@methods_to_retry).should include :find_css198 end199 end200 end201 describe "#find_xpath" do202 context "with Capybara < 2.1", :capybara_version => [0, "2.0.9"] do203 it "should not be aliased" do204 driver.respond_to?(:base_find_xpath).should be_false205 end206 it "should not be retried" do207 Sauce::Capybara::Driver.instance_variable_get(:@methods_to_retry).should_not include :find_xpath208 end209 end210 context "with Capybara >= 2.1", :capybara_version => ["2.1", "2.9.9"] do211 it "should be aliased" do212 driver.respond_to?(:base_find_xpath).should be_true213 end214 it "should be retried" do215 Sauce::Capybara::Driver.instance_variable_get(:@methods_to_retry).should include :find_xpath216 end217 end218 end219 describe '#visit' do220 it 'should route through #handle_retry' do221 path = '/lol'222 driver.should_receive(:base_visit).with(path)223 driver.visit(path)224 end225 end226 describe '#current_url' do227 it 'should route through #handle_retry' do228 url = 'http://lol'229 driver.should_receive(:base_current_url).and_return(url)230 driver.current_url.should == url231 end232 end233 describe '#within_frame' do234 it 'should route through #handle_retry and yield block' do235 driver.should_receive(:base_within_frame).and_yield236 driver.within_frame do237 "lol"238 end239 end240 end241 end242 describe '#install_hooks' do243 end244 describe 'used without rspec hooks' do245 include Capybara::DSL246 247 before :all do248 app = proc { |env| [200, {}, ["Hello Sauce!"]]}249 Capybara.app = app250 Sauce.driver_pool[Thread.current.object_id] = nil251 end252 xit "should use one of the Sauce Connect ports", :capybara_version => ["2.5.0", "2.9.9"], :js => true do253 reset_capybara(2.5)254 used_port = Capybara.current_session.server.port255 Sauce::Config::POTENTIAL_PORTS.should include used_port 256 end257 it "should use one of the Sauce Connect ports", :capybara_version => [2, "2.4.9"], :js => true do258 reset_capybara(2.0)259 used_port = Capybara.current_session.server.port260 Sauce::Config::POTENTIAL_PORTS.should include used_port 261 end262 it "should use one of the Sauce Connect ports", :capybara_version => ["1.0.9", "1.9.9"], :js => true do263 reset_capybara(1.1)264 used_port = Capybara.current_session.driver.rack_server.port265 Sauce::Config::POTENTIAL_PORTS.should include used_port 266 end267 it "should use one of the Sauce Connect ports", :capybara_version => ["1.0.0", "1.0.9"], :js => true do268 reset_capybara(1.0)269 used_port = Capybara.current_session.driver.rack_server.port270 Sauce::Config::POTENTIAL_PORTS.should include used_port 271 end272 describe "with start_local_application set false", :capybara_version => ["2.0.0", "2.9.9"] do273 before do274 @start_local_application = Sauce::Config.new[:start_local_application]275 end276 after do277 Sauce.config do |c|278 c[:start_local_application] = @start_local_application279 end280 end281 it "should not use Sauce Connect ports" do282 Sauce.config { |c| c[:start_local_application] = false }283 reset_capybara(2.0)284 Capybara.server_port.should eq nil285 end286 end287 end288 def reset_capybara(capy_version)289 Capybara.reset_sessions!290 Capybara.configure do |config|291 case capy_version292 when 1.0293 config.server_boot_timeout = 10294 config.prefer_visible_elements = true295 config.ignore_hidden_elements = false296 when 1.1297 config.server_boot_timeout = 10298 config.prefer_visible_elements = true299 config.automatic_reload = true300 config.ignore_hidden_elements = false301 when 2.0302 config.always_include_port = false303 config.match = :smart...
selenium_spec_chrome.rb
Source:selenium_spec_chrome.rb
...8Selenium::WebDriver::Chrome.path = '/usr/bin/google-chrome-beta' if ENV['CI'] && ENV['CHROME_BETA']9browser_options = ::Selenium::WebDriver::Chrome::Options.new10browser_options.headless! if ENV['HEADLESS']11browser_options.add_option(:w3c, ENV['W3C'] != 'false')12# Chromedriver 77 requires setting this for headless mode on linux13# Different versions of Chrome/selenium-webdriver require setting differently - jus set them all14browser_options.add_preference('download.default_directory', Capybara.save_path)15browser_options.add_preference(:download, default_directory: Capybara.save_path)16Capybara.register_driver :selenium_chrome do |app|17 version = Capybara::Selenium::Driver.load_selenium18 options_key = Capybara::Selenium::Driver::CAPS_VERSION.satisfied_by?(version) ? :capabilities : :options19 driver_options = { browser: :chrome, timeout: 30 }.tap do |opts|20 opts[options_key] = browser_options21 end22 Capybara::Selenium::Driver.new(app, **driver_options).tap do |driver|23 # Set download dir for Chrome < 7724 driver.browser.download_path = Capybara.save_path25 end26end27Capybara.register_driver :selenium_chrome_not_clear_storage do |app|28 version = Capybara::Selenium::Driver.load_selenium29 options_key = Capybara::Selenium::Driver::CAPS_VERSION.satisfied_by?(version) ? :capabilities : :options30 chrome_options = { browser: :chrome, clear_local_storage: false, clear_session_storage: false }.tap do |opts|31 opts[options_key] = browser_options32 end33 Capybara::Selenium::Driver.new(app, **chrome_options)34end35Capybara.register_driver :selenium_chrome_not_clear_session_storage do |app|36 version = Capybara::Selenium::Driver.load_selenium37 options_key = Capybara::Selenium::Driver::CAPS_VERSION.satisfied_by?(version) ? :capabilities : :options38 chrome_options = { browser: :chrome, clear_session_storage: false }.tap do |opts|39 opts[options_key] = browser_options40 end41 Capybara::Selenium::Driver.new(app, **chrome_options)42end43Capybara.register_driver :selenium_chrome_not_clear_local_storage do |app|44 version = Capybara::Selenium::Driver.load_selenium45 options_key = Capybara::Selenium::Driver::CAPS_VERSION.satisfied_by?(version) ? :capabilities : :options46 chrome_options = { browser: :chrome, clear_local_storage: false }.tap do |opts|47 opts[options_key] = browser_options48 end49 Capybara::Selenium::Driver.new(app, **chrome_options)50end51Capybara.register_driver :selenium_driver_subclass_with_chrome do |app|52 version = Capybara::Selenium::Driver.load_selenium53 options_key = Capybara::Selenium::Driver::CAPS_VERSION.satisfied_by?(version) ? :capabilities : :options54 subclass = Class.new(Capybara::Selenium::Driver)55 chrome_options = { browser: :chrome, timeout: 30 }.tap do |opts|56 opts[options_key] = browser_options57 end58 subclass.new(app, **chrome_options)59end60module TestSessions61 Chrome = Capybara::Session.new(CHROME_DRIVER, TestApp)62end63skipped_tests = %i[response_headers status_code trigger]64Capybara::SpecHelper.log_selenium_driver_version(Selenium::WebDriver::Chrome) if ENV['CI']65Capybara::SpecHelper.run_specs TestSessions::Chrome, CHROME_DRIVER.to_s, capybara_skip: skipped_tests do |example|66 case example.metadata[:full_description]67 when /#click_link can download a file$/68 skip 'Need to figure out testing of file downloading on windows platform' if Gem.win_platform?69 when /Capybara::Session selenium_chrome Capybara::Window#maximize/70 pending "Chrome headless doesn't support maximize" if ENV['HEADLESS']71 when /Capybara::Window#fullscreen should be able to fullscreen the window/72 skip 'Chromedriver hangs on attempts to fullscreen in headless mode' if ENV['HEADLESS']73 when /node #right_click delay should delay the mouse up/74 skip "Legacy selenium doesn't support separate right button down/up" if ENV['W3C'] == 'false'75 end76end77RSpec.describe 'Capybara::Session with chrome' do78 include Capybara::SpecHelper79 ['Capybara::Session', 'Capybara::Node', Capybara::RSpecMatchers].each do |examples|80 include_examples examples, TestSessions::Chrome, CHROME_DRIVER81 end82 context 'storage' do83 describe '#reset!' do84 it 'clears storage by default' do85 session = TestSessions::Chrome86 session.visit('/with_js')87 session.find(:css, '#set-storage').click88 session.reset!89 session.visit('/with_js')90 expect(session.evaluate_script('Object.keys(localStorage)')).to be_empty91 expect(session.evaluate_script('Object.keys(sessionStorage)')).to be_empty92 end93 it 'does not clear storage when false' do94 session = Capybara::Session.new(:selenium_chrome_not_clear_storage, TestApp)95 session.visit('/with_js')96 session.find(:css, '#set-storage').click97 session.reset!98 session.visit('/with_js')99 expect(session.evaluate_script('Object.keys(localStorage)')).not_to be_empty100 expect(session.evaluate_script('Object.keys(sessionStorage)')).not_to be_empty101 end102 it 'can not clear session storage' do103 session = Capybara::Session.new(:selenium_chrome_not_clear_session_storage, TestApp)104 session.visit('/with_js')105 session.find(:css, '#set-storage').click106 session.reset!107 session.visit('/with_js')108 expect(session.evaluate_script('Object.keys(localStorage)')).to be_empty109 expect(session.evaluate_script('Object.keys(sessionStorage)')).not_to be_empty110 end111 it 'can not clear local storage' do112 session = Capybara::Session.new(:selenium_chrome_not_clear_local_storage, TestApp)113 session.visit('/with_js')114 session.find(:css, '#set-storage').click115 session.reset!116 session.visit('/with_js')117 expect(session.evaluate_script('Object.keys(localStorage)')).not_to be_empty118 expect(session.evaluate_script('Object.keys(sessionStorage)')).to be_empty119 end120 end121 end122 context 'timeout' do123 it 'sets the http client read timeout' do124 expect(TestSessions::Chrome.driver.browser.send(:bridge).http.read_timeout).to eq 30125 end126 end127 describe 'filling in Chrome-specific date and time fields with keystrokes' do128 let(:datetime) { Time.new(1983, 6, 19, 6, 30) }129 let(:session) { TestSessions::Chrome }130 before do131 session.visit('/form')132 end133 it 'should fill in a date input with a String' do134 session.fill_in('form_date', with: '06/19/1983')135 session.click_button('awesome')136 expect(Date.parse(extract_results(session)['date'])).to eq datetime.to_date137 end...
env.rb
Source:env.rb
...23# w = Capybara.current_session.driver.browser.manage.window24# window_width = w.size.width25# window_height = w.size.height26Before('@smartphone') do27 set_window_size 360, 200028end29After('@smartphone') do30 set_window_size 900, 90031end32def set_window_size(width, height)33 if Capybara.default_driver == :poltergeist34 page.driver.resize(width, height)35 else36 window = Capybara.current_session.driver.browser.manage.window37 window.resize_to(width, height)38 end39end...
set
Using AI Code Generation
1 Capybara::Poltergeist::Driver.new(app, :js_errors => false)2Capybara::Session.new(:poltergeist)3 Capybara::Poltergeist::Driver.new(app, :js_errors => false)4Capybara::Session.new(:poltergeist)
set
Using AI Code Generation
1visit('/')2fill_in('q', :with => 'capybara')3click_button('btnG')4session = Capybara::Session.new(:selenium)5session.visit('/')6session.fill_in('q', :with => 'capybara')7session.click_button('btnG')81.rb:30:in `block in <main>': undefined method `visit' for main:Object (NoMethodError)
set
Using AI Code Generation
1 @session = Capybara::Session.new(:poltergeist)2 def search_for(term)3 @session.visit('/')4 @session.fill_in('q', :with => term)5 @session.click_button('Google Search')6 @session.click_link('Images')7google.search_for('cats')
set
Using AI Code Generation
1 def self.set_driver(driver)2Capybara::Driver.set_driver(:poltergeist)3When(/^I go to google home page$/) do4When(/^I search for "([^"]*)"$/) do |search_term|5 fill_in('q', :with => search_term)6 click_button('Google Search')7Then(/^I should see the search results$/) do8 expect(page).to have_content("Selenium - Web Browser Automation")9When(/^I go to google home page$/) do10When(/^I search for "([^"]*)"$/) do |search_term|11 fill_in('q', :with => search_term)12 click_button('Google Search')13Then(/^I should see the search results$/) do14 expect(page).to have_content("Selenium - Web Browser Automation")15Capybara::Driver.set_driver(:poltergeist)16When(/^I go to google home page$/) do17When(/^I search for "([^"]*)"$/) do |search_term|18 fill_in('q', :with => search_term)19 click_button('Google Search')20Then(/^I should see the search results$/) do21 expect(page).to have_content("S
set
Using AI Code Generation
1visit('http://www.google.com')2fill_in('q', :with => 'capybara')3click_button('Google Search')4has_content?('capybara')5save_screenshot('capybara.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!!