javascript - "window.angular is undefined."使用 Protractor 进行自动化测试时?

标签 javascript angularjs protractor

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

最佳答案

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

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

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

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

(我觉得……)

关于javascript - "window.angular is undefined."使用 Protractor 进行自动化测试时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35938841/

相关文章:

javascript - 如何将 ng-show 与复选框一起使用

angularjs - 从 Protractor 测试编排非 JavaScript 后端

javascript - 如何在特定数字处停止翻转计时器

javascript - 如何将实例与声明文件中的命名空间合并?

angularjs - SpringMVC随机不返回响应

javascript - ng-click 在 ionic 应用程序中没有做任何事情

javascript - React 从动态添加的输入文本字段中获取值

javascript - PHP:- 在 newtab 中通过 POST 验证并传递值

angularjs - 重新运行 Protractor 超时或失败的测试

protractor - 抑制 Protractor 中的错误消息