Sunday, 8 September 2013

Automation Frameworks in Selenium



Automation Frameworks With Example (Selenium)


There are 5 types of Framework  

1. Linear Framework
2. Library Architecture Framework
3. Keyword driven framework
4. Data Driven Framework
5. Hybrid Framework

Each and Every framework has its own merits and demerits. These all frameworks can be applied to any Automation Tool.
How to choose a particular framework?
There is no thumb rule to choose a framework, but it depends on many factors like Type of Application (AUT), Number of available resources, Reusability of Test Script.
We will see each and every framework with Example.
Example Scenario: User books a flight ticket on Travel portal and logout .

1.       Linear Framework
This is very simple framework and good for beginner.


Merits : Not  much coding expertise  is required .
Demerits : 1.Test case is not reusable .
                   2. Lots of Rework will be required in case of any Change.

Following code is an example of Linear Framework.
package com.project1;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.*;
import org.openqa.selenium.support.ui.Select;

 public class Linear_Framework {
      
        
       public static void main(String[] args) {
              //Initiate Driver
        WebDriver driver = new FirefoxDriver();
        //Login
        driver.navigate().to("http://newtours.demoaut.com/");
        driver.findElement(By.name("userName")).sendKeys("Mercury");
        driver.findElement(By.name("password")).sendKeys("mercury");
        driver.findElement(By.name("login")).click();
         //Book a Ticket
     Select Passangers= new Select(driver.findElement(By.cssSelector("select[name='passCount']")));
        Passangers.selectByVisibleText("2");
        Select Departingfrom = new Select(driver.findElement(By.cssSelector("select[name='fromPort']")));
        Departingfrom.selectByVisibleText("Frankfurt");
        Select FromMonth = new Select(driver.findElement(By.cssSelector("select[name='fromMonth']")));
        FromMonth.selectByVisibleText("September");
        Select ArrivingIn = new Select(driver.findElement(By.cssSelector("select[name='toPort']")));
        ArrivingIn.selectByVisibleText("New York");
        Select ToMonth = new Select(driver.findElement(By.cssSelector("select[name='toMonth']")));
        ToMonth.selectByIndex(10);
        driver.findElement(By.xpath("/html/body/div/table/tbody/tr/td[2]/table/tbody/tr[4]/td/table/tbody/tr/td[2]/table/tbody/tr[5]/td/form/table/tbody/tr[9]/td[2]/font/font/input")).click();
        driver.findElement(By.name("findFlights")).click();
        driver.findElement(By.name("reserveFlights")).click();
        driver.findElement(By.name("passFirst0")).sendKeys("Name1");
        driver.findElement(By.name("passLast0")).sendKeys("LastName");
        driver.findElement(By.name("creditnumber")).sendKeys("1234566");
        driver.findElement(By.name("buyFlights")).click();
        
      
      
         //Logout
        driver.findElement(By.linkText("SIGN-OFF")).click();
        
        
     //Close Browser
        driver.quit();
        
        }

}


2.        Library Architecture Framework
This  is a approach where all test cases are analysed to  find out reusability and these test cases are created as functions and will be stored in other files and can be called whenever needed ,In Short  Test  case will be fragmented into functions .
Merits: It will save time and Effort due to reusability.
Demerits: 1.Programming Knowledge is required.
                   2. Data is hardcoded, so Test script cannot be run using different data values.

