Skip to main content

Switching windows and tabs

Switch to another 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 sometimes used for the WebRTC media collection. Since we don’t want to dabble or think about it too much, we’ve introduced process.env.RTC_EXTRA_TABS environment variable. Use it when handling your windows:

You should assume that the base window ID is Number(process.env.RTC_EXTRA_TABS) and the pop-up window ID is going to be 1+Number(process.env.RTC_EXTRA_TABS).
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[1+Number(process.env.RTC_EXTRA_TABS)];
    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[Number(process.env.RTC_EXTRA_TABS)];
    this.switchWindow(newWindow);
  });

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){
  window.open(urlToOpen);
}, [url]);

Switch to next tab

.execute('document.getElementsByTagName("body").sendKeys(Keys.CONTROL, Keys.PAGE_DOWN)')

Open a tab per “call”

For voice calls, you may be able to squeeze more sessions to a single probe by using multiple tabs. Here’s a bit of code to get you started:
var tab = 0;
var tabs = 4; // the number of tabs you want to use
var url = process.env.RTC_SERVICE_URL;

for (tab = 0; tab < tabs-1; tab++) {
    client
        .rtcInfo('Before switch to' + tab)
        .execute("window.open('" + url + "', '_blank')")
        .pause(2 * sec)
        .windowHandles(function(tab) {
            return function (result) {
                var newWindow;
                newWindow = result.value[tab + Number(process.env.RTC_EXTRA_TABS)];
                this.switchWindow(newWindow);
            }
        } (tab));
    client.rtcInfo('Tab Opened ' + tab);

    // The code to start you call inside that tab should go here
}

Was this article helpful?

0 out of 0 found this helpful