Hello Urmila, to parallelly execute automation tests using Selenium Grid, you can use following code snippet:
public class Parallel_Execution_Test
{
WebDriver driver;
String nodeURL;
@Parameters({"Port"})
@BeforeMethod()
public void setUp(String Port) throws MalformedURLException
{
if(Port.equalsIgnoreCase("4546"))
{
nodeURL = "http://10.0.0.22:4546/wd/hub";
System.out.println("Chrome Browser Initiated");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setBrowserName("chrome");
capabilities.setPlatform(Platform.WINDOWS);
driver = new RemoteWebDriver(new URL(nodeURL),capabilities);
driver.get("https://www.google.com/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
else if(Port.equalsIgnoreCase("5566"))
{
nodeURL = "http://10.0.0.22:5566/wd/hub";
System.out.println("Firefox Browser Initiated");
DesiredCapabilities capabilities1 = DesiredCapabilities.firefox();
capabilities1.setBrowserName("firefox");
capabilities1.setPlatform(Platform.WINDOWS);
driver = new RemoteWebDriver(new URL(nodeURL),capabilities1);
driver.get("https://www.google.com/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
else if(Port.equalsIgnoreCase("4547"))
{
nodeURL = "http://10.0.0.22:4547/wd/hub";
System.out.println("Internet Browser Initiated");
DesiredCapabilities capabilities2 = DesiredCapabilities.internetExplorer();
capabilities2.setBrowserName("internet explorer");
capabilities2.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
capabilities2.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);
capabilities2.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
capabilities2.setCapability("ignoreProtectedModeSettings", true);
capabilities2.setCapability("nativeEvents", false);
capabilities2.setCapability(InternetExplorerDriver.INITIAL_BROWSER_URL, "");
capabilities2.setCapability(InternetExplorerDriver.LOG_LEVEL, "DEBUG");
capabilities2.setPlatform(Platform.WINDOWS);
driver = new RemoteWebDriver(new URL(nodeURL),capabilities2);
driver.get("https://www.google.com/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
}
@Test
public void googlePage() throws InterruptedException
{
try
{
driver.findElement(By.xpath("//*[id=search")).click();
Thread.sleep(2000);
driver.findElement(By.cssSelector("#chapternav > div > ul > li.chapternav-item.chapternav-item-ipad-air > a > figure")).click();
Thread.sleep(2000);
driver.findElement(By.linkText("Google Search")).click();
Thread.sleep(2000);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
@AfterMethod()
public void tearDown()
{
driver.quit();
System.out.println("Browser Closed");
}
}
Now create the testing.xml file to pass parameters:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite thread-count="3" name="MultipleBrowser" parallel="tests">
<test name="Chrome Test">
<parameter name="Port" value="4546"/>
<classes>
<class name="DemoAutomation.Selenium_MultiBrowser_Test"/>
</classes>
</test>
<test name="Firefox Test">
<parameter name="Port" value="5566"/>
<classes>
<class name="DemoAutomation.Selenium_MultiBrowser_Test"/>
</classes>
</test>
<test name="Internet Explorer Test">
<parameter name="Port" value="4547"/>
<classes>
<class name="DemoAutomation.Selenium_MultiBrowser_Test"/>
</classes>
</test>
</suite>