TestNG With Selenium Webdriver With Java
TestNG (Testing Next Generation) is a framework designed to simplify a broad range of testing needs, from unit to end-to-end testing. In the context of Selenium WebDriver, TestNG is widely Master TestNG with Selenium WebDriver with Java: Learn efficient test automation, parallel execution, and advanced reporting for robust software testing. used because it offers powerful features for organizing, running, and reporting tests. Here’s a detailed overview of what TestNG is and how it integrates with Selenium WebDriver in Java.
Key Features of TestNG
- Annotations: TestNG provides a wide range of annotations to control the test execution flow, such as @BeforeSuite, @BeforeTest, @BeforeClass, @BeforeMethod, @Test, @AfterMethod, @AfterClass, @AfterTest, and @AfterSuite.
- Test Configuration: Allows configuration of test suites, tests, and groups via XML files (testng.xml), making it easy to manage complex test setups.
- Parallel Execution: Supports parallel execution of tests, which can be configured through the testng.xml file, improving test execution speed.
- Data-Driven Testing: Supports data-driven testing using @DataProvider, allowing you to run the same test with multiple sets of data.
- Flexible Test Configuration: Enables grouping and prioritization of test methods, allowing you to specify the order of execution.
- Reporting: Generates detailed HTML reports for test execution, including passed, failed, and skipped tests.
Integrating TestNG with Selenium WebDriver
-
Setup the Project:
- Create a new Java project in your preferred IDE (e.g., IntelliJ IDEA, Eclipse).
- Add Selenium WebDriver and TestNG dependencies to your project.
If you’re using Maven, add the following dependencies to your pom.xml:
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.9.1</version> <!– Ensure you use the latest version –>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.7.1</version> <!– Ensure you use the latest version –>
<scope>test</scope>
</dependency>
</dependencies>
-
Write a Test Class:
- Create a test class in your Java project, e.g., GoogleSearchTest.java.
- Use TestNG annotations to structure your test.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class GoogleSearchTest {
private WebDriver driver;
@BeforeClass
public void setUp() {
System.setProperty(“webdriver.chrome.driver”, “path/to/chromedriver”);
driver = new ChromeDriver();
}
@Test
public void testGoogleSearch() {
driver.get(“https://www.google.com”);
WebElement searchBox = driver.findElement(By.name(“q”));
searchBox.sendKeys(“Selenium WebDriver”);
searchBox.submit();
WebElement results = driver.findElement(By.id(“search”));
assert results.isDisplayed();
}
@aAfterClass
public void tearDown() {
if (driver != null) {
driver.quit();
}
}
}
For Free, Demo classes Call: 020-71173125
Registration Link: Java Classes in Pune!
-
Create a TestNG XML File:
- Create a testng.xml file to define the test suite and test classes.
<!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd” >
<suite name=”Suite”>
<test name=”GoogleSearchTest”>
<classes>
<class name=”GoogleSearchTest” />
</classes>
</test>
</suite>
-
Run the Test:
- From your IDE: Right-click on the testng.xml file and select “Run As > TestNG Suite”.
- From the command line: Use the following command to run TestNG tests:
java -cp “path/to/testng.jar:path/to/your/project/classes:path/to/selenium-java.jar” org.testng.TestNG testng.xml
Example of Advanced TestNG Features
Parallel Execution
To run tests in parallel, update the testng.xml file:
<!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd” >
<suite name=”Suite” parallel=”tests” thread-count=”4″>
<test name=”GoogleSearchTest”>
<classes>
<class name=”GoogleSearchTest” />
</classes>
</test>
</suite>
Data-Driven Testing with @DataProvider
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class GoogleSearchTest {
private WebDriver driver;
@BeforeClass
public void setUp() {
System.setProperty(“webdriver.chrome.driver”, “path/to/chromedriver”);
driver = new ChromeDriver();
}
@DataProvider(name = “searchQueries”)
public Object[][] searchQueries() {
return new Object[][] {
{ “Selenium WebDriver” },
{ “TestNG” },
{ “Java programming” }
};
}
@Test(dataProvider = “searchQueries”)
public void testGoogleSearch(String query) {
driver.get(“https://www.google.com”);
WebElement searchBox = driver.findElement(By.name(“q”));
searchBox.sendKeys(query);
searchBox.submit();
WebElement results = driver.findElement(By.id(“search”));
assert results.isDisplayed();
}
@AfterClass
public void tearDown() {
if (driver != null) {
driver.quit();
}
}
}
Here are some key features and benefits of using TestNG with Selenium WebDriver:
- Annotations: TestNG uses annotations (@Test, @BeforeMethod, @AfterMethod, @BeforeClass, @AfterClass, etc.) which are easier to understand and use compared to JUnit’s annotations.
- Test Configuration: TestNG allows you to configure your test methods very flexibly. You can specify dependencies between methods, groups, parameters, priorities, and more.
- Parallel Execution: TestNG supports the parallel execution of tests, which can significantly reduce the time it takes to run your test suite.
- Data-Driven Testing: TestNG supports data-driven testing using @DataProvider annotation. This allows you to run a test method multiple times with different data sets.
- Reporting: TestNG generates detailed HTML reports and logs. This helps in analyzing the test results easily.
- Integrations: TestNG integrates well with build tools like Maven, Ant, and continuous integration tools like Jenkins. This makes it easier to run tests as part of the build process.
- Flexible Test Configuration: You can configure the test suite in an XML file (testng.xml), which allows you to define test suites and test methods, include or exclude tests, and more.
Do visit our channel to learn more: Click Here
Author:-
Chetna Malagi
Call the Trainer and Book your free demo Class For Java Call now!!!
| SevenMentor Pvt Ltd.