Skip to content

Commit b46d2de

Browse files
authored
Merge pull request iluwatar#434 from colinbut/master
iluwatar#229 Page Object Pattern
2 parents ff8037e + f182e87 commit b46d2de

File tree

18 files changed

+1051
-0
lines changed

18 files changed

+1051
-0
lines changed

page-object/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
layout: pattern
3+
title: Page Object
4+
folder: page-object
5+
permalink: /patterns/page-object/
6+
categories: Testing
7+
tags:
8+
- Testing
9+
- Web Development
10+
- Encapsulation
11+
---
12+
13+
## Intent
14+
15+
Page Object encapsulates the UI, hiding the underlying UI widgetry of an application (commonly a web application) and providing an application-specific API to allow the manipulation of UI components required for tests. In doing so, it allows the test class itself to focus on the test logic instead.
16+
17+
18+
![alt text](./etc/page-object.png "Page Object")
19+
20+
21+
## Applicability
22+
23+
Use the Page Object pattern when
24+
25+
* You are writing automated tests for your web application and you want to separate the UI manipulation required for the tests from the actual test logic.
26+
* Make your tests less brittle, and more readable and robust
27+
28+
## Credits
29+
30+
* [Martin Fowler - PageObject](http://martinfowler.com/bliki/PageObject.html)
31+
* [Selenium - Page Objects](https://github.com/SeleniumHQ/selenium/wiki/PageObjects)
32+

page-object/etc/page-object.png

48.5 KB
Loading

page-object/etc/page-object.ucls

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<class-diagram version="1.1.9" icons="true" always-add-relationships="false" generalizations="true" realizations="true"
3+
associations="true" dependencies="false" nesting-relationships="true">
4+
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
5+
sort-features="false" accessors="true" visibility="true">
6+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
7+
<operations public="true" package="true" protected="true" private="true" static="true"/>
8+
</classifier-display>
9+
<association-display labels="true" multiplicity="true"/>
10+
</class-diagram>

page-object/pom.xml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
4+
The MIT License
5+
Copyright (c) 2014 Ilkka Seppälä
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
25+
-->
26+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
27+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
28+
<modelVersion>4.0.0</modelVersion>
29+
<parent>
30+
<groupId>com.iluwatar</groupId>
31+
<artifactId>java-design-patterns</artifactId>
32+
<version>1.13.0-SNAPSHOT</version>
33+
</parent>
34+
<artifactId>page-object</artifactId>
35+
<dependencies>
36+
<dependency>
37+
<groupId>junit</groupId>
38+
<artifactId>junit</artifactId>
39+
<scope>test</scope>
40+
</dependency>
41+
<dependency>
42+
<groupId>net.sourceforge.htmlunit</groupId>
43+
<artifactId>htmlunit</artifactId>
44+
</dependency>
45+
</dependencies>
46+
</project>
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.pageobject;
24+
25+
import java.awt.Desktop;
26+
import java.io.File;
27+
import java.io.IOException;
28+
29+
/**
30+
* Page Object pattern wraps an UI component with an application specific API allowing you to
31+
* manipulate the UI elements without having to dig around with the underlying UI technology used. This is
32+
* especially useful for testing as it means your tests will be less brittle. Your tests can concentrate on
33+
* the actual test cases where as the manipulation of the UI can be left to the internals of the page object
34+
* itself.
35+
*
36+
* <p>
37+
* Due to this reason, it has become very popular within the test automation community.
38+
* In particular, it is very common in that the page object is used to represent the html pages of a
39+
* web application that is under test. This web application is referred to as AUT (Application Under Test).
40+
* A web browser automation tool/framework like Selenium for instance, is then used to drive the automating
41+
* of the browser navigation and user actions journeys through this web application. Your test class would
42+
* therefore only be responsible for particular test cases and page object would be used by the test class
43+
* for UI manipulation required for the tests.
44+
*
45+
* <p>
46+
* In this implementation rather than using Selenium, the HtmlUnit library is used as a replacement to
47+
* represent the specific html elements and to drive the browser. The purpose of this example is just to
48+
* provide a simple version that showcase the intentions of this pattern and how this pattern is used
49+
* in order to understand it.
50+
*/
51+
public final class App {
52+
53+
private App() {
54+
}
55+
56+
/**
57+
* Application entry point
58+
*
59+
* <p>
60+
* The application under development is a web application. Normally you would probably have a
61+
* backend that is probably implemented in an object-oriented language (e.g. Java) that serves
62+
* the frontend which comprises of a series of HTML, CSS, JS etc...
63+
*
64+
* <p>
65+
* For illustrations purposes only, a very simple static html app is used here. This main method
66+
* just fires up this simple web app in a default browser.
67+
*
68+
* @param args arguments
69+
*/
70+
public static void main(String[] args) {
71+
72+
try {
73+
File applicationFile = new File(App.class.getClassLoader().getResource("sample-ui/login.html").getPath());
74+
75+
// should work for unix like OS (mac, unix etc...)
76+
if (Desktop.isDesktopSupported()) {
77+
Desktop.getDesktop().open(applicationFile);
78+
79+
} else {
80+
// java Desktop not supported - above unlikely to work for Windows so try following instead...
81+
Runtime.getRuntime().exec("cmd.exe start " + applicationFile);
82+
}
83+
84+
} catch (IOException ex) {
85+
ex.printStackTrace();
86+
}
87+
88+
}
89+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<!--
2+
3+
The MIT License
4+
Copyright (c) 2014 Ilkka Seppälä
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
THE SOFTWARE.
23+
24+
-->
25+
<!DOCTYPE html>
26+
<html lang="en">
27+
<head>
28+
<meta charset="UTF-8">
29+
<title>Album List</title>
30+
<link rel="stylesheet" href="css/style.css">
31+
<link rel="stylesheet" href="css/album-list.css">
32+
</head>
33+
<body>
34+
<header>
35+
<h1>My Album Viewer</h1>
36+
</header>
37+
38+
<section>
39+
<div>
40+
<table>
41+
<tr>
42+
<th>Album Title</th>
43+
<th>Album Year</th>
44+
<th>Album Rating</th>
45+
<th>Number of Songs</th>
46+
<th>Artist</th>
47+
</tr>
48+
<tr class="album">
49+
<td><a href="album-page.html">21</a></td>
50+
<td>2011</td>
51+
<td>A</td>
52+
<td>11</td>
53+
<td>Adele</td>
54+
</tr>
55+
</table>
56+
</div>
57+
</section>
58+
59+
</body>
60+
</html>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<!--
2+
3+
The MIT License
4+
Copyright (c) 2014 Ilkka Seppälä
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
THE SOFTWARE.
23+
24+
-->
25+
<!DOCTYPE html>
26+
<html lang="en">
27+
<head>
28+
<meta charset="UTF-8">
29+
<title>Album Page</title>
30+
<link rel="stylesheet" href="css/style.css">
31+
</head>
32+
<body>
33+
<header>
34+
<h1 id="pageHeader">21</h1>
35+
</header>
36+
37+
<section>
38+
<div>
39+
<form>
40+
<table>
41+
<tr><td>Title:</td><td><input type="text" id="albumTitle" value="21"></td></tr>
42+
<tr><td>Artist:</td><td><input type="text" id="albumArtist" value="Adele"></td></tr>
43+
<tr>
44+
<td>Year:</td>
45+
<td>
46+
<select id="albumYear">
47+
<option>2011</option>
48+
<option>2012</option>
49+
<option>2013</option>
50+
<option>2014</option>
51+
<option>2015</option>
52+
<option>2016</option>
53+
</select>
54+
</td>
55+
</tr>
56+
<tr>
57+
<td>Rating:</td>
58+
<td><input type="text" id="albumRating" value="A"></td>
59+
</tr>
60+
<tr>
61+
<td>Number of Songs:</td>
62+
<td><input type="number" id="numberOfSongs" value="12"></td>
63+
</tr>
64+
<tr>
65+
<td><input type="submit" id="cancelButton" value="Cancel"></td>
66+
<td><input type="submit" id="saveButton" value="Save"></td>
67+
</tr>
68+
</table>
69+
</form>
70+
</div>
71+
</section>
72+
73+
</body>
74+
</html>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<!--
2+
3+
The MIT License
4+
Copyright (c) 2014 Ilkka Seppälä
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
THE SOFTWARE.
23+
24+
-->
25+
table {
26+
font-size: 16px;
27+
border-collapse: collapse;
28+
}
29+
30+
th {
31+
background-color: #FFFFFF;
32+
border: 1px solid black;
33+
color: black;
34+
width: 150px;
35+
height: 20px;
36+
}
37+
38+
td {
39+
border: 1px solid black;
40+
background-color: white;
41+
}
42+
43+
th, td {
44+
padding: 15px;
45+
text-align: left;
46+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!--
2+
3+
The MIT License
4+
Copyright (c) 2014 Ilkka Seppälä
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
THE SOFTWARE.
23+
24+
-->
25+
body {
26+
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
27+
}

0 commit comments

Comments
 (0)