|
| 1 | +[Home](../README.md) |
| 2 | + |
| 3 | +# WebTester's TestNG Listener |
| 4 | +The support module `webtester-support-testng` provides a custom TestNG Listener. |
| 5 | +It has the following features: |
| 6 | + |
| 7 | +- Life cycle management of class and test level `Browser` instances. |
| 8 | +- Automatic navigation to an entry point to the application under test before each test. |
| 9 | +- Injection of custom configuration properties into the following basic types: `String`, `Integer`, `Long`, `Float`, `Double` and `Boolean` |
| 10 | + |
| 11 | +## Test Listener Life Cycle |
| 12 | +The `WebTesterTestNGListener` extends TestNG's normal life cycle at certain points. |
| 13 | +The following shows the workflow of a test class with two test methods. |
| 14 | + |
| 15 | +- static `Browser` creation and injection |
| 16 | +- injection of configuration properties into static fields annotated with `@ConfigurationValue` |
| 17 | +- static methods annotated with `@BeforeClass` |
| 18 | +- instance `Browser` creation and injection |
| 19 | +- injection of configuration properties into instance fields annotated with `@ConfigurationValue` |
| 20 | +- instance methods annotated with `@BeforeMethod` |
| 21 | +- test method 1 |
| 22 | +- instance methods annotated with `@AfterMethod` |
| 23 | +- instance `Browsers` are closed |
| 24 | +- instance `Browser` creation and injection |
| 25 | +- injection of configuration properties into instance fields annotated with `@ConfigurationValue` |
| 26 | +- instance methods annotated with `@BeforeMethod` |
| 27 | +- test method 2 |
| 28 | +- instance methods annotated with `@AfterMethod` |
| 29 | +- instance `Browsers` are closed |
| 30 | +- static methods annotated with `@AfterClass` |
| 31 | +- static `Browsers` are closed |
| 32 | + |
| 33 | +## Browser Life Cycle Management |
| 34 | +All `Browser's` life cycle is managed on class as well as test level (static / non static fields). |
| 35 | +To use this feature simply declare a `Browser` field in your test class and annotate it with `@Resource`. |
| 36 | +Every uninitialized (null) field annotated in that way will be injected with a new `Browser` instance. |
| 37 | +Fields which are already initialized with a `Browser` will be included in the life cycle handling as well, |
| 38 | +but no new instances are created by the runner. |
| 39 | + |
| 40 | +### Example |
| 41 | +```java |
| 42 | +@Listeners(WebTesterTestNGListener.class) |
| 43 | +public class DifferentBrowserFieldModifiersTest { |
| 44 | + |
| 45 | + // A pre-initialized Browser which will not be initialized with a new |
| 46 | + // browser but the instance will be handled as part of the life cycle. |
| 47 | + @Resource |
| 48 | + static Browser preInitializedBrowser = new Browser(new FirefoxDriver()); |
| 49 | + |
| 50 | + // A static Browser which will be initialized with a new instance before |
| 51 | + // the first and closed after the last test is executed. |
| 52 | + @Resource |
| 53 | + @CreateUsing ( ... ) |
| 54 | + static Browser classScopedBrowser; |
| 55 | + |
| 56 | + // An instance Browser which will be initialized with a new instance |
| 57 | + // before and closed after each test is executed. |
| 58 | + @Resource |
| 59 | + @CreateUsing ( ... ) |
| 60 | + Browser testScopedBrowser; |
| 61 | + |
| 62 | + // An instance Browser field which will be ignored by the runner since |
| 63 | + // the @Resource annotation is missing. |
| 64 | + Browser notAManagedBrowser; |
| 65 | + |
| 66 | + ... |
| 67 | + |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +## @CreateUsing |
| 72 | +In order to configure the `BrowserFactory` used to create the `Browser` instances, the `@CreateUsing` |
| 73 | +annotation must be used. |
| 74 | + |
| 75 | +### Example |
| 76 | +```java |
| 77 | +@Listeners(WebTesterTestNGListener.class) |
| 78 | +public class DifferentBrowserFactoriesTest { |
| 79 | + |
| 80 | + // Uses the FirefoxFactory to create Firefox instances. |
| 81 | + @Resource |
| 82 | + @CreateUsing ( FirefoxFactory.class ) |
| 83 | + static Browser firefox; |
| 84 | + |
| 85 | + // Uses the InternetExplorerFactoryto create IE instances. |
| 86 | + @Resource |
| 87 | + @CreateUsing ( InternetExplorerFactory.class ) |
| 88 | + Browser internetExplorer; |
| 89 | + |
| 90 | + ... |
| 91 | + |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +## @EntryPoint |
| 96 | +In order for a `Browser` to be automatically navigated to an applications entry point before each test, |
| 97 | +the `@EntryPoint` annotation can used. The navigation at the beginning of each test is done weather or |
| 98 | +not a `Browser` is static! |
| 99 | + |
| 100 | +### Example |
| 101 | +```java |
| 102 | +@Listeners(WebTesterTestNGListener.class) |
| 103 | +public class EntryPointsTest { |
| 104 | + |
| 105 | + // Will begin each test on Google. |
| 106 | + @Resource |
| 107 | + @CreateUsing ( ... ) |
| 108 | + @EntryPoint ( "http://www.google.com" ) |
| 109 | + static Browser classScopedBrowser; |
| 110 | + |
| 111 | + // Will begin each test on Bing. |
| 112 | + @Resource |
| 113 | + @CreateUsing ( ... ) |
| 114 | + @EntryPoint ( "http://www.bing.com" ) |
| 115 | + Browser testScopedBrowser; |
| 116 | + |
| 117 | + ... |
| 118 | + |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +## Configuration Property Injection |
| 123 | +All custom configuration properties can be injected into the following base field types: |
| 124 | +`String`, `Integer`, `Long`, `Float`, `Double` and `Boolean`. |
| 125 | + |
| 126 | +The injection is done for all fields which are annotated with `@ConfigurationValue`. |
| 127 | + |
| 128 | +### Example |
| 129 | +```java |
| 130 | +@Listeners(WebTesterTestNGListener.class) |
| 131 | +public class ConfigurationValuesTest { |
| 132 | + |
| 133 | + // Injects the integer value of "customer.integer" |
| 134 | + @ConfigurationValue ( "custom.integer" ) |
| 135 | + static Integer customInteger; |
| 136 | + |
| 137 | + // Injects the string value of "customer.string" |
| 138 | + @ConfigurationValue ( "custom.string" ) |
| 139 | + String customString; |
| 140 | + |
| 141 | + ... |
| 142 | + |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +## Multiple Browser Instances and Configuration Property Injection |
| 147 | +Since every `Browser` has it's own `Configuration` instance a "primary" Browser has to be declared when |
| 148 | +using multiple `Browser` instances and the `Configuration` property injection feature. The primary browser |
| 149 | +will be the source for the configuration properties injected by the `WebTesterTestNGListener`. If only one |
| 150 | +browser is managed it is automatically used as the primary browser! |
| 151 | + |
| 152 | +**Note:** In case you want to inject property values into a static field your primary browser has to be |
| 153 | +static as well! |
| 154 | + |
| 155 | +### Example |
| 156 | +```java |
| 157 | +@Listeners(WebTesterTestNGListener.class) |
| 158 | +public class MultiBrowserConfigurationValuesTest { |
| 159 | + |
| 160 | + @Primary |
| 161 | + @Resource |
| 162 | + @CreateUsing ( ... ) |
| 163 | + static Browser primaryBrowser; |
| 164 | + |
| 165 | + @Resource |
| 166 | + @CreateUsing ( ... ) |
| 167 | + Browser anotherBrowser; |
| 168 | + |
| 169 | + @ConfigurationValue ( "custom.integer" ) |
| 170 | + static Integer customInteger; |
| 171 | + |
| 172 | + @ConfigurationValue ( "custom.string" ) |
| 173 | + String customString; |
| 174 | + |
| 175 | + ... |
| 176 | + |
| 177 | +} |
| 178 | +``` |
| 179 | + |
| 180 | +# Linked Documentation |
| 181 | + |
| 182 | +- [Browser](browser.md) |
| 183 | +- [Configuration](configuration.md) |
0 commit comments