Following code is an example of Library Architecture, In Main () only functions are present.
package com.project1;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.*;
import org.openqa.selenium.support.ui.Select;

 public class Library_Framework {
      
       //Initiate Driver
               static WebDriver driver = new FirefoxDriver();
       public static void main(String[] args)
       {
              //Login
       Login("Mercury", "mercury");
       FlightBook();
       Logout();
      
       }
           //Function
       public static void Login(String Username,String Password)
       {
               driver.navigate().to("http://newtours.demoaut.com/");
               driver.findElement(By.name("userName")).sendKeys(Username);
               driver.findElement(By.name("password")).sendKeys(Password);
               driver.findElement(By.name("login")).click();
       }
       public static void FlightBook()
       {
               Select Passangers= new Select(driver.findElement(By.cssSelector("select[name='passCount']")));
               Passangers.selectByVisibleText("2");
               Select Departingfrom = new Select(driver.findElement(By.cssSelector("select[name='fromPort']")));
               Departingfrom.selectByVisibleText("Frankfurt");
               Select FromMonth = new Select(driver.findElement(By.cssSelector("select[name='fromMonth']")));
               FromMonth.selectByVisibleText("September");
               Select ArrivingIn = new Select(driver.findElement(By.cssSelector("select[name='toPort']")));
               ArrivingIn.selectByVisibleText("New York");
               Select ToMonth = new Select(driver.findElement(By.cssSelector("select[name='toMonth']")));
               ToMonth.selectByIndex(10);
               driver.findElement(By.xpath("/html/body/div/table/tbody/tr/td[2]/table/tbody/tr[4]/td/table/tbody/tr/td[2]/table/tbody/tr[5]/td/form/table/tbody/tr[9]/td[2]/font/font/input")).click();
               driver.findElement(By.name("findFlights")).click();
               driver.findElement(By.name("reserveFlights")).click();
               driver.findElement(By.name("passFirst0")).sendKeys("Name1");
               driver.findElement(By.name("passLast0")).sendKeys("LastName");
               driver.findElement(By.name("creditnumber")).sendKeys("1234566");
               driver.findElement(By.name("buyFlights")).click();
               
       }
public static void Logout()
{
        driver.findElement(By.linkText("SIGN-OFF")).click();
}

public static void quitdriver()
{
        driver.quit();
}

}
3.        Keyword driven Framework

There are 2 types of keyword driven framework
1. Operation level
2. Function level

  Merits:  1.Keyword Driven Framework is very Generic so it can be used to other Test  cases     
                  as well
  Demerits : It requires lots of efforts and expertise to prepare complex  Libraries

Here I have prepared a code for Function Level Keyword driven Framework.
 Steps:

1.       Create an Excel sheet for Test case Flow.


                2.  Create individual functions for test cases (Login(),FlightBook(),Logout())
                3.   Create a function Which Can read Excel sheet and can execute mapped function.


       By following above steps,  final code should look like as below.

       package com.project1;

import java.io.File;
import java.io.IOException;
import java.util.List;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.*;
import org.openqa.selenium.support.ui.Select;

public class Keyword_Driven {
      
