异步测试不适用于量角器

时间:2014-03-28 10:29:48

标签: angularjs selenium-webdriver protractor jasmine-node

我正在尝试为异步测试调用done()但是这样做不起作用,我得到'undefined is is a function'错误。

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

我试图谷歌,并发现茉莉花可能存在一些问题,但我仍然无法解决这个问题。因为这个错误似乎真的很意外。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

您确定undefined is not a function排在done()行吗?

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

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

  1. 将量角器升级到最新版
  2. 重写整个测试,如下所示,因为此处不需要调用done()
  3. 重写:

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