我是量角器的新手,我正在尝试测试popover事件,
这是我的代码:
这是我的代码:
describe('popover directive',function() {
var buttons= element.all(by.tagName('button'));
it('should show the popover title by clicking',function() {
var popTitle="testTitle";
var popContent="testContent";
element(by.model('title')).clear().sendKeys(popTitle);
element(by.model('content')).clear().sendKeys(popContent).then(function(){
buttons.get(0).click().then(function(){
browser.sleep(1000);
expect(element(by.className('popover-title')).getText()).toMatch(popTitle);
expect(element(by.className('popover-content')).getText()).toMatch(popContent);
});
});
buttons.get(0).click();
browser.sleep(1000);
expect(element(by.css('[class="popover fade top in"]')).isPresent()).toBe(false);
});
});
1.如果我没有添加像browser.sleep()这样的延迟时间代码,测试将失败并显示一条消息:
NoSuchElementError: No element found using locator: By.className('popover-title')
这有可能不添加延迟时间代码…像browser.sleep()?如果这是不可能的,那么如何理解睡眠时间呢?这与CSS动画有一些联系吗?
2.使用browser.waitForAngular()或click().然后(function(){…})而不是browser.sleep()似乎没有用,将得到相同的错误消息.
如果有人能回答这些问题,那将是很好的,非常感谢.
解决方法
您必须添加睡眠的原因可能是因为您的动画.许多动画使用setTimeout,Angular(因此Protractor)不知道并且不等待.与setTimeout等效的角度是它的$timeout服务.
但通常您无法更改动画库以停止使用setTimeout.要在量角器中使用它,请使用带有超时(不是睡眠)的browser.wait.
buttons.get(0).click();
browser.wait(function() {
return element(by.css('[class="popover fade top in"]')).isPresent().then(function(present) {
return !present;
});
// or by checking any other element to kNow that the animation completed
},1000);
expect(element(by.css('[class="popover fade top in"]')).isPresent()).toBe(false);
使用Protractor 1.7,您将能够使用expectedConditions库,以便您可以执行此操作:
var EC = protractor.ExpectedConditions
buttons.get(0).click();
var e = element(by.css('[class="popover fade top in"]'));
browser.wait(EC.not(EC.presenceOf(e)),1000);
expect(e.isPresent()).toBe(false);