Sunday, 16 March 2014

Keyword driven automation framework in Selenium Webdriver in C#.Net

Keyword driven Automation Framework is very popular framework used in Selenium Webdriver in C#.Net.
In this article I will explain you all details about how we can design and use keyword driven automation framework in Selenium Webdriver with C#.net along with example.

Keyword driven automation framework in Selenium Webdriver with C#.Net - Introduction

In keyword driven automation framework we create the methods in C# that are mapped to the functionality of the application.

For example -
Suppose you have a  train ticket booking application which provides many features like

  1. Login to the train booking website 
  2. Search trains with given source and destination and time
  3. Select the train tickets
  4. Book tain tickets
  5. Cancel train tickets
  6. View the booking history 
  7. 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 searchTrains method in C#.Net which will search the trains 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 C#.Net

Each keyword driven automation framework has some common components as mentioned below.

  1. C# Class Library with functionality specific methods.
  2. Test Data Sheet (generally in excel format)
  3. Selenium Webdriver with C#.
  4. Reports in HTML format)
  5. Main C# driver script

1. C#.Net Class Library with functionality specific methods.


As explained earlier, we can create methods for each functionality in the application like bookTicket, makePayment etc.

Sample C# method is shown below to login to the web application.

public static boolean Login()
{
   
driver.Navigate().GoToUrl("http://www.abc.com");
IWebElement e1 = driver.FindElement(By.Id("UserName"));
e1.SendKeys("userid");
IWebElement e2 = driver.FindElement(By.Id("Password"));
e2.SendKeys("password");
IWebElement e3 = driver.FindElement(By.Name("submit"));
e3.Click();

Boolean isPresent = driver.FindElement(By.ClassName("logoutlink")).Displayed;

if (isPresent == true)
{
       Console.WriteLine("login successful");
       return true;

}
else
{
       Console.WriteLine("login 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

As displayed in below figure, the data sheet contains below columns.
  1. ID -Manual test case ID
  2. Test_Case_Name - Name of the test case
  3. Exec_Flag - Execution flag to tell if you want to execute this test case. Y means that test will be executed
  4. Test_Step_Id - Step Id of the Test case
  5. Test_Case_Steps - Step name of the test case
  6. Keyword - The name of method in function library you want to call.
  7. objectTypes - type of the web elements like webedit, weblist, webcheckbox etc
  8. objectNames - Web element identification method and expression like xpath://td
  9. objectValues - actual test data you want to enter in the webElement 
  10. parameter1 - extra parameter to drive the method control
  11. parameter2 - one more parameter to drive the method control

Sample test Datasheet in selenium webdriver in Java


3. Selenium Webdriver in C#
This is the driver instance you will create to execute the test cases.
Sample code to create a web driver instance is given below.

IWebDriver driver = new ChromeDriver(@"F:\selenium\csharp");


4. Creating html Reports in Selenium Webdriver automation framework
You can create the html reports using file handling mechanism in Java.

Below code will append the report data to existing report file

StreamWriter f = File.AppendText(@"d:\abc.txt");
f.WriteLine("Hello Baby");
f.Close();

Please note that you will need to import the classes in the package  System.IO to work with files.

5. Main driver script in Selenium webdriver automation framework

This is the main method and starting point for the framework code. The main responsibilities of this method are given below.
  1. Read the test case steps from the datasheet one row at a time
  2. Execute the method corresponding to the current step in the test case
  3. Log the verification points in the html report file
  4. Report the overall execution status like total failed/passed test cases, execution time
  5. Send an email to all stakeholders regarding the execution status.

If you want to download the full source code, data sheets, sample scripts and examples, Please buy an e-book at below url

Selenium Framework 

I will send you all the details along with pdf. You may also buy the book on selenium webdriver in Java at the url - Buy Selenium Books in C#, Java, Python etc

What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

1 comment:

  1. Wow, awesome blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your website is great, let alone the content!

    PIC Scheme Singapore

    ReplyDelete

Buy Best Selenium Books