[Selenium+java] Selenium Framework:keyword Driven & Hybrid

Source: Internet
Author: User
Tags gettext rowcount xpath xpath if testng guru99

Original from:https://www.guru99.com/creating-keyword-hybrid-frameworks-with-selenium.html

What is the Selenium Framework?

Selenium Framework is a code structure this helps to make code maintenance easy. Without frameworks, we'll place the ' code ' as well as ' data ' in the same place which is neither re-usable nor readable. Using frameworks, produce beneficial outcomes like increased code re-usage, higher portability, reduced script maintenance Cost, higher code readability, etc.

There is mainly three type of frameworks created by Selenium Webdriver to automate manual test cases

    • Data driven Test Framework
    • Keyword driven Test Framework
    • Hybrid Test Framework

Data driven Test Framework

in Data driven framework all of our test data are generated from some external files like Excel, CSV, XML or some database table. We already learned about the Data driven testing in our previous tutorial

Keyword driven Test Framework:

In keyword driven test framework, all the operations and instructions is written in some external file like Excel Workshe Et. Here's how the complete framework looks like

As can see it ' s a 5 step framework. Let's study it stepwise in detail

Step 1)

    • The driver script Execute.java would call Readguru99excelfile.java
    • Readguru99excelfile.java have POI script to read data from an Excel

Step 2)

    • Readguru99excelfile.java would read data from testcase.xlsx
    • Here's how the sheet looks like-

    • According to the keywords written in Excel file, the framework would perform the operation on UI.
    • For example, we need to click a button ' Login. ' Correspondingly, our Excel would have a keyword ' Click. ' Now the AUT can has hundreds of button on a page, to identify a Login button, in Excel we'll input Object Name as Login Button & Object Type as a name (see highlighted the row in above image). The Object Type could is Xpath, name CSS, or any other value

Step 3) Readguru99excelfile.java'll pass this data to the driver script Execute.java

Step 4)

    • For any of our UIs Web elements, we need to the create an object repository where we'll place their element locator (like XPA Th, name, CSS path, class name etc.)

    • Execute.java (our driver script) would read the entire Object Repository and store it in a variable
    • To read the This object repository, we need a readobject class which have a getobjectrepository method to read it.

Note: Think why do we need to create an object repository. The answer helps in code maintenance. For example, we is using the button with name = Btnlogin in different test cases. In the future, the developer decides to change the name from Btnlogin to submit. You'll have an cases a change in all the test. In the case of a object repository, you'll make the change just once in the repository.

Step 5)

    • The driver would pass the data from Excel & Object Repository to Uioperation class
    • Uioperation class have functions to perform actions corresponding to keywords like CLICK, SETTEXT etc ... mentioned in the exc El
    • Uioperation class is a Java class which have the actual implementation of the code to perform operations on web elements

The complete project would look like-

Let's look into an example:

Test Scenario

    • We are executing 2 test cases
    • Test Case 1:
    • Goto http://demo.guru99.com/V4/
    • Enter User ID
    • Enter Password
    • Click Reset
    • Test Case 2:
    • Goto http://demo.guru99.com/V4/
    • Enter User ID
    • Enter Password
    • Click Login
Object.properties

url=http://demo.guru99.com/v4/

Username=uid

Password=password

Title=barone

Loginbutton=btnlogin

Resetbutton=btnreset

