Keyword driven Automation Framework is very popular framework used in Selenium Webdriver with Java.
In this article I will explain you all details about how we can design and use keyword driven automation framework in Selenium Webdriver with Java along with example.
Keyword driven automation framework in Selenium Webdriver with Java - Introduction
In keyword driven automation framework we create the methods in Java that are mapped to the functionality of the application.
For example -
Suppose you have a bus ticket booking application which provides many features like
To automate the test cases for such web applications, We usually write the methods that perform specific task. For example we may write the searchBus method in Java which will search the buses for given source city, Destination city and Date.
Similarly we will create the methods for each functionality. The advantage of creating methods is that we can re-use these methods in multiple test cases with different input test data.
This speeds up the automation process with increased productivity.
The Components of keyword driven automation framework in Selenium Webdriver with Java
Each keyword driven automation framework has some common components as mentioned below.
1. Java Class Library with functionality specific methods.
As displayed in below figure, the data sheet contains below columns.
In this article I will explain you all details about how we can design and use keyword driven automation framework in Selenium Webdriver with Java along with example.
Keyword driven automation framework in Selenium Webdriver with Java - Introduction
In keyword driven automation framework we create the methods in Java that are mapped to the functionality of the application.
For example -
Suppose you have a bus ticket booking application which provides many features like
- Login to the bus booking website
- Search Buses with given source and destination and time
- Select the bus tickets
- Book bus tickets
- Cancel bus tickets
- View the booking history
- Make the payment.
To automate the test cases for such web applications, We usually write the methods that perform specific task. For example we may write the searchBus method in Java which will search the buses for given source city, Destination city and Date.
Similarly we will create the methods for each functionality. The advantage of creating methods is that we can re-use these methods in multiple test cases with different input test data.
This speeds up the automation process with increased productivity.
The Components of keyword driven automation framework in Selenium Webdriver with Java
Each keyword driven automation framework has some common components as mentioned below.
- Java Class Library with functionality specific methods.
- Test Data Sheet (generally in excel format)
- Selenium Webdriver with Java.
- Reports in HTML format)
- Main Java driver script
1. Java Class Library with functionality specific methods.
As explained earlier, we can create methods for each functionality in the application like bookTicket, makePayment etc.
Sample Java method is shown below to login to the web application.
public static boolean Login()
{
driver.navigate().to("http://www.abc.com");
WebElement e1 = driver.findElement(By.id("UserName"));
e1.sendKeys("userid");
WebElement e2 = driver.findElement(By.id("Password"));
e2.sendKeys("password");
WebElement e3 = driver.findElement(By.name("submit"));
e3.click();
Boolean isPresent = driver.findElements(By.className("logoutlink")).size()>0;
if (isPresent == true)
{
//System.out.println("Login was
successful");
return true;
}
else
{
//System.out.println("Login
was not successful");
return false;
}
}
//Please note that you can write the methods to perform more complex operations in similar //fashion
2. Test Data Sheet in Selenium Webdriver framework
- ID -Manual test case ID
- Test_Case_Name - Name of the test case
- Exec_Flag - Execution flag to tell if you want to execute this test case. Y means that test will be executed
- Test_Step_Id - Step Id of the Test case
- Test_Case_Steps - Step name of the test case
- Keyword - The name of method in function library you want to call.
- objectTypes - type of the web elements like webedit, weblist, webcheckbox etc
- objectNames - Web element identification method and expression like xpath://td
- objectValues - actual test data you want to enter in the webElement
- parameter1 - extra parameter to drive the method control
- parameter2 - one more parameter to drive the method control
Sample test Datasheet in selenium webdriver in Java |
3. Selenium Webdriver in Java
This is the driver instance you will create to execute the test cases.
Sample code to create a web driver instance is given below.
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
4. Creating html Reports in Selenium Webdriver automation framework
You can create the html reports using file handling mechanism in Java.
Below code will delete the existing report file
if ((new File(c:\\reportfile.html)).exists() )
(new File(c:\\reportfile.html)).delete();
Below code will append the data to the existing report file stored at given filePath
public static void appendToFile(String
filePath,String data) {
//This function
will be used to append text data to filepath
try{
File
temp = new File(filePath);
FileWriter
fw = new FileWriter(temp,true);
fw.append(data);
fw.close();
}catch(Exception e){}
}
Please note that you will need to import the classes in the package java.io to work with files.
5. Main driver script in Selenium webdriver automation framework
If you want to download the full source code, data sheets, sample scripts and examples, Please buy an e-book on selenium webdriver in Java at the url - Selenium Webdriver Book in Java
What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com
This is the main method and starting point for the framework code. The main responsibilities of this method are given below.
- Read the test case steps from the datasheet one row at a time
- Execute the method corresponding to the current step in the test case
- Log the verification points in the html report file
- Report the overall execution status like total failed/passed test cases, execution time
- Send an email to all stakeholders regarding the execution status.
You will need to import the excel library provided by org.apache.poi
You may also like to read below topics.
You may also like to read below topics.
- Press key in Selenium Webdriver in Java
- Read Table Data in Selenium Webdriver in Java
- Take the screenshot in Selenium Webdriver in Java
- Execute JavaScript in Selenium Webdriver in Java
- Handle multiple browser windows in Selenium Webdriver in Java
- Double-Click on the Element in Selenium Webdriver in Java
- Exceptions in Selenium Webdriver in Java
- Synchronization in Selenium Webdriver in Java
What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com
Hi ,
ReplyDeleteyour post is nice , it's helping for knowing the basics of Keyword driven, Can you please provide me the sample Code for it.
Thanks in advance
Please share the code.
ReplyDeleteThanks,
Vandana
Thanks..Request you please post the compleet code...
ReplyDeleteRegards,
Kuladip
Very helpful - Can you please share the code for one keyword and the driver script to invoke .. Thank you
ReplyDelete