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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28064207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 08:21:40  来源:igfitidea点击:

How to explain using browser.sleep in protractor

javascriptcssangularjsseleniumprotractor

提问by Kiry

I'm a new to protractor,and I'm trying to test popover event, here is my code:

我是量角器的新手,我正在尝试测试弹出事件,这是我的代码:

    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.If I don't add delay time code like browser.sleep(), the test will fail and show a message:

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

NoSuchElementError: No element found using locator: By.className('popover-title')

Is that possible not to add delay time code ...like browser.sleep()? If that is impossible, how to make sense to set the sleep time well? Is that have some connection with CSS animations?

2.Using browser.waitForAngular()or click().then(function(){...})instead of browser.sleep()seems no work, will get the same error message.

是否可以不添加延迟时间代码......比如browser.sleep()?如果这是不可能的,如何合理设置睡眠时间?这与 CSS 动画有什么联系吗?

2.使用browser.waitForAngular()click().then(function(){...})代替browser.sleep()似乎没有用,会得到同样的错误信息。

That would be great if someone can answer these questions, thanks a lot.

如果有人能回答这些问题,那就太好了,非常感谢。

回答by hankduan

The reason that you had to add a sleep is likely because of your animation. Many animations use setTimeout, which Angular (thus Protractor) does not know about and do not wait for. The angular equivalent to setTimeoutis its $timeoutservice.

您必须添加睡眠的原因可能是因为您的动画。许多动画使用setTimeout,Angular(因此是量角器)不知道也不等待。角度相当于setTimeout是它的$timeout服务。

But often you cannot change the animation library to stop using setTimeout. To work with this in protractor, use browser.waitwith a timeout (not sleep).

但通常您无法更改动画库以停止使用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);      

With Protractor 1.7, you will be able to use the expectedConditions library so you can do this:

使用 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); 

回答by Ziwdigforbugs

instead of using browser, declare a protractor instance :

而不是使用浏览器,声明一个量角器实例:

  ptor = protractor.getInstance();
  ptor.ignoreSynchronization = true;

Then use:

然后使用:

ptor.get

instead of browser.

而不是浏览器。