4

Setting up selenium Grid:

I was able to set up grid (hub & node) using below commands in command prompt and then able to launch browsers and execute test successfully.

    java -jar selenium-server-standalone-3.4.0.jar -role hub
    java -jar selenium-server-standalone-3.4.0.jar -role webdriver -hub "http://localhost:4444/grid/register" -port 5566

When trying to setup selenium grid via script, i was able to setup hub and node successfully, but not able to launch the browser.

Kindly help to resolve the issue.

Please find the code and console below:

    package config.HubNode;

    import java.net.MalformedURLException;  
    import java.net.URL;    
    import org.openqa.grid.common.RegistrationRequest;  
    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.web.Hub;  
    import org.openqa.selenium.Platform; 
    import org.openqa.selenium.WebDriver;  
    import org.openqa.selenium.remote.DesiredCapabilities;
    import org.openqa.selenium.remote.RemoteWebDriver;

    public class HubNodeConfig {

    public static void main(String[] args) throws Exception {

        HubNodeConfig objHubNodeConfig = new HubNodeConfig();
        objHubNodeConfig.HubConfig();
        System.out.println("Hub Configured Successfully");

        objHubNodeConfig.NodeConfig();
        System.out.println("Node Configured to Hub Successfully");
        objHubNodeConfig.InvokeBrowser();
    }

    public void HubConfig() throws Exception
    {
        GridHubConfiguration gridHubConfig = new GridHubConfiguration();
        gridHubConfig.role = "hub";
        gridHubConfig.host = "localhost";
        gridHubConfig.port = 4444;

        Hub myHub = new Hub(gridHubConfig);
        myHub.start();
    }

    public void NodeConfig()
    {
        GridNodeConfiguration gridNodeConfig = new GridNodeConfiguration();
        gridNodeConfig.hub = "http://localhost:4444/grid/register"
        gridNodeConfig.host = "192.xxx.xx.xx" //my ip address
        gridNodeConfig.port = 5566;
        gridNodeConfig.role = "webdriver";

        RegistrationRequest req = new RegistrationRequest(gridNodeConfig);
        req.getConfiguration();
        req.validate();
        RegistrationRequest.build(gridNodeConfig);

        SelfRegisteringRemote remote = new SelfRegisteringRemote(req);
        remote.startRegistrationProcess();
    }

    public void InvokeBrowser() throws MalformedURLException
    {
        System.setProperty("webdriver.chrome.driver", "E:\\chromedriver.exe");
        DesiredCapabilities capability = DesiredCapabilities.chrome();
        capability.setBrowserName("chrome");
        capability.setPlatform(Platform.WIN10);

        WebDriver driver = new RemoteWebDriver(new URL("http://"+gridNodeConfig.host+":"+gridNodeConfig.port+"/wd/hub"), capability);
        System.out.println("Driver Launched Successfully");
        driver.get("https://google.com");
    }
    }

Console error message and Screenshot of the Grid:



    2017-07-20 23:57:17.135:INFO::main: Logging initialized @2576ms to org.seleniumhq.jetty9.util.log.StdErrLog    
    Jul 20, 2017 11:57:17 PM org.openqa.grid.web.Hub initServer
    INFO: Will listen on 4444    
    2017-07-20 23:57:17.509:INFO:osjs.Server:main: jetty-9.4.3.v20170317     
    2017-07-20 23:57:17.569:INFO:osjs.session:main: DefaultSessionIdManager workerName=node0    
    2017-07-20 23:57:17.570:INFO:osjs.session:main: No SessionScavenger set, using defaults    
    2017-07-20 23:57:17.573:INFO:osjs.session:main: Scavenging every 600000ms    
    2017-07-20 23:57:17.592:INFO:osjsh.ContextHandler:main: Started o.s.j.s.ServletContextHandler@1a1d6a08{/,null,AVAILABLE}    
    2017-07-20 23:57:17.804:INFO:osjs.AbstractConnector:main: Started ServerConnector@77f99a05{HTTP/1.1,[http/1.1]}{0.0.0.0:4444}    
    2017-07-20 23:57:17.804:INFO:osjs.Server:main: Started @3245ms    
    Hub Configured Successfully    
    Jul 20, 2017 11:57:19 PM org.openqa.grid.internal.utils.SelfRegisteringRemote$1 run    
    INFO: Starting auto registration thread. Will try to register every 5000 ms.    
    Jul 20, 2017 11:57:19 PM org.openqa.grid.internal.utils.SelfRegisteringRemote registerToHub    
    INFO: Registering the node to the hub: http: //localhost:4444/grid/register    
    Node Configured to Hub Successfully    
    Jul 20, 2017 11:57:19 PM org.openqa.grid.internal.Registry add    
    INFO: Registered a node http: //192.xxx.xx.xx:5566    
    Jul 20, 2017 11:57:19 PM org.openqa.grid.internal.utils.SelfRegisteringRemote registerToHub   
    INFO: The node is registered to the hub and ready to use    

    Exception in thread "main" 
    org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.    
    Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
    System info: host: 'KRISH', ip: '192.xxx.xx.xx', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_131'    
    Driver info: driver.version: RemoteWebDriver
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:658)
        at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:250)
        at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:236)
        at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:137)
        at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:174)
        at config.HubNode.HubNodeConfig.InvokeBrowser(HubNodeConfig.java:71)
        at config.HubNode.HubNodeConfig.main(HubNodeConfig.java:29)
     Caused by: org.apache.http.conn.HttpHostConnectException: Connect to 192.xxx.xx.xx:5566 [/192.xxx.xx.xx] failed: Connection refused: connect 
        at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:159)
        at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:359)
        at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:381)
        at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:237)
        at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
        at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
        at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111)
        at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72)
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)
        at org.openqa.selenium.remote.internal.ApacheHttpClient.fallBackExecute(ApacheHttpClient.java:139)
        at org.openqa.selenium.remote.internal.ApacheHttpClient.execute(ApacheHttpClient.java:87)
        at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:343)
        at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:159)
        at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:142)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:637)
        ... 6 more
    Caused by: java.net.ConnectException: Connection refused: connect
        at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
        at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
        at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
        at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
        at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
        at java.net.PlainSocketImpl.connect(Unknown Source)
        at java.net.SocksSocketImpl.connect(Unknown Source)
        at java.net.Socket.connect(Unknown Source)
        at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:75)
        at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142)
        ... 21 more

    >Jul 21, 2017 12:15:30 AM org.openqa.grid.selenium.proxy.DefaultRemoteProxy onEvent     
    INFO: Marking the node http: //192.xx.xx.xx:5566 as down: cannot reach the node for 2 tries    
    Jul 21, 2017 12:16:30 AM org.openqa.grid.selenium.proxy.DefaultRemoteProxy onEvent    
    INFO: Unregistering the node http: //192.xx.xx.xx:5566 because it's been down for 60077 milliseconds    
    Jul 21, 2017 12:16:30 AM org.openqa.grid.internal.Registry removeIfPresent    
    WARNING: Cleaning up stale test sessions on the unregistered node http: //192.xx.xx.xx:5566    
    Jul 21, 2017 12:16:34 AM org.openqa.grid.internal.utils.SelfRegisteringRemote registerToHub    
    INFO: Registering the node to the hub: http: //localhost:4444/grid/register    
    Jul 21, 2017 12:16:34 AM org.openqa.grid.internal.Registry add    
    INFO: Registered a node http: //192.xx.xx.xx:5566    
    Jul 21, 2017 12:16:34 AM org.openqa.grid.internal.utils.SelfRegisteringRemote registerToHub    
    INFO: The node is registered to the hub and ready to use    
    Jul 21, 2017 12:16:46 AM org.openqa.grid.selenium.proxy.DefaultRemoteProxy onEvent    
    INFO: Marking the node http: //192.xx.xx.xx:5566 as down: cannot reach the node for 2 tries    

Screenshot of Grid Console

1 Answers1

4

Your code that starts the node has issues. Please fix it as below.

The method nodeConfig() doesn't inject in a server into the SelfRegisteringRemote.

Here's how it should look like:

