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

Wednesday, October 5, 2016

TestNG Asserts example and explanation

Assertions in TestNG

Asserts helps us to verify the conditions of the test and decide whether test has failed or passed. A test is considered successful ONLY if it is completed without throwing any exception.

There will be many situations in the test where you just like to check the presence of an element. All you need to do is to put an assert statement on to it to verify its existence.

Here we are verifying if the page title is equal to 'Google' or not. If the page title is not matching with the text / title that we provided, it will fail the test case.

Different Asserts Statements
1) Assert.assertTrue() & Assert.assertFalse()

To explain you what is assertion, lets us look into below code sample.
I am going to create new TestNG class 'AssertTest'.

package automationFramework;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.safari.SafariDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

public class AssertTest {
    public static WebDriver sDriver = new SafariDriver();
   
  @Test
  public void testOne() throws InterruptedException {
      sDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      //Open website
      sDriver.get("http://amazon.com");
      //Verify Page title
      WebElement myAccountLink = sDriver.findElement(By.id("nav-link-yourAccount"));
     
      //Adding assertion check here. Test will only continue if below statement is True.
      Assert.assertTrue(myAccountLink.isDisplayed());
    //My Account Link will be clicked only if the above condition is true
      myAccountLink.click();
     
      Thread.sleep(1000);
  }
}


Let's run the code and verify the output.

 Assert.assertFalse() works opposite of Assert.assertTrue(). It means that if you want your test to continue only if when some certain element is not present on the page. You will use Assert false, so it will fail the test in case of the element present on the page.


Like wise there are many Assertions provided by the TestNG. The below are the few which are used commonly.
assertEqual(String actual,String expected) :- It takes two string arguments and checks whether both are equal, if not it will fail the test.

assertEqual(String actual,String expected, String message) :- It takes three string arguments and checks whether both are equal, if not it will fail the test and throws the message which we provide.
assertEquals(boolean actual,boolean expected) :- It takes two boolean arguments and checks whether both are equal, if not it will fail the test.

assertEquals(java.util.Collection actual, java.util.Collection expected, java.lang.String message) :- Takes two collection objects and verifies both collections contain the same elements and with the same order. if not it will fail the test with the given message.

Assert.assertTrue(condition) :- It takes one boolean arguments and checks that a condition is true, If it isn't, an AssertionError is thrown.

Assert.assertTrue(condition, message) :- It takes one boolean argument and String message. It Asserts that a condition is true. If it isn't, an AssertionError, with the given message, is thrown.

Assert.assertFalse(condition) :- It takes one boolean arguments and checks that a condition is false, If it isn't, an AssertionError is thrown.

Assert.assertFalse(condition, message) :- It takes one boolean argument and String message. It Asserts that a condition is false. If it isn't, an AssertionError, with the given message, is thrown.

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 -


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>