在Protractor conf文件中使selenium地址动态化

问题描述 投票:0回答:1

这是简单的量角器配置文件,

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
    }
};

这里的selenium地址是硬编码的。但我想以编程方式传递不同的地址。做点什么

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);

然后我想重用这个会话ID像这样的How to connect and re-use an already opened browser window in Protractor

在上面的stackoverflow回答中,会话ID是硬编码的,但我想以编程方式添加它。基本上如果需要有人告诉我如何以编程方式在protractor配置文件中执行操作。我是所有UI技术和java脚本的新手。

selenium-webdriver protractor
1个回答
1
投票

Protractor conf文件只提供一个接口:getMultiCapabilities,使我们能够动态指定seleniumAddress。更多细节在here

我们可以在任何seleniumAddress块之外指定一个capabilities,这样的地址是所有capabilites的全局值。另外,我们可以在seleniumAddress块中指定capabilites,它将覆盖全局块。

我们使用getMultiCapabilities返回一个promise,其最终值是capabilities数组。我们在seleniumAddress中指定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.
    ...

};
© www.soinside.com 2019 - 2024. All rights reserved.