自动化测试基础篇--Selenium多窗口、句柄问题
https://webdriver.io/docs/api/webdriver.html
自动化测试中经常有点击一个链接,打开新的窗口。人为操作的话,可以通过眼睛看,识别不同的窗口点击切换。但是是自动化脚本没长眼睛,它不知道你要操作哪个窗口,这时候只能句柄来判断了。
浏览器窗口的属性用句柄(handle)来识别。窗口句柄是窗口的唯一标识,可看做窗口的身份证号。
获取当前页面的句柄:
browser.getWindowHandle();
获取所有窗口句柄
browser.getWindowHandles();
切换句柄
方法一:判断句柄是否与首页相等
方法二:直接获取list列表里面的值,取值handles[i]
browser.switchToWindow(handle);
获取新页面url,标题
browser.getUrl()
browser.getTitle()
关闭新页面
browser.closeWindow();
browser.quit();
driver.close()相当于关闭当前TAB选项卡driver.quit()是退出了整个浏览器
//get new window url and close the new window
let targetUrl;
let allHandles = [];
//get process map page handle
let parentHandle = browser.getWindowHandle();
console.log("The handle is " + parentHandle);
//click help icon to open sap help page and get the url
let helpIcon = $(this.helpIcon.replace(/\{0\}/g, processMapName));
helpIcon.click();
browser.pause(30000);
//get all handles
allHandles = browser.getWindowHandles();
if (allHandles.length > 1) {
for (let i = 0; i < allHandles.length; i++) {
console.log("The variant is " + i + " and the handle is " + allHandles[i]);
if (parentHandle !== allHandles[i]) {
browser.switchToWindow(allHandles[i]);
targetUrl = browser.getUrl()
console.log("The target url is " + targetUrl);
browser.closeWindow();
browser.switchToWindow(parentHandle);
console.log("The parent url is " + browser.getUrl());
}
}
} else {
console.log("can not get the new page handle");
}
return targetUrl;
1.01^365=37.78
0.99^365=0.03
浙公网安备 33010602011771号