“window.angular未定义。”使用量角器进行自动化测试时?

时间:2016-03-11 11:16:08

标签: javascript angularjs protractor

使用量角器提供的示例conf.js时,我似乎遇到了错误。 我正在使用grunt-protractor-runner运行我的测试,但即使使用提供的示例配置也会出错。

我的Gruntfile.js看起来像这样:

/*global module:false*/
module.exports = function(grunt) {
  // Project configuration.
    grunt.initConfig({
      protractor: {
        options: {
          configFile: "smoketest.conf.js", // Default config file
          keepAlive: false, // If false, the grunt process stops when the test fails.
          noColor: false, // If true, protractor will not use colors in its output.
          webdriverManagerUpdate: true,
          args: {
            seleniumServerJar: './node_modules/protractor/selenium/selenium-server-standalone-2.51.0.jar'
          }
        },
        smoke_test: {   // Grunt requires at least one target to run so you can simply put 'all: {}' here too.
          options: {
            configFile: "smoketest.conf.js", // Target-specific config file
            args: {
              }
          }
        },
        protractor_test: {   // Grunt requires at least one target to run so you can simply put 'all: {}' here too.
            options: {
                configFile: "./node_modules/protractor/example/conf.js", // Target-specific config file
                args: {
                }
            }
        },


      },
    })

  grunt.loadNpmTasks('grunt-protractor-runner');
  // Default task.
  grunt.registerTask('default', ['protractor:smoke_test']);

};

我正在运行使用此文件的grunt protractor:protractor_test

describe('angularjs homepage', function() {
  it('should greet the named user', function() {
    browser.get('http://www.angularjs.org');

    element(by.model('yourName')).sendKeys('Julie');

    var greeting = element(by.binding('yourName'));

    expect(greeting.getText()).toEqual('Hello Julie!');
  });

  describe('todo list', function() {
    var todoList;

    beforeEach(function() {
      browser.get('http://www.angularjs.org');

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

    it('should list todos', function() {
      expect(todoList.count()).toEqual(2);
      expect(todoList.get(1).getText()).toEqual('build an angular app');
    });

    it('should add a todo', function() {
      var addTodo = element(by.model('todoList.todoText'));
      var addButton = element(by.css('[value="add"]'));

      addTodo.sendKeys('write a protractor test');
      addButton.click();

      expect(todoList.count()).toEqual(3);
      expect(todoList.get(2).getText()).toEqual('write a protractor test');
    });
  });
});

然而,当这次运行时,我会看到错误

Error while waiting for Protractor to sync with the page: "window.angular is undefined.  This could be either because this is a non-angular page or because your test involves client-side navigation, which can interfere with Protractor's bootstrapping.  See http://git.io/v4gXM for details"`enter code here`

我去过http://git.io/v4gXM,但我似乎无法找到解决问题的方法吗?有没有其他人有这个问题,当然示例测试应该始终有效吗?

3 个答案:

答案 0 :(得分:8)

Exclaimer !!:这并没有回答你的问题,但提供了解决问题的方法。

Protractor要求Angular页面在运行它的期望之前完成同步。因此,为了解决此问题,您可以使用:

ds.Tables("Users").Rows(inc).Item(1) = txtFirstName.Text

这告诉浏览器,量角器打开不等待Angular同步(ignoreSynchronization),然后它等待angular完成它正在做的其他事情,然后它增加500毫秒的等待以给量角器一个机会找到browser.ignoreSynchronization = true; browser.waitForAngular(); browser.sleep(500); 。当等待完成时,它会强制量角器移动到包含您期望的下一行代码,在此之前,它停在addButton.click()行并等待同步(这没有发生),在它继续之前。

(我认为......)

答案 1 :(得分:0)

我有完全相同的问题(带有Jasmine2的Protractor 3.1.0)。在我看来,browser.get()电话中的beforeEach()是罪魁祸首。 将其复制到每个测试可能是一种解决方法。

答案 2 :(得分:0)

我遇到了同样的问题而且它解决了我: -

1. downgrade protractor to 3.0.0
2. add jasmine2 in conf.js