Show last authors
1 {{box cssClass="floatinginfobox" title="**Contents**"}}
2 {{toc/}}
3 {{/box}}
4
5 = Usage =
6
7 * Based on [[TestContainers>>https://www.testcontainers.org/]] and uses Docker to execute the tests under various Databases, Servlet Engines and Browsers.
8 ** [[Introductory talk about TestContainers and XWiki at Devoxx FR 2019>>https://youtu.be/0TvWv4L_IJM]] (In French)
9 * The only requirements for running these tests is to have Docker installed locally and to have the user under which you run your IDE and the Maven build be able to use the docker executable.
10 * Tests get injected ##XWikiWebDriver##, ##TestUtils##, ##TestInfo## and ##TestReference## instances as test method parameters.
11 * Test results is recording in a FLV file in the ##target## directory.
12 * A screenshot of the UI when the test is failing is also taken in the ##target## directory.
13 * When test execute it's possible to connect to the running VNC server and see the UI by using a VNC client and connect to the VNC URL printed in the console, e.g. ##vnc:~/~/vnc:[email protected]:32936##.
14 ** Note that if you want to debug and interact with the XWiki UI you should use a ##servletEngine## other than the default Jetty Standalone one since otherwise the debug point in your IDE will prevent the XWiki code from executing and you'll see a spinning wheel until you step forward in your debugger. You can use for example ##servletEngine = ServletEngine.TOMCAT##.
15 * The version for the platform dependencies is specified using the ##platform.version## property in the ##pom.xml## file executing the tests.
16 * The test configuration options can be accessed from tests by specifying a method parameter of type ##TestConfiguration## (see examples below).
17 ** You can get access to the IP address/port to use to connect to XWiki from the host machine: ##testConfiguration.getServletEngine().getIP()/getPort()##
18 ** You can get access to the IP address to use to connect to the host machine from inside XWiki: ##testConfiguration.getServletEngine().getHostIP()##
19 ** You can get access to the IP address/port used internally by XWiki (for example to generate URLs): ##testConfiguration.getServletEngine().getInternalIP()/getInternalPort()##
20
21 = Configurations options =
22
23 {{info}}
24 Note:
25 * When passing as Java annotation attributes, in functional tests, the format is ##@UITest(<Option Name> = <Value>)##
26 * When passing the options as System Properties, the format is ##-Dxwiki.test.ui.<Option Name>=<Value>##
27 ** Except for passing ##properties## where the format is ##-Dxwiki.test.ui.properties.<Property name>=<Property Value>##
28 ** Except for ##extraJARs##, ##sshPorts## & ##forbiddenServletEngines## which cannot be set from System Properties.
29 ** Except for passing ##databaseCommands## where the format is ##-Dxwiki.test.ui.database.commands.<Command name>=<Command Value>##
30 * System properties have priority over Java annotation attributes
31 * The ##xwiki.test.ui.dependencies## configuration value can only be passed as a system property.
32 {{/info}}
33
34 (% style="overflow-x: auto;" %)
35 (((
36 |=Option Name|=Default Value|=Valid Values|=Description
37 |##browser##|##firefox##|(((
38 * ##firefox## (##Browser.FIREFOX##)
39 * ##chrome## (##Browser.CHROME##)
40 )))|The browser used in the tests. Note that the version of the browser used is controlled by the version of Selenium that is defined in the ##pom.xml## of ##xwiki-platform-test-docker##.
41 |##database##|##hsqldb_embedded##|(((
42 * ##mysql## (##Database.MYSQL##)
43 * ##postgresql## (##Database.POSTGRESQL##)
44 * ##hsqldb_embedded## (##Database.HSQLDB_EMBEDDED##)
45 * ##oracle DB## (##Database.ORACLE##)
46 )))|The database used in the tests. Available tags are those listed on the Dockerhub page for the database image used. Note: for Oracle, we only have Oracle 11g Release 2.
47 |##servletEngine##|##jetty_standalone##|(((
48 * ##tomcat## (##ServletEngine.TOMCAT##)
49 * ##jetty## (##ServletEngine.JETTY##)
50 * ##jetty_standalone## (##ServletEngine.JETTY_STANDALONE##)
51 * ##external## (##ServletEngine.EXTERNAL##)
52 )))|The Servlet Engine used in the tests. Use ##external## to use your own started and alreayd provisioned XWiki instance.
53 |##verbose##|##false##|(((
54 * ##true##
55 * ##false##
56 )))|When active, displays more logs in the console (especially container startup logs)
57 |##debug##|##false##|(((
58 * ##true##
59 * ##false##
60 )))|When active, starts XWiki in debug mode when using the "Jetty Standalone" and Tomcat Servlet Engines (and starts it with suspend on, i.e. wait for the remote debugger to connect to it before progressing, to make it easy to debug XWiki's server side). You can then attach a remote debugger. Since Tomcat runs in a Docker container, you'll need to do a ##docker ps## to find the local mapped port corresponding to port 5005 and then use ##localhost## as the JVM host name.
61 |##saveDatabaseData##|##false##|(((
62 * ##true##
63 * ##false##
64 )))|By default, database data is not saved between test executions. {{warning}}Note that if you decide to save database data, they'll be saved under a ##docker## user by Docker and you need your local user to be able to remove them if you want to clean your Maven ##target## directory.{{/warning}}
65 |##offline##|##false##|(((
66 * ##true##
67 * ##false##
68 )))|When offline, the custom XWiki WAR generation and the XWiki provisioning are done solely from your local Maven repository. Otherwise, when artifacts are not present locally or newer SNAPSHOT versions are available, they'll be fetched from Maven remote repositories. Moreover no docker pull check is performed in offline mode.
69 |##databaseTag##|[[Latest supported version by XWiki>>Community.DatabaseSupportStrategy]]|Any Docker tag available from Dockerhub for the container image|Version of the database to use. Isn't supported for HSQLDB Embedded since it doesn't run in a Docker container.
70 |##servletEngineTag##|[[Latest supported version by XWiki>>Community.ServletContainerSupportStrategy.WebHome]]|Any Docker tag available from Dockerhub for the container image|Version of the Servlet Engine to use. Isn't supported for Jetty Standalone since it doesn't run in a Docker container.
71 |##jdbcDriverVersion##|Latest version validated by the XWiki dev team|Any version for the specified database that is available on Maven Central, e.g. for MySQL the groupId/artifactId is ##mysql##/##mysql-connector-java##.|
72 |##vnc##|##true##|(((
73 * ##true##
74 * ##false##
75 )))|When active a VNC container is started to record a video of the tests and more generally to allow connecting to the UI running the tests. Useful when debugging.
76 |##properties##|No default (empty)|Check the ##*.vm## files for the configuration files to see the list of possible values.|(% style="word-break: break-all;" %)Velocity properties that are applied when generating XWiki's configuration files: ##xwiki.cfg##, ##xwiki.properties## and ##hibernate.cfg.xml##. Example: Tell XWiki that minification is off: ##-Dxwiki.test.ui.properties.xwikiPropertiesAdditionalProperties=debug.minify=false## or ##@UITest(properties = { "xwikiPropertiesAdditionalProperties=debug.minify=false" })##. Note that the ##xwikiCfgPlugins## property will be merged with the default values unless you start the new property value with the ##^## character.
77 |##extraJARs##|No default (empty)|Comma-separated list of ##<groupId>:<artifactId>## or ##<groupId>:<artifactId>:<version>##|(% style="word-break: break-all;" %)There are cases where the module being tested cannot be fully deployed as an extension into a running XWiki and it needs to deploy one or several JARs into ##WEB-INF/lib##. One such example is when an extension includes a Hibernate HBM file, as right now only HBM files located in JARs in ##WEB-INF/lib## are supported. Note that when the version is not specified the current POM's version is used.
78 |##sshPorts##|No default (empty)|List of integers|The list of ports that should be SSH-forwarded when connecting from a Docker container to the host (i.e. when using the ##host.testcontainers.internal##. This is in addition to port ##8080## which is always added. For example if you need XWiki to send a mail to a SMTP server running on port 3025 on the host, you should add port 3025 to the list.
79 |##profiles##|No default (empty)|Comma-separated list of Maven profile ids|When specified the defined profiles are active when resolving dependencies from the current POM.
80 |##forbiddenServletEngines##|No default (empty)|List of ##ServletEngine##s enums|List of Servlet Engines for which the tests will be skipped (usually because they'd fail on them).
81 |##databaseProperties##|No default (empty)|See Docker documentation for the target image|List of database docker commands to use and that will override default commands (example of command ##character-set-server=utf8mb4## which will translate into ##~-~-character-set-server=utf8mb4## at runtime).
82 |##dependencies##|No default (empty)|Comma-separated list of Maven coordinates|List artifacts that will be provisioned in the running XWiki instance. When specified then current pom.xml dependencies are not used to find out what to provision. This can be used for example in cases when you don't want to use project dependencies in order to not draw older version of dependencies (such as when you need to run functional docker-based tests in a specific more recent vesion of XWiki than the one used to build the code). Accepted format is ##groupId:artifactId##, ##groupId:artifactId:version## and ##groupId:artifactId:type:version##. {{info}}Since 11.2RC1{{/info}}.
83 |##extensionOverrides##|No default (empty)|List Extension Manager overrides|See example below for the format. {{info}}Since 11.6RC1{{/info}}.
84 |##resolveExtraJARs##|##false##|(((
85 * ##true##
86 * ##false##
87 )))|Specifies if the extra JAR versions must be resolved (when their versions are not specified), by checking the transitive list of dependencies in the current POM. Note that there are 2 limitations: resolving takes time and SNAPSHOT versions will be resolved to the latest published SNAPSHOT. This is why it's off by default. When false the current POM version will be used for the missing exyta JAR versions. The main use case for using true is in contrib extensions where the current POM version usually don't match extra JARs versions and you may not want to hardcode the version and thus you can let the docker test framework find it for you. {{info}}Since 12.5RC1{{/info}}.
88
89 = Examples =
90
91 == Full Examples ==
92
93 * {{scm path="xwiki-platform-core/xwiki-platform-menu/xwiki-platform-menu-test/xwiki-platform-menu-test-docker/src/test/it/org/xwiki/menu/test/ui/MenuIT.java"}}MenuIT{{/scm}}
94 * {{scm path="xwiki-platform-core/xwiki-platform-mail/xwiki-platform-mail-test/xwiki-platform-mail-test-docker/src/test/it/org/xwiki/mail/test/ui/MailIT.java"}}MailIT{{/scm}}
95
96 == Example 1: Basic test ==
97
98 {{code language="java"}}
99 @UITest
100 public class SeleniumTest
101 {
102 @Test
103 public void test(XWikiWebDriver driver, TestUtils setup)
104 {
105 driver.get("http://xwiki.org");
106 assertThat(driver.getTitle(),
107 containsString("XWiki - The Advanced Open Source Enterprise and Application Wiki"));
108 driver.findElement(By.linkText("XWiki's concept")).click();
109 }
110 }
111 {{/code}}
112
113 == Example 2: Choosing Container + DB + Browser and versions ==
114
115 {{code language="java"}}
116 @UITest(database = Database.MYSQL, databaseTag = "5", servletEngine = ServletEngine.TOMCAT, servletEngineTag = "8",
117 browser = Browser.CHROME)
118 public class MenuIT
119 ...
120 {{/code}}
121
122 {{code language="java"}}
123 @UITest(database = Database.POSTGRESQL, databaseTag = "9", servletEngine = ServletEngine.JETTY,
124 servletEngineTag = "9", browser = Browser.CHROME)
125 public class MenuIT
126 ...
127 {{/code}}
128
129 {{code language="java"}}
130 @UITest(database = Database.HSQLDB_EMBEDDED, servletEngine = ServletEngine.JETTY_STANDALONE,
131 browser = Browser.FIREFOX)
132 public class MenuIT
133 ...
134 {{/code}}
135
136 == Example 3: Test using Greenmail + Hibernate HBM ==
137
138 {{code language="java"}}
139 @UITest(sshPorts = {
140 3025
141 },
142 properties = {
143 "xwikiDbHbmCommonExtraMappings=mailsender.hbm.xml",
144 // Pages created in the tests need to have PR since we ask for PR to send mails so we need to exclude them from
145 // the PR checker.
146 "xwikiPropertiesAdditionalProperties=test.prchecker.excludePattern=.*:MailIT\\..*"
147 },
148 extraJARs = {
149 "org.xwiki.platform:xwiki-platform-mail-send-storage"
150 }
151 )
152 public class MailIT
153 ...
154 {{/code}}
155
156 == Example 4: Passing Test Configuration ==
157
158 {{code language="java"}}
159 @Test
160 public void verifyMail(TestUtils setup, XWikiWebDriver webDriver, TestConfiguration testConfiguration)
161 throws Exception
162 {
163 ....
164 }
165 {{/code}}
166
167 == Example 5: Specifying explictly dependencies ==
168
169 Example of deploying a ##1.10-SNAPSHOT## version of ##org.xwiki.contrib.latex:latex-export## (and its dependencies) into a ##11.2-SNAPSHOT## version of XWiki. In this example the POM of ##org.xwiki.contrib.latex:latex-export## depends on XWiki 10.2. Thus this example demonstrates running the tests in a more recent version of XWiki than the code was built on.
170
171 {{code language="xml"}}
172 ...
173 <parent>
174 <groupId>org.xwiki.platform</groupId>
175 <artifactId>xwiki-platform</artifactId>
176 <version>11.2-SNAPSHOT</version>
177 </parent>
178 <groupId>org.xwiki.contrib.latex</groupId>
179 <artifactId>latex-test</artifactId>
180 <version>1.10-SNAPSHOT</version>
181 ...
182 <plugin>
183 <groupId>org.apache.maven.plugins</groupId>
184 <artifactId>maven-failsafe-plugin</artifactId>
185 <configuration>
186 <includes>
187 <include>**/AllIT.java</include>
188 </includes>
189 <systemProperties>
190 <xwiki.test.ui.dependencies>org.xwiki.contrib.latex:latex-export</xwiki.test.ui.dependencies>
191 </systemProperties>
192 </configuration>
193 </plugin>
194 {{/code}}
195
196 == Example 6: Passing extension overrides ==
197
198 The following tells the Extension Manager to use the existing core extension ##com.google.code.findbugs:annotations## whenever the extension ##com.google.code.findbugs:jsr305## is requested.
199
200 {{code language="java"}}
201 @UITest(extensionOverrides = {
202 @ExtensionOverride(
203 extensionId = "com.google.code.findbugs:jsr305",
204 overrides = {
205 "features=com.google.code.findbugs:annotations"
206 }
207 )
208 })
209 ...
210 {{/code}}
211
212 Note that in the XWikio build, these extension overrides are defined in the top level XWiki Platform POM and for this specific case it's defined as:
213
214 {{code language='xml'}}
215 ...
216 <extensionOverride>
217 <id>com.google.code.findbugs:jsr305</id>
218 <features>com.google.code.findbugs:annotations</features>
219 </extensionOverride>
220 ...
221 {{/code}}
222
223 Another example:
224
225 {{code language="java"}}
226 ...
227 extensionOverrides = {
228 @ExtensionOverride(
229 extensionId = "org.xwiki.platform:xwiki-platform-web",
230 overrides = {
231 // We set a default UI for the subwiki in the webapp, so that the Wiki Creation UI knows which extension
232 // to install on a subwiki by default (which is something we test)
233 // Otherwise the wiki creation form will display the flavor picker and the functional tests do not handle it.
234 "properties=xwiki.extension.distribution.wikiui=org.xwiki.platform:xwiki-platform-wiki-ui-wiki"
235 }
236 )
237 }
238 ...
239 {{/code}}
240
241 = Best Practices =
242
243 == Scenarios ==
244
245 Always use scenarios and use JUnit5's ##@Order(...)## annotation. For example:
246
247 {{code}}
248 @UITest
249 public class MenuIT
250 {
251 @Test
252 @Order(1)
253 public void verifyMenuInApplicationsIndex(TestUtils setup)
254 {
255 ...
256 }
257
258 @Test
259 @Order(2)
260 public void verifyMenuCreationInLeftPanelWithCurrentWikiVisibility(TestUtils setup)
261 {
262 ...
263 }
264
265 @Test
266 @Order(3)
267 public void verifyMenuIsAvailableInAdministration(TestUtils setup) throws Exception
268 {
269 ...
270 }
271 }
272 {{/code}}
273
274 == Suites ==
275
276 Use JUnit5's ##@Nested## annotation. For example:
277
278 {{code}}
279 @UITest(...)
280 public class AllIT
281 {
282 @Nested
283 @DisplayName("Office Import Tests")
284 class NestedOfficeImporterIT extends OfficeImporterIT
285 {
286 }
287
288 @Nested
289 @DisplayName("Office Export Tests")
290 class NestedOfficeExportIT extends OfficeExportIT
291 {
292 }
293 }
294 {{/code}}
295
296 == Test Reference ==
297
298 A lot of tests need to create a wiki page for testing. Example to do this:
299
300 {{code language="java"}}
301 @Test
302 public void showAndHideEditComments(TestUtils setup, TestReference reference) throws Exception
303 {
304 ViewPage vp = setup.gotoPage(reference);
305 ...
306 {{/code}}
307
308 More generic way (more verbose but can useful in some cases) using JUnit5's ##TestInfo## class:
309
310 {{code language="java"}}
311 public class MailIT
312 {
313 private String testClassName;
314
315 @BeforeEach
316 public void setUp(TestInfo info)
317 {
318 this.testClassName = info.getTestClass().get().getSimpleName();
319 }
320 ...
321 }
322 {{/code}}
323
324 == Docker out of Docker ==
325
326 The [[XWiki's CI>>https://ci.xwiki.org]] is using a [[Jenkins Agent Docker image>>https://github.com/xwiki/xwiki-jenkins-slave]] to execute the job builds. Thus, the Docker-based functional tests spawn Docker containers (using TestContainers) that execute inside Docker. This is the pattern called DOOD (Docker out of Docker). See [[TC's best practice>>https://www.testcontainers.org/supported_docker_environment/continuous_integration/dind_patterns/]] about this.
327
328 There are some consequences when writing tests for XWiki:
329 * Never use volume mapping or it won't execute fine. This is because that would require the Jenkins Agent Docker container to have that volume mapping defined too and the mapping would need to use the exact host path and container path. So, in short, it won't work. Instead copy files that you need to have in the container. Adn copy file out if need be. Here's an example from the LaTeX extension on how to achieve this:(((
330 {{code language="java"}}
331 try (GenericContainer container = new GenericContainer("blang/latex:ubuntu")) {
332 // Note: we copy files instead of mounting volumes so that this can work when using DOOD
333 // (Docker out of Docker).
334 MountableFile mountableDirectory = MountableFile.forHostPath("target/latex");
335 container.withCopyFileToContainer(mountableDirectory, "/data");
336
337 container.withCommand("/bin/sh", "-c", "sleep infinity");
338 container.withLogConsumer(new Slf4jLogConsumer(LOGGER));
339 container.start();
340 Container.ExecResult result = container.execInContainer("pdflatex", "-shell-escape", "index.tex");
341 String stdout = result.getStdout();
342 LOGGER.info(stdout);
343 assertTrue(stdout.contains("Output written on index.pdf"));
344
345 container.copyFileFromContainer("/data/index.pdf", "target/latex/index.pdf");
346 }
347 {{/code}}
348 )))
349
350 == Stdout/stderr validation errors ==
351
352 We have a [[check>>Community.Building.WebHome||anchor="HAutomaticChecks"]] that verifies if functional tests output some invalid content to stdout/stderr. If your test contains such errors it'll fail and then you have 3 options:
353 * It's a normal and expected output from the test (for ex the test verifies an error condition and it's expected it will raise a stack trace in the console). In this case add an expectation in the test. For example:(((
354 {{code language='java'}}
355 public void wrongTemplate(LogCaptureConfiguration logCaptureConfiguration)
356 {
357 ...
358 logCaptureConfiguration.registerExpected( "Possible break-in attempt!",
359 "Error getting resource [null]");
360 }
361 {{/code}}
362 )))
363 * It's a non-expected error. You then have 2 sub-choices:
364 ** Fix the problem (the best and recommended solution!)
365 ** Increase the technical debt by adding an exclude. For example:(((
366 {{code language='java'}}
367 logCaptureConfiguration.registerExcludes(
368 "java.lang.IllegalStateException: Response is committed");
369 {{/code}}
370 )))
371
372 = Test Resources =
373
374 You might need test resources to be used in interaction with the wiki (e.g. to upload an attachment). Any test resources placed in ##src/test/resources## is made automatically available to the browser container by mounting the dedicated volume mapped to ##target/test-classes##. Then, in your test, you can use the dedicated method to get access to the files located in ##target/test-classes##.
375
376 For example:
377
378 {{code language="java"}}
379 String resourceDirPath = testConfiguration.getBrowser().getTestResourcesPath();
380 File myResourceFile = new File(resourceDirPath, filename);
381 {{/code}}
382
383 = Test Coverage =
384
385 Activate support for Clover by setting the ##xwiki.test.ui.profiles## property in the ##clover## profile in the ##pom.xml## file for the test.
386
387 Example:
388
389 {{code language="xml"}}
390 <profiles>
391 <profile>
392 <id>clover</id>
393 <!-- Add the Clover JAR to the WAR so that it's available at runtime when XWiki executes.
394 It's needed because instrumented jars in the WAR will call Clover APIs at runtime when they execute. -->
395 <dependencies>
396 <dependency>
397 <groupId>org.openclover</groupId>
398 <artifactId>clover</artifactId>
399 </dependency>
400 </dependencies>
401 <build>
402 <plugins>
403 <plugin>
404 <groupId>org.apache.maven.plugins</groupId>
405 <artifactId>maven-failsafe-plugin</artifactId>
406 <configuration>
407 <systemProperties combine.children="append">
408 <!-- Tell the Docker-based test to activate the Clover profile so that the Clover JAR is added to
409 WEB-INF/lib -->
410 <property>
411 <name>xwiki.test.ui.profiles</name>
412 <value>clover</value>
413 </property>
414 </systemProperties>
415 </configuration>
416 </plugin>
417 ...
418 {{/code}}
419
420 = Existing XWiki instance =
421
422 If you want to avoid the Docker-based tests to start and stop XWiki every time you re-run the tests (for example when you're developping/debugging something), you can tell the Docker Test framework to not provision an XWiki instance and to use your XWiki instance instead.
423
424 This is achieved by specifying {{code}}@UITest(servletEngine = ServletEngine.EXTERNAL){{/code}} or passing the {{code}}xwiki.test.ui.servletEngine=external{{/code}} system property.
425
426 A common use case is to debug a failing test. Here's what you can do:
427
428 {{code language="none"}}
429 ## Run the test once to see if it fails and to generate the XWiki instance. This may take some time.
430 > mvn clean install -Dit.test=NavigationPanelIT
431 ## Open a new console, look for the generated XWiki instance inside the target folder and start it.
432 ../target/../jetty > start_xwiki.sh
433 ## Modify the test as needed then go back to the initial console and compile the tests.
434 ## Note that you may also need to build the page objects module if you change those too.
435 > mvn compiler:testCompile
436 ## Then run the test again to see the changes. This should be way faster than the initial test run.
437 > mvn surefire:test -Dxwiki.test.ui.servletEngine=external -Dtest=NavigationPanelIT
438 {{/code}}
439
440 = Architecture =
441
442 {{image reference="xwiki-testcontainers.png"/}}
443 //See [[Vincent's blog>>https://massol.myxwiki.org/xwiki/bin/view/Blog/EnvironmentTestingExperimentations#HExperimentation5:inJavaTestsusingTestContainers]] for more details//
444 )))
445
446 = CI Jobs =
447
448 There are 3 CI jobs executing Docker-based functional tests:
449
450 * [[Job 1>>https://ci.xwiki.org/view/Recommended%20Builds/job/xwiki-platform_docker/]]: run all tests on latest versions of supported configs.
451 * [[Job 2>>https://ci.xwiki.org/view/Recommended%20Builds/job/xwiki-platform_docker_all/]]: run smoke tests (ie a few tests only) on all supported configs.
452 * [[Job 3>>https://ci.xwiki.org/view/Recommended%20Builds/job/xwiki-platform_docker_unsupported/]]: run smoke tests on unsupported configs (MySQL 8.x, Tomcat 9.x, etc) that we want to support in the future.
453
454 Configuration definitions and job sources are all {{scm project="xwiki-jenkins-pipeline" path="vars/dockerConfigurations.groovy"}}here{{/scm}}.

Get Connected