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:-
- Launch any browser.
- Load Google URL.
- Type “Selenium” in search box.
- Hit “Enter” button.
- Click on first link of search result.
- Get the URL.
- Save this URL in to any external file.
- 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:-
- Browser to be launched.
- Search literal to enter in Google search box.
- 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:-
- Launch a browser :- launchBrowser(String browserName)
- Load a URL:- loadURL(String url)
- Verify title to ensure correct URL loaded: verifyTitle(String expectedTitle)
- Type a text : enterText(String textToType)
- Click:- performClick()
- Extract URL or get current url : getCurrentlyLoadedURL()
- Writing in to a file: – writeToFile(String fileName)
- Closing browser:- closeBrowser()
Let’s create some keywords which perform multiple actions together:-
- loadURLAndAssertTile(String url, String expectedTitle) :- Combination of loadURL(String url) and verifyTitle(String expectedTitle) .
- 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:-
- Keywords which are not specific to application.
- 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