0

Here is simple protractor conf file,

exports.config = {
    // The address of a running selenium server.
    'seleniumAddress': 'http://localhost:4444/wd/hub',
    // Capabilities to be passed to the webdriver instance.
    'capabilities': {
        'browserName': 'chrome'
    },

    // Options to be passed to Jasmine-node.
    'jasmineNodeOpts': {
        'showColors': true,
        'defaultTimeoutInterval': 30000
    }
};

here selenium address is harcoded. But I want to programatically pass different address . Do something like

String URL_TEMPLATE = "https://blabla.com/GoLivePage/ExternalAPIs/" + "getSeleniumGrid.jsp?locale=%s&browser=%s&fabric=%s&teamName=%s"

String URL = String.format(URL_TEMPLATE, "US", "Firefox" , "corp", "<Your Team Name>");

Document doc = Jsoup.connect(URL).timeout(0).get();

String machineName = doc.body().text();

DesiredCapabilities capabilities=new DesiredCapabilities(DesiredCapabilities.firefox());

com.openqa.selenium.Proxy tmpProxy = new Proxy();

tmpProxy.setProxyType(org.openqa.selenium.Proxy.ProxyType.DIRECT);

capabilities.setCapability(CapabilityType.PROXY, tmpProxy);

WebDriver session = null;

try {
 session = new RemoteWebDriver(new URL("http://"+machineName+"/wd/hub"), capabilities);

And then I want to reuse this session id something like this How to connect and re-use an already opened browser window in Protractor

In above stackoverflow answer the session id is hardcoded but I want to programmatically add it. Basically if need someone to tell me how to programmatically do stuff in protractor config file. I am new to all UI technologies and also to java script.

1 Answers1

1

Protractor conf file supply only one interface: getMultiCapabilities to enable us to specify seleniumAddress dynamically. More detail at here

We can specify a seleniumAddress outside any capabilities block, such address is a global value for all capabilites. In additionally, we can specify seleniumAddress inside capabilites block which will overwrite the global one.

We use getMultiCapabilities return a promise which eventual value is a capabilities array. And we specify seleniumAddress inside capabilities.

exports.config = {

    seleniumAdress: '.......' 
    // this address is the global value for all capabilites
    // we can specify it inside capabilites to overwrite the global
    // value, so you can comment the global value.

    specs: [],

    getMultiCapabilities: function() {
        const request = require('request-promise');
        const util = require('util');

        const url_tmplate = "https://blabla.com/GoLivePage/ExternalAPIs/getSeleniumGrid.jsp?" + 
            "locale=%s&browser=%s&fabric=%s&teamName=%s"

        let url = util.format(utl_template, "US", "Firefox" , "corp", "<Your Team Name>");

        return request.get(url).then(function(body){
            // adjustment below line to extract machineName from response
            let machineName = body;

            return [
                {                 
                    browserName: 'chrome',
                    seleniumAddress: "http://"+machineName+"/wd/hub"
                }
            ];
        })
        catch(function(err){
            console.log('get selenium server name fail: ' + err);
        });
    },
    // specify getMultiCapabilities in conf.js, 
    // capabilities and multiCapabilities in conf.js will be ignored.
    ...

};
yong
  • 13,357
  • 1
  • 16
  • 27