How can I wait for a condition?

前端 未结 8 721
礼貌的吻别
礼貌的吻别 2020-12-07 15:39

I\'m new on protractor, and I\'m trying to implement an e2e test. I don\'t know if this is the right way to do this, but... The page that I want to test is not a full angula

相关标签:
8条回答
  • 2020-12-07 16:08

    Protractor 1.7.0 has also introduced a new feature: Expected Conditions.

    There are several predefined conditions to explicitly wait for. In case you want to wait for an element to become present:

    var EC = protractor.ExpectedConditions;
    
    var e = element(by.id('xyz'));
    browser.wait(EC.presenceOf(e), 10000);
    
    expect(e.isPresent()).toBeTruthy();
    

    See also:

    • Expected conditions in protractor
    0 讨论(0)
  • 2020-12-07 16:10

    Have you tried putting the ng-app in the <html> tag (assuming this part of code is under your control)? This solved a lot of initialization timing problems for me.

    0 讨论(0)
  • 2020-12-07 16:23

    Thanks to answers above, this was my simplified and updated usage

    function waitFor (selector) {
      return browser.wait(function () {
        return browser.isElementPresent(by.css(selector));
      }, 50000);
    }
    
    0 讨论(0)
  • 2020-12-07 16:23

    Best way to use wait conditions in protractor that helps to show proper error message to particular element if test case failed

    const EC = ExpectedConditions;
    const ele = element(by.xpath(your xpath));
    
    return browser.wait(EC.visibilityOf(ele),9000,'element not found').then(() => {
                ele.click();
             });
    
    0 讨论(0)
  • 2020-12-07 16:23

    I'm surprised that nobody has added this solution. Basically, if you are using modal dialogues you often get an element visible and available to click but not being clickable due to the modal dialogue being in front of it. This happens because protractor moves faster than angular and is ready to click the next element while angular is still closing the modal.

    I suggest using

    public async clickElementBug(elementLocator: Locator) {
    const elem = await element(elementLocator);
    await browser.wait(
      async function() {
        try {
          await elem.click();
          return true;
        } catch (error) {
          return false;
        }
      },
      this.TIMEOUT_MILLIS,
      'Clicking of element failed: ' + elem
    );
    

    }

    0 讨论(0)
  • 2020-12-07 16:25

    I had the same problem you were having for the longest time while using protractor. In my e2e test I start in a non angular app, then get into an angular portion, then get back out to a non angular portion. Made things tricky. The key is to understand promises and how they work. Here's some examples of my real world code in a functioning e2e test. Hoping this gives you an idea of how to structure your tests. Probably some bad practice in this code, please feel free to improve upon this, but I know that it works, maybe not the best way.

    To get to angular I use

    var ptor;
    var events = require('events');
    var eventEmitter = new events.EventEmitter();
    var secondClick = require('./second-click');
    
    beforeEach(function () {
        browser.driver.get('http://localhost:8080/');
    },10000);
    
    it("should start the test", function () {
        describe("starting", function () {
            it("should find the  link and start the test", function(){
                var elementToFind = by.linkText('Start'); //what element we are looking for
                browser.driver.isElementPresent(elementToFind).then(function(isPresent){
                    expect(isPresent).toBe(true); //the test, kind of redundant but it helps pass or fail
                    browser.driver.findElement(elementToFind).then(function(start){
                        start.click().then(function(){ //once we've found the element and its on the page click it!! :) 
                            ptor = protractor.getInstance(); //pass down protractor and the events to other files so we can emit events
                            secondClick(eventEmitter, ptor); //this is your callback to keep going on to other actions or test in another file
                        });
                    });
                });
            });
        });
    },60000);
    

    While in angular this code works

     describe("type in a message ", function(){
            it("should find and type in a random message", function(){
                var elementToFind = by.css('form textarea.limited');
                browser.driver.isElementPresent(elementToFind).then(function(isPresent){
                    element(elementToFind).sendKeys(randomSentence).then(function(){
                        console.log("typed in random message");
                        continueOn();
                    });
                });
            });
        },15000);
    

    After exiting angular

    browser.driver.wait(function(){
       console.log("polling for a firstName to appear");
       return    browser.driver.isElementPresent(by.name('firstName')).then(function(el){
             return el === true;
           });
         }).
       then(function(){
           somefunctionToExecute()
        });
    

    Hope that gives some guidance and helps you out!

    0 讨论(0)
提交回复
热议问题