Selenium TestNG Integration
TestNG is a testing framework that is inspired by JUnit and NUnit. It is designed to cover all categories of
tests: unit, functional, end-to-end, integration, etc., and it requires JDK 5 or higher. TestNG is designed
to
be better than JUnit, and it makes it easier to write and run tests.
How to Integrate Selenium with TestNG
To integrate Selenium with TestNG, follow these steps:
- Install TestNG: Add the TestNG library to your project. If you are using Maven, add
the
following dependency to your
pom.xml
file:<dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.4.0</version> <scope>test</scope> </dependency>
- Create a TestNG Test Class: Create a new Java class and annotate your test methods
with
@Test
. Here is an example:public class SeleniumTestNGExample { WebDriver driver; @BeforeClass public void setUp() { System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); driver = new ChromeDriver(); } @Test public void testGoogleSearch() { driver.get("https://nicetesters.com.com"); // Add your test steps here } @AfterClass public void tearDown() { if (driver != null) { driver.quit(); } }
- Run the Test: You can run the TestNG test class from your IDE or by using the TestNG
XML configuration file. Here is an example of a 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="Test"> <classes> <class name="SeleniumTestNGExample" /> </classes> </test> </suite>
By following these steps, you can easily integrate Selenium with TestNG and start writing your automated tests.