如何在量角器/茉莉花测试中等待承诺?

时间:2015-10-23 14:26:51

标签: javascript jasmine protractor es6-promise

我正在学习Protractor(以及扩展名,Protractor使用的Jasmine),由于某种原因,我需要等待Promise继续测试。但是,我不知道该怎么做。

让我们从Protractor教程中获取代码并添加一些代码,让我们添加一个不做任何事情的简单Promise(ES6风格)

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);
    });
  });
});

这不起作用,因为这是答案

$ 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

我该怎么做才能让它发挥作用?

1 个答案:

答案 0 :(得分:0)

我通过以下方式解决了这个问题:

  • 首先,使用jasmine2作为框架代替jasemine by writin

framework: 'jasmine2'

conf.js

  • 像这样编辑文件

    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);
    
      });
    });
    

茉莉花实际上是在一起“链接”一起期待的承诺,这很方便地解决了我的问题。