量角器总是在第一次测试中失败

Protractor always fails on first test

首先是我使用的所有版本:

量角器v5.1.2 chromedriver v2.33 节点 v6.11.4 npm 3.10.10 selenium-webdriver v3.0.1

我是量角器的新手,我只是想 运行 在量角器已经给出的测试中使用它。 看起来像这样

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

这里是 conf.js

exports.config = {
  directConnect: true,

  // Capabilities to be passed to the webdriver instance.
  capabilities: {
    'browserName': 'chrome'
  },

  // Framework to use. Jasmine is recommended.
  framework: 'jasmine',

  // Spec patterns are relative to the current working directory when
  // protractor is called.
  specs: ['example_spec.js'],

  // Options to be passed to Jasmine.
  jasmineNodeOpts: {
    defaultTimeoutInterval: 30000
  }
};

当我尝试 运行 时,出现以下错误:

Failures:
1) angularjs homepage should greet the named user
  Message:
    Failed: Timed out waiting for asynchronous Angular tasks to finish after 11 seconds. This may be because the current page is not an Angular application. Please see the FAQ for more details: https://github.com/angular/protractor/blob/master/docs/timeouts.md#waiting-for-angular
    While waiting for element with locator - Locator: by.model("yourName")
  Stack:
    ScriptTimeoutError: asynchronous script timeout: result was not received in 11 seconds
      (Session info: chrome=60.0.3112.113)
      (Driver info: chromedriver=2.33.506092 (733a02544d189eeb751fe0d7ddca79a0ee28cce4),platform=Linux 3.16.0-4-amd64 x86_64)
        at WebDriverError (/usr/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/error.js:27:5)
        at ScriptTimeoutError (/usr/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/error.js:203:5)
        at Object.checkLegacyResponse (/usr/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/error.js:505:15)
        at parseHttpResponse (/usr/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/http.js:509:13)
        at doSend.then.response (/usr/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/http.js:440:13)
        at process._tickCallback (internal/process/next_tick.js:109:7)
    From: Task: Protractor.waitForAngular() - Locator: by.model("yourName")
        at Driver.schedule (/usr/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/webdriver.js:816:17)
        at ProtractorBrowser.executeAsyncScript_ (/usr/lib/node_modules/protractor/lib/browser.ts:609:24)
        at angularAppRoot.then (/usr/lib/node_modules/protractor/lib/browser.ts:643:23)
        at ManagedPromise.invokeCallback_ (/usr/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:1366:14)
        at TaskQueue.execute_ (/usr/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2970:14)
        at TaskQueue.executeNext_ (/usr/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2953:27)
        at asyncRun (/usr/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2813:27)
        at /usr/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:676:7
        at process._tickCallback (internal/process/next_tick.js:109:7)Error
        at ElementArrayFinder.applyAction_ (/usr/lib/node_modules/protractor/lib/element.ts:482:23)
        at ElementArrayFinder.(anonymous function) [as sendKeys] (/usr/lib/node_modules/protractor/lib/element.ts:96:21)
        at ElementFinder.(anonymous function) [as sendKeys] (/usr/lib/node_modules/protractor/lib/element.ts:873:14)
        at UserContext.<anonymous> (/home/kunstmann/Downloads/node-v6.11.4-linux-x64/lib/node_modules/npm/node_modules/protractor/example/example_spec.js:5:35)
        at /usr/lib/node_modules/protractor/node_modules/jasminewd2/index.js:112:25
        at new ManagedPromise (/usr/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:1067:7)
        at ControlFlow.promise (/usr/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2396:12)
        at schedulerExecute (/usr/lib/node_modules/protractor/node_modules/jasminewd2/index.js:95:18)
        at TaskQueue.execute_ (/usr/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2970:14)
        at TaskQueue.executeNext_ (/usr/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2953:27)
    From: Task: Run it("should greet the named user") in control flow
        at UserContext.<anonymous> (/usr/lib/node_modules/protractor/node_modules/jasminewd2/index.js:94:19)
    From asynchronous test: 
    Error
        at Suite.<anonymous> (/home/kunstmann/Downloads/node-v6.11.4-linux-x64/lib/node_modules/npm/node_modules/protractor/example/example_spec.js:2:3)
        at Object.<anonymous> (/home/kunstmann/Downloads/node-v6.11.4-linux-x64/lib/node_modules/npm/node_modules/protractor/example/example_spec.js:1:63)
        at Module._compile (module.js:570:32)
        at Object.Module._extensions..js (module.js:579:10)
        at Module.load (module.js:487:32)
        at tryModuleLoad (module.js:446:12)

3 specs, 1 failure
Finished in 17.5 seconds

[11:34:30] I/launcher - 0 instance(s) of WebDriver still running
[11:34:30] I/launcher - chrome #01 failed 1 test(s)
[11:34:30] I/launcher - overall: 1 failed spec(s)
[11:34:30] E/launcher - Process exited with error code 1

如我所说,这些都不是我写的。代码由量角器本身在名为 example 的文件夹中提供。 但是,现在我尝试切换测试用例,因为我不明白为什么第一个会失败。它看起来非常适合我。 现在奇怪的是。当我将一个不同的测试用例放在顶部时,新的测试用例 - 现在位于顶部 - 将失败,但之前位于顶部的那个将不再有任何问题。我可以对每个测试用例都这样做,第一个用例总是会失败,但是一旦我将它从顶部移开,它就会突然起作用。

我已经尝试按照错误消息中的 link 进行操作,但它对我没有任何作用。我也更新了一百次,但没有任何改变。

有人知道这是什么吗?这真是奇怪...

你需要告诉 Protractor 不要等待 Angular $http 和 $timeout,像这样:

旧的弃用方式:

browser.ignoreSynchronization = true;

新的正确方法:

browser.waitForAngularEnabled(false);

这有帮助吗?