【问题标题】:"window.angular is undefined." when using protractor for automated testing?“window.angular 未定义。”当使用量角器进行自动化测试时?
【发布时间】:2016-06-22 23:21:11
【问题描述】:

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

【问题讨论】:

  • 恢复到旧版本的量角器(最后知道使用我们的测试脚本)并且它可以工作......?
  • 可能是因为Jasmine如果遇到类似问题,下次尝试切换到使用Jasmine2。 (这有很多可能性,但最简单的解决方案是使用 jasmine2)
  • 升级 Protractor 后我也遇到了类似的错误。对我来说,我在 3.1.1 中没有遇到这个错误,但是我遇到了其他错误,所以我希望 3.2.1 能够修复这些错误。但是对于 3.2.1,我遇到了这个确切的错误。我认为这可能是因为我的 ng-app 指令位于 html 元素而不是 body 元素上。但是将 rootElement: 'html' 添加到我的量角器配置中并没有帮助。 github.com/angular/protractor/issues/1742

标签: javascript angularjs protractor


【解决方案1】:

Exclaimer!!:这并不能回答您的问题,但提供了解决问题的技巧。

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

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

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

(我认为...)

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 这只是示例脚本,我按照你说的在自己的测试中执行 browser.get() 仍然得到同样的错误。
    • 在回答部分它说:“通过研究问题帮助我们找到解决方案,然后贡献您的研究结果以及您尝试过的任何其他内容作为部分答案。”
    【解决方案3】:

    我遇到了同样的问题,它为我解决了:-

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

    【讨论】:

      猜你喜欢
      • 2016-04-26
      • 2023-03-18
      • 2015-03-22
      • 2012-02-03
      • 2014-04-20
      • 2019-05-25
      • 2014-05-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多