Selenium and TestNG with Java

Introduction:

TestNG is a testing framework inspired by JUnit and NUnit with added new functionality that makes it more powerful and easy to use. TestNG can be used for unit testing, integration, end-to-end and functional testing. 

Features of TestNG: 

  • Flexible test configuration 
  • Powerful execution model 
  • Supported by various tools and plugins (Eclipse, IDEA, Maven, etc.) 
  • Support of parameters 
  • Supports various kinds of annotations.

Pre-Requisite: 

  • Download Eclipse IDE 
  • Create a maven project in Eclipse
  • Add various dependencies like selenium, TestNg, WebDriver Manager 
  • Download and install TestNG from the eclipse marketplace

Configuration: 

In the first step, we would be creating a maven project inside the eclipse. We would fill in basic details like name, group id, description, etc. 

New Maven project

In Second step we would be adding a package and then create an execution file. 

Java package

After creating the project setup, we will navigate to mvn repository and search for various kinds of dependencies that we will be using in our project as shown below. 

MVN Repository
MVN Repository

We can notice a small code snippet in the above figure, this is nothing but the dependency code which we need to copy and paste into the pom.xml file. 

Likewise, we have to download mainly 3 dependencies from mvn repository. 

  • Selenium java 
  • Web Driver manager 
  • TestNG 

After downloading all required dependencies, our pom.xml file would look like the below. 

Pom file

Folder Structure: 

TesNG Folder structure

After all configuration, we will find a folder structure in the pattern as shown above. It would consist of various folders; our main test file would be located under the com.practice.test package. 

Test Case:

To understand how TestNG works we would create a simple test that will launch the browser and search a basic query as shown below. 

Test case TestNG

Explanation: 

  • In the above code, we have created a method (TestGoogle) inside the class. 
  • Above which it has annotations @Test, this signifies that this test case is based on TestNG. 
  • Right after the method, we would be adding a setup that consists of a chrome driver.
  • After all this, we would further go ahead with launching the browser (Google.com) and thus search for ‘neova solutions’ and click enter. 
  • Lastly, we would get a title after the search query is executed. 

Note: Using the same technique, we can also launch various other browsers like Firefox or internet explorer as shown below. 

TestNG Test case

Execution:

After the Execution of the code, we will see the results below, which signify that our Test has passed. 

TestNG execution code

Test Report: 

Test report TestNG

Conclusion:

In this blog, we have covered a comprehensive introduction to TestNG, various features and advantages of TestNG, and provided a step-by-step guide for setting up a TestNG project in Eclipse IDE. It also includes a basic test case that demonstrates the capabilities of TestNG, along with the test results and generated report.

Click here to read the next blog on TestNG.