我正在尝试使用 selenium web driver 和 Node js 自动化几个页面。我能够登录,但登录后我想使用由网络驱动程序启动的相同会话,以便我可以在会话受保护的页面上进行自动测试。这是我的尝试
async function login(){
Let d = await new Builder()
.forBrowser('chrome')
.build();
await d.get('https://demo.textdomain.com/')
await d.findElement(By.id('username')).sendKeys('admin ')
await d.findElement(By.id('password')).sendKeys('admin');
await d.findElement(By.css('button[type="submit"]')).click();
d.getPageSource().then(function(content) {
if(content.indexOf('Welcome text') !==-1 ) {
console.log('Test passed');
console.log('landing page');
d.get('https://demo.textdomain.com/landingpage') //this is still going to login page as i cannot use the previous session
} else {
console.log('Test failed');
return false;
}
//driver.quit();
});
}
login();
我是否在登录后不小心丢弃了浏览器?
来自类似的问题SQA StackExchange https://sqa.stackexchange.com/questions/15594/selenium-how-to-access-the-same-session-in-a-new-window#15595,您可以存储和恢复当前会话的cookie:
使用 JavaScript:
// Storing cookies:
driver.manage().getCookies().then(function (cookies) {
allCookies = cookies;
});
// Restoring cookies:
for (var key in allCookies) {
driver.manage().addCookie(key, allCookies[key]);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)