How to get handle of popup window (WebdriverIO) How to get handle of popup window (WebdriverIO) selenium selenium

How to get handle of popup window (WebdriverIO)


we not use getCurrentTabId to remember the handle of the currently open window?

For example:

var main, popup; //your window handlesclient  .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. This caused an issue on finding the handle for the new window because I could not use .newWindow('http://localhost:9001/'). The API keys and all sorts are added as parameters when using a social login. So one has little control

To handle this I registered each window ID as its opened.

The first background step in my feature is Given I open the url "/a.html"

In the step you can set an empty array as the variable windowID with let let 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

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.