Implement Keyword-Driven Framework

Context:

Following on our discussion with frameworks, in our previous section, we read about keyword driven framework. We mentioned that keywords are blocks of functionality that are abstracted from step definitions and so we just call keywords/modules when needed. We also mentioned technical keywords and business -ish keywords. We will move forward in this section to get into implementation details and we will go with the latter i.e. business keywords.

The project code base is also available to checkout anytime on github [Quick Instructions are in README.md]

It is assumed that you have Ruby, cucumber and selenium components set up on your machine already. If not, please follow the below links:

In this section, we are going to take a concrete example on an e-commerce website and see how we can implement this capability. We are also going to build on top of data driven capabilities we build in our previous section on data-driven frameworks.

For example, each of the below capabilities will still exist in our framework and we will build the keywords on top of the below capabilities.

  • Data helper still exists, user may switch between excel, yaml or strings from cucumber

Use Case:

As part of my framework, I need the following features viz.

  • visit url – url being user supplied value
  • sign_in (username,password) – signs in to application
  • Contact us – fill the contact us page and submits

As we see above, the keywords are readable, so we can re-use them in future in other step definitions too.

Object Repository:

We hear this term a lot in the Automation space and QTP has made it very popular. Object repository as the name suggests is a repository that contains the objects we want to interact with. With the advent of Selenium and more mature tools, that are open source and configurable/flexible, we can create object repository with one single line in some cases. Anyways, for this framework, I would like to externalize the locator (html locator) information instead of hard-coding it in the step definitions. Since I already know an extremely easy format in Ruby that can maintain key,value pairs i.e. YAML, I will go ahead and create a yml file that contains my locator information.

We have already seen how to parse a YAML file into a hash data structure in Test Data with YML with a single line and I am doing to do the same here.

First I would create a yml file in ‘lib/config‘ folder and name it ‘object_repository.yml‘. Since I want the locators information on sign and contact us page along with home page, below is how the file contents look.

Load the module

Keywords Module(s):

Below is an example of one keyword module, however you can add as many as you want. It is a repeatable model.

Each of the above keywords are self-explanatory. In the above example, I hard coded the data values when set. However, you can always call data_yml_hash or data_excel_hash as we discussed in data-driven frameworks and use the data from the files to set values.

Load the keywords module

Add the following line(s) of code to env.rb

Cucumber Feature:

Now lets write our cucumber features in a file under ‘features/keyword_driven.feature”

Step Definitions:

The step_definitions are in ‘step_definitions/keyword_driven.rb’

Overall Project Structure:

The overall project in Rubymine should look like this now:

modular_framework

Execution Ouput:

Either right click on any scenario (or feature) in the Rubymine editor and click Run or if you are command line person like me, execute “bundle exec cucumber features” from project root folder

  1. The first scenario should successfully sign in and hence pass
  2. The second scenario uses wrong username, so should fail
  3. Third scenario should fill contact us page and hence pass

Rubymine Execution output:

keyword_rubymine_results

Command Execution output:

keyword_cmd_results

Closing Thoughts and Project Code:

In this section, we have seen what Keywords or modules are. They are nothing but a way to modularize blocks of code. In the next framework i.e. Hybrid, we build on top of both data-driven and keyword-driven frameworks and add page-object model and build a more holistic framework.

Feel free to check out the entire project code base from my github repo: modular_framework

The README.md file has instructions to run the code from scratch too. It should be pretty easy, let me know if you have any questions.

 

Keyword Driven Framework