我正在尝试检查点击按钮打开 facebook 登录的弹出窗口。
Error : Object [object Object] has no method 'getWindowHandle'.
代码片段生成错误:
describe('Tests', function() {
var ptor;
var handlePromise;
var util = require('util');
beforeEach(function() {
ptor = protractor.getInstance();
handlePromise = ptor.getAllWindowHandles();
var handlesDone = false;
ptor.get('/SiteB_Upgrade_Device/app/index.html#/Recommendations#page');
ptor.findElement(by.id('fb')).click();
ptor.ignoreSynchronization = true;
});
describe('login', function() {
return it('should switch to popUp\'s handle', function() {
handlePromise.then(function(handles) {
var popUpHandle = handles[0];
var handle = browser.driver.switchTo().window(popUpHandle).getWindowHandle();
expect(handle).toEqual(popUpHandle);
});
},30000);
});
});
最佳答案
这是我目前用来浏览弹出窗口/标签的内容:
// do stuff that will trigger the popup
// ...
browser.getAllWindowHandles().then(function (handles) {
// switch to the popup
browser.switchTo().window(handles[1]);
// make sure the popup is now focused
expect(browser.getCurrentUrl()).toEqual('popup/url');
// do stuff with the popup
// ...
// go back to the main window
browser.switchTo().window(handles[0]);
// make sure we are back to the main window
expect(browser.getCurrentUrl()).toEqual('original/url');
});
您只需要确保您的弹出窗口确实是一个新窗口而不是某种弹出窗口(在这种情况下您可以使用 css 选择器定位它)。
当您更改选项卡/弹出窗口时要记住的另一件事是目标页面可能没有加载 angularjs,这将呈现 Protractor useles。如果您遇到这种情况,您可以简单地使用 browser.driver
作为 browser
的替代品来导航非 Angular 页面。
希望这对您有所帮助。
关于javascript - Protractor E2E测试错误: Object [object Object] has no method 'getWindowHandle' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21700162/