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?
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>