How do I parameterize the baseUrl property of the protractor config file

前端 未结 3 979
感动是毒
感动是毒 2021-01-03 23:51

I need to run my protractor tests in different contexts with different baseUrls in the config files. I don\'t want to use separate config files for each situati

相关标签:
3条回答
  • 2021-01-04 00:20

    The other option is to use gruntfile.js and have it call the protractor config file.

    //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']);
    };
    

    the Protractor config file: 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());
            }
        }
    };
    

    //To run with default url http://localhost:6034

    grunt protractor
    

    //To run with any other url

    grunt protractor --baseUrl:"http://dev.abc.com/"
    
    0 讨论(0)
  • 2021-01-04 00:32

    I know, old one. but if anyone is still looking for a way to define a url based on capability (I had to do this because Ionic 5 will run in browser on port 8100, but in the app - unchangable - without port declaration on port 80, I use Appium)

    add a baseUrl parameter inside your capability declaration.

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

    and then configure your onPrepare method as follows.

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

    OnPrepare runs for each capability you define in your multiCapabilities array. the getProcessedConfig returns the config as you defined it, with the addition of the current capability. Since that method returns a promise, I use async/await for readability.

    This way, you can have multiple capabilities running, with each different a different host.

    0 讨论(0)
  • 2021-01-04 00:37

    Seems like this is already possible, but the documentation is spotty in this area. Looking at the code, however, protractor does support a number of seemingly undocumented command line arguments.

    So, running something like this will work:

    protractor --baseUrl='http://some.server.com' my.conf.js
    
    0 讨论(0)
提交回复
热议问题