While trying to create a collection of selenium tests, I encountered an obstacle with the javascript selenium-webdriver npm package.
One specific test involves selenium verifying that a target="_blank"
link functions properly by checking the content of the loaded page. However, I am unable to get selenium to switch focus to the new window.
After consulting the documentation and Stack Overflow, it appears that this code snippet should work:
driver.switchTo().defaultContent();
Unfortunately, this method does not achieve the desired result. The test script looks like this:
//1 load the URL
driver.get( testConfig.url + '/britain/england/cornwall/hotelX' ).then(function(){
//2 locate the link and click on it
driver.findElement(By.css('.details-web')).click().then(function(){
//3 wait for 10 seconds for the new window to open
setTimeout(function(){
//switch focus to the new window (current active window)
driver.switchTo().defaultContent().then(function(){
//5 wait until the new element 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 came across this post: Selenium with Webdriver - Switch to child window without name However, it is written in Java, and my attempt to translate it to JavaScript was not successful.
Has anyone else been able to accomplish this using javascript selenium-webdriver?
Thanks, John
Following some advice from @Stanjer, I now encounter an issue where I receive an "undefined is not a function" error when attempting to run the forEach loop... 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 output of the availableWindows object:
{ 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: 有效回 },
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 }