我在量角器中有一段代码可以等待:
public waitForElement() {
return browser.wait(
ExpectedConditions.visibilityOf(element(by.id('#someEl'))),
10000,
'Unable to find the element'
);
}
我的问题是,如果它超时,我似乎无法捕获此异常。我尝试添加一个 catch(( 子句,但它不起作用,例如:
this.waitForElement()
.then(() => { /* do something */ })
.catch(() => { /* handle the error -- this code never happens if there is a timeout!!! */ });
我尝试将代码放在 try-catch 块中,但这也无济于事:
try { this.waitForElement().then(() => { }); }
catch (ex) { /* this exception is never caught, the test just fails!! */ }
我很困惑:如何才能抓住等待超时并继续测试而不会失败?
我为此创建了一个简单的测试用例,见下文
// conf
exports.config = {
capabilities: {
'browserName': 'chrome'
},
// Framework to use. Jasmine is recommended.
framework: 'jasmine',
// Spec patterns are relative to the current working directory when
// protractor is called.
specs: ['example_spec.js'],
// Options to be passed to Jasmine.
jasmineNodeOpts: {
defaultTimeoutInterval: 30000
},
allScriptsTimeout: 30000,
};
// Spec
describe('Wait for element', () => {
it('element will be found', () => {
browser.get('http://www.angularjs.org');
waitForElement(element(by.css('.hero')))
.then(() => {
console.log('element is found');
})
.catch((error) => {
console.log('error = ', error);
});
});
it('element will NOT be found', () => {
browser.get('http://www.angularjs.org');
waitForElement(element(by.css('.heroic')))
.then(() => {
console.log('element is found');
})
.catch((error) => {
console.log('element is not found, do something different');
});
});
});
function waitForElement(element) {
return browser.wait(
ExpectedConditions.visibilityOf(element),
3000,
'Unable to find the element'
);
}
这给了我以下输出
~/wswebcreation/contributions/protractor npm run test.example
> protractor@5.1.2 test.example /Users/wswebcreation/contributions/protractor
> node ./bin/protractor example/conf.js
[10:32:17] I/launcher - Running 1 instances of WebDriver
[10:32:17] I/hosted - Using the selenium server at http://localhost:4444/wd/hub/
Started
element is found .
element is not found, do something different.
2 specs, 0 failures
Finished in 6.917 seconds
[10:32:24] I/launcher - 0 instance(s) of WebDriver still running
[10:32:24] I/launcher - chrome #01 passed
所以看起来它有效
你也可以用你的方法做这样的事情
function waitForElement(element, maxWaitTime, failOnError) {
maxWaitTime = maxWaitTime || 10000;
failOnError = failOnError || false;
return browser.wait(ExpectedConditions.visibilityOf(element), maxWaitTime)
.then((found) => Promise.resolve(found))
.catch((waitError) => {
if (failOnError) {
return Promise.reject(`waitForElement: ${waitError} for expected condition ${expectation} for the element: ${element}`);
} else {
return Promise.resolve(false);
}
});
}
这使得它更加"灵活",你可以捕获方法中的错误,如果需要,让它在那里失败,否则传递一个布尔值,你可以用它来进一步。
从@wswebcreation的有用答案中取消选择特别重要的部分:为了完成这项工作,我发现您必须指定显式超时。
以下作品:
return browser.wait(ExpectedConditions.visibilityOf(...), 5000)
.catch(() => { throw new Error('my error'); });
但这不会:
return browser.wait(ExpectedConditions.visibilityOf(...))
.catch(() => { throw new Error('my error'); });
给:
Error: function timed out after 11000 milliseconds
at Timeout._onTimeout (C:projectnode_modulescucumberlibuser_code_runner.js:91:22)
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5)
其中 11000 只是我们配置的默认量角器超时。
另请注意,尽管browser.wait
的文档暗示只有当您想要暂停所有Javascript执行时才需要promise表单,而不仅仅是WebDriver:
此函数阻止 WebDriver 的控制流,而不是 JavaScript 运行。它只会延迟未来的 Web 驱动程序命令 执行(例如,它会导致量角器在发送未来之前等待 命令到硒服务器(,并且仅当 Web 驱动程序控制 流已启用。
此函数返回一个 promise,如果需要,可以使用该承诺 阻止JavaScript执行,而不仅仅是控制流。
。显然也不可能在try-catch 中捕获无超时、无承诺browser.wait
的结果,因为条件在setTimeout
中运行。
以上显然适用于量角器 5.3.2 与量角器-黄瓜框架 4.2.0 一起使用时。