Sunday, September 22, 2019

Post#108.Create First TestNG Script And execute


Create and execute Your First TestNG Program:
Before moving forward make Sure TestNG and Eclipse is ready in your System. In my previous post I have explained How to install TestNG. Now we have done with all the basic setup to get started with the test script creation using TestNG. In this post I will discuss How to create a sample script using TestNG.
Before creating TestNG script let’s understand how TestNG script is differ from normal java program and what are the benefits of using TestNG.

Difference between Java Program and TestNG Script
When we execute Java program and TestNG script then functionality wise nothing will change because our script is performing the same functionality but using TestNG you will get some additional functionality.
Basic Java Program
public class TestJava {
    public static void main(String[] args) {
               System.out.println("Hello java");
           }
Basic TestNG script
import org.testng.annotations.Test;
public class TestNGScript {
    @Test
    public  void appTest() {
        System.out.println("Hello Selenium");
}
}

To execute TestNG script we don’t have to write the separate class. We can use simple java class but here we will not write public static void main(String []args) because we are not going to execute this from JVM.
TestNG works with Annotations and annotation can be represented by @ symbol
@Test is the main annotation from where TestRunner will start execution.
In other words, you can say @Test in entry point of the program.

Some benefit of TestNG Script
1- For every single test case you will get 3 reports. These reports generated by TestNG
2-You can check execution time i.e. How much time test case has taken
3-
Parallel execution  etc
Creation of First TestNG Script
Let us begin with the creation of TestNG Script in eclipse IDE.

Step-1: Create New Project and Package
First of all, Create a new java project in your eclipse with name = "TestNGDemo" and create package "TestNGPack1" under your project. 


Step 2: Add TestNG Library


For adding TestNG library,
Go to your project's Properties -> Java Build Path -> Libraries Tab.
Click on Add Library button -> Select TestNG from Add Library popup and then click on Next and Finish buttons.


It will add TestNg library in your project as shown in bellow image. Now click on Apply then OK button to close that window.


Step 3 : Create TestNG Class
To add TestNg class

Right click on package " TestNGPack1"  -> New -> Other. It will open New wizard window as bellow.


Select TestNg from New wizard window and click on Next button.
On next screen, add class name = Program1


It will add new class “Program1” under package “TestNGPack1” as shown in bellow given image.

Step 4: Add Selenium jar files in your project.

To Run WebDriver test, you need to add Selenium jar files in your project. 
(Note: Do not add junit jar file as external jar file. it’s not required in TestNG framework).
That’s all. Now you are ready to write your WebDriver test script inside your class.

Step 5: Add/write sample WebDriver test script.

Write your own test script or add bellow given script in your class file.

package TestNGPack1;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class Program1 {
              WebDriver driver = new FirefoxDriver();
               
               //@BeforeMethod defines this method has to run before every @Test methods
               @BeforeMethod
               public void openbrowser() {
                driver.manage().window().maximize();
                driver.get("http://www.google.com/");
               }

               //@AfterMethod defines this method has to run after every @Test methods
               @AfterMethod
               public void closebrowser() {
                System.out.print("\nBrowser close");
                driver.quit();
               }
               
               @Test
               public void appTest() {
                 String title = driver.getTitle();
                 System.out.print("Current page title is : "+title);
                 System.out.print("\n'DemoTestNG -> TestNGPack1 -> Program1 -> 'appTest' has been executed successfully");
               }
}
Step 6: Run WebDriver test script with TestNG

Right Click on “Program1” Class -> Run As -> And Click on TestNG Test as shown in bellow given image.


It will run your WebDriver test with TestNG. When execution completed, you will see result as shown in bellow.

Console Output:

[TestNG] Running:
  C:\Users\Sujoy\AppData\Local\Temp\testng-eclipse-1433837431\testng-customsuite.xml

Current page title is : Google
'DemoTestNG -> TestNGPack1 -> Program1 -> 'appTest' has been executed successfully
Browser close[Utils] Attempting to create D:\WorkSpace\DemoTestNG\test-output\Default suite\Default test.xml
[Utils]   Directory D:\WorkSpace\DemoTestNG\test-output\Default suite exists: true
PASSED: appTest

===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 0
===============================================


Step 7: View test execution HTML report generated by TestNG

HTML Reports

TestNG comes with a great capability of generating user readable and comprehensible HTML reports for the test executions. These reports can be viewed in any of the browser and it can also be viewed using Eclipse’s build –in browser support.
To generate the HTML report, follow the below steps:

Step 1: Execute the newly created TestNG class. Refresh the project containing the TestNG class by right clicking on it and selecting “Refresh” option.

Step 2: A folder named as “test-output” shall be generated in the project at the “src” folder level. Expand the “test-output” folder and open on the “emailable-report.html” file with the Eclipse browser. The HTML file displays the result of the recent execution.


Step 3: The HTML report shall be opened with in the eclipse environment. Refer the below image for the same.


Refresh the page to see the results for fresh executions if any.

No comments:

Post a Comment