Showing posts with label webdriver automation. Show all posts
Showing posts with label webdriver automation. 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.

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>





 

My first Selenium code - Automation

Let's create First Automated test case using Selenium Web driver.
Right click on the Java project src folder and create a new class and name it "FirstTestCase".





Copy and paste below code in the newly created class.

public class FirstTestCase {

public static void main(String[] args) {
// Create a new instance of the Firefox driver
WebDriver driver = new FirefoxDriver();
        //Launch the website
driver.get("http://www.amazon.com");

        // Print a Log In message to the screen
        System.out.println("Successfully opened the website");

//Wait for 5 Sec
Thread.sleep(5);
        // Close the driver
        driver.quit();
    }
}


Hover over the red error messages and import browser drivers.

It will add import statement at the top saying ‘import org.openqa.selenium.WebDriver;’

The import statement in Java allows referring to classes that are declared in other packages to be accessed without referring to the full package name.


This will add the statement ‘import org.openqa.selenium. safari.SafariDriver;‘ and the error will be gone.



Once you resolve these error one more error will pop up at the statement ‘Thread.sleep(5);‘,

This time it will ask to select the ‘Add throws declaration‘ and after selecting that it will add ‘throws InterruptedException‘ at the method declaration.

Now, Run the Java test. To start the test just select Run > Run As > Java Application Or Right Click on Eclipse code and Click Run As  > Java Application.


After a few Seconds Safari Browser will open and you will see that with the help of your script, Selenium will Launch the Online Store, display the Successful Message and Close the browser.



Once the execution is finished, you will see the message in Console section of the Eclipse IDE displaying: