3

我正在尝试调用 done() 进行异步测试,但这不起作用,我收到“未定义不是函数”错误。

describe('Login screen tests', function () {
  var ptor = protractor.getInstance();
  beforeEach(function(){
    console.log('In before Each method');
    ptor.get('http://staging-machine/login/#/');
  });

  it('Blank Username & Password test', function(done) {
    ptor.findElement(protractor.By.id("submit")).click();
    var message = ptor.findElement(protractor.By.repeater('message in messages'));
    message.then(function(message){
      message.getText().then(function(text) {
        console.log("Message shown:"+text);
        expect(message.getText()).toContain('Username or Password can\'t be blank');
        done();
      });
    });
  });
});

我试着用谷歌搜索,发现茉莉花可能有一些问题,但我仍然无法解决这个问题。因为错误似乎真的出乎意料。任何帮助,将不胜感激。

4

1 回答 1

2

你确定你要undefined is not a function排队done()吗?

我认为您的问题就在这里:ptor.findElement(protractor.By.repeater('message in messages'))因为到那时您显然在 Angular 页面上,所以关于 webdriver 的 findElement 用于转发器:您不应该这样做。

无论如何,我会做两件事:

  1. 将量角器升级到最新版本
  2. 像下面这样重写整个测试,因为done()这里根本不需要调用。

改写:

describe('Login screen tests', function () {
  // Page Objects. TODO: Extract to separate module file.
  var submitBtnElm = $('#submit');
  var messagesRepElms = element.all(by.repeater('message in messages'));

  describe('Blank Username & Password test', function() {
    // Moved login get out of beforeEach since you need to get it once
    it('Opens an Angular login page', function() {
      browser.get('http://staging-machine/login/#/');
    });

    it('Clicks submit btn without entering required fields', function() {
      submitBtnElm.click();
    });

    it('Should trigger validation errors', function() {
      expect(messagesRepElms.first().isPresent()).toBeTruthy();
      expect(messagesRepElms.first().getText()).
        toContain('Username or Password can\'t be blank');
    });
  });
});
于 2014-08-17T00:47:49.433 回答