javascript - 登录后保持会话 - selenium - javascript
问题描述
我正在尝试使用 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上的类似问题,您可以存储和恢复当前会话的 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]);
}
推荐阅读
- javascript - HTML5 输入类型范围值气泡
- c - C编程打印从函数返回的二维动态数组
- sql - SQL查询两个具有相同列名的表到一个表中
- azure-devops - 无法连接到已创建的 EKS fargate 集群需要从 azur 管道进行部署
- python - 每次我运行我的脚本时,它都会返回`_curses.error: must call setupterm() first` 为什么?
- javascript - 如何通过javascript更改json数据
- javascript - 在 Highcharts JS 中删除行
- python - 如何检查 Mypy `# type:ignore` 注释是否仍然有效和必需?
- php - Woocommerce - 从 get_price_html 获取销售价格
- javascript - 如何在 Cypress 中将元素从 JSON 转换为字符串