RegistrationRequest req = RegistrationRequest.build(gridNodeConfig);
SelfRegisteringRemote remote = new SelfRegisteringRemote(req);
remote.setRemoteServer(new SeleniumServer(gridNodeConfig));
remote.startRemoteServer();
remote.startRegistrationProcess();

Also please change your RemoteWebDriver instantiation code to as below

WebDriver driver = new RemoteWebDriver(new URL("http://" + gridHubConfig.host + ":" + gridHubConfig.port + "/wd/hub"), capability);

Notice that we are connecting to the gridHubConfig host and its port and not that of the node (which is what your code was doing).

Once you fix these issues, your code should work fine and you should see an embedded Grid spun off via your code.

Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66
  • Thanks. I was able to launch browser if i setup same machine as host and node. But if hub and node machines are different, i am getting the error message **"Error forwarding the new session Error forwarding the request Connect to 169.xxx.xxx.xxx:5556 [/169.xxx.xxx.xxx] failed: Connection refused: connect"** – krishnakumar Jul 24 '17 at 08:29
  • What exactly are you trying to do ? Are you trying to register a node with an already running hub ? – Krishnan Mahadevan Jul 24 '17 at 08:30
  • one machine will be hub and other machines will be nodes(may be five nodes), i will have my code being executed from one machine(hub), the execution should take place in other machines(nodes) – krishnakumar Jul 24 '17 at 08:38
  • What execution are you talking about? You cannot spin off a `node` on a different machine. For you to be able to do what you are trying, you would need to start the hub and the nodes manually on each of the nodes and then finally run your tests against it. You cannot create a Grid dynamically on remote machines before starting to run your tests. – Krishnan Mahadevan Jul 24 '17 at 08:40
  • Thanks much :) so spinning off node in different machine using grid dynamically is not feasible. we need to do manually on remote machines before start executing tests. Is my understanding correct? – krishnakumar Jul 24 '17 at 08:48
  • Yes. That is correct. The maximum you can do is, spin off a Hub and have the hub manage spinning of nodes in a dynamic fashion. I built a PoC library that does something like this by using Docker. You can refer to [just-ask](https://github.com/RationaleEmotions/just-ask) for more details. Also if my answer has helped you, please help accept it. – Krishnan Mahadevan Jul 24 '17 at 08:50
  • @KrishnanMahadevan Can you clarify on the below, also JVM way can it spawn VM based nodes ? Explicitly register the ghost node by loading the URL http://localhost:4444/grid/admin/EnrollServlet in a browser. – Sudheej Jul 24 '17 at 15:41
  • @xtechkid - You should be able to customize `just-ask` to work with any supplier. The instructions are detailed [here](https://github.com/RationaleEmotions/just-ask#how-to-customize-and-use). Although I must admit I have never tried it with a VM based node because I dont have the infrastructure for it. If you can share the details as an issue on that project I can try and figure out the specifics. The `Ghost node` essentially works as a proxy for all the nodes and it manages the node pool for the hub. – Krishnan Mahadevan Jul 24 '17 at 16:52
  • @KrishnanMahadevan our ( @krishnakumar) primary issue is keeping the nodes alive, our infra is a large scale CI-CD model where teamcity would start the selenium hub, we tried keeping VM nodes alive but every time when hub restarts nodes do not reconnect back automatically. Even if they connect the tests hung.. we would end up restarting the node on each VM. – Sudheej Jul 24 '17 at 17:01
  • 1
    @xtechkid - Why do you have your CI system start the hub ? Why can't it be something to which your tests just connect to ? If you are looking for a self healing grid, you can take a look at [SeLion grid](http://paypal.github.io/SeLion/html/documentation.html#selion-grid) that we built which has this capability. I have explained Self Healing as a concept in [this](https://rationaleemotions.wordpress.com/2013/01/28/building-a-self-maintaining-grid-environment/) blog post of mine. – Krishnan Mahadevan Jul 24 '17 at 17:05
  • @KrishnanMahadevan ours is an unsupervised agile build model, which auto deploys and auto starts testing phase. Let me take a look at self healing grid. Thanks. – Sudheej Jul 24 '17 at 17:06