[英]How to get handle of popup window (WebdriverIO)
我对自动化测试非常陌生,目前我完全陷入以下问题:
我打开了一个网页(第一个窗口)在同一个测试中,我调用.newWindow(第二个窗口)并在该窗口中执行一些操作。 最后一个操作将打开新的弹出窗口(弹出窗口)。 我需要的是将焦点放在弹出窗口上。
根据WebdriverIO API我可以使用.switchTab http://webdriver.io/api/window/switchTab.html但是为了能够切换到弹出窗口我必须指示句柄,但我不明白如何获得弹出窗口的句柄:(
这是我的代码:
//this is the part where I have already second window open
it('should open email letter', function(done) {
client
.pause(2000)
.clickAndWait('[title="Password restore"]', 4000)
.clickAndWait('[title="Restore password"]', 7000) //this is the part where popup window opens
.pause(2000)
.windowHandles(function(err,res){
console.log(res, handles)
}) // I have got three handles but i dont know how to use them now
.........
java中有很多例子,但我找不到适合我语言的东西。 请原谅我的愚蠢,我真的是一个非常初学者,如果有人能向我解释,我将不胜感激。
非常感谢提前!
我们不使用getCurrentTabId
来记住当前打开窗口的句柄?
例如:
var main, popup; //your window handles
client
.getCurrentTabId(function (err, handle) {
main = handle;
})
.newWindow('http://localhost:9001/') //you are on popup window
.getCurrentTabId(function (err, handle) {
popup = handle;
})
.switchTab(main) //you are back to main window
.switchTab(popup) //you are on popup again
.close(popup) //extra bonus!
我注意到你说“最后一个动作打开了新的弹出窗口(弹出窗口)。我需要的是将焦点设置在一个弹出窗口。”
我有这个问题。 但点击登录facebook时,新窗口打开了。 这导致找到新窗口句柄的问题,因为我无法使用.newWindow('http://localhost:9001/')
。 使用社交登录时,API密钥和所有排序都将作为参数添加。 所以一个人无法控制
为了解决这个问题,我将每个窗口ID注册为已打开。
我的功能的第一步背景是Given I open the url "/a.html"
在该步骤中,您可以使用let let windowID = []
将空数组设置为变量windowID
所以我的步骤文件看起来像这样
const url = 'http://localhost:8080'
let windowID = []
this.Given(/^I open the url "([^"]*)"$/, (path) => {
browser
.url(url + path)
console.log(`# Navigating to ${url + path}`)
expect(browser.getUrl()).toEqual(url + path)
windowID.main = browser.getTabIds()
});
在单击Facebook按钮后的步骤中,您可以检查所有打开的窗口ID并删除与windowID.main
匹配的ID
this.When(/^I authenticate with facebook$/, function (arg1) {
// log the ID of the main window
console.log('Main Window ID' + windowID.main)
browser
.pause(2000)
.getTabIds().forEach(function (value) {
if (value === windowID.main) {
// we do not need to do anything with windowID.main as it's already set
return
}
// if the value does not match windowID.main then we know its the new facebook login window
windowID.facebook = value
})
// log both of these
console.log('Main Window ID: ' + windowID.main)
console.log('Facebook Window ID: ' + windowID.facebook)
// Do the login
browser
.switchTab(windowID.facebook)
.setValue('input[name="email"]', process.env.FACEBOOK_EMAIL)
.setValue('input[name="pass"]', process.env.FACEBOOK_PASSWORD)
.submitForm('form')
});
请注意,我将凭据添加为环境变量。 这是个好主意,您不希望将个人凭据提交给代码库。 人们可能会很清楚地思考,但你可能不会,谁知道。
你几年前就回答了你的问题,但我在试图找到一个解决方案时首先找到了这个帖子,所以这似乎是一个明智的地方。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.