Selenium网格不适用于最新的selenium版本

时间:2017-12-07 23:11:03

标签: selenium selenium-webdriver selenium-grid selenium-grid2

升级到最新的selenium后,Selenium网格给RegistrationRequest()一个错误。我找不到req.setRole(),req.setConfiguration()等。看起来像RegistrationRequest()类在selenium新版本中有变化,但不知道如何使用该类并修改我现有的代码。知道如何解决这个问题吗?

这是我的代码,

package selenium;

import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.openqa.grid.common.GridRole;
import org.openqa.grid.common.RegistrationRequest;
import org.openqa.grid.internal.utils.configuration.GridHubConfiguration;
import org.openqa.grid.internal.utils.SelfRegisteringRemote;
import org.openqa.grid.web.Hub;
import org.openqa.selenium.remote.DesiredCapabilities;

public class SeleniumGrid {

    private static Hub hub;
    private static URL remoteURL;
    private static RegistrationRequest req = new RegistrationRequest();
    private Map<String, Object> nodeConfig = new HashMap<String, Object>();
    private static SelfRegisteringRemote remote = new SelfRegisteringRemote(req);

    SeleniumBase sb = new SeleniumBase();

    public void setup() throws Exception {

        hub = getHub("localhost", 4458);
        remoteURL = new URL("http://" + hub.getUrl() + ":" + 5555);

        // req = setRegRequest();
        req.setRole(GridRole.NODE);
        req.addDesiredCapability(sb.getCapability());

        List<DesiredCapabilities> dc = req.getCapabilities();

        for (DesiredCapabilities c : dc) {
            System.out.println("Using capabilities: " + c.toString());
        }

        // nodeConfig.put(req.AUTO_REGISTER, true);
        nodeConfig.put(req.HUB_HOST, hub.getHost());
        nodeConfig.put(req.HUB_PORT, hub.getPort());
        nodeConfig.put(req.PORT, 5555);

        // nodeConfig.put(RegistrationRequest.PROXY_CLASS, "org.openqa.grid.selenium.proxy.DefaultRemoteProxy");
        nodeConfig.put(req.MAX_SESSION, 1);
        // nodeConfig.put(RegistrationRequest.CLEAN_UP_CYCLE, 2000);
        nodeConfig.put(req.REMOTE_HOST, remoteURL);
        nodeConfig.put(req.MAX_INSTANCES, 1);

        nodeConfig.put(req.BROWSER, "firefox");
        nodeConfig.put(req.UNREGISTER_IF_STILL_DOWN_AFTER, 20000);

        nodeConfig.put(req.HOST, hub.getHost());

        System.out.println("Hub Port: " + hub.getHost() + hub.getPort());
        System.out.println(req.HOST);
        System.out.println(req.HUB_HOST);
        System.out.println(req.HUB_PORT);

        req.setConfiguration(nodeConfig);

        remote.startRemoteServer();

        remote.startRegistrationProcess();

    }

    public RegistrationRequest setRegRequest() {
        RegistrationRequest request = new RegistrationRequest();

        request.setRole(GridRole.NODE);
        request.addDesiredCapability(sb.getCapability());

        List<DesiredCapabilities> dc = request.getCapabilities();

        for (DesiredCapabilities c : dc) {
            System.out.println("Using capabilities: " + c.toString());
        }
        return request;

    }

    public Hub getHub(String host, int port) {
        GridHubConfiguration config = new GridHubConfiguration();
        config.setHost(host);
        config.setPort(port);
        Hub someHub = new Hub(config);

        return someHub;

    }

    public void tearDown() throws Exception {
        remote.stopRemoteServer();
        hub.stop();
    }
}

1 个答案:

答案 0 :(得分:1)

这里是一个完整的工作示例,启动集线器,启动节点,将节点连接到集线器,对本地分离的集线器运行测试,然后继续关闭节点,然后枢纽。

import org.openqa.grid.common.GridRole;
import org.openqa.grid.internal.utils.SelfRegisteringRemote;
import org.openqa.grid.internal.utils.configuration.GridHubConfiguration;
import org.openqa.grid.internal.utils.configuration.GridNodeConfiguration;
import org.openqa.grid.selenium.proxy.DefaultRemoteProxy;
import org.openqa.grid.web.Hub;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.server.SeleniumServer;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.net.MalformedURLException;
import java.net.URL;

public class SeleniumGrid {

    private Hub hub;
    private SelfRegisteringRemote remote;

    @BeforeClass
    public void setup() throws Exception {
        hub = getHub("localhost", 4458);
        hub.start();
        remote = new SelfRegisteringRemote(getNodeConfiguration());
        remote.addBrowser(DesiredCapabilities.firefox(), 2);
        SeleniumServer server = new SeleniumServer(remote.getConfiguration());
        remote.setRemoteServer(server);
        remote.startRemoteServer();
        remote.startRegistrationProcess();
    }

    @Test
    public void test() throws MalformedURLException {
        URL url = new URL(hub.getUrl() + "/wd/hub");

        RemoteWebDriver driver = new RemoteWebDriver(url, DesiredCapabilities.firefox());
        try {
            driver.get("http://www.google.com");
            System.err.println("Title " + driver.getTitle());
        } finally {
            driver.quit();
        }
    }

    @AfterClass
    public void tearDown() throws Exception {
        remote.stopRemoteServer();
        hub.stop();
    }

    private GridNodeConfiguration getNodeConfiguration() {
        GridNodeConfiguration nodeConfiguration = new GridNodeConfiguration();

        nodeConfiguration.register = true;
        nodeConfiguration.hub = String.format("http://%s:%d", hub.getConfiguration().host, hub.getConfiguration().port);
        nodeConfiguration.port = 5555;
        nodeConfiguration.proxy = DefaultRemoteProxy.class.getCanonicalName();
        nodeConfiguration.maxSession = 1;
        nodeConfiguration.cleanUpCycle = 2000;
        nodeConfiguration.unregisterIfStillDownAfter = 20000;
        nodeConfiguration.role = GridRole.NODE.toString();
        nodeConfiguration.port = 5555;
        nodeConfiguration.getHubHost();
        nodeConfiguration.hub = String.format("http://%s:%d", hub.getConfiguration().host, hub.getConfiguration().port);
        return nodeConfiguration;
    }

    private Hub getHub(String host, int port) {
        GridHubConfiguration config = new GridHubConfiguration();
        config.host = host;
        config.port = port;
        return new Hub(config);
    }
}