Step 1: Machine setup
1. Suppose you have two machines:
Machine 1 IP is : 192. 168. 0. 100
Machine 2 IP is : 192. 168. 0. 101
Step 2: Start hub and nodes
In machine 1, we start the hub and a node.
hub
java -jar selenium-server-standalone-2.53.0.jar -role hub
IE node:
java -jar selenium-server-standalone-2.53.0.jar -Dwebdriver.ie.driver="<yourpath>/IEDriverServer.exe" -role
webdriver -hub http://localhost:4444/grid/register -browser browserName="internet explorer"
Note:
1) For IE webdriver, please download the corresponding version and bit against the selenium-server-standalone server and windows bit, for example, if you use selenium-server-standalone-2.53.0 in win7 64bit OS, you should download the IEDriverServer_x64_2.53.0.zip
2) Here is the URL: http://selenium-release.storage.googleapis.com/index.html
In machine 2, config the other two nodes:
For Firefox node:
java -jar selenium-server-standalone-2.53.0.jar -role webdriver -hub http://192.168.0.100:4444/grid/register -browser browserName=firefox
For Chrome node:
java -jar selenium-server-standalone-2.53.0.jar -Dwebdriver.chrome.driver="<yourpath>/chromedriver.exe" -role
webdriver -hub http://192.168.0.100:4444/grid/register -port 6666 -browser browserName=chrome
Step 3: Write your test case
Write a test case with testng:
package testcases; import java.net.MalformedURLException; import java.net.URL; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; import org.testng.annotations.Parameters; import org.testng.annotations.Test; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; public class GridParallelTests{ private WebDriver dr; DesiredCapabilities dc; String baseUrl; @Parameters({"browser", "nodeUrl", "webSite"}) @BeforeMethod public void setUp(String browser, String nodeUrl, String webSite){ baseUrl = webSite; if(browser.equals("ie")) dc = DesiredCapabilities.internetExplorer(); else if(browser.equals("ff")) dc = DesiredCapabilities.firefox(); else if(browser.equals("chrome")) dc = DesiredCapabilities.chrome(); else System.out.println("browser is wrong, only can be ie, ff, chrome"); String url = nodeUrl + "/wd/hub"; URL urlInstance = null; try{ urlInstance = new URL(url); } catch (MalformedURLException e){ e.printStackTrace(); System.out.println("Error: Please check the url format, should be:http://"); } dr = new RemoteWebDriver(urlInstance, dc); dr.get(webSite); } @Test public void test(){ dr.get(baseUrl); dr.findElement(By.id("kw")).sendKeys("Hello Webdriver!"); dr.findElement(By.id("su")).click(); try{ Thread.sleep(10000); } catch(InterruptedException e){ e.printStackTrace(); } System.out.println("title:" + dr.getTitle()); } @AfterMethod public void quit(){ dr.close(); } }
Step 4: Config testng xml file
Write the testng xml file:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite"> <test name="Test1"> <parameter name="browser" value="ff"/> <parameter name="nodeUrl" value="http://machine1:5555"/> <parameter name="webSite" value="http://www.baidu.com"/> <classes> <class name="testcases.GridParallelTests"/> </classes> </test> <!-- Test --> <test name="Test2"> <parameter name="browser" value="chrome"/> <parameter name="nodeUrl" value="http://machine1:6666"/> <parameter name="webSite" value="http://www.baidu.com"/> <classes> <class name="testcases.GridParallelTests"></class> </classes> </test> <test name="test3"> <parameter name="browser" value="ie"/> <parameter name="nodeUrl" value="http://machine2:5555"/> <parameter name="webSite" value="http://www.baidu.com"/> <classes> <class name="testcases.GridParallelTests"></class> </classes> </test> </suite> <!-- Suite -->
Step 5: Run test suite
In Eclipse, run the xml file as test suite: