【问题标题】:ng e2e fails some of the test cases which are successful with protractor protractor.conf commandng e2e 失败了一些使用 protractor protractor.conf 命令成功的测试用例
【发布时间】:2020-04-19 02:14:46
【问题描述】:

在进行端到端测试时,我遇到了一个问题,即当我使用protractor protractor.conf 运行所有测试用例时,每个测试用例都会成功执行。另一方面,当使用ng e2e 时,其中一些测试用例会失败(非常尴尬的行为)。任何人都可以帮助我解决这个问题,因为根据我的理解,ng e2e 构建代码然后执行量角器 protractor.conf,而在量角器的情况下,我们已经在本地主机上有一个仅经过测试的服务应用程序。配置文件附上

// Protractor configuration file, see link for more information
// https://github.com/angular/protractor/blob/master/lib/config.ts

const { SpecReporter } = require('jasmine-spec-reporter');
const regressionSuite = require('./e2e/suites/regression.suite')
const smokeSuite = require('./e2e/suites/smoke.suite')

exports.config = {
  allScriptsTimeout: 1200000,
  getPageTimeout: 1200000,
  suites: { 
    registersmoke:smokeSuite.register,
    forgetsmoke: smokeSuite.forgot,
    loginsmoke: smokeSuite.login,
    addMember: smokeSuite.addMember,
    addRoom: smokeSuite.addRoom,
  },
  capabilities: {
    browserName: 'chrome'
  },
  directConnect: true,
  baseUrl: 'http://localhost:4200/',
  framework: 'jasmine',
  rootElement: 'body',
  jasmineNodeOpts: {
    showColors: true,
    defaultTimeoutInterval: 1200000,
    print: function() {}
  },
  onPrepare() {
    require('ts-node').register({
      project: 'e2e/tsconfig.e2e.json'
    });
    browser.waitForAngularEnabled(false);
    browser.driver.manage().window().maximize();
    jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } }));
  }
};

会感谢你的。

【问题讨论】:

    标签: angular automation protractor e2e-testing qa


    【解决方案1】:

    当您运行ng e2e 时,应用程序将被构建,然后测试将在构建的代码上运行。将安装运行端到端测试所需的所有依赖项。这意味着当测试和源代码位于同一个项目文件夹中时,测试将在构建应用程序后创建的dist 目录中运行。我对此不是 100% 确定,但量角器配置中的 baseURL 字段被 localhost url 覆盖。您的测试将在本地构建的应用程序上运行。

    首先我有一个问题:

    测试运行时,您是否检查了浏览器上的测试运行情况?如果你没有,那么你应该用ng e2eprotractor protractor.conf.js检查它。

    我认为您的应用中的一些 API 可能无法正确响应,因为您没有在应用代码中模拟端点。你可以使用ng-apimock 之类的东西来做到这一点。这不是一个角度包,它是一个可以与量角器一起使用的插件。当您使用 protractor 命令运行测试时,实际的服务器可能会到达您的 API 定义明确的位置,但是当您使用 ng e2e 运行它时,API 不会被模拟,因此您的某些测试可能会失败。您可以通过将 VPN 连接到服务器来解决此问题,以便 API 可访问或模拟 API。当您在 CI 上运行测试时,这很有用。但我总是建议不要在测试中的任何地方使用模拟。

    【讨论】:

    • 当我运行ng e2e 时,它指的是所有请求的 dev 服务器,它没有回复这就是某些测试用例失败的原因。有什么方法可以指定目标服务器吗?
    • 是的!你需要使用一个叫做 ng-apimock 的东西。这将启动一个模拟服务器,您可以为 2XX 4XX 5XX 等不同场景的 API 端点定义模拟。您可以使用可用于量角器的 ng-apimock 插件或配置单独提供的 npm 包。现在模拟服务器将在您配置的端口上运行,向您的项目添加一个配置,当它在测试环境中本地运行时将使用该端口。这将模拟您的所有 API。该软件包还有助于前端比后端增长得更快。看看它真的很好!
    • 谢谢,但我被要求不要使用模拟。现在我尝试在ng e2e 之后提供--env=uat 作为ng e2e --env=uat 并且它起作用了。感谢您的帮助
    • 是的,你永远不应该使用模拟!绝不!!我只是告诉你,因为你问:D
    猜你喜欢
    • 1970-01-01
    • 2020-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-07
    • 1970-01-01
    相关资源
    最近更新 更多