Thursday, April 18, 2019

How to Use TestNG Framework for Creating Selenium Scripts | Selenium Test Case with TestNG Framework | TestNG Tutorials

In this tutorial, we will learn how to write a test case using the TestNG framework and generate TestNG report.


Topics to be covered?


  1. Use of @BeforeMethod, @Test and @AfterMethod annotation for writing a test case.
  2. Use of 'Priority' keyword.
  3. Generate TestNG report to analyze the result of test case execution.
Note - If are not aware of TestNG and its annotations, you can follow the link - TestNG Introduction







Let us take the example of Facebook application. We have been told to automate two scenarios - 
  1. Open the Facebook application and Print the Login Page title on the Console
  2. Open the Facebook application and Enter Username and Password.
Note  - After validating every scenario the browser page should be closed.

If you observe the above two scenarios, you must be noticing that the first thing which is common is to Open the application and the last thing is to close the browser. We can say these two things are our pre-requisites which must be handled before validating our test scenarios.

Below we will see how to handle such scenarios with the help of TestNG annotations:

  • @BeforeMethod - Method under this annotation will execute before the execution of each @test annotation.
Note -This annotation basically covers the initial pre-requisite part of a test case. In our case Open the application.

  • @Test - Defines a particular functionality of the application. We write our test case under the method of this annotation.
Note - In our case, the above scenario will be covered under this annotation.

  • @AfterMethod - Method under this annotation will execute after the execution of each @test annotation.
Note - It will cover the action that will be performed after the execution of every test case. In our case, closing the Browser Page.


I hope you have understood under which situation we need to use the above three annotations.

Now we will see how to automate the above two scenarios with the help of a program.

One more important thing to note is that we don't need to use the main() method if you are using TestNG annotations. The call of the main() method is handled by the annotations of the TestNg automatically.






 package com.testng;  
   
 import java.util.concurrent.TimeUnit;  
   
 import org.openqa.selenium.By;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.chrome.ChromeDriver;  
 import org.testng.annotations.AfterMethod;  
 import org.testng.annotations.BeforeMethod;  
 import org.testng.annotations.Test;  
   
 public class FirstTestNGScript {  
        
      WebDriver driver;// declaring driver as public.  
        
      @BeforeMethod  
      public void LaunchApplication()  
      {  
           System.setProperty("webdriver.chrome.driver", "F:\\software\\chromedriver_win32\\chromedriver.exe");  
           driver = new ChromeDriver();  
           driver.manage().window().maximize();  
           driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);  
           driver.get("https://www.facebook.com/");//launching the application       
      }  
        
        
      //Test case 1  
      @Test  
      public void printLoginPageTitle()  
      {  
           String Loginpagetitle = driver.getCurrentUrl();  
           System.out.println(Loginpagetitle);//Printing the title on the console  
      }  
        
      //Test Case 2  
      @Test  
      public void enterUsernamePassword()  
      {  
           //Enter Username and Password  
           driver.findElement(By.xpath("//input[@id='email']")).sendKeys("xyz@gmail.com");  
           driver.findElement(By.xpath("//input[@id='pass']")).sendKeys("**********");  
      }  
        
        
        
      @AfterMethod  
      public void closeBrowserPage()  
      {  
           driver.close();//Closing the browser page  
      }  
        
        
        
   
 }  
   



Order of above test cases execution:

  1. FB application will be launched.
  2. 2nd Test Case will be executed.
  3. Close of browser.
  4. Again FB application will be launched.
  5. 1st Test Case will be executed.
  6. Close of browser.


You must have thinking that why the 2nd test will be executed before the 1st one?


  • The answer is that TestNG follows the execution order of test cases based on the name of the methods written under @Test annotations.
  • It follows the alphabetical order of the methods name.
  • In our case, 'e' in enterUsernamePassword() method comes before 'p' in printLoginPageTitle() method based on alphabetical order.
  • In case, the first letter is the same in both the methods then the second letter of both the methods will be taken into consideration and so on.

I hope your doubt must be cleared.


What is the workaround, if we want to execute the first test case before the second one?

  • Well TestNG provides one keyword - 'Priority'
  • It is used as @Test(priority=0).
  • If we set the priority of a test case as 0, it means the test case will be executed first.
  • Priority keyword follows index = 0,1,2,3....

Now We will use this concept in our previous program to change the execution order.


 package com.testng;  
   
 import java.util.concurrent.TimeUnit;  
   
 import org.openqa.selenium.By;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.chrome.ChromeDriver;  
 import org.testng.annotations.AfterMethod;  
 import org.testng.annotations.BeforeMethod;  
 import org.testng.annotations.Test;  
   
 public class FirstTestNGScript {  
        
      WebDriver driver;// declaring driver as public.  
        
      @BeforeMethod  
      public void LaunchApplication()  
      {  
           System.setProperty("webdriver.chrome.driver", "F:\\software\\chromedriver_win32\\chromedriver.exe");  
           driver = new ChromeDriver();  
           driver.manage().window().maximize();  
           driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);  
           driver.get("https://www.facebook.com/");//launching the application       
      }  
        
        
      //Test case 1  
      @Test(priority=2)  
      public void printLoginPageTitle()  
      {  
           String Loginpagetitle = driver.getCurrentUrl();  
           System.out.println(Loginpagetitle);//Printing the title on the console  
      }  
        
      //Test Case 2  
      @Test(priority=1)  
      public void enterUsernamePassword()  
      {  
           //Enter Username and Password  
           driver.findElement(By.xpath("//input[@id='email']")).sendKeys("xyz@gmail.com");  
           driver.findElement(By.xpath("//input[@id='pass']")).sendKeys("**********");  
      }  
        
        
        
      @AfterMethod  
      public void closeBrowserPage()  
      {  
           driver.close();//Closing the browser page  
      }  
        
        
        
   
 }  
   



Order of above test cases execution:

  1. FB application will be launched.
  2. 1nd Test Case will be executed.
  3. Close of browser.
  4. Again FB application will be launched.
  5. 2st Test Case will be executed.
  6. Close of browser.

Now we will learn how to open TestNG report:

  • First, after the execution of the script, we can see the report on the Eclipse console.





  • Second, We can open the customized HTML TestNG report as explained below - 
--> Refresh the TestNG Project.
--> One 'test-output' folder will be generated.
--> Expand the folder.
--> Locate one 'index.html' file.



--> Open the file. This is the customized TestNG  HTML report.






That's all guys!!! Do comment and ask your doubts if you have any. Thanks.

No comments:

Post a Comment