简体   繁体   中英

How to wait for promise in protractor/ jasmine test?

I am learning about Protractor (and, by extension, Jasmine, that Protractor uses) and I need, for a certain reason, need to wait for a Promise to continue testing. However, I am not sure how to do that.

Let's take the code from Protractor tutorial and add some code, and let's add a simple Promise (in ES6 style) that doesn't do anything

describe('angularjs homepage todo list', function() {
  it('should add a todo', function() {
    browser.get('https://angularjs.org');

    element(by.model('todoList.todoText')).sendKeys('write first protractor test');
    element(by.css('[value="add"]')).click();

    var todoList = element.all(by.repeater('todo in todoList.todos'));

    Promise.resolve().then( () => {
      expect(todoList.count()).toEqual(3);
      expect(todoList.get(2).getText()).toEqual('write first protractor test');

      // You wrote your first test, cross it off the list
      todoList.get(2).element(by.css('input')).click();
      var completedAmount = element.all(by.css('.done-true'));
      expect(completedAmount.count()).toEqual(2);
    });
  });
});

This doesn't work, because this is the answer

$ protractor conf.js
Using the selenium server at http://localhost:4444/wd/hub
[launcher] Running 1 instances of WebDriver
F

Failures:

  1) angularjs homepage todo list should add a todo
   Message:
     Error while waiting for Protractor to sync with the page: "angular could not be found on the window"
   Stacktrace:
     undefined

Finished in 0.212 seconds
1 test, 1 assertion, 1 failure

[launcher] 0 instance(s) of WebDriver still running
[launcher] chrome #1 failed 1 test(s)
[launcher] overall: 1 failed spec(s)
[launcher] Process exited with error code 1

What should I do to make it work?

I solved it in a following way:

  • first, used jasmine2 as framework instead of jasemine by writin

framework: 'jasmine2'

to conf.js

  • Editing the file like this

     var flow = browser.flow() describe('angularjs homepage todo list', function() { it('should add a todo', function() { browser.get('https://angularjs.org'); element(by.model('todoList.todoText')).sendKeys('write first protractor test'); element(by.css('[value="add"]')).click(); var todoList = element.all(by.repeater('todo in todoList.todos')); flow.execute(function(){Promise.resolve()}); // or, more ES6-y version // flow.execute(() => Promise.resolve()); expect(todoList.count()).toEqual(3); expect(todoList.get(2).getText()).toEqual('write first protractor test'); // You wrote your first test, cross it off the list todoList.get(2).element(by.css('input')).click(); var completedAmount = element.all(by.css('.done-true')); expect(completedAmount.count()).toEqual(2); }); }); 

Jasmine is actually "chaining" the promises in expect together itself, which conveniently solves my problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM