[英]Selenium - Interact with exsting browser session using selenium-webdriver library in NodeJS
http://tarunlalwani.com/post/reusing-existing-browser-session-selenium/ 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.这篇文章提供了将现有会话用于 selenium 的见解,但这是在 Python/Java 中。 Wanted to implement the same logic in NodeJS using selenium-webdriver library.想要使用 selenium-webdriver 库在 NodeJS 中实现相同的逻辑。
I am able to access the session id using:我可以使用以下方法访问会话 ID:
driver.getSession().then( function(session) {
console.log('Session:'+session.getId());
});
But how to get the executor value?但是如何获取executor值呢?
CHecked and found webdriver.WebDriver.attachToSession
method will attach to the existing session, but need the value for executor and session for the same.检查并发现webdriver.WebDriver.attachToSession
方法将附加到现有会话,但需要 executor 和 session 的值相同。
It is possible to attach to existing webdriver session with Node可以使用 Node 附加到现有的 webdriver 会话
You just need to create WebDriver object manually, without builder.您只需要手动创建 WebDriver 对象,无需构建器。
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为了更容易启动和重用 WebDriver 会话,我创建了一个脚本并将其打包在 NPM 包中,以便您可以在一个命令中启动或重用 WebDriver 会话
npx webdriver-reuse-session
See the github readme for more information.有关详细信息,请参阅 github 自述文件。
Then you can change the script from Maksym from然后你可以从 Maksym 更改脚本
//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.希望这对您或其他人有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.