Windows 如何处理弹出窗口(WebdriverIO)

Windows 如何处理弹出窗口(WebdriverIO),windows,selenium,popup,popupwindow,webdriver-io,Windows,Selenium,Popup,Popupwindow,Webdriver Io,我对自动化测试非常陌生,目前完全陷入以下问题: 我打开了一个网页(第一个窗口) 在同一测试中,我调用.newWindow(第二个窗口)并在该窗口中执行一些操作。最后一个操作将打开新的弹出窗口(弹出窗口)。 我需要的是在弹出窗口上设置焦点 根据WebdriverIO API,我可以使用.switchTab 但要切换到弹出窗口,我必须指示句柄,但我不知道如何获得弹出窗口的句柄:( 这就是我的代码: //this is the part where I have already second wind

我对自动化测试非常陌生,目前完全陷入以下问题:

我打开了一个网页(第一个窗口) 在同一测试中,我调用.newWindow(第二个窗口)并在该窗口中执行一些操作。最后一个操作将打开新的弹出窗口(弹出窗口)。 我需要的是在弹出窗口上设置焦点

根据WebdriverIO API,我可以使用.switchTab 但要切换到弹出窗口,我必须指示句柄,但我不知道如何获得弹出窗口的句柄:(

这就是我的代码:

//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('s)http://localhost:9001/)
。使用社交登录时,API键和所有排序都作为参数添加。因此,用户几乎无法控制

为了处理这个问题,我将每个窗口ID注册为其打开的窗口ID

我的功能的第一个后台步骤是
,因为我打开了url“/a.html”

在该步骤中,您可以使用let
let windowID=[]

所以我的step文件看起来像这样

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

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')
  });
请注意,我将凭据添加为一个环境变量。这是一个好主意,您不想将您的个人凭据提交到代码库。显然,人们可能会认为您很好,但您可能不会,谁知道呢


您的问题在几年前就得到了回答,但我是在试图找到解决方案时首先找到这篇文章的,所以添加这篇文章似乎是一个明智的选择。

您的代码中的
client
是什么?