How to Automate TestNG in Selenium?
Last Updated :
06 Sep, 2024
TestNG is an open-source test automation framework for Java, designed to make the process of testing more efficient and effective. Standing for "Next Generation," TestNG offers advanced features like annotations, data-driven testing, and parallel execution.
When combined with Selenium, it provides a powerful solution for automating web applications, enabling testers to create, organize, and run tests seamlessly.
TestNG in Selenium
- TestNG provide advanced features such as annotations, data driven testing, test sequencing, and parallel testing to help you organize and execute your selenium test more effectively.
- TestNG provides a wide range of annotations that you can use to customize your tests, such as @BeforeSuite, @AfterSuite, @BeforeTest, @AfterTest, @BeforeMethod, @afterMethod.
- It supports data-driven testing, allowing you to run the same test case with multiple test data sets.
Installing and Setting up
- Create a New Maven Project: If you're using an IDE like IntelliJ IDEA or Eclipse, create a new Maven project to manage dependencies easily.
- Add Dependencies: If using Maven, include the following dependencies in your pom.xml file.
XML
<dependencies>
<!-- Selenium Java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.20.0</version> <!-- Use the latest version -->
</dependency>
<!-- TestNG -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.7.0</version> <!-- Use the latest version -->
<scope>test</scope>
</dependency>
</dependencies>
Install TestNG
- Install Eclipse IDE from the Eclipse Website. It serves as a platform for you to code on and write your test cases.
- Once installed, go to help and navigate to the 'Eclipse Marketplace'. The referenced snapshot is below:
- Eclipse Marketplace'. You will be directed to the marketplace modal. Type TestNG in the search keyword and hit 'Go'. The referenced snapshot is below:
How to Write your First TestNG Test
- Create your first TestNG class by right-clicking on the package and selecting the '' folder from the 'New' option.
- Then after create a your own package. The referenced snapshot given below
- Then After your package creation in that package create a new class. The referenced snapshot given below
Example
Java
package TestNGFrameWork;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import io.github.bonigarcia.wdm.WebDriverManager;
public class FirstTest {
@Test
public void TestGoogle() throws Exception {
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com/");
driver.findElement(By.name("q")).sendKeys("amazon", Keys.ENTER);
System.out.println(driver.getTitle());
Thread.sleep(1000);
driver.quit();
}
@Test
public void TestFacebook() throws Exception {
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.get("https://www.facebook.com/");
driver.findElement(By.name("email")).sendKeys("xyz", Keys.ENTER);
System.out.println(driver.getTitle());
Thread.sleep(1000);
driver.quit();
}
}
Explanation above code:
- Package: TestNGFramework is a custom package name.
- Imports: The code imports necessary classes from Selenium WebDriver and TestNG libraries.
- WebDriver: Interface for controlling the browser.
- ChromeDriver: Implementation of WebDriver for Chrome.
- By: Class to locate elements
- Keys: Class to simulate keyboard actions
- WebDriverManager: Library to manage browser driver binaries automatically.
- TestNG annotations (@Test,@BeforeTest,@AfterTesr):For defining test methods and lifecycle management.
- Class: FirstTest contains the test methods to be executed.
Test Methods
Test Method 1: 'TestGoogle'
- Annotation '@Test': Marks this as a test case.
- 'WebDriverManager.chromedriver( ).setup()': Sets up the ChromeDriver binary automatically.
- new ChromeDriver( ): Create a new instance of ChromeDriver, Which launches a new chrome browser window.
- driver.get("https://www.google.com/"): Navigates to Google's homepage.
- driver.findElement(By.name("q")).sendKeys("amazon", Keys.ENTER): Finds the search box on google's homepage(element with name "q"), sends the text "amazon", and simulates pressing the Enter Key.
- System.out.println(driver.getTitle()): Prints the title of the current page to the console.
- Thread.sleep(1000): Pause execution for 1 second (1000 miiliseconds).
- driver.quit(): Close the browser and ends the WebDriver session.
Test Method2: TestFacebook
- Annotation '@Test': Marks this as a test case.
- 'WebDriverManager.chromedriver( ).setup()': Sets up the ChromeDriver binary automatically.
- new ChromeDriver( ): Create a new instance of ChromeDriver, Which launches a new chrome browser window.
- driver.get("https://www.facebook.com/"): Navigates to Google's homepage.
- driver.findElement(By.name("email")).sendKeys("xyz", Keys.ENTER): Finds the email input field (element with name "email"), sends the text "xyz", and simulates pressing the Enter Key.
- System.out.println(driver.getTitle()): Prints the title of the current page to the console.
- Thread.sleep(1000): Pause execution for 1 second (1000 miiliseconds).
- driver.quit(): Close the browser and ends the WebDriver session.
After Finish the code, then you select right click and again click on 'Run As' and click on TestNG Test. The snippet given below
Output
outputConclusion
By integrating TestNG with Selenium, you can streamline your test automation process, making it more organized and scalable. TestNG's powerful features, such as test sequencing and parallel execution, enhance the efficiency of your Selenium tests. Adopting these best practices will lead to more reliable and maintainable test suites, ultimately improving the quality of your automated testing efforts.
Similar Reads
How to create Selenium test cases Selenium IDEÂ is an open-source tool that is widely used in conducting automated web testing and browser automation. This tool is intended mainly for Web Application testers and developers to develop, edit, and run automated test cases for Web Applications. Table of Content What are Selenium test cas
6 min read
How to Create Test Suite in TestNG? In this article we will learn about test suites and how can we build them using Java and TestNG. TestNG is a modern testing framework that is used very widely used today. It has a broad base of features that allow us to write unit tests and club them together in groups. Table of Content What is a Te
11 min read
Automation Using Selenium in C# With Example Selenium is an open-source Web UI automation testing suite. It was developed by Jason Huggins in 2004 as an internal tool at Thought Works. It supports automation across different browsers, platforms, and programming languages which includes Java, Python, C#, etc. It can be easily be deployed on Win
3 min read
Automation Using Selenium in C# With Example Selenium is an open-source Web UI automation testing suite. It was developed by Jason Huggins in 2004 as an internal tool at Thought Works. It supports automation across different browsers, platforms, and programming languages which includes Java, Python, C#, etc. It can be easily be deployed on Win
3 min read
Best Practices for Selenium Test Automation Selenium Test Automation is a cornerstone of modern web application testing. To ensure that your Selenium scripts are both effective and reliable, it's crucial to follow best practices that enhance test quality and maintainability. Implementing the right strategies can save time, reduce troubleshoot
6 min read
How to Automate Click Using Selenium WebDriver? Selenium is one of the most popular and powerful tools for automating web applications. Selenium is widely used for automating user interactions like filling out forms, clicking on a button, navigating to a web page, and many more. One of the most common tasks while working with Selenium is clicking
5 min read