【问题标题】:How can I know the base url used in a running test in protractor?我如何知道量角器运行测试中使用的基本 url?
【发布时间】:2017-11-24 00:11:42
【问题描述】:

我正在尝试在量角器中进行导航测试,并且没有看到与配置中的 baseUrl 和测试中使用的 url 有任何一致性。

量角器.conf.js

exports.config = {
  baseUrl: 'http://localhost:4200/'
}

navbar.e2e-spec.ts

import { NavbarPage } from './navbar.po';
import * as protractor from './../protractor.conf.js';
describe('navbar', () => {
  let navbar: NavbarPage;
  const baseUrl = protractor.config.baseUrl; 

  beforeEach(() => {
    navbar = new NavbarPage();
    browser.get('/');
  });
  it(`should see showcase nav item, be able to (click) it,
  and expect to be navigated to showcase page`, () => {
    const anchorShowcase = navbar.anchorShowcase;
    expect(anchorShowcase.isDisplayed()).toBe(true);
    anchorShowcase.click();
    browser.waitForAngular();
    expect(browser.getCurrentUrl()).toBe(baseUrl + '/showcase');
  });
});

虽然当我运行 e2e 测试时它使用不同的端口:

** NG Live Development Server is listening on localhost:49154, open your browser on http://localhost:49154/ **

为什么测试 url 设置为端口 49154。如果你启动一个新的 angular-cli 项目,这显然是默认的:https://github.com/angular/angular-cli

我如何才能控制 baseUrl / 或者 http://localhost:49154/ 是否可以安全地用于我的所有 angular cli 项目?

【问题讨论】:

    标签: protractor angular-cli e2e-testing


    【解决方案1】:

    默认情况下,当您执行ng e2e 时,该命令将--serve 值作为true。这意味着它将在特定 URL 中构建和服务。不是你传入的baseUrl protractor.conf.js

    这就是为什么,在测试您的应用程序时,您会得到一个随机 URL,例如 http://localhost:49154/

    现在,由于您不想在测试期间构建并想要测试现有构建(URL),例如http://localhost:4200/,您需要在命令行中传递--no-serve,它将从protractor.conf.js 中选择baseUrl

    您也可以在命令行中传递baseUrl,如下所示。请注意,这不是baseUrl,而是--base-href=

    ng e2e --no-serve --base-href=https://someurl.com:8080
    

    【讨论】:

      【解决方案2】:

      当运行 Angular CLI 的 ng e2e 命令时,它在 wiki 中声明默认端口将是随机的,如下所示:

      https://github.com/angular/angular-cli/wiki/e2e

      serve 子菜单下。

      e2e 命令可以接收与serve 相同的所有参数,因此要保持端口相同,只需将--port my-port-number 传递给ng e2e 命令。

      就该端口是否可以安全使用而言,我不会使用它,毕竟它只是一个随机端口。除非您有更改它的用例,否则我会坚持使用默认值。端口主要与开发服务器相关,与生产代码运行的地方无关。

      【讨论】:

        【解决方案3】:

        Aniruddha Das 的解决方案不再起作用,因为 Angular CLI 6.x 版本中没有此选项,您可以尝试关注 -

        ng e2e --dev-server-target=
        

        请看以下reference

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多