Support & information center

Switching windows and tabs

Switch to another window

In some cases, the tested application will open a new window. For example, there are cases where the user should sign-in using an external service such as Google or Facebook sign in. External sign-in is usually performed in a separate pop-up window.

The following sample code performs the following flow:

  1. Switch to another window
  2. Enter user’s credentials
  3. Return to the original window

Window ID

Please note that in all tests, window 0 is used for the WebRTC media collection so you should assume that the base window ID is 1 and the pop-up window ID is 2.

client // Click on the sign-in button .click('#sign-in') .pause(1000) // Switch to the new window to sign in .windowHandles(function(result) { var newWindow; newWindow = result.value[2]; this.switchWindow(newWindow); }); client .setValue("#Email", ['my email',client.Keys.ENTER]) .setValue("#Passwd", ['my pass',client.Keys.ENTER]) .pause(1000) // Switch back to the base window .windowHandles(function(result) { var newWindow; newWindow = result.value[1]; this.switchWindow(newWindow); });
Code language: JavaScript (javascript)

Ensure you perform actions in the correct window

When you click inside a popup window (for example Facebook login window) and the window is closed, every command you run next is addressed to the closed window until you call this.switchWindow() to switch back to active tab.

Please note that any action that you will try to perform before you switch back to active tab, such as a click or taking a screenshot, will fail and the test may fail with it.

Open a new tab

client.execute(function(urlToOpen){; }, [EnThinnaiUrl]);
Code language: JavaScript (javascript)

Switch to next tab

.execute('document.getElementsByTagName("body").sendKeys(Keys.CONTROL, Keys.PAGE_DOWN)')
Code language: JavaScript (javascript)

Was this article helpful?

Related Articles