就我个人而言,我只是用它来更好地记录错误。即
it('should display the facility name', function () {
var el = element(by.css('div.facilityName'));
expect(el.isDisplayed()).toBe(true, 'Expected element ' + el.locator() + ' to be present and visible';
});
这会在我的控制台中返回:
失败:
1) 应显示设施名称
预期 false 为真,“预期 By(css selector, div.facilityName) 存在且可见”。
同样,我将它与一些辅助函数一起使用(我在非 Angular 上使用 Protractor,所以我没有等待 Angular 同步可用的能力,所以我使用如下帮助函数:
/**
* @description Prevents test execution until the given element is present in the DOM
* @param [el] The element locator [time] The optional max timeout in ms [opts] The options
*/
Util.prototype.waitForElementPresent = function (el, time, opts) {
var timeout = time || 0,
counter = 0,
verbose = opts ? opts.verbose : false;
return browser.wait(function() {
if (verbose) {
process.stdout.write( !counter ? 'waitForElementPresent [' + el.locator() + '] ' : '.');
counter = counter + 1;
}
return el.isPresent();
}, timeout).then(function () {
if (verbose) {
process.stdout.write('\n');
}
});
};
用法:
it('should display the facility name', function () {
var el = element(by.css('div.facilityName'));
Util.waitForPresentAndVisible(el, 10000, {verbose: true});
expect(el.isDisplayed()).toBe(true, 'Expected element ' + el.locator() + ' to be present and visible';
});
这会打印到控制台:
waitForElementPresent [By(css selector, div.facilityName)]..........
这有点微不足道,因为无论如何错误都会包括失败代码的行和索引。但我发现在某些情况下它有助于改进错误记录。
我很好奇其他人如何使用它。