Run antrun plugin before suit tests of testng in maven build

776 views Asked by At

When I run the build of my project, before running the tests, I need to download and unzip the chromedriver.

<dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.8</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.relevantcodes</groupId>
        <artifactId>extentreports</artifactId>
        <version>2.41.2</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.8</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <configuration>
                        <target>
                            <get src="https://chromedriver.storage.googleapis.com/2.26/chromedriver_win32.zip"
                                 dest="${project.basedir}"
                                 verbose="false"
                                 usetimestamp="true" />
                            <unzip src="${project.basedir}/chromedriver_win32.zip" dest="${project.basedir}/drivers/" />
                            <delete file="${project.basedir}/chromedriver_win32.zip" />
                        </target>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <executions>
                <execution>
                    <phase>test</phase>
                        <goals>
                            <goal>test</goal>
                        </goals>
                            <configuration>
                                <suiteXmlFiles>
                                    <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
                                </suiteXmlFiles>
                            </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

To download I'm using the antrun plugin, but, even declaring the surefire plugin later in pom.xml, as I saw in other questions, to run the tests later too, the build did not download the driver before.

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building identificationkey 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ identificationkey ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\malibu\workspace\identificationkey\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ identificationkey ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ identificationkey ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 2 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ identificationkey ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.19.1:test (default-test) @ identificationkey ---

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running TestSuite
Tests run: 4, Failures: 1, Errors: 0, Skipped: 3, Time elapsed: 1.32 sec <<< FAILURE! - in TestSuite
setUp(br.ufrn.imd.ihc.identificationkey.run.LoginTest)  Time elapsed: 0.743 sec  <<< FAILURE!
java.lang.IllegalStateException: The driver executable does not exist: C:\Users\malibu\workspace\identificationkey\drivers\chromedriver.exe
    at br.ufrn.imd.ihc.identificationkey.run.LoginTest.setUp(LoginTest.java:25)


Results :

Failed tests: 
  LoginTest.setUp:25 ยป IllegalState The driver executable does not exist: C:\Use...

Tests run: 4, Failures: 1, Errors: 0, Skipped: 3

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9.784 s
[INFO] Finished at: 2016-12-22T17:52:02-03:00
[INFO] Final Memory: 18M/244M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test (default-test) on project identificationkey: There are test failures.
[ERROR] 
[ERROR] Please refer to C:\Users\malibu\workspace\identificationkey\target\surefire-reports for the individual test results.
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

What am I doing wrong? Any help would be appreciated.

1

There are 1 answers

0
Steve C On

You need to be aware of the Maven lifecycle (see Introduction to the Build Lifecycle), which dictates the order in which maven processes your build.

In your case, you have specified <phase>package</phase> for your maven-antrun-plugin execution, which comes after test.

I would use something like <phase>process-test-resources</phase> for your use case.