如何参数化量角器配置文件的baseUrl属性

时间:2014-11-15 21:37:53

标签: protractor

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

protractor.conf.js:

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

并调用量角器:

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

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

同样,我试过这个:

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

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

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

对我来说,做我需要做的最好的方法是什么?

3 个答案:

答案 0 :(得分:25)

似乎已经可以这样了,但是这方面的文档很多。但是,Looking at the code,量角器支持一些看似无证的命令行参数。

所以,运行这样的东西会起作用:

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

答案 1 :(得分:1)

另一种选择是使用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']);
};

Protractor配置文件: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());
        }
    }
};

//使用默认网址http://localhost:6034

运行
grunt protractor

//与任何其他网址一起运行

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

答案 2 :(得分:1)

我知道,老一个。但如果有人仍在寻找基于功能定义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将返回您定义的配置,并添加当前功能。由于该方法返回了Promise,因此我使用async / await来提高可读性。

这样,您可以运行多种功能,并且每个功能都不同。

相关问题