简体   繁体   English

如何处理弹出窗口(WebdriverIO)

[英]How to get handle of popup window (WebdriverIO)

I am very very new to automated testing and I am currently completely stuck with the following issue: 我对自动化测试非常陌生,目前我完全陷入以下问题:

I have a webpage open(first window) In the same test I call a .newWindow(second window) and do some stuff in that window. 我打开了一个网页(第一个窗口)在同一个测试中,我调用.newWindow(第二个窗口)并在该窗口中执行一些操作。 The last action opens new popup window(popup window). 最后一个操作将打开新的弹出窗口(弹出窗口)。 What I need, is to set the focus on a popup window. 我需要的是将焦点放在弹出窗口上。

According to WebdriverIO API I can use .switchTab http://webdriver.io/api/window/switchTab.html But to be able to switch to a popup window I have to indicate handle, but I don't understand how to get the handle of a popup window :( 根据WebdriverIO API我可以使用.switchTab http://webdriver.io/api/window/switchTab.html但是为了能够切换到弹出窗口我必须指示句柄,但我不明白如何获得弹出窗口的句柄:(

That s my piece of code: 这是我的代码:

//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
 .........

There is a lot of examples in java, but i didnt find anything that would fit mine language. java中有很多例子,但我找不到适合我语言的东西。 Please, excuse me my dumbness, I am really a very beginner and I will appreciate if somebody could explain that to me. 请原谅我的愚蠢,我真的是一个非常初学者,如果有人能向我解释,我将不胜感激。

Thanks a lot in advance! 非常感谢提前!

we not use getCurrentTabId to remember the handle of the currently open window? 我们不使用getCurrentTabId来记住当前打开窗口的句柄?

For example: 例如:

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!

I notice you stated "The last action opens new popup window(popup window). What I need, is to set the focus on a popup window." 我注意到你说“最后一个动作打开了新的弹出窗口(弹出窗口)。我需要的是将焦点设置在一个弹出窗口。”

I had this issue. 我有这个问题。 But the new window was opened when clicking on login with facebook. 但点击登录facebook时,新窗口打开了。 This caused an issue on finding the handle for the new window because I could not use .newWindow('http://localhost:9001/') . 这导致找到新窗口句柄的问题,因为我无法使用.newWindow('http://localhost:9001/') The API keys and all sorts are added as parameters when using a social login. 使用社交登录时,API密钥和所有排序都将作为参数添加。 So one has little control 所以一个人无法控制

To handle this I registered each window ID as its opened. 为了解决这个问题,我将每个窗口ID注册为已打开。

The first background step in my feature is Given I open the url "/a.html" 我的功能的第一步背景是Given I open the url "/a.html"

In the step you can set an empty array as the variable windowID with let let windowID = [] 在该步骤中,您可以使用let let windowID = []将空数组设置为变量windowID

So my step file would look like this 所以我的步骤文件看起来像这样

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()
  });

During the step after clicking the Facebook button you can then check all the open window ID's and drop the one that matches windowID.main 在单击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')
  });

note that I add credentials as an environment variable. 请注意,我将凭据添加为环境变量。 This is a good idea, you don't want to commit your personal credentials to the code base. 这是个好主意,您不希望将个人凭据提交给代码库。 One may think well obviously, but you may not, who knows. 人们可能会很清楚地思考,但你可能不会,谁知道。

You had your question answered years ago, but I found this post first when trying to find a solution so it seems a sensible place to put this addition. 你几年前就回答了你的问题,但我在试图找到一个解决方案时首先找到了这个帖子,所以这似乎是一个明智的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM