From last 6 month or so, I am happily playing with Selenium API. Earlier, I invested more than 3 years in QTP/UFT. After spending that much of time in QTP, it’s very obvious that moving to selenium is not easy game and you cannot help yourself but compare this two things. You missed a lot of things from QTP when you move to Selenium, I can enlist few of those as follows:- Actions
Datatables
Checkpoints
Smart Identifications
Recovery scenarios
Readily available test execution results and
Object Repository and OR Manager
This is not the complete list but still this are the majors which I have missed.
“Journey becomes easy and better when you have good companion.” I went through many forums to find out good companion for learning selenium and finally settled on a book Selenium Testing Tools Cookbook by Unmesh Gundecha. When I started to learn selenium, I thought of creating Object Repository in selenium. The book by Unmesh, itself was presented with recipe of implementation of Object Repository [OR]. I have done some changes in implementation as per need which we will discuss in this post. What is Object Repository (OR) anyway?
Object Repository is a mechanism to store objects information from application under test for performing action on those objects in a test script, it acts as interface between Test script and application in order to identify the objects during the execution
Setting Up Object Repository for Selenium WebDriver
We know that, QTP internally uses XML to store object repository and someone already answered on stackoverflow to show use of XML as object repository for Webdriver. Personally I prefer to use properties files as they are more readable than XML files. As you might aware that property file uses key-value pair format. You must decide the format for your properties file, I am using it in following way:-
[logical_name]=[locator_type]~[locator_value]
Where -
[logical_name]:Logical Name generally would be label of element.[As it displayed on the UI]
[locator_type]:Locator type must be one from the following-id, name, className, linkText, partialLinkText, cssSelector, xpath, tagName.
[locator_value]:Locator value - Value of locator_type
Sample property file screenshot-
|
Sample Object Repository Selenium WebDriver |
|
Fetching objects from the Object Repository:- Now that we have created properties file (OR), we need to have mechanism in a place to fetch this objects in our test script. Let's design a class which will help us to achieve following objectives.:- |
Get WebElement as mentioned in our properties file.[method getElement()]
Get Locator as mentioned in our properties file. [method getLocator()]
Using Object Repository in Selenium WebDriver script:-
Now let us try to automate the scenario of login to application which uses Login OR [Login.properties file as shown in image], ObjectMap class.
Above script demonstrate login to wordpress. It uses getLocator method from the class ObjectMap. This is very straightforward example of OR, it does not uses Page Object Model or TestNG. If you notice, we have not used getElement method from the ObjectMap class and used one OR in a script like shared OR. May be in future, I will post related articles which will show use of OR as Local OR along with Page Object.
This is all for now.
Cheers!!