diff --git a/page-object/pom.xml b/page-object/pom.xml new file mode 100644 index 000000000..00f87ac9d --- /dev/null +++ b/page-object/pom.xml @@ -0,0 +1,40 @@ + + + + 4.0.0 + + com.iluwatar + java-design-patterns + 1.12.0-SNAPSHOT + + page-object + pom + + sample-application + test-automation + + diff --git a/page-object/sample-application/pom.xml b/page-object/sample-application/pom.xml new file mode 100644 index 000000000..74116fdb3 --- /dev/null +++ b/page-object/sample-application/pom.xml @@ -0,0 +1,35 @@ + + + + 4.0.0 + + page-object + com.iluwatar + 1.12.0-SNAPSHOT + + sample-application + diff --git a/page-object/sample-application/src/main/java/com/iluwatar/pageobject/App.java b/page-object/sample-application/src/main/java/com/iluwatar/pageobject/App.java new file mode 100644 index 000000000..155097c73 --- /dev/null +++ b/page-object/sample-application/src/main/java/com/iluwatar/pageobject/App.java @@ -0,0 +1,93 @@ +/** + * The MIT License + * Copyright (c) 2014 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.pageobject; + +import java.awt.Desktop; +import java.io.File; +import java.io.IOException; + +/** + * Page Object pattern wraps an UI component with an application specific API allowing you to + * manipulate the UI elements without having to dig around with the underlying UI technology used. This is + * especially useful for testing as it means your tests will be less brittle. Your tests can concentrate on + * the actual test cases where as the manipulation of the UI can be left to the internals of the page object + * itself. + * + *

+ * Due to this reason, it has become very popular within the test automation community. + * In particular, it is very common in that the page object is used to represent the html pages of a + * web application that is under test. This web application is referred to as AUT (Application Under Test). + * A web browser automation tool/framework like Selenium for instance, is then used to drive the automating + * of the browser navigation and user actions journeys through this web application. Your test class would + * therefore only be responsible for particular test cases and page object would be used by the test class + * for UI manipulation required for the tests. + * + *

+ * In this implementation rather than using Selenium, the HtmlUnit library is used as a replacement to + * represent the specific html elements and to drive the browser. The purpose of this example is just to + * provide a simple version that showcase the intentions of this pattern and how this pattern is used + * in order to understand it. + */ +public final class App { + + private App() { + } + + /** + * Application entry point + * + *

+ * The application under development is a web application. Normally you would probably have a + * backend that is probably implemented in an object-oriented language (e.g. Java) that serves + * the frontend which comprises of a series of HTML, CSS, JS etc... + * + *

+ * For illustrations purposes only, a very simple static html app is used here. This main method + * just fires up this simple web app in a default browser. + * + * @param args arguments + */ + public static void main(String[] args) { + + String currentWorkingDir = System.getProperty("user.dir"); + File applicationFile = new File(currentWorkingDir + + "/page-object/sample-application/src/main/resources/sample-ui/login.html"); + + // should work for unix like OS (mac, unix etc...) + if (Desktop.isDesktopSupported()) { + try { + Desktop.getDesktop().open(applicationFile); + } catch (IOException e) { + e.printStackTrace(); + } + } else { + // java Desktop not supported - above unlikely to work for Windows so try following instead... + try { + Runtime.getRuntime().exec("cmd.exe start " + applicationFile); + } catch (IOException e) { + e.printStackTrace(); + } + + } + } +} diff --git a/page-object/sample-application/src/main/resources/credentials.txt b/page-object/sample-application/src/main/resources/credentials.txt new file mode 100644 index 000000000..6f1bf2fb6 --- /dev/null +++ b/page-object/sample-application/src/main/resources/credentials.txt @@ -0,0 +1,25 @@ +==== + The MIT License + Copyright (c) 2014 Ilkka Seppälä + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +==== + +username - admin +password - password \ No newline at end of file diff --git a/page-object/sample-application/src/main/resources/sample-ui/album-list.html b/page-object/sample-application/src/main/resources/sample-ui/album-list.html new file mode 100644 index 000000000..e02e65cf3 --- /dev/null +++ b/page-object/sample-application/src/main/resources/sample-ui/album-list.html @@ -0,0 +1,60 @@ + + + + + + Album List + + + + +

+

My Album Viewer

+
+ +
+
+ + + + + + + + + + + + + + + +
Album TitleAlbum YearAlbum RatingNumber of SongsArtist
212011A11Adele
+
+
+ + + \ No newline at end of file diff --git a/page-object/sample-application/src/main/resources/sample-ui/album-page.html b/page-object/sample-application/src/main/resources/sample-ui/album-page.html new file mode 100644 index 000000000..5bf843be2 --- /dev/null +++ b/page-object/sample-application/src/main/resources/sample-ui/album-page.html @@ -0,0 +1,74 @@ + + + + + + Album Page + + + +
+

21

+
+ +
+
+
+ + + + + + + + + + + + + + + + + + + +
Title:
Artist:
Year: + +
Rating:
Number of Songs:
+
+
+
+ + + \ No newline at end of file diff --git a/page-object/sample-application/src/main/resources/sample-ui/css/album-list.css b/page-object/sample-application/src/main/resources/sample-ui/css/album-list.css new file mode 100644 index 000000000..9c828e1a0 --- /dev/null +++ b/page-object/sample-application/src/main/resources/sample-ui/css/album-list.css @@ -0,0 +1,22 @@ +table { + font-size: 16px; + border-collapse: collapse; +} + +th { + background-color: #FFFFFF; + border: 1px solid black; + color: black; + width: 150px; + height: 20px; +} + +td { + border: 1px solid black; + background-color: white; +} + +th, td { + padding: 15px; + text-align: left; +} \ No newline at end of file diff --git a/page-object/sample-application/src/main/resources/sample-ui/css/style.css b/page-object/sample-application/src/main/resources/sample-ui/css/style.css new file mode 100644 index 000000000..6efb32df9 --- /dev/null +++ b/page-object/sample-application/src/main/resources/sample-ui/css/style.css @@ -0,0 +1,3 @@ +body { + font-family: "Trebuchet MS", Arial, Helvetica, sans-serif; +} \ No newline at end of file diff --git a/page-object/sample-application/src/main/resources/sample-ui/login.html b/page-object/sample-application/src/main/resources/sample-ui/login.html new file mode 100644 index 000000000..1d7786057 --- /dev/null +++ b/page-object/sample-application/src/main/resources/sample-ui/login.html @@ -0,0 +1,48 @@ + + + + + + Login + + + +
+

Login

+
+ +
+
+ + + + +
Username:
Password:
+ +
+
+ + \ No newline at end of file diff --git a/page-object/test-automation/pom.xml b/page-object/test-automation/pom.xml new file mode 100644 index 000000000..feac74bd9 --- /dev/null +++ b/page-object/test-automation/pom.xml @@ -0,0 +1,48 @@ + + + + 4.0.0 + + page-object + com.iluwatar + 1.12.0-SNAPSHOT + + test-automation + + + junit + junit + test + + + net.sourceforge.htmlunit + htmlunit + 2.22 + + + + diff --git a/page-object/test-automation/src/main/java/com/iluwatar/Page.java b/page-object/test-automation/src/main/java/com/iluwatar/Page.java new file mode 100644 index 000000000..515872efc --- /dev/null +++ b/page-object/test-automation/src/main/java/com/iluwatar/Page.java @@ -0,0 +1,57 @@ +/** + * The MIT License + * Copyright (c) 2014 Ilkka Seppälä + *

+ * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + *

+ * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + *

+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar; + +import com.gargoylesoftware.htmlunit.WebClient; + +/** + * Encapsulation for a generic 'Page' + */ +public abstract class Page { + + /** + * Application Under Test path + * This directory location is where html web pages are located + */ + public static final String AUT_PATH = "../sample-application/src/main/resources/sample-ui/"; + + protected WebClient webClient; + + /** + * Constructor + * + * @param webClient {@link WebClient} + */ + public Page(WebClient webClient) { + this.webClient = webClient; + } + + /** + * Checks that the current page is actually the page this page object represents + * + * @return true if so, otherwise false + */ + public abstract boolean isAt(); + + +} diff --git a/page-object/test-automation/src/main/java/com/iluwatar/pages/AlbumListPage.java b/page-object/test-automation/src/main/java/com/iluwatar/pages/AlbumListPage.java new file mode 100644 index 000000000..00edc5688 --- /dev/null +++ b/page-object/test-automation/src/main/java/com/iluwatar/pages/AlbumListPage.java @@ -0,0 +1,95 @@ +/** + * The MIT License + * Copyright (c) 2014 Ilkka Seppälä + *

+ * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + *

+ * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + *

+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.pages; + +import com.gargoylesoftware.htmlunit.WebClient; +import com.gargoylesoftware.htmlunit.html.HtmlAnchor; +import com.gargoylesoftware.htmlunit.html.HtmlPage; +import com.iluwatar.Page; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.util.List; + +/** + * Page Object encapsulating the Album List page (album-list.html) + */ +public class AlbumListPage extends Page { + + private static final String ALBUM_LIST_HTML_FILE = "album-list.html"; + private static final String PAGE_URL = "file:" + AUT_PATH + ALBUM_LIST_HTML_FILE; + + private HtmlPage page; + + private List albumLinks; + + + /** + * Constructor + */ + public AlbumListPage(WebClient webClient) { + super(webClient); + try { + page = this.webClient.getPage(PAGE_URL); + + // uses XPath to find list of html anchor tags with the class album in it + albumLinks = (List) page.getByXPath("//tr[@class='album']//a"); + + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + } + + /** + * {@inheritDoc} + */ + @Override + public boolean isAt() { + return "Album List".equals(page.getTitleText()); + } + + /** + * Selects an album by the given album title + * + * @param albumTitle the title of the album to click + * @return the album page + */ + public AlbumPage selectAlbum(String albumTitle) { + for (HtmlAnchor anchor : albumLinks) { + if (anchor.getTextContent().equals(albumTitle)) { + try { + anchor.click(); + return new AlbumPage(webClient); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + throw new IllegalArgumentException("No links with the album title: " + albumTitle); + } + + +} diff --git a/page-object/test-automation/src/main/java/com/iluwatar/pages/AlbumPage.java b/page-object/test-automation/src/main/java/com/iluwatar/pages/AlbumPage.java new file mode 100644 index 000000000..5f72be8fd --- /dev/null +++ b/page-object/test-automation/src/main/java/com/iluwatar/pages/AlbumPage.java @@ -0,0 +1,185 @@ +/** + * The MIT License + * Copyright (c) 2014 Ilkka Seppälä + *

+ * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + *

+ * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + *

+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.pages; + +import com.gargoylesoftware.htmlunit.WebClient; +import com.gargoylesoftware.htmlunit.html.HtmlNumberInput; +import com.gargoylesoftware.htmlunit.html.HtmlOption; +import com.gargoylesoftware.htmlunit.html.HtmlPage; +import com.gargoylesoftware.htmlunit.html.HtmlSelect; +import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput; +import com.gargoylesoftware.htmlunit.html.HtmlTextInput; +import com.iluwatar.Page; + +import java.io.IOException; +import java.net.MalformedURLException; + +/** + * Page Object encapsulating the Album Page (album-page.html) + */ +public class AlbumPage extends Page { + + private static final String ALBUM_PAGE_HTML_FILE = "album-page.html"; + private static final String PAGE_URL = "file:" + AUT_PATH + ALBUM_PAGE_HTML_FILE; + + private HtmlPage page; + + private HtmlTextInput albumTitleInputTextField; + private HtmlTextInput artistInputTextField; + private HtmlSelect albumYearSelectOption; + private HtmlTextInput albumRatingInputTextField; + private HtmlNumberInput numberOfSongsNumberField; + + private HtmlSubmitInput cancelButton; + private HtmlSubmitInput saveButton; + + /** + * Constructor + */ + public AlbumPage(WebClient webClient) { + super(webClient); + try { + page = this.webClient.getPage(PAGE_URL); + initializeHtmlElements(); + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } + + } + + + private void initializeHtmlElements() { + albumTitleInputTextField = (HtmlTextInput) page.getElementById("albumTitle"); + artistInputTextField = (HtmlTextInput) page.getElementById("albumArtist"); + albumYearSelectOption = (HtmlSelect) page.getElementById("albumYear"); + albumRatingInputTextField = (HtmlTextInput) page.getElementById("albumRating"); + numberOfSongsNumberField = (HtmlNumberInput) page.getElementById("numberOfSongs"); + + cancelButton = (HtmlSubmitInput) page.getElementById("cancelButton"); + saveButton = (HtmlSubmitInput) page.getElementById("saveButton"); + } + + + /** + * {@inheritDoc} + */ + @Override + public boolean isAt() { + return "Album Page".equals(page.getTitleText()); + } + + + /** + * Sets the album title input text field + * + * @param albumTitle the new album title value to set + * @return {@link AlbumPage} + */ + public AlbumPage changeAlbumTitle(String albumTitle) { + albumTitleInputTextField.setText(albumTitle); + return this; + } + + + /** + * Sets the artist input text field + * + * @param artist the new artist value to set + * @return {@link AlbumPage} + */ + public AlbumPage changeArtist(String artist) { + artistInputTextField.setText(artist); + return this; + } + + + /** + * Selects the select's option value based on the year value given + * + * @param year the new year value to set + * @return {@link AlbumPage} + */ + public AlbumPage changeAlbumYear(int year) { + HtmlOption yearOption = albumYearSelectOption.getOptionByValue(Integer.toString(year)); + albumYearSelectOption.setSelectedAttribute(yearOption, true); + return this; + } + + + /** + * Sets the album rating input text field + * + * @param albumRating the new album rating value to set + * @return {@link AlbumPage} + */ + public AlbumPage changeAlbumRating(String albumRating) { + albumRatingInputTextField.setText(albumRating); + return this; + } + + /** + * Sets the number of songs number input field + * + * @param numberOfSongs the new number of songs value to be set + * @return {@link AlbumPage} + */ + public AlbumPage changeNumberOfSongs(int numberOfSongs) { + numberOfSongsNumberField.setText(Integer.toString(numberOfSongs)); + return this; + } + + + /** + * Cancel changes made by clicking the cancel button + * + * @return {@link AlbumListPage} + */ + public AlbumListPage cancelChanges() { + try { + cancelButton.click(); + } catch (IOException e) { + e.printStackTrace(); + } + return new AlbumListPage(webClient); + } + + + /** + * Saves changes made by clicking the save button + * + * @return {@link AlbumPage} + */ + public AlbumPage saveChanges() { + try { + saveButton.click(); + } catch (IOException e) { + e.printStackTrace(); + } + return this; + } + +} diff --git a/page-object/test-automation/src/main/java/com/iluwatar/pages/LoginPage.java b/page-object/test-automation/src/main/java/com/iluwatar/pages/LoginPage.java new file mode 100644 index 000000000..19810f354 --- /dev/null +++ b/page-object/test-automation/src/main/java/com/iluwatar/pages/LoginPage.java @@ -0,0 +1,120 @@ +/** + * The MIT License + * Copyright (c) 2014 Ilkka Seppälä + *

+ * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + *

+ * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + *

+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.pages; + +import com.gargoylesoftware.htmlunit.WebClient; +import com.gargoylesoftware.htmlunit.html.HtmlPage; +import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput; +import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput; +import com.gargoylesoftware.htmlunit.html.HtmlTextInput; +import com.iluwatar.Page; +import java.io.IOException; +import java.net.MalformedURLException; + +/** + * Page Object encapsulating the Login Page (login.html) + */ +public class LoginPage extends Page { + + private static final String LOGIN_PAGE_HTML_FILE = "login.html"; + private static final String PAGE_URL = "file:" + AUT_PATH + LOGIN_PAGE_HTML_FILE; + + private HtmlPage page; + + private HtmlTextInput usernameInputTextField; + private HtmlPasswordInput passwordInputPasswordField; + private HtmlSubmitInput loginButton; + + + /** + * Constructor + * + * @param webClient {@link WebClient} + */ + public LoginPage(WebClient webClient) { + super(webClient); + try { + page = this.webClient.getPage(PAGE_URL); + + usernameInputTextField = (HtmlTextInput) page.getElementById("username"); + passwordInputPasswordField = (HtmlPasswordInput) page.getElementById("password"); + loginButton = (HtmlSubmitInput) page.getElementById("loginButton"); + + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + + /** + * {@inheritDoc} + */ + @Override + public boolean isAt() { + return "Login".equals(page.getTitleText()); + } + + + /** + * Enters the username into the username input text field + * + * @param username the username to enter + * @return {@link LoginPage} + */ + public LoginPage enterUsername(String username) { + usernameInputTextField.setText(username); + return this; + } + + + /** + * Enters the password into the password input password field + * + * @param password the password to enter + * @return {@link LoginPage} + */ + public LoginPage enterPassword(String password) { + passwordInputPasswordField.setText(password); + return this; + } + + + /** + * Clicking on the login button to 'login' + * + * @return {@link AlbumListPage} + * - this is the page that user gets navigated to once successfully logged in + */ + public AlbumListPage login() { + try { + loginButton.click(); + } catch (IOException e) { + e.printStackTrace(); + } + return new AlbumListPage(webClient); + } + + +} diff --git a/page-object/test-automation/src/test/java/com/iluwatar/pageobject/AlbumListPageTest.java b/page-object/test-automation/src/test/java/com/iluwatar/pageobject/AlbumListPageTest.java new file mode 100644 index 000000000..3b420e366 --- /dev/null +++ b/page-object/test-automation/src/test/java/com/iluwatar/pageobject/AlbumListPageTest.java @@ -0,0 +1,45 @@ +/** + * The MIT License + * Copyright (c) 2014 Ilkka Seppälä + *

+ * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + *

+ * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + *

+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.pageobject; + + +import com.gargoylesoftware.htmlunit.WebClient; +import com.iluwatar.pages.AlbumListPage; +import com.iluwatar.pages.AlbumPage; +import org.junit.Test; + +import static org.junit.Assert.assertTrue; + + +public class AlbumListPageTest { + + @Test + public void testSelectAlbum() { + AlbumListPage albumListPage = new AlbumListPage(new WebClient()); + + AlbumPage albumPage = albumListPage.selectAlbum("21"); + + assertTrue(albumPage.isAt()); + } + +} diff --git a/page-object/test-automation/src/test/java/com/iluwatar/pageobject/AlbumPageTest.java b/page-object/test-automation/src/test/java/com/iluwatar/pageobject/AlbumPageTest.java new file mode 100644 index 000000000..8ca464165 --- /dev/null +++ b/page-object/test-automation/src/test/java/com/iluwatar/pageobject/AlbumPageTest.java @@ -0,0 +1,56 @@ +/** + * The MIT License + * Copyright (c) 2014 Ilkka Seppälä + *

+ * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + *

+ * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + *

+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.pageobject; + +import com.gargoylesoftware.htmlunit.WebClient; +import com.iluwatar.pages.AlbumListPage; +import com.iluwatar.pages.AlbumPage; +import org.junit.Test; + +import static org.junit.Assert.assertTrue; + +public class AlbumPageTest { + + private AlbumPage albumPage = new AlbumPage(new WebClient()); + + @Test + public void testSaveAlbum() { + + AlbumPage albumPageAfterChanges = albumPage.changeAlbumTitle("25") + .changeArtist("Adele Laurie Blue Adkins") + .changeAlbumYear(2015) + .changeAlbumRating("B") + .changeNumberOfSongs(20) + .saveChanges(); + + assertTrue(albumPageAfterChanges.isAt()); + + } + + @Test + public void testCancelChanges() { + AlbumListPage albumListPage = albumPage.cancelChanges(); + assertTrue(albumListPage.isAt()); + } + +} diff --git a/page-object/test-automation/src/test/java/com/iluwatar/pageobject/LoginPageTest.java b/page-object/test-automation/src/test/java/com/iluwatar/pageobject/LoginPageTest.java new file mode 100644 index 000000000..322a4b97c --- /dev/null +++ b/page-object/test-automation/src/test/java/com/iluwatar/pageobject/LoginPageTest.java @@ -0,0 +1,45 @@ +/** + * The MIT License + * Copyright (c) 2014 Ilkka Seppälä + *

+ * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + *

+ * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + *

+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.pageobject; + +import com.gargoylesoftware.htmlunit.WebClient; +import com.iluwatar.pages.AlbumListPage; +import com.iluwatar.pages.LoginPage; +import org.junit.Test; + +import static org.junit.Assert.assertTrue; + +public class LoginPageTest { + + @Test + public void testLogin() { + LoginPage loginPage = new LoginPage(new WebClient()); + + AlbumListPage albumListPage = loginPage.enterUsername("admin") + .enterPassword("password") + .login(); + + assertTrue(albumListPage.isAt()); + } + +} diff --git a/pom.xml b/pom.xml index 0e3318fb6..0e28a8081 100644 --- a/pom.xml +++ b/pom.xml @@ -31,7 +31,7 @@ UTF-8 5.0.1.Final 4.2.4.RELEASE - 1.3.3.RELEASE + 1.3.3.RELEASE 1.9.2.RELEASE 1.4.190 4.12 @@ -126,6 +126,7 @@ semaphore hexagonal abstract-document + page-object