       //Initiate Driver
        static WebDriver driver = new FirefoxDriver();
public static void main(String[] args)
{
      
        //Login
        driver.navigate().to("http://newtours.demoaut.com/");
        
      
              Workbook workbook;
              try {
                     workbook = Workbook.getWorkbook(new File("C:\\Users\\Public\\Documents\\Test Excel\\Keyword_driven.xls"));
                     Sheet sheet = workbook.getSheet(0);
                    
              //code to read Test Case
                     int row=sheet.getRows();
                     for(int i=1;i<row;i++)
                     {
                           String Testcasename=sheet.getCell(0,i).getContents().toString();
                           //Call function ExecuteFunction to Read Mapped Function
                           ExecuteFunction(Testcasename);
                          
                    
                     }
                     //Close Browser
                     quitdriver();
                    
              } catch (BiffException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
              } catch (IOException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
              }
             
}
public static void ExecuteFunction(String Casename)
{
       String Option=Casename.trim();
      
 if(Option.equalsIgnoreCase("TC_Login") )
 {
      
        //Execute Login Function
        Login("Mercury", "mercury");
 }
 else if(Option.equalsIgnoreCase("TC_Book Flight"))
 {
        //Execute FlighBook Function
        FlightBook();
 }
 else if(Option.equalsIgnoreCase("TC_Logout"))
 {
       //Execute Logout
        Logout();
 }
 }
public static void Login(String Username,String Password)
{
        driver.findElement(By.name("userName")).sendKeys(Username);
        driver.findElement(By.name("password")).sendKeys(Password);
        driver.findElement(By.name("login")).click();
}
public static void FlightBook()
{
        Select Passangers= new Select(driver.findElement(By.cssSelector("select[name='passCount']")));
        Passangers.selectByVisibleText("2");
        Select Departingfrom = new Select(driver.findElement(By.cssSelector("select[name='fromPort']")));
        Departingfrom.selectByVisibleText("Frankfurt");
        Select FromMonth = new Select(driver.findElement(By.cssSelector("select[name='fromMonth']")));
        FromMonth.selectByVisibleText("September");
        Select ArrivingIn = new Select(driver.findElement(By.cssSelector("select[name='toPort']")));
        ArrivingIn.selectByVisibleText("New York");
        Select ToMonth = new Select(driver.findElement(By.cssSelector("select[name='toMonth']")));
        ToMonth.selectByIndex(10);
        driver.findElement(By.xpath("/html/body/div/table/tbody/tr/td[2]/table/tbody/tr[4]/td/table/tbody/tr/td[2]/table/tbody/tr[5]/td/form/table/tbody/tr[9]/td[2]/font/font/input")).click();
        driver.findElement(By.name("findFlights")).click();
        driver.findElement(By.name("reserveFlights")).click();
        driver.findElement(By.name("passFirst0")).sendKeys("Name1");
        driver.findElement(By.name("passLast0")).sendKeys("LastName");
        driver.findElement(By.name("creditnumber")).sendKeys("1234566");
        driver.findElement(By.name("buyFlights")).click();
        
}
public static void Logout()
{
 driver.findElement(By.linkText("SIGN-OFF")).click();
}

public static void quitdriver()
{
 driver.quit();
}

}

For Operation level framework different keywords are written in excel sheet and can be read using Function .this is quite complex.
Excel sheet can be as follows:



4.       Data-Driven  framework

In this type of framework Data is not hardcoded with script ,but data is provided from external  source.
This external source can be Excel sheet, TXT file, Database etc.

Merits:     As data is provided from external source same script can be run for different datasets.
Demerits:  Good efforts and technical expertise is required to create functions  which connects to        
                     Database
Following code is an example of Data-driven Framework
package com.project1;

import java.io.File;
import java.io.IOException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.*;
import org.openqa.selenium.support.ui.Select;

import jxl.*;
import jxl.read.biff.BiffException;

public class DataDriven_Framework {
        public static void main(String[] args) {
               WebDriver driver = new FirefoxDriver();
               //Login
               driver.navigate().to("http://newtours.demoaut.com/");
               
               try {
                     Workbook workbook = Workbook.getWorkbook(new File("C:\\Users\\Public\\Documents\\Test Excel\\Excel1.xls"));
                     Sheet sheet = workbook.getSheet(0);
                    
                     driver.findElement(By.name("userName")).sendKeys(sheet.getCell(0, 1).getContents());
                     driver.findElement(By.name("password")).sendKeys(sheet.getCell(1, 1).getContents());
                     driver.findElement(By.name("login")).click();
                     Select Passangers= new Select(driver.findElement(By.cssSelector("select[name='passCount']")));
                     Passangers.selectByVisibleText(sheet.getCell(2, 1).getContents());
                     Select Departingfrom = new Select(driver.findElement(By.cssSelector("select[name='fromPort']")));
                      Departingfrom.selectByVisibleText(sheet.getCell(3, 1).getContents());
                      Select FromMonth = new Select(driver.findElement(By.cssSelector("select[name='fromMonth']")));
                      FromMonth.selectByVisibleText(sheet.getCell(4, 1).getContents());
                      Select ArrivingIn = new Select(driver.findElement(By.cssSelector("select[name='toPort']")));
                      ArrivingIn.selectByVisibleText(sheet.getCell(6,1).getContents());
                      Select ToMonth = new Select(driver.findElement(By.cssSelector("select[name='toMonth']")));
                      ToMonth.selectByVisibleText(sheet.getCell(5,1).getContents());
                      driver.findElement(By.xpath("/html/body/div/table/tbody/tr/td[2]/table/tbody/tr[4]/td/table/tbody/tr/td[2]/table/tbody/tr[5]/td/form/table/tbody/tr[9]/td[2]/font/font/input")).click();
                      driver.findElement(By.name("findFlights")).click();
                      driver.findElement(By.name("reserveFlights")).click();
                      driver.findElement(By.name("passFirst0")).sendKeys(sheet.getCell(7, 1).getContents());
                      driver.findElement(By.name("passLast0")).sendKeys(sheet.getCell(8, 1).getContents());
                      driver.findElement(By.name("creditnumber")).sendKeys(sheet.getCell(9, 1).getContents());
                      driver.findElement(By.name("buyFlights")).click();
                    
                    
                    
              } catch (BiffException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
              } catch (IOException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
              }
        }
        }


