http://tarunlalwani.com/post/reusing-existing-browser-session-selenium/
This post provides the insight for using existing session for selenium, but this is in Python/Java. Wanted to implement the same logic in NodeJS using selenium-webdriver library.
I am able to access the session id using:
driver.getSession().then( function(session) {
console.log('Session:'+session.getId());
});
But how to get the executor value?
CHecked and found webdriver.WebDriver.attachToSession
method will attach to the existing session, but need the value for executor and session for the same.
It is possible to attach to existing webdriver session with Node
You just need to create WebDriver object manually, without builder.
const _http = require('selenium-webdriver/http');
//todo: replace with your session ID and selenium url
let sessionId = 'cddab287623789c665c1dbc5c64bf702';
let url = 'http://localhost:4444/wd/hub';
let driver = new WebDriver(
sessionId,
new _http.Executor(Promise.resolve(url)
.then(
url => new _http.HttpClient(url, null, null))
)
);
More complex example: test will try to use existing session, and if it does not work - will create a new one.
const {Builder, By, Key, until, WebDriver} = require('selenium-webdriver');
const _http = require('selenium-webdriver/http');
(async function example() {
//todo: replace this value with session ID after test is executed first time
let sessionId = 'cddab287623789c665c1dbc5c64bf702';
let url = 'http://localhost:4444/wd/hub';
let browser = 'chrome';
let startUrl = 'http://www.google.com/ncr';
//Connect to existing session
let driver = await new WebDriver(
sessionId,
new _http.Executor(Promise.resolve(url)
.then(
url => new _http.HttpClient(url, null, null))
)
);
//Trying to open URL. If does not work - we need to re-create a session
await driver.get(startUrl).catch(async r => {
console.log('Session "' + sessionId + '" not found. Creating new session.');
driver = await new Builder()
.usingServer(url)
.forBrowser(browser)
.build();
driver.getSession().then(function(e){
console.log('Session: ' + JSON.stringify(e, null, 2));
});
driver.get(startUrl);
});
console.log('Starting test execution');
try {
await driver.findElement(By.name('q')).sendKeys('webdriver', Key.RETURN);
await driver.wait(until.titleIs('webdriver - Google Search'), 1000);
} finally {
//todo: We intentionally do not close the session in order to use it next time
// await driver.quit();
}
})();
In order to make it easier to start and reuse a WebDriver session I have created a script and packaged it in an NPM package so that you can start or reuse a WebDriver session in one command
npx webdriver-reuse-session
See the github readme for more information.
Then you can change the script from Maksym from
//todo: replace with your session ID and selenium URL
let sessionId = 'cddab287623789c665c1dbc5c64bf702';
TO
let sessionId = fs.readFileSync('.seleniumSessionId.txt')
And then this will be automatically updated, every time you start your test.
Hope this helps your or others out there.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.