In testing, it is always important to test application in different browsers. We can perform automation on multiple browsers using selenium and testng. If there are more number of tests that need to be executed parallelly on different browsers also, we can do this using testng. We will look into the below examples in detail:
Below is the sample test which will only run one browser at a time.
package com.pack;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class ParallelTest {
private WebDriver driver;
String baseURL = "http://www.google.com/";
@Parameters({ "browser" })
@BeforeTest
public void openBrowser(String browser) {
try {
if (browser.equalsIgnoreCase("Firefox")) {
driver = new FirefoxDriver();
} else if (browser.equalsIgnoreCase("chrome")) {
System.setProperty("webdriver.chrome.driver",
"D:/Dev/Jars/chromedriver.exe");
driver = new ChromeDriver();
} else if (browser.equalsIgnoreCase("IE")) {
System.setProperty("webdriver.ie.driver",
"D:/Dev/Jars/IEDriverServer.exe");
driver = new InternetExplorerDriver();
}
} catch (WebDriverException e) {
System.out.println(e.getMessage());
}
}
@Test
public void login_TestCase() {
driver.navigate().to(baseURL);
//do something
}
@Test
public void search_TestCase() {
driver.navigate().to(baseURL);
//do something
}
@AfterTest
public void closeBrowser() {
driver.quit();
}
}
In the above code, we have OpenBrowser method with BeforeTest annotation along with parameter 'browser'. In the xml we will define three tests tags to run each test with different browser. We will compare the browser value with the parameter value and based on that we will create the driver instance. We have now defined three tests with three browsers (Firefox, Google Chrome and Internet Explorer)
You can find more details about defining parameters in testng
Below is the testng.xml file which will run all the tests which are defined. We are passing parameter value with browser name for each test. Tests will be executed in there browsers one by one.
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel test suite">
<test name="Firefox Test">
<parameter name="browser" value="Firefox"/>
<classes>
<class name="com.pack.ParallelTest"/>
</classes>
</test>
<test name="Chrome Test">
<parameter name="browser" value="chrome"/>
<classes>
<class name="com.pack.ParallelTest"/>
</classes>
</test>
<test name="Internet Explorer Test">
<parameter name="browser" value="IE"/>
<classes>
<class name="com.pack.ParallelTest"/>
</classes>
</test>
</suite>
We can also use TestNG to execute tests Simultaneously by defining the"parallel" attribute to "tests" to run all the tests in defined browser with the help of testng.xml configuration file.
<suite name="Parallel test suite" parallel="tests">
We just need to update the single line in above testng.xml. By defining parallel="tests" in suite tag, tests will get executed simultaneously. In order to execute to execute tests simultaneously it is recommended to have good system configuration, so that the execution time can be saved.
You can find more details about Executing multiple tests using testng
It looks like below:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel test suite" parallel="tests">
<test name="Firefox Test">
<parameter name="browser" value="Firefox"/>
<classes>
<class name="com.pack.ParallelTest"/>
</classes>
</test>
<test name="Chrome Test">
<parameter name="browser" value="chrome"/>
<classes>
<class name="com.pack.ParallelTest"/>
</classes>
</test>
<test name="Internet Explorer Test">
<parameter name="browser" value="IE"/>
<classes>
<class name="com.pack.ParallelTest"/>
</classes>
</test>
</suite>
After executing you can view the report, which will look like below:
Comments
Selenium, TestNG
Above code is not working for me, it is giving report as below
[TestNG] Running:
C:\compatibility\org.compatibility\testng.xml
===============================================
Suite
Total tests run: 1, Failures: 0, Skips: 1
Configuration Failures: 1, Skips: 0
===============================================
Can anybody help me to get ans for this
Add new comment