【问题标题】:Protractor "- Failed: Cannot read property 'ver' of null"量角器“- 失败:无法读取属性 'ver' of null”
【发布时间】:2017-12-19 23:01:31
【问题描述】:

我在尝试启动测试时遇到了一个恼人的量角器问题。

量角器版本:5.1.2

量角器.conf.js

var SpecReporter = require('jasmine-spec-reporter');

exports.config = {
  allScriptsTimeout: 11000,
  specs: [
    './e2e/**/*.e2e-spec.ts'
  ],
  capabilities: {
    'browserName': 'chrome'
  },
  directConnect: true,
  baseUrl: 'http://localhost:8080/src/',
  framework: 'jasmine',
  jasmineNodeOpts: {
    showColors: true,
    defaultTimeoutInterval: 30000,
    print: function() {}
  },
  useAllAngular2AppRoots: true,
  beforeLaunch: function() {
    require('ts-node').register({
      project: 'e2e'
    });
  },
  onPrepare: function() {
    jasmine.getEnv().addReporter(new SpecReporter());
  },  
  rootElement: '*[ng-app]',
};

端到端测试文件“app.comp.e2e-spec.js”

import { browser, element, by, $ } from 'protractor';

describe('testproject App', function() {

  it('should display message saying app works', (done) => {
    browser.get('/');
    expect($('.my-span').isPresent()).toBe(true);
    return done();
  });

});

每次我启动测试(protractor protractor.conf.js)时都会遇到以下错误消息:

1) testproject App should display message saying app works
- Failed: Cannot read property 'ver' of null

Executed 1 of 1 spec (1 FAILED) in 0.984 sec.
[11:27:52] I/launcher - 0 instance(s) of WebDriver still running
[11:27:52] I/launcher - chrome #01 failed 1 test(s)
[11:27:52] I/launcher - overall: 1 failed spec(s)
[11:27:52] E/launcher - Process exited with error code 1

我当然有一个在 localhost:8080/index 上运行的 Angular 应用程序,并且在使用浏览器手动访问时它工作得很好。有人有解决这个问题的想法吗?提前致谢

【问题讨论】:

  • 感谢所有信息,但这真的是您拥有的所有日志吗,日志中没有对文件的引用吗?
  • @wswebcreation 请在此处找到完整的命令输出:pastebin.com/3tLdGGvw
  • 我记得jasmine-spec-reporter 中有一个选项可以在日志中启用堆栈跟踪,您可以打开或禁用jasmine-spec-reporter 并再次将日志放在这里吗?
  • 错误“无法读取 null 的属性 x”表示打字稿错误。您正在引用 ts-node 所以大概您正在使用 typescript - 您可以发布您的 tsconfig.json 吗?
  • @wswebcreation 启用堆栈跟踪的输出:pastebin.com/1G9tYXtU

标签: angular protractor e2e-testing


【解决方案1】:

在我的情况下,当在应用配置/加载期间触发意外警报消息时会引发此错误。这是由于无法与外部系统通信而发生的,这仅在 Travis CI 中发生。由于这是唯一的错误消息,因此需要很长时间才能确定真正的原因。 (尤里卡时刻是当我在正常和错误处理程序中使用 .then() 和单独的故意失败的 expect() 测试来跟踪 browser.get() 调用时;这用更有用和更有用的信息取代了“ver”失败有关意外警报的失败消息。)

解决方案是移除对外部系统的依赖(一开始就不应该在测试配置中)。

【讨论】:

    【解决方案2】:
    expect($('.my-span').isPresent()).toBe(true);
    

    isPresent() 返回的 Promise 不是 true 或 false。

    要检查元素是否存在,请使用 promise 返回的值:

    element(anyFinder).isPresent().then(function(isPresent) {
      if ( isPresent) {
       // The element is present
      } else {
      // The element is not present
    }
    });
    

    【讨论】:

    • 部分正确。使用 Jasmine 你仍然可以使用expect($('.my-span').isPresent()).toBe(true);,它会在 Jasmine 中解决它
    • 感谢 Dharam,但正如 wswebcreation 所说,仍然可以使用其他方式,无论如何使用您的代码我有相同的错误消息
    • 为您的代码添加一些等待,并使用 .isDisplayed()).toEqual(false); 进行检查
    • 我已经尝试添加诸如 browser.wait 之类的东西,但结果是一样的。此外,正如我所见,对于某些人来说,它可以正常工作而无需任何额外的提升,所以我想了解出了什么问题,使用量角器测试 Angular2 似乎(目前)还不一致。
    • @Bil5,使用 Angular 2 进行测试并不一致。您能提供更多信息吗?
    猜你喜欢
    • 2020-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-15
    • 1970-01-01
    • 2022-07-24
    • 2020-05-27
    • 2020-08-13
    相关资源
    最近更新 更多