如何解释在量角器中使用browser.sleep

Kir*_*iry 6 javascript css selenium angularjs protractor

我是量角器的新手,我正在尝试测试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);      
        });
    }); 
Run Code Online (Sandbox Code Playgroud)

1.如果我没有添加延迟时间代码browser.sleep(),测试将失败并显示一条消息:

NoSuchElementError: No element found using locator: By.className('popover-title')
Run Code Online (Sandbox Code Playgroud)

是否有可能不添加延迟时间代码...喜欢browser.sleep()?如果这是不可能的,那么如何理解睡眠时间呢?这与CSS动画有一些联系吗?

2.使用browser.waitForAngular()click().then(function(){...})代替browser.sleep()似乎没有工作,将得到相同的错误消息.

如果有人能回答这些问题,那将是很好的,非常感谢.

han*_*uan 8

您必须添加睡眠的原因可能是因为您的动画.许多动画使用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);      
Run Code Online (Sandbox Code Playgroud)

使用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); 
Run Code Online (Sandbox Code Playgroud)