├── README.md
├── pom.xml
└── src
└── test
└── java
└── com
└── example
├── pageobjects
├── GitHubHomePage.java
├── GitHubLoginPage.java
└── GitHubPage.java
├── setup
└── SeleniumDriver.java
└── test
└── GitHubLoginTest.java
/README.md:
--------------------------------------------------------------------------------
1 | page-objects-webdriver
2 | ======================
3 | [](https://buildhive.cloudbees.com/job/michal-lipski/job/page-objects-webdriver/)
4 |
5 | Example of Page Object pattern with selenium WebDriver.
6 |
7 |
8 | Take a look at: http://www.fluentlenium.org/ for more complex tool.
9 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | page-objects
8 | page-objects
9 | 1.0
10 |
11 |
12 | UTF-8
13 |
14 |
15 |
16 |
17 | org.seleniumhq.selenium
18 | selenium-java
19 | 3.4.0
20 |
21 |
22 | junit
23 | junit
24 | 4.12
25 |
26 |
27 | org.easytesting
28 | fest-assert-core
29 | 2.0M6
30 |
31 |
32 |
33 |
34 |
35 | org.apache.maven.plugins
36 | maven-compiler-plugin
37 |
38 | 1.6
39 | 1.6
40 |
41 |
42 |
43 |
44 | org.apache.maven.plugins
45 | maven-resources-plugin
46 | 2.6
47 |
48 |
49 | UTF-8
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
--------------------------------------------------------------------------------
/src/test/java/com/example/pageobjects/GitHubHomePage.java:
--------------------------------------------------------------------------------
1 | package com.example.pageobjects;
2 |
3 | import org.openqa.selenium.support.ui.ExpectedCondition;
4 | import org.openqa.selenium.support.ui.ExpectedConditions;
5 |
6 | /**
7 | * Page object representing github home page.
8 | *
9 | * @author mlipski
10 | */
11 | public class GitHubHomePage extends GitHubPage {
12 |
13 | @Override
14 | protected ExpectedCondition getPageLoadCondition() {
15 | return ExpectedConditions.titleContains("The world's leading software development platform · GitHub");
16 | }
17 |
18 | @Override
19 | public String getPageUrl() {
20 | return "";
21 | }
22 |
23 | public GitHubLoginPage goToLoginPage() {
24 | return new GitHubLoginPage().openPage(GitHubLoginPage.class);
25 | }
26 |
27 | public GitHubHomePage open() {
28 | return new GitHubHomePage().openPage(GitHubHomePage.class);
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/src/test/java/com/example/pageobjects/GitHubLoginPage.java:
--------------------------------------------------------------------------------
1 | package com.example.pageobjects;
2 |
3 | import org.openqa.selenium.WebElement;
4 | import org.openqa.selenium.support.FindBy;
5 | import org.openqa.selenium.support.ui.ExpectedCondition;
6 | import org.openqa.selenium.support.ui.ExpectedConditions;
7 |
8 | /**
9 | * Page object representing github login page.
10 | */
11 | public class GitHubLoginPage extends GitHubPage {
12 |
13 | @FindBy(id = "login_field")
14 | WebElement loginField;
15 |
16 | @FindBy(name = "password")
17 | WebElement passwordField;
18 |
19 | @FindBy(name = "commit")
20 | WebElement commitButton;
21 |
22 | @FindBy(className = "flash-error")
23 | WebElement errorBox;
24 |
25 | @Override
26 | protected ExpectedCondition getPageLoadCondition() {
27 | return ExpectedConditions.visibilityOf(loginField);
28 | }
29 |
30 | @Override
31 | public String getPageUrl() {
32 | return "/login";
33 | }
34 |
35 | public void login(String login, String password) {
36 | loginField.sendKeys(login);
37 | passwordField.sendKeys(password);
38 | commitButton.click();
39 |
40 | }
41 |
42 | public boolean isLoginError() {
43 | return errorBox.isDisplayed();
44 | }
45 |
46 | public String getErrorMessage() {
47 | return errorBox.getText();
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/src/test/java/com/example/pageobjects/GitHubPage.java:
--------------------------------------------------------------------------------
1 | package com.example.pageobjects;
2 |
3 | import static com.example.setup.SeleniumDriver.getDriver;
4 |
5 | import java.util.concurrent.TimeUnit;
6 |
7 | import org.openqa.selenium.support.PageFactory;
8 | import org.openqa.selenium.support.ui.ExpectedCondition;
9 | import org.openqa.selenium.support.ui.FluentWait;
10 | import org.openqa.selenium.support.ui.Wait;
11 |
12 | public abstract class GitHubPage {
13 |
14 | private static final String BASE_URL = "https://github.com";
15 | private static final int LOAD_TIMEOUT = 30;
16 | private static final int REFRESH_RATE = 2;
17 |
18 | public T openPage(Class clazz) {
19 | T page = PageFactory.initElements(getDriver(), clazz);
20 | getDriver().get(BASE_URL + getPageUrl());
21 | ExpectedCondition pageLoadCondition = ((GitHubPage) page).getPageLoadCondition();
22 | waitForPageToLoad(pageLoadCondition);
23 | return page;
24 | }
25 |
26 | private void waitForPageToLoad(ExpectedCondition pageLoadCondition) {
27 | Wait wait = new FluentWait(getDriver())
28 | .withTimeout(LOAD_TIMEOUT, TimeUnit.SECONDS)
29 | .pollingEvery(REFRESH_RATE, TimeUnit.SECONDS);
30 |
31 | wait.until(pageLoadCondition);
32 | }
33 |
34 | /**
35 | * Provides condition when page can be considered as fully loaded.
36 | *
37 | * @return
38 | */
39 | protected abstract ExpectedCondition getPageLoadCondition();
40 |
41 | /**
42 | * Provides page relative URL/
43 | *
44 | * @return
45 | */
46 | public abstract String getPageUrl();
47 | }
48 |
--------------------------------------------------------------------------------
/src/test/java/com/example/setup/SeleniumDriver.java:
--------------------------------------------------------------------------------
1 | package com.example.setup;
2 |
3 | import org.openqa.selenium.WebDriver;
4 | import org.openqa.selenium.firefox.FirefoxDriver;
5 | import org.openqa.selenium.htmlunit.HtmlUnitDriver;
6 |
7 | /**
8 | * Selenium driver wrapper
9 | *
10 | * @author mlipski
11 | */
12 | public class SeleniumDriver {
13 |
14 | static WebDriver driver;
15 |
16 | public static WebDriver getDriver() {
17 | if (driver == null) {
18 | driver = new FirefoxDriver(); //can be replaced with HtmlUnitDriver for better performance
19 | }
20 | return driver;
21 | }
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/src/test/java/com/example/test/GitHubLoginTest.java:
--------------------------------------------------------------------------------
1 | package com.example.test;
2 |
3 | import static com.example.setup.SeleniumDriver.getDriver;
4 | import static org.fest.assertions.api.Assertions.assertThat;
5 |
6 | import com.example.pageobjects.GitHubHomePage;
7 | import com.example.pageobjects.GitHubLoginPage;
8 | import org.junit.AfterClass;
9 | import org.junit.Test;
10 |
11 | public class GitHubLoginTest {
12 |
13 | @AfterClass
14 | public static void tearDown() {
15 | getDriver().close();
16 | }
17 |
18 | @Test
19 | public void should_not_login_with_wrong_credentials() {
20 | //given
21 | GitHubLoginPage loginPage = new GitHubHomePage().open().goToLoginPage();
22 | //when
23 | loginPage.login("user", "password");
24 | //then
25 | assertThat(loginPage.isLoginError()).isTrue();
26 | }
27 | }
28 |
--------------------------------------------------------------------------------