Mobile testing is a critical aspect of software development, ensuring that mobile applications deliver a seamless and high-quality user experience across various devices and platforms. As the mobile app landscape continues to evolve rapidly, efficient and reliable testing methodologies are essential to ensure the app’s functionality, performance, and compatibility. One powerful approach to mobile testing is using Serenity ScreenPlay in combination with DockerAndroid.
Serenity ScreenPlay is a testing framework that leverages the ScreenPlay pattern, offering a modular and organized way to structure and execute tests. It promotes collaboration between testers, developers, and business stakeholders by providing a domain-specific language that represents user interactions as tasks and activities. This pattern enhances test readability, maintainability, and reusability, making it an excellent fit for mobile testing scenarios.
DockerAndroid is a tool that simplifies the setup and management of Android emulators and devices using Docker containers. It provides a convenient way to create isolated and reproducible testing environments for Android apps. With DockerAndroid, you can easily manage different Android versions, configurations, and device types in separate containers, streamlining the testing process and avoiding conflicts between tests.
Combining Serenity ScreenPlay with DockerAndroid offers a comprehensive and efficient solution for mobile testing. By utilizing the ScreenPlay pattern, you structure your tests as realistic user journeys, making them more intuitive and robust. DockerAndroid complements this approach by ensuring consistent and isolated testing environments, reducing the risk of interference between tests and enabling parallel execution.
In this blog post, we’ll explore how to set up a mobile testing environment using Serenity ScreenPlay and DockerAndroid. We’ll cover the step-by-step process of configuring the testing framework, creating ScreenPlay tasks and interactions, and integrating DockerAndroid to manage Android emulator. By the end of this guide, you’ll have a solid foundation to conduct effective mobile testing and improve the quality of your mobile applications. Let’s dive in and harness the power of Serenity ScreenPlay and DockerAndroid for successful mobile testing.
Automating mobile app tests using Appium with Screenplay pattern, and Docker involves several steps. This approach combines the power of Appium for mobile testing, Screenplay pattern for test organization, and Docker for containerization. Here’s a detailed guide on how to set this up:
- Prerequisites:
- Basic knowledge of mobile app testing, Appium and Docker
- Programming language: Java or any language supported by Appium
- IDE: Use IntelliJ IDEA or Eclipse for Java development
- Setup the Project:
- Create a new project directory for your automation tests and navigate to it in the terminal
- Establish a Maven architecture based project
- Or create a maven project from IDE
- Install Dependencies:
- Java Development Kit (JDK): Make sure JDK is installed
- Maven: Install Maven for project management
- Docker: Install Docker to create containers for your tests
- Screenplay Setup:
- Add Dependencies: Update your `pom.xml` with Selenium, Screenplay, and Appium dependencies
- Create Feature Files: Write Cucumber feature files to define your test scenarios
- Implement Step Definitions: Create step definition classes that map to the steps in your feature files
- Implement Screenplay Pattern: Create Screenplay-style classes for better test organization. This involves creating Actors, Tasks, and Interactions to define user actions and app interactions.
- Dockerize Tests:
- Run appropriate Docker command to fetch images
- Create a Docker container from the image you selected. Make sure the Appium server is running and reachable from within the container.
- Test Execution:
- Write Test Scenarios: Write Cucumber scenarios using the Gherkin syntax.
- Run Tests Locally: Use Maven commands to run your Cucumber tests locally against the Appium server.
- Reporting:
- Integrate Reporting: Configure Serenity Report to generate test reports.
- CI/CD Integration (Optional):
- Push to Version Control: Push your code to a version control system like Git.
- Set Up CI/CD Pipeline: Use platforms like Jenkins, GitLab CI/CD, or Travis CI to automate the testing process.
2. Setup the Project
Setting up the project involves creating the directory structure, installing the required dependencies, and configuring your development environment. Here’s a step-by-step guide to help you set up your automation project using Appium, Cucumber with Screenplay pattern, and Docker:
1. Create Project Directory:
Create a new directory for your project. You can name it something like “MobileAppAutomation”.
- mkdir MobileAppAutomation
- cd MobileAppAutomation
2. Initialize Maven Project:
Initialize a Maven project by creating a `pom.xml` file. Maven will manage your project’s dependencies and build process.
mvn archetype:generate -DgroupId=com.example -DartifactId=mobile-app-automation -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
This command will generate the basic structure for your Maven project.
3. Update Dependencies:
Open the `pom.xml` file in a text editor and add the necessary dependencies. You’ll need dependencies for Appium, Cucumber, and any other libraries you intend to use.
Here’s an example of how the dependencies section might look:
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>8.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-shutterbug</artifactId>
<version>2.5.4</version>
</dependency>
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-core</artifactId>
<version>3.3.10</version>
</dependency>
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-junit</artifactId>
<version>3.3.10</version>
</dependency>
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-screenplay</artifactId>
<version>3.3.10</version>
</dependency>
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-screenplay-webdriver</artifactId>
<version>3.3.10</version>
</dependency>
4. Create Directories:
Create the necessary directories to organize your project:
- mkdir src
- mkdir src/main
- mkdir src/test
- mkdir src/test/java
- mkdir src/test/resources
- mkdir features
Your project directory structure should now look something like this:
MobileAppAutomation
├── pom.xml
├── src
│ ├── main
│ │ └── java
│ └── test
│ ├── java
│ └── resources
├── features
5. Configure IDE:
If you’re using an IDE like IntelliJ IDEA or Eclipse, import the Maven project. This will help you manage and write your code more efficiently.
6. Initial Code Structure:
Create your initial test structure. In the `src/test/java` directory, you can start by creating packages for different parts of your project (e.g., `stepdefinitions`, `screenplay`, `utilities`, etc.).
7. Version Control (Optional):
If you’re using version control (e.g., Git), initialize a repository in your project directory and commit the initial code.
3. Install Dependencies
Installing the necessary dependencies for your automation project involves setting up Java, Maven, and Docker. Here’s how to install each of these dependencies:
1. Java Development Kit (JDK):
- Check if Java is already installed: Open a terminal and run `java -version`. If it’s not installed, you’ll see an error message.
- To Install Java:
- Visit the Oracle JDK download page (for Oracle JDK) or OpenJDK website (for OpenJDK).
- Download the appropriate version for your operating system (e.g., Windows, macOS, Linux).
- Install Java following the provided instructions.
2. Maven:
- Check if Maven is already installed:
- Open a terminal and run `mvn -version`. If it’s not installed, you’ll see an error message.
- Install Maven:
- Visit the Apache Maven download page: https://maven.apache.org/download.cgi
- Download the latest binary zip archive.
- Extract the archive to a directory on your system.
- Add the extracted `bin` directory to your system’s PATH environment variable to make the `mvn` command available globally.
3. Docker:
- Visit the Docker website: https://www.docker.com/get-started
- Download the Docker Desktop application for your operating system (Windows or macOS).
- Install Docker by following the provided instructions.
- After installation, start the Docker Desktop application.
After installing these dependencies, you should be all set to start setting up your automation project using Appium, Cucumber with Screenplay pattern, and Docker. Remember that specific installation steps might vary depending on your operating system, so make sure to refer to the official documentation for each tool if you encounter any issues during installation.
4 Screenplay Setup:
Setting up Serenity Screenplay pattern involves configuring Cucumber, creating feature files, implementing step definitions, and structuring your test code using the Screenplay pattern. Here’s a step-by-step guide to help you set up Cucumber with the Screenplay pattern:
1. Create Feature Files:
Feature files describe the scenarios you want to test in a human-readable format using the Gherkin syntax. Create a `features` directory in your project and add `.feature` files with your test scenarios.
Example feature file (`loginMobileDocker.feature`):
Feature: Website Login
Scenario: Successful login
Given the user opens the app
When the user enters valid credentials
Then the user should be logged in
2. Install Cucumber Dependencies:
Make sure you’ve added the Cucumber dependencies to your `pom.xml` file as mentioned earlier. The dependencies should include `cucumber-java` and `cucumber-junit`.
3. Create Step Definitions:
Create step definition classes that map the Gherkin steps in your feature files to actual Java code. Create a package (e.g., `stepdefinitions`) in the `src/test/java` directory to store your step definitions.
Example step definition class (`LoginStepDefinitions.java`):
import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import io.cucumber.java.en.Then;
public class LoginStepDefinitions {
@Given(“the user opens the app”)
public void theUserOpensTheApp() {
// Implement step
}
@When(“the user enters valid credentials”)
public void theUserEntersValidCredentials() {
// Implement step
}
@Then(“the user should be logged in”)
public void theUserShouldBeLoggedIn() {
// Implement step
}
}
4. Implement Screenplay Classes:
The Screenplay pattern encourages writing tests in a more modular and structured way by defining actors, tasks, and interactions. Create a package (e.g., `screenplay`) in the `src/test/java` directory to store your Screenplay classes.
Example Screenplay classes (`LoginScreenplay.java`, `OpenAppTask.java`):
// LoginScreenplay.java
public class LoginScreenplay {
public static Performable openTheApp() {
return instrumented(OpenAppTask.class);
}
}
// OpenAppTask.java
public class OpenAppTask implements Task {
@Override
public <T extends Actor> void performAs(T actor) {
// Implement task to open the app
}
}
5. Wire Everything Together:
Use Cucumber’s `@RunWith` annotation to specify the JUnit runner class and use `@CucumberOptions` to specify the location of your feature files and step definitions.
Example JUnit runner class (`TestRunner.java`):
package e2e_tests.cucumber;
import io.cucumber.junit.CucumberOptions;
import net.serenitybdd.cucumber.CucumberWithSerenity;
import org.junit.runner.RunWith;
@RunWith(CucumberWithSerenity.class)
@CucumberOptions(
plugin = {
“pretty”,
“html:target/serenity-reports/serenity-html-report”,
“json:target/serenity-reports/cucumber_report.json”
},
features = “src/test/resources/features/loginMobileDocker.feature”
)
public class TestRunner {
}
6. Write Your Test Code:
In your Screenplay classes, use the `Actor`, `Task`, and `Interaction` classes to structure your test code. Use the Screenplay pattern’s concepts to create a more organized and maintainable test suite.
With these steps completed, you should have Cucumber set up with the Screenplay pattern for your mobile app automation project. Write your step definitions and Screenplay classes to implement your test scenarios using the defined actors, tasks, and interactions.
5. Dockerize Tests
Dockerizing your tests involves creating a Docker image that contains your test environment, dependencies, and test scripts. For our case, we do not install any Appium server, Android studio or any external Emulator separately on the test machine. Our plan is to install all these dependencies and use those through Docker. In this document, we are testing over browser based execution. Here’s how you can Dockerize your tests:
1. Assume that Docker is already installed on your system.
2. Make sure that your machine supports virtualization. To check if the virtualization is enabled is:
sudo apt install cpu-checker
kvm-ok
3. Run DockerAndroid container:
docker run -d -p 6080:6080 -e EMULATOR_DEVICE=”Samsung Galaxy S10″ -e WEB_VNC=true –device /dev/kvm –name android-container budtmo/DockerAndroid:emulator_11.0
4. Execute the container: docker exec -it appium-test bash
5. Make a new directory for chrome-driver download and extract:
- mkdir appiumdriver
- cd appiumdriver
- curl https://chromedriver.storage.googleapis.com/83.0.4103.39/chromedriver_linux64.zip –output driver.zip
- unzip driver.zip
6. To see if container is running, goto: http://localhost:6080 in your browser. You can see the Emulator there as well. Our test execution will run on the Emulator.
7. Execute ‘appium’ command under the ‘Terminal’ tab on Docker image
8. To see if Appium server is running or not, goto: http://localhost:4723
9. We can see all the info from Docker:
Alternatively, from point 3 to 5 can also be achieved by writing a simple docker-compose.yml file:
version: ‘3.8’
services:
appium-test:
build:
context: .
dockerfile: Dockerfile
container_name: appium-test
ports:
– “6080:6080”
– “4723:4723”
environment:
– EMULATOR_DEVICE=Samsung Galaxy S10
– WEB_VNC=true
devices:
– /dev/kvm
command: [“appium”]
and by writing a simple Dockerfile:
FROM budtmo/DockerAndroid:emulator_11.0
USER root
RUN mkdir -p /usr/appiumdriver \
&& chown -R androidusr:androidusr /usr/appiumdriver
USER androidusr
WORKDIR /usr/appiumdriver
RUN curl -o driver.zip https://chromedriver.storage.googleapis.com/83.0.4103.39/chromedriver_linux64.zip \
&& unzip driver.zip \
&& rm driver.zip
CMD [“tail”, “-f”, “/dev/null”]
And after that from terminal, you just need to execute these two commands:
docker-compose up -d
docker exec -it appium-test Appium
6. Test Execution
While you are ready with all the setup, it’s time to run the execution. You can run the command:
mvn clean verify
Here’s how the execution looks like in the Emulator. Our given url: https://opensource-demo.orangehrmlive.com/web/index.php/auth/login opens under chrome browser and the further executions continues:
For native app testing, you just need to copy your ‘.apk’ file to docker machine repo: /usr/YourOwn.apk and declare the path into ‘Serenity.Properties’ like:
appium.app = /usr/svlfg.apk
After that you need to set ‘SystemPropertyVariables’ in pom.xml file under ‘maven-failsafe-plugin’:
<systemPropertyVariables>
<properties>src/test/resources/serenity.properties</properties>
</systemPropertyVariables>
7. Reporting
The default reporting feature of Serenity BDD offers detailed insights into the execution of your automated tests, making it easier to understand test results and identify issues. Serenity BDD’s default reporting is an excellent starting point for understanding test results and sharing insights with team members and stakeholders. These reports aid in improving collaboration between testers, developers, and business representatives, enabling more effective communication regarding the application’s quality and status.
8. CI/CD Integration (Optional)
Integrating your automation project into a Continuous Integration/Continuous Deployment (CI/CD) pipeline enables automatic execution of tests whenever changes are made to your codebase. We will try to discuss it in separate blog post.