Scientech Easy How to Write First TestNG Test Case in Simple Steps | Scientech Easy

Thursday, May 2, 2019

How to Write First TestNG Test Case in Simple Steps

In the previous tutorial, we have known a basic of TestNG and successfully installed TestNG plugin into our Eclipse. It will help us to execute TestNG tests or suites using Eclipse. Now let's move ahead one step and we learn how to write our first TestNG test case in simple steps.

Writing First TestNG Test in Eclipse


Before creating our first TestNG test, we have to create the first Java project into Eclipse to add TestNG test classes.
Java Project 
A Java project is a place that contains Java source codes and related files to compile our program. It can b used to maintain proper management of source code and related files. Let's create a Java project into Eclipse. If you already know to create Java project in Eclipse, you can skip this section.

How to Create Java Project in Eclipse?


To create a Java project in eclipse, perform the following simple steps.
Step 1: Open Eclipse and go to File > New > Other. A dialog window with multiple actions will be displayed.

Step 2: Select Java project in Eclipse as shown in the following screenshot and click on Next.
How to Write First TestNG Test Case in Simple Steps
Step 3: A dialog window will be opened to fill the project name. Enter "FirstTestNGProject" as the project name and then press the Finish button to create a project.
How to Write First TestNG Test Case in Simple Steps
This will create a new Java project in Eclipse.
Step 4: Now go to Project > Properties. Select Java Build Path on the left-hand side on Properties dialog window as shown in following below screenshot. This will show the build path for newly created project.
How to Write First TestNG Test Case in Simple Steps
Step 5: Click on Libraries Tab and then click on Add Library option.
Step 6: Now Select TestNG on Add Library dialog window as shown in below screenshot and click on Next button.
How to Write First TestNG Test Case in Simple Steps
Step 7: Click on Finish on the next dialog window. This will set the default TestNG library to this project. You will notice that TestNG is included on the library list.

Step 8: Now add the JAR files that contain the Selenium API. We downloaded these files from http://docs.seleniumhq.org/download/ when we were installing Selenium and Eclipse in the previous chapters. Click on Add External JARS... and then navigate to that place where you have placed Selenium JAR files. After adding the external JARs, your screen will look like this.
How to Write First TestNG Test Case in Simple Steps
Step 9: Click on Finish button and verify that your FirstTestNGProject is visible on Eclipse's Package Explorer window.
How to Write First TestNG Test Case in Simple Steps
We have successfully created a new Java project in Eclipse and added Selenium API and TestNG library to the build path of project. Now we can move forward and can create our first TestNG test class for this newly created Java project.

Creating First TestNG Test Case


Perform the following important steps to create the first TestNG test case.
Step 1: Right-click on the "src" package folder and select New > Other. It will open a new wizard window in eclipse.
Step 2: Select TestNG class option from the wizard window and click on Next as shown in below screenshot.
How to Write First TestNG Test Case in Simple Steps

Step 3: Now click on the browse button on the next window and select Java project where you need to add your class. You can see in the below image. Click on the Ok button.
How to Write First TestNG Test Case in Simple Steps
Step 4: Enter package name and test class name indicated below on the appropriate input box as shown in below screenshot and click on Finish.
How to Write First TestNG Test Case in Simple Steps
 As you can see in the above screenshot, the window is also giving you an option to select different annotations while creating a new TestNG class. If you select them, the plugin will declare dummy methods with these annotations while creating the class.



Eclipse will automatically generate a template for TestNG class as shown below.
How to Write First TestNG Test Case in Simple Steps

Coding Your First Test Case


Let's create our first Test Case in which we will get the title of Google home page as output. Write the following code to your newly created test case as shown below step by step.
Program source code 1:
    package firstTestNGProject; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; public class FirstTest { // Create WebDriver reference. public WebDriver driver; @Test public void homePage() { // Create an object of FirefoxDriver class to open Firefox browser. driver=new FirefoxDriver(); System.out.println("Launching Firefox browser"); // Declare URl of google home page with data type string. String URL="https://www.google.com"; // Creating string literal. // Call get() method and pass URL as a parameter. This method is used to open the URL of web application in the browser. It will wait until the whole page gets loaded. driver.get(URL); // Call getTitle() method to get the title of home page. Since this method will return the title of homepage as a string. That's why we will store it using a variable getTitle of type String. String getTitle=driver.getTitle(); System.out.println("Title of home page: " +getTitle); // Call close() method to close browser. driver.close(); } }
Key points:
Now notice the following key points in the above TestNG test case.
1. TestNG does not require a main() method.
2. Methods need not be static.
3. A class named FirstTest has a test method, denoted by the TestNG annotation @Test mentioned before the homePage() function.

4. When the test method will be executed, the following actions will be performed by code.
a. Firefox browser will be launch.
b. The homepage of Google will open.
c. We will get the output as the title of the homepage.
d. At last, the browser will be closed.
5. Since we used annotations in the above test case, we needed to import the package org.testng.annotations.*.

Running First Test Case


In this section, we will learn how to run a newly created test case through Eclipse.
To run the test, simply select test class "FirstTest.java" in the package of src and then right-click on it, selecting Run as from the menu, and choosing TestNG test. Eclipse will provide two results for your test execution, one in the Console window and the other on the TestNG Results window.
Output on Console Window:
    Output: Launching Firefox browser Title of home page: Google PASSED: homePage =============================================== Default test Tests run: 1, Failures: 0, Skips: 0 ===============================================
TestNG Result Window:
How to Write First TestNG Test Case in Simple Steps

Steps to Generate HTML Reports


TestNG has the ability to generate HTML reports in a readable format.
Step 1: In the previous section, we have successfully run our test case. Now we will generate reports in HTML format.  For this, go to Project Explorer window on the left side and right-click on the project name (FirstTestNGProject) in the Project Explorer window and then click on the "Refresh" option.
How to Write First TestNG Test Case in Simple Steps
Step 2: Notice that a "test-output" folder has been created in the Project folder. Expand it and look for an index.html file as shown in the screenshot. This HTML  report file is the results of the most recent test run.
How to Write First TestNG Test Case in Simple Steps
Step 3: Double-click on that index.html file to open it within the built-in web browser of Eclipse. You can also refresh this page any time by simply pressing F5 just like in an ordinary web browser when you want to rerun your test. 
How to Write First TestNG Test Case in Simple Steps
Final words 
This tutorial has covered all the important steps related to how to write first TestNG test case with screenshots. I hope that you will have successfully created a test case using TestNG and enjoyed this tutorial.
Thanks for reading!