[英]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.