Tuesday, October 4, 2016

TestNG with WebDriver Introduction and setup

TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionalities that make it more powerful and easier to use

Features of TestNG

  • Support for annotations
  • Support for parameterization
  • Advance execution methodology that do not require test suites to be created
  • Support for Data Driven Testing using Dataproviders
  • Enables user to set execution priorities for the test methods
  • Supports threat safe environment when executing multiple threads
  • Readily supports integration with various tools and plug-ins like build tools (Ant, Maven etc.), Integrated Development Environment (Eclipse).
  • Facilitates user with effective means of Report Generation using ReportNG.

TestNG is designed to cover all categories of tests:  unit, functional, end-to-end, integration, etc.

TestNG Plugin Installation in Eclipse
  1. Launch eclipse IDE -> Click on the Help option within the menu -> Select “Eclipse Marketplace” option within the dropdown.


  2. Enter the keyword “TestNG” in the search textbox and click on “Go” button as shown below. 
  3. Click on the “Go” button and click on the Install button to install TestNG.
  4. Click on “Confirm” button. Next, the application would prompt you to accept the license and then click on the “Finish” button.
  5. The installation is initiated now and the progress can be seen as following:
 Restart Eclipse. Next, Verify that TestNG appears in eclipse Preference. Click on Eclipse menu tab -> Preferences. TestNG should be visible in the Preference.




Annotations in TestNG

@BeforeSuite: The annotated method will be run before all tests in this suite have run.
@AfterSuite: The annotated method will be run after all tests in this suite have run.
@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the tag is run.
@AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the tag have run.
@BeforeGroups: The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked.
@AfterGroups: The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked.
@BeforeClass: The annotated method will be run before the first test method in the current class is invoked.
@AfterClass: The annotated method will be run after all the test methods in the current class have been run.
@BeforeMethod: The annotated method will be run before each test method.
@AfterMethod: The annotated method will be run after each test method.
@Test: The annotated method is a part of a test case.

Now, Let's create first TestNG class. 


Right click on src and click on TestNG -> Create TestNG class


Fill out Package name and Class name along with select couple of annotations. In this example i am using @BeforeMethod and @AfterMethod and rewriting out FirstTestCase.





Let’s take an example of FirstTestCase and divide the test case in to three parts .
@BeforeMethod : Launch Safari and direct it to the Base URL
@Test : Find Page title and print console message
@AfterMethod : Close Safari browser

package automationFramework;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.safari.SafariDriver;
import org.testng.annotations.AfterMethod;

public class TestNG {
    public WebDriver sDriver;
   
  @Test
  public void main() {
      String pageTitle = sDriver.getTitle().toString();
      System.out.println(pageTitle);
  }
  @BeforeMethod
  public void beforeMethod() {

      // Create a new instance of the Safari driver
      sDriver = new SafariDriver();
     
      //Put a Implicit wait, this means that any search for elements on the page could take the time the implicit wait is set for before throwing exception
      sDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
     
      String webSiteURL = "https://www.amazon.com/";
      //Launch the website
      sDriver.get(webSiteURL);
  }

  @AfterMethod
  public void afterMethod() {
      // Close the driver
      sDriver.close();
  }
}


Run the test by right click on the test case script and select Run As > TestNG Test.

TestNG produces reports in various formats.


  



Open project folder -> test-output and view index.html, emailable-report.html. Also view Default test.html inside Default suite folder.






No comments:

Post a Comment