[英]selenium-webdriver npm: how to switch focus to new window from target=“_blank”
在建立一组硒测试时,我使用javascript selenium-webdriver npm有点困难。
此特定测试要求selenium通过查询已加载页面的内容来检查target="_blank"
链接是否有效。 但是我似乎无法让硒将焦点转移到新窗口。
通过阅读文档和SO,看来这应该可行:
driver.switchTo().defaultContent();
但这实际上什么也没做。 这是简单的测试:
//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);
});
});
我还发现了这篇文章: 使用Webdriver的Selenium-切换到没有名称的子窗口但是它是在Java中,我可以将其翻译为javascript,但走得并不远。
有没有其他人能够使用javascript selenium-webdriver做到这一点?
谢谢,约翰
感谢@Stanjer尝试修复,但现在尝试运行foreach时我只是无法定义是不起作用的...? 或“ ReferenceError:未定义availableWindows”
//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 );
}
});
}
});
});
availableWidnows的console.log:
{ 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 }
因此,如果您想在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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.