Readguru99excelfile.java
Package Excelexportandfileio;import Java.io.file;import Java.io.fileinputstream;import java.io.IOException;import Org.apache.poi.hssf.usermodel.hssfworkbook;import Org.apache.poi.ss.usermodel.sheet;import Org.apache.poi.ss.usermodel.workbook;import Org.apache.poi.xssf.usermodel.xssfworkbook;public Class    readguru99excelfile {public Sheet readexcel (String filepath,string filename,string sheetname) throws ioexception{    Create a object of file class to open xlsx file File = new file (filepath+ "\ \" +filename);     Create an object of FileInputStream class to read Excel file FileInputStream InputStream = new FileInputStream (file);    Workbook guru99workbook = null; Find the file extension by spliting file name in substing and getting only extension name String Fileextensionname =    Filename.substring (Filename.indexof (".")); Check condition If the file is xlsx file if (Fileextensionname.equals (". xlsx")) {//if It was xlsx file then create O Bject of XSSFWOrkbook class Guru99workbook = new Xssfworkbook (InputStream); }//check condition If the file is an XLS file else if (Fileextensionname.equals (". xls")) {//if It is xls file th    En create object of xssfworkbook class Guru99workbook = new Hssfworkbook (InputStream);     }//read sheet inside the workbook by its name sheet Guru99sheet = Guru99workbook.getsheet (sheetname);        return guru99sheet; }}
Readobject.java
Package Operation;import Java.io.file;import Java.io.fileinputstream;import java.io.ioexception;import Java.io.inputstream;import Java.util.properties;public class ReadObject {    properties P = new Properties ();    Public Properties Getobjectrepository () throws ioexception{        //read object repository file        InputStream stream = New FileInputStream (New File (System.getproperty ("User.dir") + "\\src\\objects\\object.properties");        Load all Objects        p.load (stream);         return p;    }    }
Uioperation.java
Package Operation;import Java.util.properties;import Org.openqa.selenium.by;import org.openqa.selenium.WebDriver;    public class Uioperation {Webdriver driver;    Public uioperation (Webdriver driver) {this.driver = driver;         } public void Perform (Properties p,string operation,string objectname,string objecttype,string value) throws exception{        System.out.println (""); Switch (Operation.touppercase ()) {case "click"://perform click Driver.findelement (this.geto            Bject (P,objectname,objecttype)). Click ();        Break Case "SETTEXT"://set text on Control driver.findelement (This.getobject (p,objectname,objecttype)). SE            Ndkeys (value);                    Break            Case "Gotourl"://get URL of Application Driver.get (P.getproperty (value));        Break Case "GETTEXT"://get text of an element driver.findelement (This.getobject (P,objectname,objecttype)). GetText ();        Break        Default:break; }}/** * Find element by using object type and value * @param p * @param objectName * @param ob Jecttype * @return * @throws Exception * * Private by GetObject (Properties p,string objectname,string Object Type) throws exception{//find by XPath if (Objecttype.equalsignorecase ("XPath")) {RET        Urn By.xpath (P.getproperty (objectName)); }//find by class else if (Objecttype.equalsignorecase ("CLASSNAME")) {return by.classn                    Ame (P.getproperty (objectName)); }//find by name else if (objecttype.equalsignorecase ("name")) {return By.name (P.GETPR                    Operty (ObjectName)); }//find by CSS else if (objecttype.equalsignorecase ("CSS")) {return By.cssselector (p.                    GetProperty (ObjectName)); }//fIND by link Else if (objecttype.equalsignorecase ("link")) {return By.linktext (P.getproperty (o                    Bjectname)); }//find by partial link else if (objecttype.equalsignorecase ("Partiallink")) {return                    By.partiallinktext (P.getproperty (objectName));        }else {throw new Exception ("Wrong object type"); }    }}
Executetest.java
Package Testcases;import Java.util.properties;import operation. Readobject;import operation. Uioperation;import Org.apache.poi.ss.usermodel.row;import Org.apache.poi.ss.usermodel.sheet;import Org.openqa.selenium.webdriver;import Org.openqa.selenium.firefox.firefoxdriver;import Org.testng.annotations.test;import Excelexportandfileio.readguru99excelfile;public class ExecuteTest {@Test public void Testlogin () throws Exception {//TODO auto-generated method Stubwebdriver webdriver = new Firefoxdriver (); Readguru99excelfile file = new Readguru99excelfile (); ReadObject object = new ReadObject (); Properties allobjects = Object.getobjectrepository (); Uioperation operation = new Uioperation (webdriver);//read keyword Sheetsheet guru99sheet = file.readexcel ( System.getproperty ("User.dir") + "\ \", "testcase.xlsx", "keywordframework");//find number of rows in Excel file int Rowco    UNT = Guru99sheet.getlastrownum ()-guru99sheet.getfirstrownum (); Create a loop over all the rows of Excel file To read it for (int i = 1; i < rowcount+1; i++) {//loop through all the rows row row = Guru99sheet.getrow        (i); Check If the first cell contain a value, if yes, that means it is the new testcase name if (Row.getcell (0). Tostrin g (). Length () ==0) {//print testcase detail on console System.out.println (Row.getcell (1). toString () + "----"        + Row.getcell (2). ToString () + "----" + Row.getcell (3). ToString () + "----" + Row.getcell (4). ToString ()); Call perform function to perform operation on UI operation.perform (AllObjects, Row.getcell (1). toString (), row     . Getcell (2). ToString (), Row.getcell (3). ToString (), Row.getcell (4). ToString ()); } else{//print The new testcase name when it started System.out.println ("New TESTCASE-&G t; "            +row.getcell (0). toString () + "Started"); }        }    }}

After execution, output would look like-

Download the Selenium Project Files for the Demo on this Tutorial

Hybrid Test Framework

Hybrid Test Framework is a concept where we are using the advantage of both Keyword and Data driven framework.

Here for keywords, we'll use the Excel files to maintain test cases, and for test data, the We can use data, provider of Testng Framework.

Here's our hybrid framework, we don ' t need to change anything in Keyword driven framework, here we just need to replace E Xecutetest.java file with Hybridexecutetest.java file.

This hybridexecutetest file have all the code for keyword driven with data provider concept.

The complete pictorial representation of hybrid framework would look like

Hybridexecutetest.java
Package Testcases;import Java.io.ioexception;import Java.util.properties;import operation. Readobject;import operation. Uioperation;import Org.apache.poi.ss.usermodel.row;import Org.apache.poi.ss.usermodel.sheet;import Org.openqa.selenium.webdriver;import Org.openqa.selenium.firefox.firefoxdriver;import Org.testng.annotations.dataprovider;import Org.testng.annotations.test;import Excelexportandfileio.readguru99excelfile;public class Hybridexecutetest {webdriver webdriver = null; @Test ( Dataprovider= "Hybriddata") public void Testlogin (String testcasename,string keyword,string objectname,string Objecttype,string value) throws Exception {//TODO auto-generated Method stub if (testcasename!=null&&    Amp;testcasename.length ()!=0) {webdriver=new firefoxdriver (); }readobject object = new ReadObject (); Properties allobjects = Object.getobjectrepository ();    Uioperation operation = new Uioperation (webdriver);           Call perform function to perform operation on UI Operation.perform (allobjects, keyword, objectName, objectType, value); } @DataProvider (name= "Hybriddata") public object[][] Getdatafromdataprovider () throws ioexception{object[][] Object    = NULL; Readguru99excelfile file = new Readguru99excelfile ();//read keyword Sheetsheet guru99sheet = file.readexcel ( System.getproperty ("User.dir") + "\ \", "testcase.xlsx", "keywordframework");//find number of rows in Excel file int Rowco    UNT = Guru99sheet.getlastrownum ()-guru99sheet.getfirstrownum ();    Object = new Object[rowcount][5];        for (int i = 0; i < RowCount; i++) {//loop through all the rows row row = Guru99sheet.getrow (i+1); Create a loop to print cell values in a row for (int j = 0; J < Row.getlastcellnum (); j + +) {//prin        T Excel data in console object[i][j] = Row.getcell (j). ToString ();     }} System.out.println ("");        return object; }}

Summary:

    • We can create three types of test framework using Selenium webdriver.
    • These is Data driven, Keyword driven, and Hybrid test framework.
    • We can achieve Data-driven framework using TestNG ' s Data provider.
    • In Keyword driven framework, keywords is written in some external files like Excel file and Java code would call the this file and execute test cases.
    • The hybrid framework is a mix of keyword driven and data driven framework.

[Selenium+java] Selenium Framework:keyword Driven & Hybrid

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.