简体   繁体   English

Angular CLI使用配置和E2E测试

[英]Angular CLI use configuration with E2E tests

I'm going through updating a Angular CLI project to v6 我正在将Angular CLI项目更新到v6

The problem I'm having is that before v6 I could use the command ng e2e -e=e2e and the tests would run properly with the given environment. 我遇到的问题是在v6之前我可以使用命令ng e2e -e=e2e ,测试可以在给定的环境下正常运行。 In v6 environments are changed to configuration but ng e2e -c=e2e doesn't work. 在v6中,环境更改为配置,但ng e2e -c=e2e不起作用。

The error I get is: 我得到的错误是:

Configuration 'e2e' could not be found in project 'admin-e2e'.
Error: Configuration 'e2e' could not be found in project 'admin-e2e'.
at Architect.getBuilderConfiguration (c:\_inmoment\admin\client\node_modules\@angular-devkit\architect\src\architect.js:102:23)
at MergeMapSubscriber._loadWorkspaceAndArchitect.pipe.operators_1.concatMap [as project] (c:\_inmoment\admin\client\node_modules\@angular\cli\models\architect-command.js:64:55)
at MergeMapSubscriber._tryNext (c:\_inmoment\admin\client\node_modules\rxjs\internal\operators\mergeMap.js:122:27)
at MergeMapSubscriber._next (c:\_inmoment\admin\client\node_modules\rxjs\internal\operators\mergeMap.js:112:18)
at MergeMapSubscriber.Subscriber.next (c:\_inmoment\admin\client\node_modules\rxjs\internal\Subscriber.js:103:18)
at TapSubscriber._next (c:\_inmoment\admin\client\node_modules\rxjs\internal\operators\tap.js:109:26)
at TapSubscriber.Subscriber.next (c:\_inmoment\admin\client\node_modules\rxjs\internal\Subscriber.js:103:18)
at MergeMapSubscriber.notifyNext (c:\_inmoment\admin\client\node_modules\rxjs\internal\operators\mergeMap.js:141:26)
at InnerSubscriber._next (c:\_inmoment\admin\client\node_modules\rxjs\internal\InnerSubscriber.js:30:21)
at InnerSubscriber.Subscriber.next (c:\_inmoment\admin\client\node_modules\rxjs\internal\Subscriber.js:103:18)

I've tried to add a configuration section to the e2e section of the angular.json. 我试图将配置部分添加到angular.json的e2e部分。

I've tried to add another section to the architect section of the project that always uses the e2e config. 我试图在项目的架构师部分添加另一个部分,它总是使用e2e配置。

Does anyone know how to use the --configuration flag with e2e tests? 有谁知道如何在e2e测试中使用--configuration标志?

Just not sure what I'm missing. 只是不确定我错过了什么。

First remove the "devServerTarget": "project:serve" from option section of the e2e and try to place it in particular configuration you need. 首先从e2e的选项部分删除"devServerTarget": "project:serve" ,并尝试将其置于您需要的特定配置中。 In this case if you are running tests in local, use it and for the Jenkins or CI, remove it and place the baseUrl to target a particular url. 在这种情况下,如果您在本地运行测试,请使用它,对于Jenkins或CI,将其删除并放置baseUrl以定位特定网址。

Also after this configuration, you can pass --base-url from command line to dynamically changing the base url from jenkins for e2e-ci . 此外,在此配置之后,您可以从命令行传递--base-url ,以便从jenkins为e2e-ci动态更改基本URL。

ng e2e --configuration=e2e-ci --suite=home --base-url="http://google.com"

Below is the configuration 以下是配置

"project-e2e": {
  "root": "e2e/",
  "sourceRoot": "e2e",
  "projectType": "application",
  "prefix": "",
  "architect": {
    "e2e": {
      "builder": "@angular-devkit/build-angular:protractor",
      "options": {
        "protractorConfig": "./protractor.conf.js"
        // remove "devServerTarget": "project:serve" here. it normally for all
      },
      "configurations": {
        "production": {
          "devServerTarget": "project:serve:production",  // use it here for local test to pull localhost:4200 and run test on it
          "serve": false,
          "webdriverUpdate": false
        },
        "e2e-ci": { // this one is for jenkins/CI so no dev server require but pass the baseUrl. see there is no devServerTarget option here
          "protractorConfig": "./protractor.conf.js",  
          "baseUrl": "https://specificurltotest.org"
        }
      }
    },
    "lint": {
      "builder": "@angular-devkit/build-angular:tslint",
      "options": {
        "tsConfig": "e2e/tsconfig.e2e.json",
        "exclude": [
          "**/node_modules/**"
        ]
      }
    }
  }
}

I figured it out. 我想到了。 This won't work if you need to use different configurations. 如果您需要使用不同的配置,这将不起作用。 But, if there is a single configuration you want to use for your e2e tests this is how you do it. 但是,如果您想要用于e2e测试的单一配置,那么您就是这样做的。

In the main section of the angular.json for your file there are configurations. 在文件的angular.json的主要部分中有配置。 You can reference any of those configurations! 您可以参考任何这些配置! All I had to do was update the devServerTarget section for the e2e tests to reference the proper build configuration. 我所要做的就是更新devServerTarget部分以获取e2e测试,以引用正确的构建配置。

So change this: 所以改变这个:

"e2e": {
  "builder": "@angular-devkit/build-angular:protractor",
  "options": {
    "protractorConfig": "./protractor.conf.js",
    "devServerTarget": "admin:serve"
  }
}

To this: 对此:

"e2e": {
  "builder": "@angular-devkit/build-angular:protractor",
  "options": {
    "protractorConfig": "./protractor.conf.js",
    "devServerTarget": "admin:serve:e2e"
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM