4

Our goal is to make acceptance test for a system, which can be disabled for a specified period, and ensure system gets disabled and back to enabled.

Our plan is to:

  • first disable the system for X minutes from maintenance client using Selenium
  • then run Java based (or other) test to query the system, and assert the system is disabled
  • then we should sleep for X minutes and run another test to ensure system is enabled again.

We like to keep the tests for enabled and disabled cases decoupled, thus the sleep must be introduced either in maven itself, or as some plugin type solution.

Question: How to specify in which order the test goals are ran by maven, and add parameterized delay in between, which will be fed to Selenium?

A_Di-Matteo
  • 26,902
  • 7
  • 94
  • 128
Tuomas Toivonen
  • 21,690
  • 47
  • 129
  • 225

1 Answers1

4

You could apply the following configuration:

  • Configure the maven-surefire-plugin or the maven-failsafe-plugin (better for integration tests, which sounds more appropriate in this use case) to execute the first set of tests via its include/exclude mechanism, as first execution of this plugin
  • Configure the maven-surefire-plugin (or the maven-failsafe-plugin) to execute a sample test case whose only purpose would be to sleep for a determined (or configurable) time, again via include/exclude mechanism, as second execution of this plugin (Maven will respect execution order as per pom.xml file declaration)
  • Configure the maven-surefire-plugin (or the maven-failsafe-plugin) to execute the second set of tests (or a single check test, in this use case), again via include/exclude, as third execution (which will then be executed as last one).

Using the same plugin with several executions for the same phase will ensure you the declaration order will be followed during Maven execution.

Below a sample snippet of the approach above:

<profile>
    <id>check-test</id>
    <build>
        <properties>
           <sleep.time>2000</sleep.time>
        </properties>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <executions>
                    <execution>
                        <id>first-execution</id>
                        <phase>test</phase>
                        <configuration>
                            <includes>
                                <include>*FirstTestsSample.java</include>
                            </includes>
                        </configuration>
                    </execution>
                    <execution>
                        <id>second-execution</id>
                        <phase>test</phase>
                        <configuration>
                            <includes>
                                <include>SleepTest.java</include>
                            </includes>
                            <systemPropertyVariables>
                               <sleepParam>${sleep.time}</sleepParam>
                            </systemPropertyVariables>
                        </configuration>
                    </execution>
                    <execution>
                        <id>third-execution</id>
                        <phase>test</phase>
                        <configuration>
                            <includes>
                                <include>CheckTest.java</include>
                            </includes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</profile>

Note, I wrapped everything in a Maven profile for cleanness since you probably don't want this behavior as part of your default build but only executed on demand (or as part of a CI job).

If you need to configure the sleeping time, you could then configure the concerned configuration section of each execution via the systemPropertyVariables option.

You can then invoke your build as following:

mvn clean verify -Pcheck-test -Dsleep.time=3000

Where the -P is enabling the profile by its id and we are also overriding the default value of the sleep.time property via command line, passed then as value of the sleepParam system variable which can be fetched from Java code via a System.gerProperty("sleepParam") call.


Also note, the maven-failsafe-plugin may better suit your scenario since it better handles post-execution of integration/acceptance tests, as also described in its official page, even though your use case may still be served by the `maven-surefire-plugin'.

Community
  • 1
  • 1
A_Di-Matteo
  • 26,902
  • 7
  • 94
  • 128
  • Can this be implemented at the testng or junit level. Using the dummy test with an argument for Thread.sleep to use which can be given the value in the testng.xml. The test methods order can be fixed using the dependsOnMethods. – Grasshopper Aug 16 '16 at 16:13
  • If I want to run Selenium / Robot framework test before those three samples to set the stage, do I just bind correct plugin to the same phase, and declare it before surefire/failsafe, i.e is the plugin execution order for mutual phase same as the declaration order? And is there any way to inject the sleep time parameter to build and still to plugin executions? – Tuomas Toivonen Aug 16 '16 at 19:14
  • @TuomasToivonen yes, you can still add a further plugin invocation to the same phase by declaring its execution before the sample above (recommended: as part of the same profile). About injection of parameter, check my update on the answer. – A_Di-Matteo Aug 16 '16 at 20:43
  • @Grasshopper probably yes, your approach/suggestion would also work, at different level of granularity/detail. I would still configure it via a surefire plugin configuration as part of a profile though, in order to keep it away from default build and enabled only on demand. – A_Di-Matteo Aug 16 '16 at 20:49