How to Connect to Browser Using Ruby Selenium Webdriver

Ruby/Selenium Opening URLs In Same Chrome Tab Instead Of Different Tabs

To open a link in a new tab:

require 'selenium-webdriver'

driver = Selenium::WebDriver.for :chrome

driver.navigate.to "https://www.google.com"

# open a new tab and set the context
driver.execute_script "window.open('_blank', 'tab2')"
driver.switch_to.window "tab2"

driver.get "http://stackoverflow.com/"

When using Selenium WebDriver and Ruby how do know what browser type i am using?

Very simple way of getting what browser is currently used in the test.
WebDriver was defined in this way:

def setup
client = Selenium::WebDriver::Remote::Http::Default.new
client.timeout = 120
case $browserName
when "firefox"
$browser = Selenium::WebDriver.for(:firefox, :http_client => client)
when "chrome"
$browser = Selenium::WebDriver.for(:chrome, :http_client => client)
when "safari"
$browser = Selenium::WebDriver.for(:safari, :http_client => client)
when "IE"
$browser = Selenium::WebDriver.for(:internet_explorer, :http_client => client)
else
puts "ERROR: Wrong browser name!!"
end

$browser.get ""

$wait = Selenium::WebDriver::Wait.new(:timeout => 30)

end

So with this simple statement I can get the name defined in the Webdriver:

browser_name = $browser.browser.to_s

Just simply

if browser_name == "firefox"
#some code
end

Selenium webdriver with Ruby : Use existing session id

I was able to solve this issue by using the following code patch.

class RemoteWebDriver < Selenium::WebDriver::Driver
def initialize(bridge)
@bridge = bridge
end
end

class RemoteBridge < Selenium::WebDriver::Remote::Bridge
def self.handshake(**opts)
capabilities = opts.delete(:desired_capabilities) { Capabilities.new }
@session_id = opts.delete(:session_id)
Selenium::WebDriver::Remote::W3C::Bridge.new(capabilities, @session_id, **opts)
end
end

caps = Selenium::WebDriver::Remote::Capabilities.firefox()
remote_bridge = RemoteBridge.handshake(url: <YOUR_REMOTE_WEB_DRIVER_URL>, desired_capabilities: caps, session_id: <YOUR_SESSION_ID>)
remote_web_driver = RemoteWebDriver.new(remote_bridge)

How to use same browser window for automated test using selenium-webdriver (ruby)?

The Before hook is run before each scenario. This is why a new browser is opened each time.

Do the following instead (in the env.rb):

require "selenium-webdriver"

driver = Selenium::WebDriver.for :ie
accept_next_alert = true
driver.manage.timeouts.implicit_wait = 30
driver.manage.timeouts.script_timeout = 30
verification_errors = []

Before do
@driver = driver
end

at_exit do
driver.close
end

In this case, a browser will be opened at the start (before any tests). Then each test will grab that browser and continue using it.

Note: While it is usually okay to re-use the browser across tests. You should be careful about tests that need to be run in a specific order (ie become dependent). Dependent tests can be hard to debug and maintain.



Related Topics



Leave a reply



Submit