首页 文章

在Bitbucket管道中运行Angular 2量角器

提问于
浏览
4

我是Bitbucket Pipelines的新手,所以我正在努力尝试在构建上运行我的Angular 2应用程序的Protractor E2E测试 . 我的bitbucket-pipelines.yml看起来像这样

image: adrianmarinica/bitbucket-pipelines-protractor

pipelines:
  default:
    - step:
        caches:
          - node
        script: # Modify the commands below to build your repository.
          - npm install
          - protractor protractor.conf.js

当安装所有依赖项并且量角器开始运行时,我会收到此错误

enter image description here

如何在我的本地机器上成功运行测试?

3 回答

  • 0

    为了对bitbucket管道进行e2e测试,我要对bitbucket-pipelines.yml,package.json和protractor.conf.js进行一些更改 .

    首先, bitbucket-pipelines.yml 看起来像这样 . 我们使用了adrianmarinica提供的docker镜像而不是默认的节点镜像 .

    # You can specify a custom docker image from Docker Hub as your build environment.
    image: adrianmarinica/bitbucket-pipelines-protractor
    
    pipelines:
        branches:
            master:
                - step:
                    caches:
                        - node
                    script:
                        - npm install
                        - npm start
                        - protractor protractor.conf.js
    

    然后, package.json 看起来像这样

    "scripts": {
        ...
        "start": "ng serve &"
        ...
      }
    

    这里的关键变化是start命令中的“&” . 这将在后台运行ng,允许量角器命令被触发 .

    最后,对 protractor.conf.js 进行了一些调整

    const { SpecReporter } = require('jasmine-spec-reporter');
    
    exports.config = {
      allScriptsTimeout: 1800000,
      specs: [
        './e2e/**/*.e2e-spec.ts'
      ],
      getPageTimeout: 120000,
      capabilities: {
        'browserName': 'chrome',
        'chromeOptions': {
          'args': [
            '--no-sandbox',
            '--disable-gpu'
          ]
        }
      },
      useAllAngular2AppRoots: true,
      directConnect: true,
      baseUrl: 'http://localhost:4200/',
      framework: 'jasmine',
      jasmineNodeOpts: {
        showColors: true,
        defaultTimeoutInterval: 120000,
        print: function () { }
      },
      beforeLaunch: function () {
        require('ts-node').register({
          project: 'e2e/tsconfig.e2e.json'
        });
      },
      onPrepare() {
        jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } }));
      }
    };
    

    如果您的测试在本地成功运行,它们也应该在管道中,根据此配置,环境应该是相同的 .

  • -1

    这只是我的例子:(非AngularJS App)

    内部来源:test_specs.js,conf.js,bitbucket.pipeline.yml

    资料来源:( Bitbucket)test_spec.js

    describe("Facebook App test", function(){
    
           beforeEach(function(){
           browser.driver.ignoreSynchronization = true;
    
           });
    
          it("should launch the browser", function(){
    
           browser.driver.get("https://www.facebook.com");    
    
           console.log("Launch the browser");
    
          }
    
    });
    
    ---------------------------
    
    Source: conf.js
    
    exports.config = {
    directConnect: true,
    
    allScriptsTimeout: 20000,
    capabilities: {
    'browserName': 'chrome'
    },
    
    // Framework to use. Jasmine is recommended.
    framework: 'jasmine',
    
    // Spec patterns are relative to the current working directory when
    // protractor is called.
    specs: ['test_spec.js'],
    
    // Options to be passed to Jasmine.
    jasmineNodeOpts: {
    //showColors: true,
    defaultTimeoutInterval: 30000
    
    }
    };
    
    -----------------------------
    
    bitbucket.pipeline.yml ( **Is this correct?** )
    
    pipelines:
        branches:
            master:
                - step:
                    caches:
                        - node
                    script:
                        - npm install
                        - npm start
                        - protractor protractor.conf.js
    
  • 6

    默认情况下,Protractor等待角度变量出现在网页中 . 它等待默认时间为十秒,然后超时 . 如果您的应用程序不是角度应用程序,您可以通过设置将其关闭

    browser.waitForAngularEnabled(false);
    

    describe 块中,在 it 部分之前 .

    PS - 如果上述方法不起作用,请在 describe 块之前的第一个 describe 块中尝试 browser.ignoreSynchronization = true; ,这将使Protractor不等待Angular的承诺 .

相关问题