Reputation: 113
I am new to selenium web driver and currently working with the Ruby programming language. I currently have a scenario where I need to execute multiple ruby scripts with the selenium web driver but they have to use the same browser session. I am following the documentation provided in the following link: https://github.com/SeleniumHQ/selenium/wiki/Ruby-Bindings
Currently, my code looks like the following.
require 'selenium-webdriver'
# ---------------------------------------------------------
url = "http://localhost:4444/wd/hub"
driver1 = Selenium::WebDriver.for :remote, url: url, desired_capabilities: :firefox
driver1.get "http://www.msn.com"
puts driver1.title
# store the session id
sessionid = driver1.session_id
# ---------------------------------------------------------
# Try using the existing session id from driver1
driver2 = Selenium::WebDriver.for :remote, url: url, desired_capabilities: :firefox
# The below statement gives error on assignment
driver2.session_id = sessionid
driver2.get "http://www.yahoo.com"
I read this post Can Selenium interact with an existing browser session? and tried to follow the steps provided here unable to use an existing web-driver / browser session.
Has anyone successfully reused the existing session with selenium-webdriver along with Ruby? Kindly let me know what is being missed in this code snippet.
I was able to get this done in python.
from selenium import webdriver
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
executor_url = "http://localhost:4444/wd/hub"
# Create a desired capabilities object as a starting point.
capabilities = DesiredCapabilities.FIREFOX.copy()
capabilities['platform'] = "WINDOWS"
capabilities['version'] = "10"
# ------------------------ STEP 1 --------------------------------------------------
# driver1 = webdriver.Firefox()
driver1 = webdriver.Remote(command_executor=executor_url, desired_capabilities=capabilities)
driver1.get('http://google.com/')
url = driver1.command_executor._url
print(driver1.command_executor._url)
print(driver1.session_id)
print(driver1.title)
# Serialize the session id in a file
session_id = driver1.session_id
# ------------------ END OF STEP 1 --------------------------------------------------
# Pass the session id from step 1 to step 2
# ------------------------ STEP 2 --------------------------------------------------
def attach_to_session(executor_url, session_id):
original_execute = WebDriver.execute
def new_command_execute(self, command, params=None):
if command == "newSession":
# Mock the response
return {'success': 0, 'value': None, 'sessionId': session_id}
else:
return original_execute(self, command, params)
# Patch the function before creating the driver object
WebDriver.execute = new_command_execute
temp_driver = webdriver.Remote(command_executor=executor_url)
# Replace the patched function with original function
WebDriver.execute = original_execute
return temp_driver
# read the session id from the file
driver2 = attach_to_session(executor_url, session_id)
driver2.get('http://msn.com/')
print(driver2.command_executor._url)
print(driver2.session_id)
print(driver2.title)
driver2.close()
Upvotes: 0
Views: 1180
Reputation: 113
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)
Upvotes: 0