【问题标题】:How do I parameterize the baseUrl property of the protractor config file如何参数化量角器配置文件的 baseUrl 属性
【发布时间】:2014-11-15 21:37:52
【问题描述】:

我需要在配置文件中使用不同的baseUrls 在不同的上下文中运行我的量角器测试。我不想为每种情况使用单独的配置文件,因为这更难维护。相反,我想将基本 url 作为命令行参数传递。到目前为止,这是我尝试过的:

量角器.conf.js:

exports.config = {
  onPrepare : {
    ...
    exports.config.baseUrl = browser.params.baseUrl;
    ...
  }
}

并调用量角器:

protractor protractor.conf.js --params.baseUrl 'http://some.server.com'

这不起作用,因为在调用 onPrepare 之前似乎已经配置了 browser 实例。

同样,我也试过这个:

exports.config = {
  baseUrl : browser.params.baseUrl
}

但这也不起作用,因为在生成配置时似乎浏览器实例不可用。

看起来我可以使用标准节点process.argv 来访问所有命令行参数,但这似乎与量角器的精神背道而驰。

什么是我做我需要做的最好的方式?

【问题讨论】:

    标签: protractor


    【解决方案1】:

    似乎这已经成为可能,但该领域的文档参差不齐。 Looking at the code,然而,量角器确实支持许多看似未记录的命令行参数。

    所以,运行这样的东西就可以了:

    protractor --baseUrl='http://some.server.com' my.conf.js
    

    【讨论】:

    • 感谢分享。如果您觉得相关,您应该提出拉取请求,将它们添加到文档中。
    • 是的,我会考虑的。
    • 对于其他任何人,如果您需要按照 OP 的要求在 onPrepare 块中访问它,可以通过以下代码 github.com/angular/protractor/blob/1.7.0/docs/… 进行访问并使用 config.baseUrl。
    • 谢谢@AndrewEisenberg!
    • 量角器 ~4.0.9 的当前语法似乎是:protractor --baseUrl http://some.server.com/ my.conf.js。使用 --baseUrl='etc' 时出现错误。
    【解决方案2】:

    另一个选项是使用 gruntfile.js 并让它调用量角器配置文件。

    //gruntfile.js

    module.exports = function (grunt) {
        grunt.registerTask("default", "", function () {
        });
    
        //Configure main project settings
        grunt.initConfig({
            //Basic settings and infor about our plugins
            pkg: grunt.file.readJSON('package.json'),
    
            //Name of plugin
            cssmin: {
            },
    
            protractor: {
                options: {
                    configFile: "conf.js", // Default config file
                    keepAlive: true, // If false, the grunt process stops when the test fails.
                    noColor: false, // If true, protractor will not use colors in its output.
                    args: {
                        baseUrl: grunt.option('baseUrl') || 'http://localhost:6034/'
                    }
                },
                your_target: {   // Grunt requires at least one target to run so you can simply put 'all: {}' here too.
                    options: {
                        configFile: "conf.js", // Target-specific config file
                        args: {
                            baseUrl: grunt.option('baseUrl') || 'http://localhost:63634/'
                        }
                    }
                },
            },
    
            //uglify
            uglify: {
            }
        });
    
        //Load the plugin
        grunt.loadNpmTasks('grunt-contrib-cssmin');
        grunt.loadNpmTasks('grunt-contrib-uglify');
        grunt.loadNpmTasks('grunt-protractor-runner');
    
        //Do the Task
        grunt.registerTask('default', ['cssmin']);
    };
    

    量角器配置文件:conf.js

    exports.config = {
        directConnect: true,
    
        // Capabilities to be passed to the webdriver instance.
        capabilities: {
            'browserName': 'chrome',
            'chromeOptions': {
                args: ['--no-sandbox']
            }
        },
    
        chromeOnly: true,
    
        // Framework to use. Jasmine is recommended.
        framework: 'jasmine',
    
        // Spec patterns are relative to the current working directory when
        // protractor is called.
        specs: ['specs/*/*_spec.js'],
    
        suites : {
          abcIdentity : 'specs/abcIdentity/*_spec.js'  //picks up all the _spec.js files
        },
    
        params: {
            UserName: 'abc@test.com',
            Password: '123'
        },
    
        // Options to be passed to Jasmine.
        jasmineNodeOpts: {
            defaultTimeoutInterval: 30000,
            includeStackTrace: true
        },
    
        onPrepare: function () {
            browser.driver.manage().window().maximize();
            if (process.env.TEAMCITY_VERSION) {
                var jasmineReporters = require('jasmine-reporters');
                jasmine.getEnv().addReporter(new jasmineReporters.TeamCityReporter());
            }
        }
    };
    

    //使用默认url运行http://localhost:6034

    grunt protractor
    

    //使用任何其他 url 运行

    grunt protractor --baseUrl:"http://dev.abc.com/"
    

    【讨论】:

      【解决方案3】:

      我知道,老家伙。但是如果有人仍在寻找一种基于功能定义 url 的方法(我必须这样做,因为 Ionic 5 将在端口 8100 上的浏览​​器中运行,但在应用程序中 - 不可更改 - 没有端口 80 上的端口声明,我使用 Appium )

      在您的功能声明中添加一个 baseUrl 参数。

      {
          browserName: 'chrome',
          baseUrl: 'http://localhost:8100' //not required but as example
      }
      
      {
          ...
          app: 'path to app.apk',
          baseUrl: 'http://localhost'
          ... 
      }
      

      然后配置你的 onPrepare 方法如下。

       async onPrepare() {
          const config = await browser.getProcessedConfig();
      
          if(config.capabilities.hasOwnProperty('baseUrl')) {
              browser.baseUrl = config.capabilities.baseUrl;
          }
      }
      

      OnPrepare 针对您在 multiCapabilities 数组中定义的每个功能运行。 getProcessedConfig 返回您定义的配置,并添加当前功能。由于该方法返回一个承诺,因此我使用 async/await 以提高可读性。

      这样,您可以运行多个功能,每个不同的主机都不同。

      【讨论】:

        【解决方案4】:

        基本 url 应声明为 baseUrl: "", in config.ts

        我正在使用黄瓜钩子,下面的代码被添加到钩子文件中,以根据环境传递所需的 url

         if(browser.params.baseUrl==="QA"){
             console.log("Hello QA")
             await browser.get("https://www.google.com");   
         } else {
             console.log("Hi Dev")
             await browser.get("https://www.gmail.com");
         }
        

        使用量角器命令运行测试

        protractor --params.baseUrl 'QA' typeScript/config/config.js --cucumberOpts.tags="@CucumberScenario"
        
           
        

        【讨论】:

          猜你喜欢
          • 2015-02-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-06-28
          • 1970-01-01
          • 2015-07-14
          相关资源
          最近更新 更多