How can I wait for a condition?

前端 未结 8 766
礼貌的吻别
礼貌的吻别 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: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!

提交回复
热议问题