Within your web app's UI there are areas that your tests interact with. A Page Object simply models these as objects within the test code. This reduces the amount of duplicated code and means that if the UI changes, the fix need only be applied in one place.
Example for Page Object:
Homepage.java
package com.sai.pageobjects;
import com.thoughtworks.selenium.Selenium;
public class HomePage {
private Selenium selenium;
public HomePage(Selenium selenium) {
if (!selenium.getText("//div[2]/div[2]/div[2]/div/div").equals("Now you can use Gmail in more languages! Learn more")) {
throw new IllegalStateException("This is not Home Page of logged in user, current page" +
"is: " +selenium.getLocation());
}
}
public HomePage manageProfile() {
// Page encapsulation to manage profile functionality
return new HomePage(selenium);
}
}
import com.thoughtworks.selenium.Selenium;
public class HomePage {
private Selenium selenium;
public HomePage(Selenium selenium) {
if (!selenium.getText("//div[2]/div[2]/div[2]/div/div").equals("Now you can use Gmail in more languages! Learn more")) {
throw new IllegalStateException("This is not Home Page of logged in user, current page" +
"is: " +selenium.getLocation());
}
}
public HomePage manageProfile() {
// Page encapsulation to manage profile functionality
return new HomePage(selenium);
}
}
SignInPage.java
package com.sai.pageobjects;
import com.thoughtworks.selenium.Selenium;
public class SignInPage {
private Selenium selenium;
public SignInPage(Selenium selenium) {
this.selenium = selenium;
if(!selenium.getTitle().equals("Gmail: Email from Google")) {
throw new IllegalStateException("This is not sign in page, current page is: "
+selenium.getLocation());
}
}
/**
* Login as valid user
*
* @param userName
* @param password
* @return HomePage object
*/
public HomePage loginValidUser(String userName, String password) {
selenium.type("Email", userName);
selenium.type("Passwd", password);
selenium.click("signIn");
selenium.waitForPageToLoad("30000");
return null ;
}
}
testLogin.java
package com.sai.pageobjects;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.server.SeleniumServer;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.SeleneseTestCase;
@SuppressWarnings("deprecation")
public class TestLogin extends SeleneseTestCase{
@Before
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.com/");
SeleniumServer seleniumServer = new SeleniumServer();
seleniumServer.boot();
selenium.start();
selenium.windowFocus();
selenium.windowMaximize();
}
@Test
public void testLogin() throws Throwable {
selenium.open("/");
selenium.click("//li[7]/a/span[2]");
Thread.sleep(10000);
SignInPage signInPage = new SignInPage(selenium);
HomePage homePage = signInPage.loginValidUser("username", "password");
Assert.assertEquals(selenium.isElementPresent("compose button"),
"Login was unsuccessful");
}
}
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.server.SeleniumServer;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.SeleneseTestCase;
@SuppressWarnings("deprecation")
public class TestLogin extends SeleneseTestCase{
@Before
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.com/");
SeleniumServer seleniumServer = new SeleniumServer();
seleniumServer.boot();
selenium.start();
selenium.windowFocus();
selenium.windowMaximize();
}
@Test
public void testLogin() throws Throwable {
selenium.open("/");
selenium.click("//li[7]/a/span[2]");
Thread.sleep(10000);
SignInPage signInPage = new SignInPage(selenium);
HomePage homePage = signInPage.loginValidUser("username", "password");
Assert.assertEquals(selenium.isElementPresent("compose button"),
"Login was unsuccessful");
}
}