Starting and stopping Appium with Maven

When we run our tests we want the ability to start and stop everything, so we are going to add another profile and a series of additional plugins. First of all, we need to add some properties into our <properties> block in preparation for this new profile with all these new plugins:

<appium-maven-plugin.version>0.2.0</appium-maven-plugin.version>
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
<frontend-maven-plugin.nodeVersion>v7.4.0</frontend-maven-plugin.nodeVersion>
<frontend-maven-plugin.npmVersion>4.1.1</frontend-maven-plugin.npmVersion>
<port-allocator-maven-plugin.version>1.2</port-allocator-maven-plugin.version>

Next we have our new profile, this is quite a big one:

<profile>
<id>startAndStopAppium</id>
<activation>
<property>
<name>invokeAppium</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>install node and npm</id>
<phase>process-resources</phase>
<goals>
<goal>install-node-and-npm</goal>
</goals>
<configuration>
<nodeVersion>${frontend-maven-
plugin.nodeVersion}</nodeVersion>
<npmVersion>${frontend-maven-
plugin.npmVersion}</npmVersion>
</configuration>
</execution>
<execution>
<id>npm install</id>
<phase>process-resources</phase>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>install</arguments>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>port-allocator-maven-plugin</artifactId>
<version>${port-allocator-maven-plugin.version}
</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>allocate-ports</goal>
</goals>
<configuration>
<ports>
<port>
<name>appium.port</name>
</port>
</ports>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${maven-failsafe-plugin.version}</version>
<configuration>
<systemPropertyVariables>
<appiumServerLocation>http://localhost:
${appium.port}/wd/hub</appiumServerLocation>
<enableDebugMode>${enableDebugMode}
</enableDebugMode>
<screenshotDirectory>${project.build.directory}
/screenshots</screenshotDirectory>
<remoteDriver>${remote}</remoteDriver>
<appiumConfig>${appiumConfig}</appiumConfig>
</systemPropertyVariables>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.lazerycode.appium</groupId>
<artifactId>appium-maven-plugin</artifactId>
<version>${appium-maven-plugin.version}</version>
<configuration>
<nodeDefaultLocation>
${basedir}/node</nodeDefaultLocation>
<appiumLocation>${basedir}
/node_modules/appium</appiumLocation>
<appiumPort>${appium.port}</appiumPort>
</configuration>
<executions>
<execution>
<id>start appium</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop appium</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>

The first thing you will notice when you invoke this profile is that we have set a property called invokeAppium in the activation block. This is how we ensure that when we are running our Appium tests we, either used this profile or the previous one. We cannot run them both at the same time.

Next, we have frontend-maven-plugin. We are using this to install an isolated node and npm that is only used as part of the build for this project. We then use npm to download the Appium server. This also requires us to have package.json in the root of our project where we specify the version of Appium that we want to download:

{
"name": "mastering-selenium-appium",
"private": false,
"license": "Apache 2",
"version": "0.0.0",
"description": "Download appium for automated tests",
"devDependencies": {
"appium": "1.8.1",
"deviceconsole":"1.0.1"
},
  "scripts": {
"prestart": "npm install",
"pretest": "npm install"
}
}

The next plugin we have configured is port-allocator-maven-plugin. This allows us to search for ports that are currently not in use so that we can ensure that when we start up our Appium server, it doesn't try to use a port that is already in use. We then have our failsafe-maven-plugin configuration.  It's very similar to the configuration in the previous profile, but this time we need to make sure we are passing in the custom ports being used by the Appium server instance that has been started up as a part of this build. Finally, we have our appium-maven-plugin configuration. This has a simple job, it starts Appium before the integration-test phase and then shuts it down again afterwards.

We now have everything, we need to download Appium, start it up, run our tests, and then shut down the Appium server again. You can do this using:

mvn clean verify -PstartAndStopAppium

This may be a bit slow the first time you run it because it has to download node and install it as well as Appium, but once the initial download is complete it will reuse it in future test runs.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.123.252