“window.angular 未定义."当使用量角器进行自动化测试时? [英] "window.angular is undefined." when using protractor for automated testing?

查看:22
本文介绍了“window.angular 未定义."当使用量角器进行自动化测试时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I seem to have an error when using the example conf.js provided with protractor. I am running my tests with grunt-protractor-runner but it errors even when using the example config provided.

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

My Gruntfile.js looks like this:

/*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:

I am running grunt protractor:protractor_test which uses this file:

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');
    });
  });
});

但是,当它运行时,我会遇到错误

however, when this runs i am presented with the error

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

I have been to http://git.io/v4gXM but i cannot seem to find anything to fix my issue? Has anybody else had this issue, surely the example test should work always??

推荐答案

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

Exclaimer!!: This doesn't answer your question as such but provides a hack to solve it.

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

Protractor requires the Angular page to finish synchronization before it runs it's expectations. Therefore, in order to work around this issue you can use:

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

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

This tells the browser that protractor opens to not wait for the Angular to synchronize (ignoreSynchronization), then it waits for angular to finish everything else it's doing, then it adds a 500 millisecond wait to give protractor a chance to find addButton.click(). When the wait finishes, it forces protractor to move onto the next line of code which contains your expect, before this, it was stopping at the addButton.click() line and waiting for the sync (which wasn't happening), before it moved on.

(我认为...)

这篇关于“window.angular 未定义."当使用量角器进行自动化测试时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