Saturday, 16 July 2016

Highlight Element in Selenium WebDriver

  I missed highlighting object feature of QTP when I used to debug my selenium API call on webelement. Number of times I wondered, why didn't by default any method is provided to perform highlight action. But I no longer wish for same as I have found work around.  

      Remember this - ‘JavaScript comes in handy to perform any fancy tasks on DOM.’

Logic to perform highlight WebElement in Selenium WebDriver:-


       To create highlight effect, as in QTP, we don't just need to highlight the element but also have to de-emphasize/play down the same element. Highlighting element once is not enough, as the operation would be fast and we may don't realise highlighting effect. Better we break down logic as in below steps:-
  1. Highlight element using javascript.
  2. Play-down same element using javascript.
  3. Perform above two actions in a loop for significant time.

Below code demonstrate use of JavascriptExecutor to perform highlight element in Selenium WebDriver.

As you can see in above code, method highlightElement accepts WebElement as parameter and perform highlight operations for 5 seconds [i<5].

This is all for now.
Cheers!!

Sunday, 10 July 2016

Object Repository in Selenium/Webdriver

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:-
  1. Actions
  2. Datatables
  3. Checkpoints
  4. Smart Identifications
  5. Recovery scenarios
  6. Readily available test execution results and
  7. 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
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.:-

  1. Get WebElement as mentioned in our properties file.[method getElement()]
  2. 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!!

Copyrights held by Amol Chavan. Powered by Blogger.