0

I am performing the cross-browser testing (with TestNG) of my GWT Web Application using the Selenium Grid on 3 different machines A, B, and C. Machine A is working as the hub as well as the node. While the other two machines B and C are given the role of nodes.

Now, the task in front of me is to let the hub decide by itself which Node out of the three nodes are available to it and then it will execute the tests on those particular nodes. For e.g.: If the Machine B is closed or is not working currently then the hub won't execute it and will continue the tests with Machine A and C.

Moreover, the other task is not to change the code for every particular Node since that is what I am doing here according to my little knowledge and experience in the Selenium Grid. Below mentioned is the code which I have written after starting the hub as well as the nodes on all of those machines.

package testNgPackage;

public class Browser {
    //ThreadLocal will provide thread-safe tests
    protected ThreadLocal<RemoteWebDriver> threadLocal = null;
    @BeforeTest
    @Parameters("browser")
    public void setup(String browser) throws MalformedURLException{
        String nodeMachine1 = "http://xxx/wd/hub";
        String nodeMachine2 = "http://yyy/wd/hub";
        String nodeMachine3 = "http://zzz/wd/hub";
        if(browser.equalsIgnoreCase("chrome")) {

            System.setProperty("webdriver.chrome.driver", ".src/Drivers/chromedriver.exe");
            DesiredCapabilities capability = null;
            capability = DesiredCapabilities.chrome();
            capability.setPlatform(Platform.VISTA);
            capability.setBrowserName("chrome");
            threadLocal = new ThreadLocal<RemoteWebDriver>();
            threadLocal.set(new RemoteWebDriver(new URL(nodeMachine1), capability));
        }
        else if(browser.equalsIgnoreCase("firefox")) {

            System.setProperty("webdriver.gecko.driver", ".src/Drivers/geckodriver.exe");
            DesiredCapabilities capability = null;
            capability = DesiredCapabilities.firefox();
            capability.setPlatform(Platform.WIN10);
            capability.setBrowserName("firefox");
            threadLocal = new ThreadLocal<RemoteWebDriver>();
            threadLocal.set(new RemoteWebDriver(new URL(nodeMachine2), capability));
        }

    }

    public WebDriver getDriver() {
        return threadLocal.get();
    }


    @AfterTest
    public void closeBrowser(){
        getDriver().close();
    }

}

The code on which the test is to be performed extends this Browser class but writing that code is of no use here. Also mentioned below is the testng.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
 <suite name="Parallel test suite" parallel="tests" thread-count="2">
    <test name="FirefoxTest">
       <parameter name="browser" value="Firefox" />
       <classes>
         <class name="testNgPackage.TestNGClass"/>
       </classes>
    </test>
    <test name="ChromeTest">
       <parameter name="browser" value="Chrome" />
       <classes>
         <class name="testNgPackage.TestNGClass"/>
       </classes>
    </test>
</suite>

What should I do to optimise my code and accomplish the tasks?

KhiladiBhaiyya
  • 633
  • 1
  • 14
  • 43

1 Answers1

1

When you launch browser using selenium grid, you have to pass the grid hub URL like http://hubIP:hubPort/wd/hub. No need to pass all node machines URLS.

So please replace the line,

String nodeMachine1 = "http://xxx/wd/hub";

with hub URL as follows,

String hubMachine = "http://hubIP:hubPort/wd/hub";

for example, the hub URL should be http://xx.xx.xx.xx:4444/wd/hub. By default selenium grid will be started with port 4444.

and replace the line

threadLocal.set(new RemoteWebDriver(new URL(nodeMachine1), capability));

with

threadLocal.set(new RemoteWebDriver(new URL(hubMachine ), capability));

Also, please make sure you have started the hub and register the node properly.

You no need to set the driver property here in code and pass it while you register the node to hub like

>java -Dwebdriver.chrome.driver=<path to driver> -jar <jarname> -role node -hub <hubURL>
Murthi
  • 5,299
  • 1
  • 10
  • 15
  • Murthi, I have set the Driver property here so that my code can be generic. By that I mean, it could run on any PC because I have copied all the drivers in a folder made by me in the src of my project. I think writing down the drivers for each and every node will become a time consuming task for me. – KhiladiBhaiyya Sep 26 '17 at 02:08
  • driver files should be on the machine where the browser is launching or installed. means, driver file should be copied to the node machine if you are running on selenium grid. You approach is good for executing on local machine. – Murthi Sep 26 '17 at 04:46
  • Murthi, do I need to remove the browser capabilities also from the code and include them in the command line while launching the node? Or is it fine to write them in the code as I have mentioned here and then include only the path of the drivers on the command line? – KhiladiBhaiyya Sep 26 '17 at 05:21
  • browser capabilities should be there in code. it will be used to select the node by selenium grid based on the capabilities. You can capabilities when you register the node if you want to restrict the node for particular browser otherwise it will take all capabilities for the installed browsers. – Murthi Sep 26 '17 at 05:24