My Blog List

Tuesday, November 8, 2011

Maintain Locators - Selenium Automation Projects

Automating your application is easy on the way to reach your goal in your selenium tests. But give me answers to below questions or answer yourself.
1. Is it readable?
2. Is it easily modifiable while your script grows?
3. Is it easily identifiable?
4. Can anybody edit your locator? etc....
For all these questions, your answer might be a big NO.To solve this prob, here is the approach.


Maintaining Locators:
Always maintain your locators in a single file which is supported by your script driven language like below.
Java   ---  you have .properties file
Python  --- Config parser or dictionaries
C#  --- .config file
Ruby  --- YAML file
So maintain your locators in a single file and edit whenever you want like QTP Object Repository.
Your file should contain your locators like this.
PageName.ElementName=ElementLocatorString


Ex: locators.properties file
# Here registration is my page and firstname is element on registration page.
# Just for ID or Name
registration.firstname=firstname
# XPATH
registration.lastname=//form[@name='lastname']/input[@type='text'][2]
# CSS
registration.submit=css=form submit


Accessing Locators in Scripts:
Just create a variable of type properties in globally in your class.
public static Properties locators = new Properties();
Then, load your locators file to JVM.
InputStream locatorStream = ClassName.class.getResourceAsStream("/locators.properties");
locators.load(locatorStream);
Here ClassName refers your class name.
Thats all. Use it in your scripts like 
locators.getProperty("registration.firstname")


The above spoken is an approach. Also you can start with another approach.
1. Design Page Objects representing your pages or part of pages
2. Use your locators directly inside you page object classes you design.
3. Now your script contains only your methods your page object classes providing.
Now maintaining your locators is easy while automation project grows. Just find you page object class for a specific page and change your locators/methods as you want. No need to care about your scripts.
Refer this post: PageObjects


Hope this helps you start your own framework.

1 comment:

  1. Thanks for sharing great information in your blog. Got to learn new things from your Blog . It was very nice blog to learn about Selenium

    ReplyDelete