我在量角器中有一段代码可以等待:
public waitForElement() {
return browser.wait(
ExpectedConditions.visibilityOf(element(by.id('#someEl'))),
10000,
'Unable to find the element'
);
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,如果超时,我似乎无法捕获此异常。我尝试添加一个 catch() 子句,但它不起作用,例如:
this.waitForElement()
.then(() => { /* do something */ })
.catch(() => { /* handle the error -- this code never happens if there is a timeout!!! */ });
Run Code Online (Sandbox Code Playgroud)
我尝试将代码放在 try-catch 块中,但这也无济于事:
try { this.waitForElement().then(() => { }); }
catch (ex) { /* this exception is never caught, the test just fails!! */ }
Run Code Online (Sandbox Code Playgroud)
我很难过:我怎样才能赶上等待超时并继续测试而不会失败?
我为此创建了一个简单的测试用例,见下文
// 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'
);
}Run Code Online (Sandbox Code Playgroud)
这给了我以下输出
~/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
Run Code Online (Sandbox Code Playgroud)
所以看起来它有效
你也可以用你的方法做这样的事情
~/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
Run Code Online (Sandbox Code Playgroud)
这使它更加“灵活”,您可以捕获方法中的错误,如果需要,让它在那里失败,否则传递一个可用于进一步操作的布尔值。
| 归档时间: |
|
| 查看次数: |
7357 次 |
| 最近记录: |