5.       Hybrid Frameworks
This is combination of Keyword Driven and Data Driven Framework  ,Both above examples can be used to explain it
Merits: Best features can be used of any type of framework
Demerits: Highly Complex Framework


42 comments:

  1. Hi Friends,

    I have one doubt that is what is modular frame work, anybody know than please replay to here or my mail also...gopalmcajava@gmail.com

    Thanks!
    Gopal

    ReplyDelete
    Replies
    1. @Gopal
      You can refer "Library Architecture Framework" for modular framework ,both are almost same,still definitions differ from organization to organization.but at generic level modular framework is same as "Library Architecture Framework"

      Thanks
      Priyanka

      Delete
    2. Hi Gopal,
      "Linear Framework" is also known as modular framework.
      both are almost same.

      Delete
  2. @ priyanka
    what is the exact difference between keyboard driven and date driven frame work , both are using the excel sheet , i couldnt figure out , can you explain ,by the way blog is very use ful , thanks for your time

    ReplyDelete
    Replies
    1. Thanks Jack ,

      Difference between keyword driven and data driven is as follows :

      1.In data driven we only fetch required data from excelsheet like username ,password


      2.In keyword driven we fetch 'Keywords'from excel sheet like function name,object name etc.
      and after fetching those keywords we need function to implement those keywords in our script


      Thanks
      Priyanka

      Delete
    2. Nice, explanation Priya.

      Useful blog for all automation lovers

      Delete
    3. This comment has been removed by the author.

      Delete
    4. Hi Priyanka your blog is really awesome..i got clear idea about framewoks..and could you explain how to generate HTML report using Junit..Thanks in advance.

      Delete
  3. Hi,

    Can you provide example of hybrid framework here or mail me at

    rakesh.testengr@gmail.com

    Thanks.

    ReplyDelete
  4. Excellent and Very useful blog... Thanks

    ReplyDelete
  5. Thanks Priyanka..its very informative and precise..

    ReplyDelete
  6. Hi Priyanka ,
    Iam a newbie to Automation and i have some questions .

    Lets say i have written the Java Selenium code to test all parts of the website . But based on need, I might sometimes want to run the tests only on few features of this website , or maybe run all the tests . I might also want to run it only on some browsers, maybe all browsers. So how would you design your framework so that you have this flexibility ? Would you have different testClasses for each feature so that a main testclass can then call all or some these testclasses to test the features i want ? Would you then have testSetup and testtearDown on each of these classes ?

    How would you provide the input to the framework as to what versions of the browser you want to test ? Would you just use a properties file ?

    Or would you prefer using a TESTNG/JUnit where you can have features classified as groups and then you can test each group ?

    Thanks for your informative post.

    ReplyDelete
    Replies
    1. Hi Shyam,

      As you are using Java there are lots of options available to fulfill your requirements.

      -To Execute only few test cases out of many, you can use TestNg feature Group execution
      -For this kind of case you don't require teardown method in each test case ,You can start your browser at suite level.
      -It would be better approach to feed browser version information in framework rather than using property file.

      Also I would suggest you to use Jenkins for execution

      Thanks
      Priyanka

      Delete
  7. Hi Priyanka,

    Can you give some doc on Frame work for folder structure, Ex: Where we need to keep class files , property file, Exel file. This will be very helpful for me.

    Thank you

    ReplyDelete
    Replies
    1. Hi Ravi,

      There is no thumb rule for folder structure and you can keep your supporting files according to your convenience.I can suggest following things.

      Common Function file ->Lib folder
      Excel File -> Config folder
      property file -> Config Folder

      Else you can always design your own folder structure.

      Delete
  8. Can some one provide me your valuable suggestions to learn selenium web driver. I am new to automation trying to learn it.

    Thank you guys...

    ReplyDelete
  9. Thanks a lot for sharing the information.
    The below link has examples for automation framework in Java . (Includes folder structure pattern, reporting mechanism etcc..)
    http://seleniumeasy.com

    ReplyDelete
    Replies
    1. Thank you Varma, link which you shared is helpful.

      Delete
  10. Hi Priyanka Gajjar,
    Can you explain the Folder Structure of keyword Driven Framework?
    Ex: Where we need to keep class files , property file, Exel files, Error Reports, Screenshots and new added key words all those things..

    ReplyDelete
  11. Hey Priyanka,
    Im new to Selenium..can ypu please help me out with some sample projects...Sandeep.008d@gmail.com

    ReplyDelete
  12. Hi , i need detail explanation about Hybrid Framework

    ReplyDelete
  13. Hi this is Kathiresan i am having 3 years of experience as a dot net developer and i am certified. i have knowledge on OOPS concepts in .NET but dont know indepth. After learning android will be enough to get a good career in IT with good package? and i crossed Android Training in Chennai website where someone please help me to identity the syllabus covers everything or not??

    Thanks,
    kathiresan

    ReplyDelete
  14. Hi this is Kathiresan i am having 3 years of experience as a dot net developer and i am certified. i have knowledge on OOPS concepts in .NET but dont know indepth. After learning android will be enough to get a good career in IT with good package? and i crossed Android Training in Chennai website where someone please help me to identity the syllabus covers everything or not??

    Thanks,
    kathiresan

    ReplyDelete
  15. Thanks for sharing this informative blog. If you are looking for best PHP Training Institute in Chennai visit FITA IT training and placement academy

    ReplyDelete
  16. HI Priyanka . I am Krishna Singh please post the operation level keyword driven and Hybrid frame work with same Login, book flight, Log out also.

    send me the code to rksingh478@gmail.com please..

    ReplyDelete
  17. Good explanation on the framework. Thank you Priyanka.

    ReplyDelete
  18. Good explanation on the framework. Thank you Priyanka.

    ReplyDelete
  19. can u say me the same code using parameters please




    ReplyDelete
  20. hi priyanka,this blog is very useful,plc can u provide hybrid frame work doc,i'm confused how to start writing scripts etc,plz explain in easy way

    ReplyDelete


  21. This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharng this information,this is useful to me...
    Android training in chennai
    Ios training in chennai

    ReplyDelete
  22. Thanks for posting useful information.You have provided an nice article, Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things...Really it was an awesome article...very interesting to read..please sharing like this information......
    Web Design Development Company
    Mobile App Development Company

    ReplyDelete
  23. You have provided an nice article, Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things...

    Android App Development Company

    ReplyDelete
  24. great and nice blog thanks sharing..I just want to say that all the information you have given here is awesome...Thank you very much for this one.
    web design Company
    web development Company
    web design Company in chennai
    web development Company in chennai
    web design Company in India
    web development Company in India

    ReplyDelete
  25. it is really amazing...thanks for sharing....provide more useful information...
    Mobile app development company

    ReplyDelete
  26. I wondered upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon.

    Fitness SMS
    Salon SMS
    Investor Relation SMS

    ReplyDelete
  27. I simply want to say I’m very new to blogs and actually loved you’re blog site. Almost certainly I’m going to bookmark your blog post . You absolutely come with great well written articles. Thanks a lot for sharing your blog.
    Android Training in chennai |Best Android Training in Velachery | android development course fees in chennai

    ReplyDelete