Write you first selenium test case with TestNG ?


In my previous article, we have used java console application to test very simple search functionality but, in real time application it is not the case you need to test so many use case’s if you follow the same console application approach it is very difficult to maintain and test your application

We should not put all the testing logic in single main method, we need to divide it into separate test cases then only it is easy to maintain and debug test cases, we can use TestNG  which is advanced framework than Junit will help us to write test cases, executing test case in an order, generate reports, creating a test suite, parallel execution etc…

I am going to create a new class using Eclipse IDE just by right-clicking on package folder, and the class name is “SearchTest.java”


In the context menu select “Create TestNG class” and then provide class name in the next window, here I am using SearchTest 



Once the class is created you can replace the SearchText.java file code with the below code

The beforeSuite() method executes before running all other test cases in the suite so, I am placing common logic which is creating a WebDriver instance and opening page etc…

The afterSuite() method executes once all test cases are successfully executed, you can execute cleanup tasks here

There are two test cases I have created one is for entering keyword in the textbox and another one is clicking on search button

Execute the test case by following this navigation Run -> Run As -> TestNG Test


Once, the execution is successful you should see the test case report status, how many test cases are passed, failed etc…