how test separate environmental name with protract

2019-03-06 04:17发布

问题:

[

The above pic i am run protractor Conf.js with particular environmental name which is stored in JSON file

how to test particular environmental URL only in the protractor test case?

回答1:

FIRST METHOD - You have to pass the parameters using params variable in command line. Update your conf.js file to include a parameter called baseUrl and other url variables as shown below -

params: {
    baseUrl: "http://default_url" //provide your default url to be used
}

later pass the value in the command prompt. Here's how -

protractor conf.js --params.baseUrl 'http://www.google.com'

Wherever you have code to get the url in your spec's, use the following code -

browser.get(browser.params.baseUrl);

SECOND METHOD - If at all you don't want to pass the url to the params object everytime, then you can store them in your conf.js or even your specs file and call them. Here's an example -

Your conf.js file -

params: {
    baseUrl: ""
},
onPrepare: function(){
    switch(browser.params.baseUrl){
      case 'firsturl':
        browser.get("http://firsturl.com"); //replace firsturl with your actual url
        break;
      case 'secondurl':
        browser.get("http://www.secondurl.com");
        break;
      default:
        browser.get("http://www.defaulturl.com");
 }
}

Now pass the url's that you want to use through command line -

protractor conf.js --params.baseUrl 'firsturl' //to get first url
protractor conf.js //to open default url

THIRD METHOD - If at all you have a problem of running a test suite with many spec's, in that case above second method wouldn't work. You need to use browser.get() in each of your test spec files, in such cases use following method -

Update your conf.js file -

params: {
    baseUrl: "",
    url: ""
},
onPrepare: function(){
    switch(browser.params.baseUrl){
      case 'firsturl':
        browser.params.url = "http://firsturl.com"; //replace firsturl with your actual url
        break;
      case 'secondurl':
        browser.params.url = "http://www.secondurl.com";
        break;
      default:
        browser.params.url = "http://www.defaulturl.com";
 }
}

Your command line commands -

protractor conf.js --params.baseUrl 'firsturl' //to get first url
protractor conf.js //to open default url

Your test spec files need to include the browser.get() command. Here's how -

browser.get(browser.params.url);

Hope it helps.



回答2:

To use only in one place a URL from json:

var jsonFile = require("./path/to/json/relative/to/currentRunFolder");
urlToUse = jsonFile.url; // or however you stored it.

use it in any spec, and you can change the json file every run. I hope i understood correctly.

To pass parameters to protractor, use --params.jsonFile="./path/to/json/relative/to/currentRunFolder"

and again use it as above using browser.params.jsonFile