Wednesday, 26 November 2014

User defined Generic Library functions in Selenium Web Driver Framework



Utility or User defined Generic Library functions in Selenium Web Driver Framework

Any framework must be reusable, if any functionality needs to be repeatedly used such functions can be written in 1 utility file and called multiple times from any other testcase. They are also called generic functions.

It doesn’t make sense in making the code redundant. A better approach is to create a utility file and keep all reusable functions in one place and use as per requirement.

User defined functions (UDF) are the functions which are created by users as per their own requirement. In order to use a UDF, it is necessary to call the appropriate package file at the beginning of the program.

Benefits of User Defined Functions

·         - Can be used in a number of places without rewriting the code.
·         - Code can be made less complex and easier to write.
·         - Parameters can be passed to the function.
·         - Simpler to invoke.
·         - Cleaner and more understandable code

For example:  It is a three steps process to open a URL. First Instantiate a New driver, second Apply an Implicit wait on the driver and third Navigate to URL.  Browser can be any browser; it can be Mozilla, IE or any. It makes sense to create a function for opening a browser which will accept an argument (Browser Type) and it will open that particular browser. This ‘Browser Type’ argument will be driven from separate config file. To achieve this few more functions are required.

Here instead of writing the entire code of finding element, we can write it in a simpler form using a generic function getObject().

Example code looks like this
driver.findElement(By.xpath("//*[@id='yes']")).click();

We can rewrite it like
getObject("demo_yes_btn").click();

The code is much simpler to understand and as it uses generic function getObject(). Here “demo_yes_btn” is the object created at Object repository.

Code for function getObject() is as below :

public static WebElement getObject(String xpathKey) {
            try{
                  return driver.findElement(By.xpath(TestObjectRepository.getProperty(xpathKey)));
            }catch(Throwable t){
                  // report error
                  takeScreenShot(xpathKey);
                  Assert.assertTrue(t.getMessage(),false);
                 
                  return null;
            }
           
      }

Steps to generic function Utility

    1.       Create a package in your folder structure and name it as “LibraryFunctions”.
    2.       Now create a class under the package called as “LibraryFunc”.
    3.       Now write all the methods, which you think might be re-used in you test cases. Example you could include, the following :
a)      Initialization of browser
b)      Finding objects
c)       Taking screenshots
d)      Handling alerts and pop ups etc


    4.       Now write the code for getObject() given above.
 public static WebElement getObject(String xpathKey) {
            try{
                  return driver.findElement(By.xpath(TestObjectRepository.getProperty(xpathKey)));
            }catch(Throwable t){
                  // report error
                  takeScreenShot(xpathKey);
                  Assert.assertTrue(t.getMessage(),false);
                 
                  return null;
            }
           
      }
    5.       Now if you want to use this getObject() Function in any of the test cases, just import the LibraryFunc java and use  the function inside it.

4.       Now create a test case , which uses the Library function and reuses it.
Sample code :
 package TestPackage;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import LibraryFunctions.LibraryFunc;



public class testlibfunc extends LibraryFunc{

     
@Test
      public void testlib()
      {
     
            driver.get(TestConfig.getProperty("sitename"));
     
           
      getObject("demo_yes_btn").click();
      //used instead of
     // driver.findElement(By.xpath("//*[@id='yes']")).click();

                 
      }


      }


   7.       Similarly , Generic functions can be written for initializing browser, taking screen shots, handling alerts etc.
 

No comments:

Post a Comment