Showing posts with label TestNG Reporter Logger. Skipping a Test Case. Show all posts
Showing posts with label TestNG Reporter Logger. Skipping a Test Case. 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.
 

TestNG Prioritizing/Sequencing and Skipping a Test Case

In TestNG "Priority" is used to schedule the test cases. When there are multiple test cases, we want to execute test cases in order. Like First we need to execute a test case "Registration" before login.
In order to achive, we use need to add annotation as @Test(priority=??).

If you don't mention the priority, it will take all the test cases as "priority=0" and execute.
If we define priority as "priority=", these test cases will get executed only when all the test cases which don't have any priority as the default priority will be set to "priority=0"

Let's create a new TestNG class 'MultipleTestCases' without setting Priority and execute it without setting.
Note - By default, methods annotated by @Test are executed alphabetically. Take a look over the next topic to see how to prioritize @Test.

package automationFramework;

import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;

public class MultipleTestCases {
    public WebDriver sDriver;
  @Test
  public void testOne() {
      System.out.println("This is test One");
  }
  @Test
  public void testTwo() {
      System.out.println("This is test Two");
  }
  @Test
  public void testThree(){
      System.out.println("This is test Three");
  }
  @Test
  public void testFour(){
      System.out.println("This is test four");
  }
}


Update TestNG.xml and execute the test.

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


The output will be like this -


So like I mentioned above, Without setting priority Test cases are executed in alphabetical order.
Now let's set Priority in the above example and run the test again.

 package automationFramework;

import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;

public class MultipleTestCases {
    public WebDriver sDriver;
  
  @Test(priority = 0)
  public void testOne() {
      System.out.println("This is test One");
  }
  @Test(priority = 1)
  public void testTwo() {
      System.out.println("This is test Two");
  }
  @Test(priority=2)
  public void testThree(){
      System.out.println("This is test Three");
  }
  @Test(priority = 3)
  public void testFour(){
      System.out.println("This is test four");
  }
}





Skipping Test Case

Sometimes, it happens that our code is not ready and the test case written to test that method/code fails. In such cases, annotation @Test(enabled = false) helps to disable this test case.
If a test method is annotated with @Test(enabled = false), then the test case that is not ready to test is bypassed.
Now, let's see @Test(enabled = false) in action.

To use two or more parameters in a single annotation, separate them with a comma:
@Test(priority = 3, enabled = false)

Let's continue to use 'MutipleTestCases' class and SKIP testcase 1 and testcase 2 and see the output.

package automationFramework;

import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;

public class MultipleTestCases {
    public WebDriver sDriver;
   
  @Test(priority = 0)
  public void testOne() {
      System.out.println("This is test One");
  }
  @Test(priority = 1, enabled = false)
  public void testTwo() {
      System.out.println("This is test Two");
  }
  @Test(priority=2, enabled = false)
  public void testThree(){
      System.out.println("This is test Three");
  }
  @Test(priority = 3)
  public void testFour(){
      System.out.println("This is test four");
  }
}


 And the output is -