Selenium Framework 5: Understand Keyword Driven Framework in Selenium

A “Keyword” is a wrapper of an atomic action or a chain of actions to achieve a task. These keywords are reusable and building blocks of writing a flow. It shows abstraction concept of OOPS. It is callable. Mostly “Keyword” will be a method.

Let’s understand it considering a real time scenario:-

You need to write automation script for below test case:-

  1. Launch any browser.
  2. Load Google URL.
  3. Type “Selenium” in search box.
  4. Hit “Enter” button.
  5. Click on first link of search result.
  6. Get the URL.
  7. Save this URL in to any external file.
  8. Close the browser.

Let’s apply keyword driven strategies on it.

Strategy 1:-

Create a single keyword or method to perform all above steps. To make a keyword reusable , we must identify its driving factors. For example:- User may ask to launch any browser. So my keyword must provide that flexibility to user. We could create specific keyword for each browser like launchChrome, launchFirefox which will not be a good design. We can create a common keyword to launch a browser by parameterizing keyword.

I will identify driving factors of keyword which will act as parameters to it.

Driving factors:-

  1. Browser to be launched.
  2. Search literal to enter in Google search box.
  3. External file to store data.

searchStoreURL( String browserName , String searchLiteral, File fileToSaveData)

You can use above keyword for different search literals, browsers and files to save. It is reusable. It exhibits abstraction concept of OOPS. Whoever wants to perform similar tasks, they will call this keyword without bothering how it is done which is called abstraction in OOPS.

Strategy 2:-

In above strategy, you mixed up every steps. You have written logic to launch browsers, entering URL, typing literals, click, extracting URL, writing in to a file and closing browsers all in once. If someone just want to load a URL or writing in to a file, they can not use above keyword. We should conclude now that a keyword is a lower level functions. Let’s break above scenario in number of lower level keywords:-

  1. Launch a browser :- launchBrowser(String browserName)
  2. Load a URL:- loadURL(String url)
  3. Verify title to ensure correct URL loaded: verifyTitle(String expectedTitle)
  4. Type a text : enterText(String textToType)
  5. Click:- performClick()
  6. Extract URL or get current url : getCurrentlyLoadedURL()
  7. Writing in to a file: – writeToFile(String fileName)
  8. Closing browser:- closeBrowser()

Let’s create some keywords which perform multiple actions together:-

  1. loadURLAndAssertTile(String url, String expectedTitle) :- Combination of loadURL(String url) and verifyTitle(String expectedTitle) .
  2. clickOnFirstResult()

All above keywords represent lower level functionality to perform atomic task or a chain of tasks. These can be reused for many other scenarios. These are actually reusable to a great extent.

Let’s think more on these keywords and divide them into two categories:-

  1. Keywords which are not specific to application.
  2. Keywords which are specific to application.

[table id=9 /]

All general keywords listed above can be used for any other scenario, applications where similar technology is used. General keywords can be categorised as Selenium Framework (If using selenium) while application specific keywords can be categorized as application specific framework.

Above is called a Keyword driven approach. Now it’s up to you how you utilise these keywords. You can use directly call it in scripts or through external sources like excel, json, database etc.

You mainly need to focus on keywords to make it more custom, parameterized, reusable, scalable and manageable, powerful. You should add validation. logging, reporting stuff , Exception handling in keywords. Powerful keywords will reduce number of lines in test scripts.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappySelenium

Leave a Reply

Your email address will not be published. Required fields are marked *