【发布时间】:2016-05-13 00:59:56
【问题描述】:
我有一个应用程序在单击按钮时启动 $timeout,因此我必须将 ignoreSynchronization 设置为 true。在此期间,我需要等待元素被添加到页面中,并且在等待期间我遇到了一些有趣的行为:
在 browser.wait(element, timeout, error message) 期间传入的等待超时什么也不做。唯一重要的等待是在浏览器上设置的implicitTimeout。最重要的是,将使用整个隐式超时。如果发现该元素存在,它将继续检查直到超时结束。这意味着测试将始终在给定的最长时间内缓慢运行。
describe('Cool Page', () =>{
beforeEach(function(){
browser.ignoreSynchronization = true;
return browser.sleep(250);
});
afterEach(function(){
browser.ignoreSynchronization = false;
return browser.sleep(250);
});
it('can open menu with timeout', function(){
// No timeout at this point
coolPage.wait.ellipsesIcons().then(() =>{
// Clicking the ellipses icons kicks off a $timeout of 100 seconds
coolPage.ellipsesIcons.click().then(() =>{
coolPage.wait.dropdownOptions().then(() => {
expect(coolPage.dropdownOptions.getText()).toContain('Option 1');
});
});
});
});
})
.
export = new CoolPage;
class CoolPageextends PageObject {
private elements;
constructor(){
super();
... // Lots of other stuff
this.initWait();
}
... // Initializing elements and other things
private initWait() {
this.wait = {
ellipsesIcons: () => {
// Timeout of 5 seconds will be used - regardless of isPresent resolving as true or false, the entire 5 seconds will be used
browser.manage().timeouts().implicitlyWait(5000);
// The 2 milliseconds passed in here does nothing at all
browser.wait(element(by.css('.fa-ellipses-h')).isPresent(), 2, 'Ellipses Icon(...) was not present in time');
// Must reset implicit wait back to the original 25 seconds it was set too in the conf
browser.manage().timeouts().implicitlyWait(25000);
return browser.sleep(150);
},
dropdownOptions: () => {
// This two seconds wait WILL be used
browser.manage().timeouts().implicitlyWait(2000);
// This five second wait WILL NOT be used
browser.wait(element(by.css('[name="change-status"]')).isPresent(), 5000, 'Options actions menu item was not present in time');
browser.manage().timeouts().implicitlyWait(25000);
return browser.sleep(150);
},
}
}
通过 browser.wait 传入的超时在这里不起作用。我的问题是:
- browser.wait 超时有什么作用?
- 什么时候使用隐式等待?我以为它只是在等待页面加载
- 有什么方法可以传入实际使用的超时时间吗?
- 如果没有,有什么方法可以让等待在满足条件后立即退出,而不是使用整个超时时间?
【问题讨论】:
标签: javascript selenium selenium-webdriver webdriver protractor