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

新手上路,请多包涵

我似乎在使用量角器提供的示例 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 但我似乎找不到任何东西来解决我的问题?有没有其他人遇到过这个问题,示例测试应该总是有效吗?

原文由 James T 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 522
1 个回答

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

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

 browser.ignoreSynchronization = true;
browser.waitForAngular();
browser.sleep(500);

这告诉浏览器打开量角器不等待 Angular 同步(忽略同步),然后它等待角度完成它正在做的所有其他事情,然后它增加 500 毫秒的等待时间让量角器有机会找到 addButton.click() 。等待完成后,它会强制量角器移动到包含您期望的下一行代码,在此之前,它停在 addButton.click() 行并等待同步(这没有发生),在它继续前进之前。

(我认为…)

原文由 Joe 发布,翻译遵循 CC BY-SA 3.0 许可协议

推荐问题