我使用 Protractor 进行端到端测试。当我开始测试时,我会看到这条消息
conFusion App E2E Testing menu 0 item should show the first comment author as Message: Failed: No element found using locator: by.model("FiltText")
如何让 Protractor 等到元素出现在 DOM 中?
对应的 Protractor 配置代码为:
exports.config = {
allScriptsTimeout:11000,
specs: [
'e2e/*.js'
],
capabilities: {
'browserName': 'chrome'
},
baseUrl: 'http://localhost:3001/',
framework: 'jasmine',
directConnect: true,
jasmineNodeOpts: {
showColors: true,
defaultTimeoutInterval: 30000
}
};
包含端到端测试的scenarios.js代码
describe('menu 0 item', function() {
beforeEach(function() {
browser.get('index.html#/menu/0');
});
it('should have a name', function() {
var name = element(by.binding('dish.name'));
expect(name.getText()).
toEqual('Uthapizza Hot $4.99');
});
it('should show the number of comments as', function() {
expect(element.all(by.repeater('comment in dish.comments'))
.count()).toEqual(5);
});
it('should show the first comment author as', function() {
element(by.model('FiltText')).sendKeys('author');
expect(element.all(by.repeater('comment in dish.comments'))
.count()).toEqual(5);
var author = element.all(by.repeater('comment in dish.comments'))
.first().element(by.binding('comment.author'));
expect(author.getText()).toContain('25 Cent');
});
});
最佳答案
您可以使用 ExpectedConditions:
var EC = protractor.ExpectedConditions;
var timeout = 5000; // in miliseconds
// Waits for the element to be present on the dom.
browser.wait(EC.presenceOf(element(by.model("FiltText"))), timeout);
// Here you can safely use your element, because here it is guaranted to be present.
因为 Protractor 使用控制流,此命令将不会完成,直到 FiltText
可见或时间大于超时。
更多信息在这里: http://www.protractortest.org/#/api?view=ProtractorExpectedConditions
关于angularjs - Protractor :don't wait for elements to appears in Dom,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43307515/