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

Tro*_*i94 9 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']);

};
Run Code Online (Sandbox Code Playgroud)

我正在运行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');
    });
  });
});
Run Code Online (Sandbox Code Playgroud)

但是,当这个运行时,我会看到错误

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`
Run Code Online (Sandbox Code Playgroud)

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

Joe*_*Joe 8

Exclaimer !!:这不能回答你的问题,但提供了一个解决它的黑客.

量角器要求Angular页面在运行期望之前完成同步.因此,为了解决此问题,您可以使用:

browser.ignoreSynchronization = true;
browser.waitForAngular();
browser.sleep(500); 
Run Code Online (Sandbox Code Playgroud)

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

(我认为...)


Tom*_*mVW 0

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