Why we need to do this?
It seems easy to code or make a project automated in the Selenium Web Driver, but more difficult to manage or maintain, especially when its larger in size and if someone else want to modify it in your absence(if any modification is required)*.He or she can understand this in a better way and gets all the required locators, link path etc at a single place.
1.Create a new Java Class:
This Java class will read the Property file which contains the Locators, path etc. of your project.
Code Snippet:
package your_package_name; import java.io.File; import java.io.FileInputStream; import java.util.Properties; public class ComProFile{ String webElementXPath; public String getElementName(String webElementKey){ Properties props=null; FileInputStream fin =null; try{ File f = new File("absolute path for object.properties file "); fin = new FileInputStream(f); props = new Properties(); props.load(fin); webElementXPath = props.getProperty(webElementKey); }catch(Exception e){ System.out.println(e.getMessage()); } return webElementXPath; } }
2. Create the Object.Property file in your Project:
Follow the below mentioned steps to add the Object.Property file :
- Right click on project package>>New>>File
- Enter the name of the file as Object.Property in File Name field .(name is as you need )
- Click Finish
You will see, Object.Property has been created under your Project Package.Now Open this file and enter all the locators in this file as below format:
e.g.
URL = www.myapp.com #ScreenShot File Location Screenshot_File = E:/Sumer/workspace/myapp/ #Excel File locator xlfile = E:/Sumer/workspace/myapp/Datafiles/scriptdata.xls #User name XPath Web Elements userName = //*[@id='firstname']
3.Use of ComProFile class in the Statement block:
driver.get(c.getElementName("URL")); driver.findElement(By.xpath(c.getElementName("userName "))).sendKeys("Name");
c is object of the ComProFile class and "userName" is the locator name defined in the Object.Property file.
No comments:
Post a Comment