Showing posts with label Log4j & Reporter. Show all posts
Showing posts with label Log4j & Reporter. Show all posts

Wednesday, October 5, 2016

Selenium Logging using Log4j and TestNG Reporter Logger

I am going to show couple of ways to perform logging using Log4j as well as TestNG Reporter.

Let's start with create new TestNG class 'LoggingDemo'. I am just writing single test to open amazon.com website and clicking on Account link.

Install log4j from http://logging.apache.org/log4j/2.x/index.html. I am using log4j 2 in this example.
After the download is complete, Add Log4j jar libraries to eclipse project just like we added selenium jars.


Initialize log4j and TestNG reporter log and I am going to use both in this class.

package automationFramework;

import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import org.apache.commons.logging.Log;
import org.apache.log4j.xml.DOMConfigurator;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.safari.SafariDriver;
import org.testng.Reporter;
import org.testng.annotations.Test;

public class LoggingDemo {
    private static WebDriver sDriver = new SafariDriver();
    private static Logger Log = Logger.getLogger(Log.class.getName());
   
  @Test
  public static void testOne() throws InterruptedException {
     
      Log.info("New driver has been initiated");
      DOMConfigurator.configure("log4j.xml");
      sDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      Log.info("Implicit wait applied on the driver for 10 seconds");
      sDriver.get("http://amazon.com");
      Log.info("Web application launched");
     
      // Let's set TestNG reporter log now.
      Reporter.log("Application Lauched successfully....");
      sDriver.findElement(By.id("nav-link-yourAccount")).click();
      Log.info("Click action performed on My Account link");
      Thread.sleep(1000);
      sDriver.quit();
      Log.info("Browser closed");
     
      // This is the third main event
      Reporter.log("User is Logged out and Application is closed...");
  }
}


Log4j output will look like this in console -

Oct 05, 2016 11:54:57 AM automationFramework.LoggingDemo testOne
INFO: New driver has been initiated
Oct 05, 2016 11:54:57 AM automationFramework.LoggingDemo testOne
INFO: Implicit wait applied on the driver for 10 seconds
Oct 05, 2016 11:54:59 AM automationFramework.LoggingDemo testOne
INFO: Web application launched
Oct 05, 2016 11:54:59 AM automationFramework.LoggingDemo testOne
INFO: Click action performed on My Account link
Oct 05, 2016 11:55:00 AM automationFramework.LoggingDemo testOne
INFO: Browser closed

 
Now, Let's see TestNG Reporter log by going to output folder and click on 'Reporter output'


Log4J is low level logging whereas TestNG Reporter logger is high level logging.
 

Tuesday, October 4, 2016

Annotations in Selenium TestNG, Test Case Grouping and Dependent Test Case

In this blog, I am going to focus on the importance of different types of annotations and their usage.


Before writing test scripts or setting up a project, we should know the hierarchy in which the annotations work. The execution will always remain the same.


For example, compile and run the below script and notice the execution order. It will be as following:
  • BeforeSuite
  • BeforeTest
  • BeforeClass
  • BeforeMethod
  • Test Case 1
  • AfterMethod
  • BeforeMethod
  • Test Case 2
  • AfterMethod
  • AfterClass
  • AfterTest
  • AfterSuite
Let's consider below example -

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class Sequencing {
@Test
public void testCase1() {
System.out.println("This is the Test Case 1");
}
@Test
public void testCase2() {
System.out.println("This is the Test Case 2");
}
@BeforeMethod
public void beforeMethod() {
System.out.println("This will execute before every Method");
}
@AfterMethod
public void afterMethod() {
System.out.println("This will execute after every Method");
}
@BeforeClass
public void beforeClass() {
System.out.println("This will execute before the Class");
}
@AfterClass
public void afterClass() {
System.out.println("This will execute after the Class");
}
@BeforeTest
public void beforeTest() {
System.out.println("This will execute before the Test");
}
@AfterTest
public void afterTest() {
System.out.println("This will execute after the Test");
}
@BeforeSuite
public void beforeSuite() {
System.out.println("This will execute before the Test Suite");
}
@AfterSuite
public void afterSuite() {
System.out.println("This will execute after the Test Suite");
}
}
Output of the above code will be like this:


It is clearly visible that the @Suite annotation is the very first and the very lastly executed. Then @Test followed by @Class. Now if you notice, the @Method has executed twice. As @Test is a method in the class, hence @Method will always executed for each @Test method.

Test Case Grouping in TestNG

Group test is a new innovative feature in TestNG, which doesn’t exist in JUnit framework. It permits you to dispatch methods into proper portions and perform sophisticated groupings of test methods. Not only can you declare those methods that belong to groups, but you can also specify groups that contain other groups. Then, TestNG can be invoked and asked to include a certain set of groups (or regular expressions), while excluding another set. Group tests provide maximum flexibility in how you partition your tests and doesn't require you to recompile anything if you want to run two different sets of tests back to back.

Groups are specified in your testng.xml file using the <groups> tag. It can be found either under the <test> or <suite> tag. Groups specified in the <suite> tag apply to all the <test> tags underneath.

Now, let's take an example to see how group test works.

Create a new TestNG class 'ModuleASuite'. Then create 4 test methods, group tests as 'Regression' and one as 'Smoke Test'. See example below.

package com.example.group;
import org.testng.annotations.Test;
public class groupExamples {
 @Test(groups="Regression")
 public void testCaseOne()
 {
 System.out.println("Im in testCaseOne - And in Regression Group");
 }
 @Test(groups="Regression")
 public void testCaseTwo(){
 System.out.println("Im in testCaseTwo - And in Regression Group");
 }
 @Test(groups="Smoke Test")
 public void testCaseThree(){
 System.out.println("Im in testCaseThree - And in Smoke Test Group");
 }
 @Test(groups="Regression")
 public void testCaseFour(){
 System.out.println("Im in testCaseFour - And in Regression Group");
 }
}

 Now, Update the TestNG.xml file. We will execute the group “Regression” which will execute the test methods which are defined with group as “Regression”

<suite name = "Test - Suite">
    <test name="website QA">
        <groups>
            <run>
            <include name="Regression"/>
            </run>
        </groups>
        <classes>
            <class name="automationFramework.ModuleASuite" />
        </classes>
    </test>
</suite>


Run TestNG.xml and the output will be like this -







Dependent Test Case


Sometimes, you may need to invoke methods in a Test case in a particular order or you want to share some data and state between methods. This kind of dependency is supported by TestNG as it supports the declaration of explicit dependencies between test methods.
TestNG allows you to specify dependencies either with:
  • Using attributes dependsOnMethods in @Test annotations OR
  • Using attributes dependsOnGroups in @Test annotations.
Take a look over the below example:

package automationFramework;

import org.testng.annotations.Test;

public class ModuleBSuite {

      @Test (dependsOnMethods = { "OpenBrowser" })
      public void SignIn() {
          System.out.println("This will execute second (SignIn)");
      }

      @Test
      public void OpenBrowser() {
          System.out.println("This will execute first (Open Browser)");
      }

      @Test (dependsOnMethods = { "SignIn" })
      public void LogOut() {
          System.out.println("This will execute third (Log Out)");
      }
}

Update and run TestNG and the output will be like this - 

<suite name = "Test - Suite">
    <test name="website QA">
        <classes>
            <class name="automationFramework.ModuleBSuite" />
        </classes>
    </test>
</suite>





 

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.