Best Selenium code snippet using Selenium.get
client_spec.rb
Source:client_spec.rb
...26 context "when the suite fails" do27 it "returns false" do28 mock.proxy(driver).start.ordered29 mock.proxy(driver).open("/specs").ordered30 mock.strong(driver).get_eval("window.JsTestServer.status()") do31 {"runner_state" => Client::RUNNING_RUNNER_STATE, "console" => ""}.to_json32 end33 mock.strong(driver).get_eval("window.JsTestServer.status()") do34 {"runner_state" => Client::RUNNING_RUNNER_STATE, "console" => ".."}.to_json35 end36 mock.strong(driver).get_eval("window.JsTestServer.status()") do37 {"runner_state" => Client::RUNNING_RUNNER_STATE, "console" => "...F."}.to_json38 end39 mock.strong(driver).get_eval("window.JsTestServer.status()") do40 {"runner_state" => Client::FAILED_RUNNER_STATE, "console" => "...F..\n\nFailure\n/specs/foo_spec.js"}.to_json41 end42 Client.run.should be_false43 end44 end45 context "when the suite passes" do46 it "returns true" do47 mock.proxy(driver).start.ordered48 mock.proxy(driver).open("/specs").ordered49 mock.strong(driver).get_eval("window.JsTestServer.status()") do50 {"runner_state" => Client::RUNNING_RUNNER_STATE, "console" => ""}.to_json51 end52 mock.strong(driver).get_eval("window.JsTestServer.status()") do53 {"runner_state" => Client::RUNNING_RUNNER_STATE, "console" => ".."}.to_json54 end55 mock.strong(driver).get_eval("window.JsTestServer.status()") do56 {"runner_state" => Client::RUNNING_RUNNER_STATE, "console" => "....."}.to_json57 end58 mock.strong(driver).get_eval("window.JsTestServer.status()") do59 {"runner_state" => Client::PASSED_RUNNER_STATE, "console" => "......\n\nPassed"}.to_json60 end61 Client.run.should be_true62 end63 end64 end65 describe "with overridden runner parameters" do66 it "allows overrides for :host, :port, :browser, and :url" do67 driver = FakeSeleniumDriver.new68 host = "myhost"69 port = 999970 browser = "*iexplore"71 spec_url = "http://myspechost:7777/specs/path"72 expected_selenium_client_params = {73 :host => host, :port => port, :browser => browser, :url => "http://myspechost:7777"74 }75 mock.proxy(Selenium::Client::Driver).new(expected_selenium_client_params) do76 driver77 end78 mock.proxy(driver).start.ordered79 mock.proxy(driver).open("/specs/path").ordered80 mock.strong(driver).get_eval("window.JsTestServer.status()") do81 {"runner_state" => Client::RUNNING_RUNNER_STATE, "console" => ""}.to_json82 end83 mock.strong(driver).get_eval("window.JsTestServer.status()") do84 {"runner_state" => Client::RUNNING_RUNNER_STATE, "console" => ".."}.to_json85 end86 mock.strong(driver).get_eval("window.JsTestServer.status()") do87 {"runner_state" => Client::RUNNING_RUNNER_STATE, "console" => "....."}.to_json88 end89 mock.strong(driver).get_eval("window.JsTestServer.status()") do90 {"runner_state" => Client::PASSED_RUNNER_STATE, "console" => "......\n\nPassed"}.to_json91 end92 Client.run(:selenium_host => host, :selenium_port => port, :selenium_browser => browser, :spec_url => spec_url).93 should be_true94 end95 end96 end97 end98 describe ".run_argv" do99 attr_reader :request, :response100 before do101 stub(Client).puts102 end103 context "when passed-in Hash contains :selenium_browser" do...
server.rb
Source:server.rb
...11 # server.start12 #13 # Automatically download the given version:14 #15 # server = Selenium::Server.get '2.6.0'16 # server.start17 #18 # or the latest version:19 #20 # server = Selenium::Server.get :latest21 # server.start22 #23 # Run the server in the background:24 #25 # server = Selenium::Server.new(jar, :background => true)26 # server.start27 #28 # Add additional arguments:29 #30 # server = Selenium::Server.new(jar)31 # server << ["--additional", "args"]32 # server.start33 #34 class Server35 class Error < StandardError; end36 CL_RESET = WebDriver::Platform.windows? ? '' : "\r\e[0K"37 def self.get(required_version, opts = {})38 new(download(required_version), opts)39 end40 #41 # Download the given version of the selenium-server-standalone jar.42 #43 def self.download(required_version)44 required_version = latest if required_version == :latest45 download_file_name = "selenium-server-standalone-#{required_version}.jar"46 if File.exists? download_file_name47 return download_file_name48 end49 begin50 open(download_file_name, "wb") do |destination|51 net_http.start("selenium.googlecode.com") do |http|52 resp = http.request_get("/files/#{download_file_name}") do |response|53 total = response.content_length54 progress = 055 segment_count = 056 response.read_body do |segment|57 progress += segment.length58 segment_count += 159 if segment_count % 15 == 060 percent = (progress.to_f / total.to_f) * 10061 print "#{CL_RESET}Downloading #{download_file_name}: #{percent.to_i}% (#{progress} / #{total})"62 segment_count = 063 end64 destination.write(segment)65 end66 end67 unless resp.kind_of? Net::HTTPSuccess68 raise Error, "#{resp.code} for #{download_file_name}"69 end70 end71 end72 rescue73 FileUtils.rm download_file_name if File.exists? download_file_name74 raise75 end76 download_file_name77 end78 #79 # Ask Google Code what the latest selenium-server-standalone version is.80 #81 def self.latest82 net_http.start("code.google.com") do |http|83 resp = http.get("/p/selenium/downloads/list")84 resp.body.to_s[/selenium-server-standalone-(\d+.\d+.\d+).jar/, 1]85 end86 end87 #88 # The server port89 #90 attr_accessor :port91 #92 # The server timeout93 #94 attr_accessor :timeout95 #96 # Whether to launch the server in the background97 #98 attr_accessor :background99 #100 # Path to log file, or 'true' for stdout.101 #102 attr_accessor :log103 #104 # @param [String] jar Path to the server jar.105 # @param [Hash] opts the options to create the server process with106 #107 # @option opts [Integer] :port Port the server should listen on (default: 4444).108 # @option opts [Integer] :timeout Seconds to wait for server launch/shutdown (default: 30)109 # @option opts [true,false] :background Run the server in the background (default: false)110 # @option opts [true,false,String] :log Either a path to a log file,111 # or true to pass server log to stdout.112 # @raise [Errno::ENOENT] if the jar file does not exist113 #114 def initialize(jar, opts = {})115 raise Errno::ENOENT, jar unless File.exist?(jar)116 @jar = jar117 @host = "127.0.0.1"118 @port = opts.fetch(:port, 4444)119 @timeout = opts.fetch(:timeout, 30)120 @background = opts.fetch(:background, false)121 @log = opts[:log]122 @additional_args = []123 end124 def start125 process.start126 poll_for_service127 process.wait unless @background128 end129 def stop130 begin131 Net::HTTP.get(@host, "/selenium-server/driver/?cmd=shutDownSeleniumServer", @port)132 rescue Errno::ECONNREFUSED133 end134 stop_process if @process135 poll_for_shutdown136 @log_file.close if @log_file137 end138 def webdriver_url139 "http://#{@host}:#{@port}/wd/hub"140 end141 def <<(arg)142 if arg.kind_of?(Array)143 @additional_args += arg144 else145 @additional_args << arg.to_s...
selenium_element_spec.rb
Source:selenium_element_spec.rb
...23 end24 it "should flash an element" do25 bridge = double('bridge')26 @selenium_driver.should_receive(:attribute).and_return('blue')27 @selenium_driver.should_receive(:instance_variable_get).and_return(bridge)28 bridge.should_receive(:executeScript).exactly(10).times29 @selenium_element.flash30 end31 it "should be able to return the text contained in the element" do32 @selenium_driver.should_receive(:text).and_return("my text")33 @selenium_element.text.should == "my text"34 end35 it "should know when it is equal to another" do36 @selenium_driver.should_receive(:==).and_return(true)37 @selenium_element.should == @selenium_element38 end39 it "should return its tag name" do40 @selenium_driver.should_receive(:tag_name).and_return("h1")41 @selenium_element.tag_name.should == "h1"42 end43 it "should know its value" do44 @selenium_driver.should_receive(:attribute).with('value').and_return("value")45 @selenium_element.value.should == "value"46 end47 it "should know how to retrieve the value of an attribute" do48 @selenium_driver.should_receive(:attribute).and_return(true)49 @selenium_element.attribute('readonly').should be true50 end51 it "should be clickable" do52 @selenium_driver.should_receive(:click)53 @selenium_element.click54 end55 it "should be double clickable" do56 Selenium::WebDriver::Mouse.should_receive(:new).and_return(@selenium_driver)57 @selenium_driver.should_receive(:double_click)58 @selenium_element.double_click59 end60 61 it "should be right clickable" do62 @selenium_driver.should_receive(:context_click)63 @selenium_element.right_click64 end65 it "should be able to block until it is present" do66 wait = double('wait')67 ::Selenium::WebDriver::Wait.should_receive(:new).and_return(wait)68 wait.should_receive(:until)69 @selenium_element.when_present(10)70 end71 72 it "should return the element when it is present" do73 wait = double('wait')74 ::Selenium::WebDriver::Wait.should_receive(:new).and_return(wait)75 wait.should_receive(:until)76 element = @selenium_element.when_present(10)77 element.should === @selenium_element78 end79 it "should return when an element is not present" do80 wait = double('wait')81 ::Selenium::WebDriver::Wait.should_receive(:new).and_return(wait)82 wait.should_receive(:until)83 @selenium_element.when_not_present84 end85 it "should be able to block until it is visible" do86 wait = double('wait')87 ::Selenium::WebDriver::Wait.should_receive(:new).and_return(wait)88 wait.should_receive(:until)89 @selenium_element.when_visible(10)90 end91 92 it "should return the element when it is visible" do93 wait = double('wait')94 ::Selenium::WebDriver::Wait.should_receive(:new).and_return(wait)95 wait.should_receive(:until)96 element = @selenium_element.when_visible(10)97 element.should === @selenium_element98 end99 it "should be able to block until it is not visible" do100 wait = double('wait')101 ::Selenium::WebDriver::Wait.should_receive(:new).and_return(wait)102 wait.should_receive(:until)103 @selenium_element.when_not_visible(10)104 end105 it "should return the element when it is not visible" do106 wait = double('wait')107 ::Selenium::WebDriver::Wait.should_receive(:new).and_return(wait)108 wait.should_receive(:until)109 element = @selenium_element.when_not_visible(10)110 element.should === @selenium_element111 end112 it "should be able to block until a user define event fires true" do113 wait = double('wait')114 ::Selenium::WebDriver::Wait.should_receive(:new).and_return(wait)115 wait.should_receive(:until)116 @selenium_element.wait_until(10, "Element blah") {}117 end118 it "should send keys to the element" do119 @selenium_driver.should_receive(:send_keys).with([:control, 'a'])120 @selenium_element.send_keys([:control, 'a'])121 end122 123 it "should clear its' contents" do124 @selenium_driver.should_receive(:clear)125 @selenium_element.clear126 end127 it "should fire an event" do128 @selenium_driver.should_receive(:instance_variable_get).with(:@bridge).and_return(@selenium_driver)129 @selenium_driver.should_receive(:executeScript)130 @selenium_element.fire_event('onfocus')131 end132 it "should find the parent element" do133 @selenium_driver.should_receive(:instance_variable_get).with(:@bridge).and_return(@selenium_driver)134 @selenium_driver.should_receive(:executeScript).and_return(@selenium_driver)135 @selenium_driver.should_receive(:tag_name).twice.and_return(:div)136 @selenium_element.parent137 end138 it "should set the focus" do139 @selenium_driver.should_receive(:instance_variable_get).and_return(@selenium_driver)140 @selenium_driver.should_receive(:executeScript)141 @selenium_element.focus142 end143 it "should scroll into view" do144 @selenium_driver.should_receive(:location_once_scrolled_into_view)145 @selenium_element.scroll_into_view146 end147end...
selenium_spec_marionette.rb
Source:selenium_spec_marionette.rb
...51 it "should reset browser when quit" do52 expect(@driver.browser).to be53 @driver.quit54 #access instance variable directly so we don't create a new browser instance55 expect(@driver.instance_variable_get(:@browser)).to be_nil56 end57 context "with errors" do58 before do59 @original_browser = @driver.browser60 end61 after do62 # Ensure browser is actually quit so we don't leave hanging processe63 RSpec::Mocks.space.proxy_for(@original_browser).reset64 @original_browser.quit65 end66 it "warns UnknownError returned during quit because the browser is probably already gone" do67 expect_any_instance_of(Capybara::Selenium::Driver).to receive(:warn).with(/random message/)68 allow(@driver.browser).to(69 receive(:quit)70 .and_raise(Selenium::WebDriver::Error::UnknownError, "random message")71 )72 expect { @driver.quit }.not_to raise_error73 expect(@driver.instance_variable_get(:@browser)).to be_nil74 end75 it "ignores silenced UnknownError returned during quit because the browser is almost definitely already gone" do76 expect_any_instance_of(Capybara::Selenium::Driver).not_to receive(:warn)77 allow(@driver.browser).to(78 receive(:quit)79 .and_raise(Selenium::WebDriver::Error::UnknownError, "Error communicating with the remote browser")80 )81 expect { @driver.quit }.not_to raise_error82 expect(@driver.instance_variable_get(:@browser)).to be_nil83 end84 end85 end86 context "storage" do87 describe "#reset!" do88 it "does not clear either storage by default" do89 @session = TestSessions::SeleniumMarionette90 @session.visit('/with_js')91 @session.find(:css, '#set-storage').click92 @session.reset!93 @session.visit('/with_js')94 expect(@session.driver.browser.local_storage.keys).not_to be_empty95 expect(@session.driver.browser.session_storage.keys).not_to be_empty96 end...
selenium_spec.rb
Source:selenium_spec.rb
...31 describe "#reset!" do32 it "freshly reset session should not be touched" do33 @session.instance_variable_set(:@touched, true)34 @session.reset!35 expect(@session.instance_variable_get(:@touched)).to eq false36 end37 end38 describe "exit codes" do39 before do40 @current_dir = Dir.getwd41 Dir.chdir(File.join(File.dirname(__FILE__), '..'))42 end43 after do44 Dir.chdir(@current_dir)45 end46 it "should have return code 1 when running selenium_driver_rspec_failure.rb" do47 `rspec spec/fixtures/selenium_driver_rspec_failure.rb`48 expect($?.exitstatus).to be 149 end50 it "should have return code 0 when running selenium_driver_rspec_success.rb" do51 `rspec spec/fixtures/selenium_driver_rspec_success.rb`52 expect($?.exitstatus).to be 053 end54 end55 56 describe "#accept_alert" do57 it "supports a blockless mode" do58 @session.visit('/with_js')59 @session.click_link('Open alert')60 expect(@session.driver.browser.switch_to.alert).to be_kind_of Selenium::WebDriver::Alert61 @session.accept_alert62 expect{@session.driver.browser.switch_to.alert}.to raise_error("No alert is present")63 end64 end65 end66end67RSpec.describe Capybara::Selenium::Driver do68 before do69 @driver = Capybara::Selenium::Driver.new(TestApp, browser: :firefox)70 end71 72 describe '#quit' do73 it "should reset browser when quit" do74 expect(@driver.browser).to be75 @driver.quit76 #access instance variable directly so we don't create a new browser instance77 expect(@driver.instance_variable_get(:@browser)).to be_nil78 end79 end80end...
paths.rb
Source:paths.rb
...8 @@selenium_path9 end10 11 def selenium_tests_path12 return SeleniumOnRailsConfig.get("selenium_tests_path") if SeleniumOnRailsConfig.get("selenium_tests_path")13 File.expand_path(File.join(RAILS_ROOT, 'test/selenium'))14 end15 16 def view_path view17 File.expand_path(File.dirname(__FILE__) + '/../views/' + view)18 end19 20 # Returns the path to the layout template. The path is relative in relation21 # to the app/views/ directory since Rails doesn't support absolute paths22 # to layout templates.23 def layout_path24 'layout.rhtml'25 end26 27 def fixtures_path28 return SeleniumOnRailsConfig.get("fixtures_path") if SeleniumOnRailsConfig.get("fixtures_path")29 File.expand_path File.join(RAILS_ROOT, 'test/fixtures')30 end31 32 def log_path log_file33 File.expand_path(File.dirname(__FILE__) + '/../../log/' + File.basename(log_file))34 end3536 def skip_file? file37 file.split('/').each do |f|38 return true if f.upcase == 'CVS' or f.starts_with?('.') or f.ends_with?('~') or f.starts_with?('_')39 end40 false41 end42 43 private ###############################################4445 def find_selenium_path46 sel_dirs = SeleniumOnRailsConfig.get :selenium_path do47 File.expand_path(File.dirname(__FILE__) + '/../../selenium-core')48 end4950 sel_dirs.to_a.each do |seleniumdir|51 ['', 'core', 'selenium', 'javascript'].each do |subdir|52 path = File.join seleniumdir, subdir53 return path if File.exist?(File.join(path, 'TestRunner.html'))54 end55 end56 57 raise 'Could not find Selenium Core installation'58 end59 60 end
...
selenium_on_rails_config_test.rb
Source:selenium_on_rails_config_test.rb
...13 @config_file = File.expand_path(File.dirname(__FILE__) + '/../config.yml')14 @selenium_content = File.read(File.dirname(__FILE__) + '/fixtures/selenium.yml')15 @config_content = File.read(File.dirname(__FILE__) + '/fixtures/config.yml')16 end17 def test_get_selenium_yaml18 File.expects(:exist?).with(@selenium_file).returns(true)19 IO.expects(:read).with(@selenium_file).returns(@selenium_content)20 IO.expects(:read).with(@config_file).never21 IO.expects(:exist?).with(@config_file).never22 23 assert_equal ["test_cache"], SeleniumOnRailsConfig.get(:environments)24 assert_equal({"firefox"=>"script/openfirefox"}, SeleniumOnRailsConfig.get(:browsers))25 end26 def test_get_when_config_yml_exists_but_selenium_yaml_not27 File.expects(:exist?).with(@selenium_file).returns(false)28 File.expects(:exist?).with(@config_file).returns(true)29 IO.expects(:read).with(@config_file).returns(@config_content)30 IO.expects(:read).with(@selenium_file).never31 32 assert_equal ["test"], SeleniumOnRailsConfig.get(:environments)33 expected_config = {"safari"=>"/Applications/Safari.app/Contents/MacOS/Safari",34 "firefox"=>"/Applications/Firefox.app/Contents/MacOS/firefox-bin"} 35 36 assert_equal(expected_config, SeleniumOnRailsConfig.get(:browsers))37 end38end...
selenium_configs_controller_test.rb
Source:selenium_configs_controller_test.rb
2class SeleniumConfigsControllerTest < ActionController::TestCase3 setup do4 @selenium_config = selenium_configs(:one)5 end6 test "should get index" do7 get :index8 assert_response :success9 assert_not_nil assigns(:selenium_configs)10 end11 test "should get new" do12 get :new13 assert_response :success14 end15 test "should create selenium_config" do16 assert_difference('SeleniumConfig.count') do17 post :create, selenium_config: { browser: @selenium_config.browser, url: @selenium_config.url, user_id: @selenium_config.user_id }18 end19 assert_redirected_to selenium_config_path(assigns(:selenium_config))20 end21 test "should show selenium_config" do22 get :show, id: @selenium_config23 assert_response :success24 end25 test "should get edit" do26 get :edit, id: @selenium_config27 assert_response :success28 end29 test "should update selenium_config" do30 patch :update, id: @selenium_config, selenium_config: { browser: @selenium_config.browser, url: @selenium_config.url, user_id: @selenium_config.user_id }31 assert_redirected_to selenium_config_path(assigns(:selenium_config))32 end33 test "should destroy selenium_config" do34 assert_difference('SeleniumConfig.count', -1) do35 delete :destroy, id: @selenium_config36 end37 assert_redirected_to selenium_configs_path38 end39end...
get
Using AI Code Generation
1element = driver.find_element(:name, 'q')2element = driver.find_element(:name, 'q')3element = driver.find_element(:name, 'q')4element = driver.find_element(:name, 'q')5element = driver.find_element(:name, 'q')6element = driver.find_element(:name, 'q')7element = driver.find_element(:name, 'q')
get
Using AI Code Generation
1element = driver.find_element(:name, 'q')2element = driver.find_element(:name, 'q')3element = driver.find_element(:name, 'q')4element = driver.find_element(:name, 'q')5element = driver.find_element(:name, 'q')6element = driver.find_element(:name, 'q')7element = driver.find_element(:name, 'q')
get
Using AI Code Generation
1element = driver.find_element(:name, 'q')2element = driver.find_element(:name, 'q')3element = driver.find_element(:name, 'q')4element = driver.find_element(:name, 'q')5element = driver.find_element(:name, 'q')6element = driver.find_element(:name, 'q')7element = driver.find_element(:name, 'q')
get
Using AI Code Generation
1element = driver.find_element(:name, 'q')2element = driver.find_element(:name, 'q')3element = driver.find_element(:name, 'q')4element = driver.find_element(:name, 'q')5element = driver.find_element(:name, 'q')
get
Using AI Code Generation
1driver.manage.window.resize_to(800, 600)2driver.manage.window.move_to(0, 0)3driver.manage.window.move_to(800, 600)4driver.manage.window.move_to(1600, 1200)5driver.manage.window.move_to(3200, 2400)
get
Using AI Code Generation
1element = driver.find_element(:name, 'q')2element = driver.find_element(:name, 'q')3element = driver.find_element(:name, 'q')4element = driver.find_element(:name, 'q')5element = driver.find_element(:name, 'q')
get
Using AI Code Generation
1element = driver.find_element(:name, 'q')2element = driver.find_element(:name, 'q')3element = driver.find_element(:name, 'q')
get
Using AI Code Generation
1driver.get("https://www.google.com")2driver.get("https://www.google.com")3search_bar = driver.find_element(:name, 'q')4search_bar.send_keys("Hello World!")5driver.get("https://www.google.com")6search_bar = driver.find_element(:name, 'q')7search_bar.send_keys("Hello World!")8search_button = driver.find_element(:name, 'btnK')9driver.get("https://www.google.com")10search_bar = driver.find_element(:name, 'q')11search_bar.send_keys("Hello World!")12search_button = driver.find_element(:name, 'btnK')
get
Using AI Code Generation
1element = driver.find_element(:name, 'q')2element = driver.find_element(:name, 'q')3element = driver.find_element(:name, 'q')4element = driver.find_element(:name, 'q')5element = driver.find_element(:name, 'q')6element = driver.find_element(:name, 'q')7element = driver.find_element(:name, 'q')
get
Using AI Code Generation
1element = driver.find_element(:name, 'q')2element = driver.find_element(:name, 'q')3element = driver.find_element(:name, 'q')4element = driver.find_element(:name, 'q')5element = driver.find_element(:name, 'q')
get
Using AI Code Generation
1driver.manage.window.resize_to(800, 600)2driver.manage.window.move_to(0, 0)3driver.manage.window.move_to(800, 600)4driver.manage.window.move_to(1600, 1200)5driver.manage.window.move_to(3200, 2400)
get
Using AI Code Generation
1element = driver.find_element(:name, 'q')2element = driver.find_element(:name, 'q')3element = driver.find_element(:name, 'q')4element = driver.find_element(:name, 'q')5element = driver.find_element(:name, 'q')
get
Using AI Code Generation
1driver.get("https://www.google.com")2driver.get("https://www.google.com")3search_bar = driver.find_element(:name, 'q')4search_bar.send_keys("Hello World!")5driver.get("https://www.google.com")6search_bar = driver.find_element(:name, 'q')7search_bar.send_keys("Hello World!")8search_button = driver.find_element(:name, 'btnK')9driver.get("https://www.google.com")10search_bar = driver.find_element(:name, 'q')11search_bar.send_keys("Hello World!")12search_button = driver.find_element(:name, 'btnK')
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!!