我随机得到错误:



运行

$ ng e2e --webdriverUpdate=false --devServerTarget=

在我的spec.ts文件中,我进行了以下2个测试,第一个始终有效,第二个随机失败并出现上述错误。
  beforeEach(async () => {
    myPage = new MyPage();
    browser.get('my-page');
  });

  it('should work', async () => {
    console.log('should work');
    expect(true).toBeTruthy();
  });

  it('should display the title', async () => {
    const title = await $('my-title-selector').getText();
    expect(title).toEqual('My-Title');
  });

这是MyPage PageObject:
import { $, $$ } from 'protractor';

export class MyPage {
  title = $('my-title-selector');
}

这是我的protractor.conf.js
  // Protractor configuration file, see link for more information
// https://github.com/angular/protractor/blob/master/lib/config.ts

const { SpecReporter } = require('jasmine-spec-reporter');

exports.config = {
  allScriptsTimeout: 11000,
  specs: [
    './src/**/*.e2e-spec.ts'
  ],
  capabilities: {
    'browserName': 'chrome'
  },
  SELENIUM_PROMISE_MANAGER: false,
  directConnect: true,
  baseUrl: 'http://localhost:4200/',
  framework: 'jasmine',
  jasmineNodeOpts: {
    showColors: true,
    defaultTimeoutInterval: 30000,
    print: function () { }
  },
  onPrepare() {
    require('ts-node').register({
      project: require('path').join(__dirname, './tsconfig.e2e.json')
    });
    jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } }));
  },
};

你有什么想法吗?

最佳答案

如果您使用的是async/await(您正在使用!),则需要等待所有 promise 。所以我的猜测是,您的beforeEach promise 无法加载页面,并且您正在寻找可能未由Protractor正确引导的Web元素。

beforeEach(async () => {
  myPage = new MyPage();
  await browser.get('my-page');   // browser.get returns a webdriver.promise.Promise
});

it('should work', async () => {
  console.log('should work');
  expect(true).toBeTruthy();
});

it('should display the title', async () => {
  const title = await $('my-title-selector').getText();  // <-- this is right, getText returns a webdriver.promise.Promise<string>
  expect(title).toEqual('My-Title');
});

如果您使用的是Protractor 5.4,则它仍在使用selenium-webdriver控制流/Promise库,而不是 native Promises。因此,webdriver.promise.Promise来自selenium-webdriver类型,promise namespace ,Promise对象。在 Protractor 6中(超出beta时),它将切换为本地Promises。

希望能有所帮助。

关于 Angular 7, Protractor ,随机错误 "both angularJS testability and angular testability are undefined",我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54233440/

10-16 11:48