簡體   English   中英

“window.angular未定義。”使用量角器進行自動化測試時?

[英]“window.angular is undefined.” when using protractor for automated testing?

在使用conf.js提供的示例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 !!:這不能回答你的問題,但提供了一個解決它的黑客。

量角器要求Angular頁面在運行期望之前完成同步。 因此,為了解決此問題,您可以使用:

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

這告訴瀏覽器,量角器打開不等待Angular同步(ignoreSynchronization),然后它等待angular完成它正在做的其他所有事情,然后它增加500毫秒等待讓量角器有機會找到addButton.click() 當等待完成時,它會強制量角器移動到包含您期望的下一行代碼,在此之前,它停在addButton.click()行並等待同步(這沒有發生),然后它繼續前進。

(我認為...)

我有完全相同的問題(Protractor 3.1.0與Jasmine2)。 在我看來,你的beforeEach()調用中的browser.get()是罪魁禍首。 將其復制到每個測試可能是一種解決方法。

我遇到了同樣的問題,它解決了我: -

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM