|
| 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 | +} |
0 commit comments