Skip to content

Commit 1f1fcae

Browse files
stailleboisiluwatar
authored andcommitted
Refactor Page-object pattern (iluwatar#812)
* Refactor : create 2 sub-modules for page-object pattern * Replace e.printStrackTrace with logger
1 parent 2aa9e78 commit 1f1fcae

19 files changed

Lines changed: 1121 additions & 18 deletions

File tree

page-object/pom.xml

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,9 @@
3232
<version>1.21.0-SNAPSHOT</version>
3333
</parent>
3434
<artifactId>page-object</artifactId>
35-
<dependencies>
36-
<dependency>
37-
<groupId>org.junit.jupiter</groupId>
38-
<artifactId>junit-jupiter-api</artifactId>
39-
<scope>test</scope>
40-
</dependency>
41-
<dependency>
42-
<groupId>org.junit.jupiter</groupId>
43-
<artifactId>junit-jupiter-engine</artifactId>
44-
<scope>test</scope>
45-
</dependency>
46-
<dependency>
47-
<groupId>net.sourceforge.htmlunit</groupId>
48-
<artifactId>htmlunit</artifactId>
49-
</dependency>
50-
</dependencies>
51-
</project>
35+
<packaging>pom</packaging>
36+
<modules>
37+
<module>sample-application</module>
38+
<module>test-automation</module>
39+
</modules>
40+
</project>
48.5 KB
Loading
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>
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-2016 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.21.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-2016 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+
import org.slf4j.Logger;
30+
import org.slf4j.LoggerFactory;
31+
32+
/**
33+
* Page Object pattern wraps an UI component with an application specific API allowing you to
34+
* manipulate the UI elements without having to dig around with the underlying UI technology used. This is
35+
* especially useful for testing as it means your tests will be less brittle. Your tests can concentrate on
36+
* the actual test cases where as the manipulation of the UI can be left to the internals of the page object
37+
* itself.
38+
*
39+
* <p>
40+
* Due to this reason, it has become very popular within the test automation community.
41+
* In particular, it is very common in that the page object is used to represent the html pages of a
42+
* web application that is under test. This web application is referred to as AUT (Application Under Test).
43+
* A web browser automation tool/framework like Selenium for instance, is then used to drive the automating
44+
* of the browser navigation and user actions journeys through this web application. Your test class would
45+
* therefore only be responsible for particular test cases and page object would be used by the test class
46+
* for UI manipulation required for the tests.
47+
*
48+
* <p>
49+
* In this implementation rather than using Selenium, the HtmlUnit library is used as a replacement to
50+
* represent the specific html elements and to drive the browser. The purpose of this example is just to
51+
* provide a simple version that showcase the intentions of this pattern and how this pattern is used
52+
* in order to understand it.
53+
*/
54+
public final class App {
55+
56+
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
57+
private App() {
58+
}
59+
60+
/**
61+
* Application entry point
62+
*
63+
* <p>
64+
* The application under development is a web application. Normally you would probably have a
65+
* backend that is probably implemented in an object-oriented language (e.g. Java) that serves
66+
* the frontend which comprises of a series of HTML, CSS, JS etc...
67+
*
68+
* <p>
69+
* For illustrations purposes only, a very simple static html app is used here. This main method
70+
* just fires up this simple web app in a default browser.
71+
*
72+
* @param args arguments
73+
*/
74+
public static void main(String[] args) {
75+
76+
try {
77+
File applicationFile = new File(App.class.getClassLoader().getResource("sample-ui/login.html").getPath());
78+
79+
// should work for unix like OS (mac, unix etc...)
80+
if (Desktop.isDesktopSupported()) {
81+
Desktop.getDesktop().open(applicationFile);
82+
83+
} else {
84+
// java Desktop not supported - above unlikely to work for Windows so try following instead...
85+
Runtime.getRuntime().exec("cmd.exe start " + applicationFile);
86+
}
87+
88+
} catch (IOException ex) {
89+
LOGGER.error("An error occured.", ex);
90+
}
91+
92+
}
93+
}
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-2016 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-2016 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: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 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+
25+
The MIT License
26+
Copyright (c) 2014-2016 Ilkka Seppälä
27+
28+
Permission is hereby granted, free of charge, to any person obtaining a copy
29+
of this software and associated documentation files (the "Software"), to deal
30+
in the Software without restriction, including without limitation the rights
31+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
32+
copies of the Software, and to permit persons to whom the Software is
33+
furnished to do so, subject to the following conditions:
34+
35+
The above copyright notice and this permission notice shall be included in
36+
all copies or substantial portions of the Software.
37+
38+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
39+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
40+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
41+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
42+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
43+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
44+
THE SOFTWARE.
45+
46+
-->
47+
table {
48+
font-size: 16px;
49+
border-collapse: collapse;
50+
}
51+
52+
th {
53+
background-color: #FFFFFF;
54+
border: 1px solid black;
55+
color: black;
56+
width: 150px;
57+
height: 20px;
58+
}
59+
60+
td {
61+
border: 1px solid black;
62+
background-color: white;
63+
}
64+
65+
th, td {
66+
padding: 15px;
67+
text-align: left;
68+
}

0 commit comments

Comments
 (0)