Selenium DDF : Creating Functions To Locate Elements By All Locator Types

We have Implemented getElementByXPath function to locate element by XPath In PREVIOUS STEP. As you know, There are many different element locators available In web driver. So obviously, Sometimes you needs to locate elements by other locator types too ( cssSelector, ID, Name etc..) when It Is not possible to locate element by XPath. Let us Implement those functions In our DDF.

STEP 29

Prerequisite : All previous data driven framework creation steps should be Implemented. You can get list of steps with links on THIS PAGE.

How To Implement
In this step, We will Implement different functions for different element locator types to locate web page elements. We have already Implemented getElementByXPath function In previous step so I think no more detailed description required. We have to use same thing In all other locators functions too. We have to Implement all these functions In SuiteBase.java file so that we can access them In our all test cases. I have Implemented bellow given new functions In SuiteBase.java file.
  • getElementByID -> To locate element by ID locator.
  • getElementByName ->  To locate element by Name locator.
  • getElementByCSS ->  To locate element by cssSelector locator.
  • getElementByClass ->  To locate element by Class locator.
  • getElementByTagName ->  To locate element by tagName locator.
  • getElementBylinkText ->  To locate element by linkText locator.
  • getElementBypLinkText ->  To locate element by Partial linkText locator.
All these functions will use key of element which Is stored Inside Objects.properties file. Using value of that key, It will locate the element.

Download Required Files
You have to download modified SuiteBase.javaSuiteOneCaseOne.java and Objects.properties files. Download bellow given zip folder to get all of them.
Extract the zip folder and replace all three files with existing files In eclipse. View SuiteBase.java, file to know how all above functions are Implemented and view SuiteOneCaseOne.java to know how few of them(Example getElementByName, getElementByID, getElementByCSS) are used In our test case. If you know, Previously we have used getElementByXPath function to click on buttons and get text from text box. Now, few of those lines are modified using above functions.

If you view Objects.properties file, I have created different blocks for all different locators types to store and manage them In proper way. This structure will helps you to find any locator very easily In future.

Run Test
Run test using "Shortcut to Run Test.bat" file from desk top and observe execution and results.

What We Have Achieved In This Step
We have Implemented all above described functions to locate the elements using different element locating methods. We have also modified Objects.properties file to maintain different types of element objects In good manner. It will help us to maintain all element objects very easily.


2 comments:

  1. The Framework tutorial is really helpful.
    Please post one for BDD Framework.

    ReplyDelete
  2. Thanks for this valuable tutorials.

    ReplyDelete