Showing posts with label TestNG WebDriver. Show all posts
Showing posts with label TestNG WebDriver. 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.

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 -


Tuesday, October 4, 2016

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: