如何使用配置指定ng e2e的端口和主机

13 protractor angular-cli angular

如何ng e2e使用配置文件为命令设置端口和主机?

我知道可以在命令行中使用以下语句

ng e2e --port <port number> --host <host>
Run Code Online (Sandbox Code Playgroud)

这很好用,但在配置文件中设置端口和地址会更方便.我已经查看了内部.angular-cli.json,发现有一个文件被调用protractor.config.js,我无法弄清楚在该文件中使用的baseUrl设置,有一个设置,但是更改它对使用的端口没有任何影响ng e2e命令.它似乎使用随机端口.

除了baseUrl我也试过设置port,seleniumPort但他们没有任何区别.

输入ng e2e命令后,控制台中将显示以下输出

** NG Live Development Server is running on http://localhost:49155 **
Run Code Online (Sandbox Code Playgroud)

protractor.config.js我已经baseUrl设定为http://localhost:50000.我观察到测试正在执行,我可以看到浏览器使用的地址http://localhost:49155不是我想要的地址.

ng --version 返回以下内容

@ angular/cli:1.0.0
节点:7.7.1
os:win32 x64
@ angular/common:4.0.3
@ angular/compiler:4.0.3
@ angular/core:4.0.3
@ angular/forms:4.0.3
@ angular/http:4.0.3
@ angular/platform-b​​rowser:4.0.3
@ angular/platform-b​​rowser-dynamic:4.0.3
@ angular/router:4.0.3
@ angular/cli:1.0.0
@ angular/compiler- cli:4.0.3

Ahm*_*lam 11

我还没有看到运行时dev服务器运行的端口的配置 ng e2e

你最好的选择可能是一个NPM脚本:https://docs.npmjs.com/misc/scripts

  1. 添加"e2e":"ng e2e --port <port number> --host <host>"package.jsonscripts
  2. 使用通过运行脚本npm run e2e,这将运行脚本e2epackage.json,在这种情况下,你ng e2e --port <port number> --host <host>

它不是一个配置文件,但它就像一个.


Jen*_*son 7

angular.json通过修改项目来设置主机和端口webapp-e2e,例如:

"e2e": {
  "builder": "@angular-devkit/build-angular:protractor",
  "options": {
    "protractorConfig": "./protractor.conf.js",
    "devServerTarget": "webapp:serve",
    "port": 4201
  }
Run Code Online (Sandbox Code Playgroud)

一旦angular-cli调用了webapp:serve-command,它将覆盖正在使用的端口。忘记protractor.conf.js文件-直到稍后再使用。

因此,您可以独立或并行运行ng serve --watch(用于服务和单元测试)和ng e2e(用于e2e测试)。

br,詹斯

  • 当我添加端口属性时,在运行ng e2e时收到以下错误消息:数据路径“ ['e2e']”不应具有其他属性(端口)。 (4认同)

小智 5

baseUrl 属性在 protractor.config.js 中以某种方式不起作用,但您可以在同一个 config.js 中的 onPrepare 方法中以编程方式设置该属性

{
    onPrepare() {
        browser.baseUrl = 'http://localhost:4200/';
    },
}
Run Code Online (Sandbox Code Playgroud)

我一直在 Angular 1.x 中使用这个解决方案,仍然适用于 Angular 2/4


小智 5

我发现,当我们调用命令时ng e2e,默认情况下,开发服务器将在http:// localhost:49152 /上运行

为了能够使用配置为ng e2e指定端口和主机,我们将使用文件baseUrl上的属性protractor.conf.js。我们将需要:

  • 分别提供服务
  • 然后我们在致电时将serve设置为falseng e2e,因此ProtratcorbaseUrl直接与

例如:

  • 我们首先需要在protractor.conf.js上输入:
    baseUrl:'http://localhost:8088'
  • 运行命令:
    ng serve --port 8088
  • 在另一个外壳上运行测试:
    ng e2e --serve false

还有另一种我们不使用的策略baseUrl。我们只需要:
-在shell 1中运行:ng serve --port 4200
-在shell 2中运行:ng e2e --serve false --base-href=http://localhost:4200