【问题标题】:How to set baseurl in protractor tests dynamically based on different spec files?如何根据不同的规范文件动态设置量角器测试中的baseurl?
【发布时间】:2021-05-05 22:17:15
【问题描述】:

我正在开发一个服务于 2 个品牌的 Angular 应用程序 (V9)。在编写 e2e 测试时,我遇到了一个问题。每个品牌都有一个单独的网址,例如,brand1:“https://qa1.brandX.com”和brand2:“https://qa1.brandY.com”。

这是 protractor.conf.js 的代码

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

const { SpecReporter } = require('jasmine-spec-reporter');

exports.config = {
  sauceUser: 'username',
  sauceKey: 'key',
  allScriptsTimeout:20000,
  specs: [
    './src/**/*.e2e-spec.ts'
  ],
  multiCapabilities: [
    {
      browserName: 'chrome',
      version: 'latest',
      platform: 'Windows 10',
      name: 'chrome test demo',
      'tunnel-identifier': 'getTunnel'',
      /**
       * If this is set to be true, specs will be sharded by file (i.e. all
       * files to be run by this set of capabilities will run in parallel).
       * Default is false.
       */
      shardTestFiles: true,

      /**
       * Maximum number of browser instances that can run in parallel for this
       * set of capabilities. This is only needed if shardTestFiles is true.
       * Default is 1.
       */
      maxInstances: 1,
    },
    {
      browserName: 'firefox',
      version: 'latest',
      platform: 'Windows 10',
      name: 'demo-protractor-jasmine-ts',
      'tunnel-identifier': 'getTunnel',
      /**
       * If this is set to be true, specs will be sharded by file (i.e. all
       * files to be run by this set of capabilities will run in parallel).
       * Default is false.
       */
      shardTestFiles: true,

      /**
       * Maximum number of browser instances that can run in parallel for this
       * set of capabilities. This is only needed if shardTestFiles is true.
       * Default is 1.
       */
      maxInstances: 1,
    },
    {
      browserName: 'MicrosoftEdge',
      version: 'latest',
      platform: 'Windows 10',
      name: 'Edge test test',
      'tunnel-identifier': 'getTunnel'',
      /**
       * If this is set to be true, specs will be sharded by file (i.e. all
       * files to be run by this set of capabilities will run in parallel).
       * Default is false.
       */
      shardTestFiles: true,

      /**
       * Maximum number of browser instances that can run in parallel for this
       * set of capabilities. This is only needed if shardTestFiles is true.
       * Default is 1.
       */
      maxInstances: 1,
    }
  ],
  directConnect: false,
  framework: 'jasmine',
  useAllAngular2AppRoots: true,
  jasmineNodeOpts: {
    showColors: true,
    defaultTimeoutInterval: 30000,
    print: function() {}
  },
  onPrepare() {
    browser.baseUrl = 'https://qa1.brandX.com';
    require('ts-node').register({
      project: require('path').join(__dirname, './tsconfig.e2e.json')
    });
    jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } }));
  }
};

这仅在我在单个 e2e 文件中有一组测试时才有效,即 app.e2e-spec.ts(对于品牌 X)。如果我想在brandY.e2e-spec.ts 中为brandY 运行另一组测试怎么办。 我应该如何为这组测试设置 baseurl?我试过了,但它总是在 https://qa1.brandX.com

上运行测试

关于如何解决这个问题的任何想法?

【问题讨论】:

    标签: angular protractor e2e-testing saucelabs


    【解决方案1】:

    为什么不直接使用局部变量

    // brandX.e2e-spec.ts
    let baseUrl = 'https://qa1.brandX.com'
    
    describe('X', () => {
    
        it('X', async () => {
            await browser.get(baseUrl)
        });
    })
    
    

    // brandY.e2e-spec.ts
    let baseUrl = 'https://qa1.brandY.com'
    
    describe('Y', () => {
    
        it('Y', async () => {
            await browser.get(baseUrl)
        });
    })
    
    

    【讨论】:

      【解决方案2】:

      在运行测试时,您可以在测试中将基本 URL 作为参数传递。

      .\node_modules\.bin\protractor --params.baseurl = <url here>
      

      你可以使用它来调用它

      await browser.get(browser.params.baseurl);
      

      或者另一种方法是设置一个详细说明您的环境配置的 json 文件。导入那个json文件并使用它

      const json = require('<path of your json file>');   
      
      await browser.get(json.brandproperties[brandName].baseurl);
      

      json 文件示例

      {
       "brandproperties" : {
         "brandX" : {
            "baseurl" : "https://sampleX.com"
         },
         "brandY" : {
            "baseurl" : "https://sampleY.com" 
         }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-21
        • 1970-01-01
        • 2018-12-09
        相关资源
        最近更新 更多