Skip to content

Commit 53fc8b7

Browse files
committed
Page Object pattern implementation
1 parent f57b94a commit 53fc8b7

File tree

18 files changed

+1053
-1
lines changed

18 files changed

+1053
-1
lines changed

page-object/pom.xml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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.12.0-SNAPSHOT</version>
33+
</parent>
34+
<artifactId>page-object</artifactId>
35+
<packaging>pom</packaging>
36+
<modules>
37+
<module>sample-application</module>
38+
<module>test-automation</module>
39+
</modules>
40+
</project>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
<artifactId>page-object</artifactId>
31+
<groupId>com.iluwatar</groupId>
32+
<version>1.12.0-SNAPSHOT</version>
33+
</parent>
34+
<artifactId>sample-application</artifactId>
35+
</project>
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
String currentWorkingDir = System.getProperty("user.dir");
73+
File applicationFile = new File(currentWorkingDir
74+
+ "/page-object/sample-application/src/main/resources/sample-ui/login.html");
75+
76+
// should work for unix like OS (mac, unix etc...)
77+
if (Desktop.isDesktopSupported()) {
78+
try {
79+
Desktop.getDesktop().open(applicationFile);
80+
} catch (IOException e) {
81+
e.printStackTrace();
82+
}
83+
} else {
84+
// java Desktop not supported - above unlikely to work for Windows so try following instead...
85+
try {
86+
Runtime.getRuntime().exec("cmd.exe start " + applicationFile);
87+
} catch (IOException e) {
88+
e.printStackTrace();
89+
}
90+
91+
}
92+
}
93+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
24+
username - admin
25+
password - password
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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
table {
2+
font-size: 16px;
3+
border-collapse: collapse;
4+
}
5+
6+
th {
7+
background-color: #FFFFFF;
8+
border: 1px solid black;
9+
color: black;
10+
width: 150px;
11+
height: 20px;
12+
}
13+
14+
td {
15+
border: 1px solid black;
16+
background-color: white;
17+
}
18+
19+
th, td {
20+
padding: 15px;
21+
text-align: left;
22+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body {
2+
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
3+
}

0 commit comments

Comments
 (0)