Create
a file OR.xml under objectRepositoryFile package. Then copy the following lines.
OR.xml
<?xml version="1.0"
encoding="UTF-8"?>
<OR>
<Page name="HomePage">
<element name = 'lnk_myaccount'>
<propertytype>link</propertytype>
<propertyvalue>My
Account</propertyvalue>
</element>
<element name = 'lnk_login'>
<propertytype>link</propertytype>
<propertyvalue>Login</propertyvalue>
</element>
</Page>
<Page name="LoginPage">
<element name =
'txt_email'>
<propertytype>name</propertytype>
<propertyvalue>username</propertyvalue>
</element>
<element name = 'txt_password'>
<propertytype>name</propertytype>
<propertyvalue>password</propertyvalue>
</element>
<element name = 'btn_login'>
<propertytype>xpath</propertytype>
<propertyvalue>.//*[@id='loginfrm']/div[4]/button</propertyvalue>
</element>
</Page>
<Page
name="MyAccountPage">
<element name = 'lnk_accname'>
<propertytype>link</propertytype>
<propertyvalue>John</propertyvalue>
</element>
<element name = 'lnk_logout'>
<propertytype>link</propertytype>
<propertyvalue>Logout</propertyvalue>
</element>
</Page>
</OR>
Create
2 classes under objectRepositoryMain package and name them XMLObjectMap.java and XMLObjectRepository.java. Then copy following codes in respective file.
XMLObjectMap.java
package
objectRepositoryMain;
import java.io.File;
import
javax.xml.parsers.DocumentBuilder;
import
javax.xml.parsers.DocumentBuilderFactory;
import
org.openqa.selenium.By;
import
org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import
org.w3c.dom.NodeList;
public class XMLObjectMap {
Document
doc;
public XMLObjectMap(String path)
{
try{
File
inputfile = new File(path);
DocumentBuilderFactory
dbFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder docbuilder = dbFactory.newDocumentBuilder();
doc = docbuilder.parse(inputfile);
doc.getDocumentElement().normalize();
}catch(Exception e){
System.out.println("Exception
caught");
}
}
public By getLocator(String
element) throws Exception{
String
locatorType=null;
String locatorValue = null;
System.out.println("Root element
:"
+ doc.getDocumentElement().getNodeName());
//Get list of all
element tag
NodeList nodelist = doc.getElementsByTagName("element");
//Traverse
individual element
for (int tmp = 0; tmp < nodelist.getLength(); tmp++) {
Node nNode = nodelist.item(tmp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
//System.out.println("\nCurrent
Element :" + nNode.getNodeValue());
Element eElement = (Element) nNode;
String elementname =eElement.getAttribute("name");
//The element you are seaching
for
if(elementname.equalsIgnoreCase(element)){
//System.out.println("\nCurrent Element :" + objectname);
String propertytype =eElement.getElementsByTagName("propertytype").item(0).getTextContent();
String propertyvalue= eElement.getElementsByTagName("propertyvalue").item(0).getTextContent();
System.out.println("Element Name:
"+ elementname);
System.out.println("Element type
: "+
propertytype);
System.out.println("Element value
: "+
propertyvalue);
locatorType=propertytype;
locatorValue=propertyvalue;
break;
}
}
}
switch(locatorType){
case "id": return By.id(locatorValue);
case "xpath": return By.xpath(locatorValue);
case "link": return By.partialLinkText(locatorValue);
case "name": return By.name(locatorValue);
default: throw new Exception("Unknown
locator type '" + locatorType + "'");
}
}
}
XMLObjectRepository.java
package objectRepositoryMain;
import
java.util.concurrent.TimeUnit;
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.firefox.FirefoxDriver;
//import package1.ObjectMapXml;
public class XMLObjectRepository {
public static WebDriver driver;
public static String URL="http://www.phptravels.net/";
public static String objectRepopath="D:\\Automation\\AutomationFramework\\DesignObjectRepository\\src\\objectRepositoryFile\\OR.xml";
public static String browserpath="D:\\Automation\\AutomationFramework\\DesignObjectRepository\\Lib\\BrowserDrivers\\geckodriver-v0.18.0-win32\\geckodriver.exe";
public static void main(String[] args) throws Exception {
// TODO Auto-generated
method stub
XMLObjectMap
objMap=new XMLObjectMap(objectRepopath);
System.setProperty("webdriver.gecko.driver", browserpath);
driver=new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10,
TimeUnit.SECONDS);
driver.get(URL);
//Thread.sleep(5000);
//Home page
driver.findElement(objMap.getLocator("lnk_myaccount")).click();
driver.findElement(objMap.getLocator("lnk_login")).click();
//Thread.sleep(5000);
//Login Page
driver.findElement(objMap.getLocator("txt_email")).sendKeys("user@phptravels.com");
driver.findElement(objMap.getLocator("txt_password")).sendKeys("demouser");
driver.findElement(objMap.getLocator("btn_login")).click();
//Thread.sleep(5000);
//My Account
driver.findElement(objMap.getLocator("lnk_accname")).click();
driver.findElement(objMap.getLocator("lnk_logout")).click();
driver.close();
}
}
Now the project structure in eclipse should look like the given bellow image.
No comments:
Post a Comment