user1037355
user1037355

Reputation:

selenium-webdriver npm: how to switch focus to new window from target="_blank"

In building up a bank of selenium tests i have a bit of a brick wall with the javascript selenium-webdriver npm.

This particular test requires selenium to check that a target="_blank" link works by querying the content of the page loaded. However i cannot seem to get selenium to shift focus to the new window.

From reading the docs and S.O. it would seem this should work:

driver.switchTo().defaultContent();

But this actually does nothing. This is the simple test:

//1 load up the url
    driver.get( testConfig.url + '/britain/england/cornwall/hotelX' ).then(function(){
        //2 find the link and click it
        driver.findElement(By.css('.details-web')).click().then(function(){
            //3 timeout of 10 seconds to allow the other window to open
            setTimeout(function(){
                //switch focus to the new window (ie the current active window)
                driver.switchTo().defaultContent().then(function(){
                    //5 wait till the new el is visible
                    driver.wait( function(){
                        return driver.isElementPresent(By.css("#infinite-footer"));
                    }, testConfig.timeout).then(function(){
                        driver.close();
                        if( callback ){
                            callback( callback );
                        }
                    });
                });
            },10*1000);
        });
    });

I also found this post: Selenium with Webdriver - Switch to child window without name But it is in Java, i had a go at translating it to javascript but didn't get very far.

Has anyone else managed to do this with the javascript selenium-webdriver?#

Thanks, John

Attempted fix thanks to @Stanjer, but now i just get undefined is not function when trying to run the foreach...? or "ReferenceError: availableWindows is not defined "

//1 load up the url
        driver.get( testConfig.url + '/britain/england/cornwall/hotelX' ).then(function(){
            //2 find the link and click it
            driver.findElement(By.css('.details-web')).click().then(function(){
                var parent = driver.getWindowHandle();
                driver.sleep(1000);
                var availableWindows = driver.getAllWindowHandles();
                var newWindow = null;
                availableWindows.forEach(function(window) {
                    console.log( window );
                    if (window != parent) {
                        newWindow = window;
                    }
                });
                if (newWindow != null) {
                    driver.switchTo().window(newWindow);

                    driver.wait( function(){
                        return driver.isElementPresent(By.css("#infinite-footer"));
                    }, testConfig.timeout).then(function(){
                        driver.close();
                        if( callback ){
                            callback( callback );
                        }
                    });
                }
            });
        });

The console.log of the availableWidnows:

{ closure_uid_233425945: 273,
  flow_:
   { events_: {},
     closure_uid_233425945: 1,
     activeFrame_:
      { events_: [Object],
        closure_uid_233425945: 29,
        flow_: [Circular],
        parent_: [Object],
        children_: [Object],
        lastInsertedChild_: [Object],
        pendingTask_: null,
        isLocked_: true,
        isBlocked_: false,
        pendingCallback: false,
        pendingRejection: false,
        cancellationError_: null },
     schedulingFrame_:
      { events_: {},
        closure_uid_233425945: 204,
        flow_: [Circular],
        parent_: [Object],
        children_: [Object],
        lastInsertedChild_: [Object],
        pendingTask_: null,
        isLocked_: false,
        isBlocked_: false,
        pendingCallback: false,
        pendingRejection: false,
        cancellationError_: null },
     shutdownTask_: null,
     eventLoopTask_: null,
     hold_:
      { _idleTimeout: 2147483647,
        _idlePrev: [Object],
        _idleNext: [Object],
        _idleStart: 410669389,
        _onTimeout: [Function: wrapper],
        _repeat: true },
     yieldCount_: 2 },
  stack_: null,
  parent_:
   { closure_uid_233425945: 271,
     flow_:
      { events_: {},
        closure_uid_233425945: 1,
        activeFrame_: [Object],
        schedulingFrame_: [Object],
        shutdownTask_: null,
        eventLoopTask_: null,
        hold_: [Object],
        yieldCount_: 2 },
     stack_: { [Task: WebDriver.getAllWindowHandles()] name: 'Task' },
     parent_: null,
     callbacks_: [ [Object] ],
     state_: 'pending',
     handled_: true,
     pendingNotifications_: false,
     value_: undefined },
  callbacks_: null,
  state_: 'pending',
  handled_: false,
  pendingNotifications_: false,
  value_: undefined }

Upvotes: 0

Views: 2539

Answers (1)

Stan
Stan

Reputation: 3461

So, if you want the exact copy of that code in js:

parent = driver.getWindowHandle();

driver.sleep(1000);
var availableWindows = driver.getAllWindowHandles();
var newWindow = null;
availableWindows.foreach(function(window) {
    if (window != parent) {
        newWindow = window;
    }
}
if (newWindow != null) {
    driver.switchTo().window(newWindow);
}

Upvotes: 1

Related Questions