It will work similar to the
intellisense property present in Visual Studio i.e when you type a . (dot)
after the object/class name, it will automatically display all methods and
variable of the class as shown in bellow image.
Create a file under objectRepositoryFile package and name it ObjectRepository.java. Then copy the following codes.
ObjectRepository.java
package
objectRepositoryFile;
import
org.openqa.selenium.By;
public class ObjectRepository {
public static class PageObjects{
//FOr Home Page
public static By home_lnk_Login(){
return By.partialLinkText("Login");
}
public static By
home_lnk_myaccount(){
return By.partialLinkText("My
Account");
}
//For LogIn Page
public static By
login_txt_UserName(){
return By.name("username");
}
public static By
login_txt_Password(){
return By.name("password");
}
public static By
login_btn_LogIn(){
return By.xpath(".//*[@id='loginfrm']/div[4]/button");
}
//For MyAccount
page
public static By
myaccount_lnk_accname(){
return By.partialLinkText("John");
}
public static By
myaccount_lnk_Logout(){
return By.partialLinkText("Logout");
}
}
}
Create a file under objectRepositoryMain package and name it IntellObjectRepository.java. Then copy the following codes.
IntellObjectRepository.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
objectRepositoryFile.ObjectRepository.PageObjects;
public class
IntellObjectRepository {
public static WebDriver driver;
public static String URL="http://www.phptravels.net/";
//Login Page
public static By username=PageObjects.login_txt_UserName();
public static By password=PageObjects.login_txt_Password();
public static By acclogin=PageObjects.login_btn_LogIn();
//Home Page
//public
static By
login=By.partialLinkText("Login");
public static By myaccount=PageObjects.home_lnk_myaccount();
public static By login=PageObjects.home_lnk_Login();
//My My Account
page
public static By accname=PageObjects.myaccount_lnk_accname();
public static By logout=PageObjects.myaccount_lnk_Logout();
public static String driverpath="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
//Firefox
System.setProperty("webdriver.gecko.driver", driverpath);
driver=new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10,
TimeUnit.SECONDS);
driver.get(URL);
//Home Page
driver.findElement(myaccount).click();
driver.findElement(login).click();
Thread.sleep(2000);
//login Page
driver.findElement(username).sendKeys("user@phptravels.com");
driver.findElement(password).sendKeys("demouser");
driver.findElement(acclogin).click();
Thread.sleep(2000);
//My My Account
page
driver.findElement(accname).click();
driver.findElement(logout).click();
driver.close();
}
}
Now
the project structure in eclipse should look like as shown in the bellow image.
Run IntellObjectRepository.java to
see the result.

No comments:
Post a Comment