Create a file OR.properties under objectRepositoryFile package.
Then copy the following lines.
OR.properties
#Home Page
#Home Page
home.lnk_myaccount=link:My Account
home.lnk_login=link:Login
#Login Page
login.txt_email=name:username
login.txt_password=name:password
login.btn_login=xpath:.//*[@id='loginfrm']/div[4]/button
#MyAccount Page
myaccount.lnk_accname=link:John
myaccount.lnk_logout=link:Logout
Create 2 classes
under objectRepositoryMain package and name them PropObjectMap.java and PropObjectRepository.java . Then copy following codes in respective file.
PropObjectMap.java
package
objectRepositoryMain;
import
java.io.FileInputStream;
import java.io.IOException;
import
java.util.Properties;
import
org.openqa.selenium.By;
public class PropObjectMap {
Properties
pr;
public PropObjectMap(String
path)
{
pr=new Properties();
try{
FileInputStream
fi=new FileInputStream(path);
pr.load(fi);
fi.close();
}catch(IOException e){
System.out.println("Exception
caught"+ e.getMessage());
}
}
public By getLocator(String
element) throws Exception{
String
locator=pr.getProperty(element);
String
locatorType=locator.split(":")[0];
String
locatorValue=locator.split(":")[1];
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 + "'");
}
}
}
PropObjectRepository.java
package
objectRepositoryMain;
import
java.util.concurrent.TimeUnit;
//import org.openqa.selenium.By;
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.firefox.FirefoxDriver;
//import package1.ObjectMap;
public class PropObjectRepository {
public static WebDriver driver;
public static String URL="http://www.phptravels.net/";
public static String driverpath="D:\\Automation\\AutomationFramework\\DesignObjectRepository\\Lib\\BrowserDrivers\\geckodriver-v0.18.0-win32\\geckodriver.exe";
public static String objectrepopath="D:\\Automation\\AutomationFramework\\TestFramework\\src\\ObjectPackage\\ObjectRepo.properties";
public static void main(String[] args) throws Exception {
PropObjectMap
objMap = new PropObjectMap(objectrepopath);
System.setProperty("webdriver.gecko.driver", "D:\\Automation\\AutomationFramework\\TestFramework\\Lib\\BrowserDrivers\\geckodriver-v0.18.0-win32\\geckodriver.exe");
driver=new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10,
TimeUnit.SECONDS);
driver.get(URL);
Thread.sleep(2000);
//Home page
driver.findElement(objMap.getLocator("home.lnk_myaccount")).click();
driver.findElement(objMap.getLocator("home.lnk_login")).click();
Thread.sleep(2000);
//Login Page
driver.findElement(objMap.getLocator("login.txt_email")).sendKeys("user@phptravels.com");
driver.findElement(objMap.getLocator("login.txt_password")).sendKeys("demouser");
driver.findElement(objMap.getLocator("login.btn_login")).click();
Thread.sleep(2000);
//My Account
driver.findElement(objMap.getLocator("myaccount.lnk_accname")).click();
driver.findElement(objMap.getLocator("myaccount.lnk_logout")).click();
driver.close();
}
}
No comments:
Post a Comment