Appium Writing Your First Test Case with Appium Estimated reading: 4 minutes 316 views Now that you have Appium set up on your local machine, it’s time to dive into writing your first test case. This guide will walk you through the process of creating a simple test case in Java.Create a New Maven ProjectUse your favorite IDE to create a new Maven project. We’re going to be using Eclipse in this setup. Eclipse IDE LaunchingCreating A New ProjectStart a new project by navigating to the top menu and selecting File then New and finally Other... File > New > OtherIn the wizard window that appears, scroll down to find the Maven folder. Expand the Maven folder by clicking on the small triangle next to it, and then select Maven Project. Click Next to proceed with the project setup. Choosing Maven ProjectIn the next step of the wizard, you will see an option to create a simple project. To proceed, click the checkbox labeled Create a simple project (skip archetype selection). Once you have checked this option, click the Next > button to move forward with the project setup. Check The Create A Simple Project BoxOn the next step of the wizard, you will need to create a group ID and an artifact ID for your project. For this guide, use com.test.app.todo as both the group ID and artifact ID. Enter these values in the respective fields provided. After filling in the details, click the Finish button to continue. Create a Group Id & Artifact Id nameCreate a New Test ClassWith our Maven project created, the next step is to create a new test class. Navigate to the directory src/test/java/. Inside this directory, create a new package by following this path: com.test. Create A New Package To Store Your Test Files Setting 'com.test' As A Package NameOnce the package is created, add a new class within it and name the class FirstAppiumTest. Creating A New Class Set The Class Name As FirstAppiumTest The FirstAppiumTest Class Is CreatedAdd Dependencies to pom.xmlNow, navigate to the pom.xml file in your project. This file is essential for managing your project’s dependencies and configurations. Open the pom.xml file, as we will be adding the necessary dependencies to run an Appium test script. Once the file is open, so we can add the xml dependencies below.pom.xml<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>test-todo-app</groupId> <artifactId>test-todo-app</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>io.appium</groupId> <artifactId>java-client</artifactId> <version>8.3.0</version> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.7.0</version> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-remote-driver</artifactId> <version>4.7.0</version> </dependency> <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest</artifactId> <version>2.2</version> <scope>test</scope> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.11.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M5</version> </plugin> </plugins> </build> </project>Add Your APK To The ProjectTo add your test APK to your project, begin by creating a new directory within your project’s structure. Navigate to the src/test/resources/ directory. Within this directory, create a new folder named app. This is where you will store your APK file for testing purposes. Create A New Folder To Store The Test App Setting The Directory As 'App' Added The Test App In The 'App' FolderWrite Your First Test ScriptLet’s take a look at our test application and discuss the test scenario that we’ll be automating. The test application we’ll be using is a TODO app. If you need access to the APK file, please join our Patreon. Members get access to all the existing frameworks we’ve created and more. You can follow along with this guide or use your own test application and tweak the elements as needed. In this test, our goal is to simply click on the Add Task button to display the Add Task screen. Add A Task Button Viewing Add Task ScreenFirstAppiumTest.javapackage com.test; import io.appium.java_client.android.AndroidDriver; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.remote.DesiredCapabilities; import java.io.File; import java.net.MalformedURLException; import java.net.URL; public class FirstAppiumTest { public static void main(String[] args) throws MalformedURLException, InterruptedException { File app = new File("src/test/resources/app/app-debug.apk"); // Set the desired capabilities for your test DesiredCapabilities caps = new DesiredCapabilities(); caps.setCapability("deviceName", "emulator-5554"); caps.setCapability("platformName", "android"); caps.setCapability("app",app.getAbsolutePath()); // Initialize the Appium driver AndroidDriver driver = new AndroidDriver (new URL("http://127.0.0.1:4273/wd/hub"), caps); // Add test case scenario here WebElement addTaskButton = driver.findElement(By.id("ADD_YOUR_ID_HERE")); addTaskButton.click(); // quick debug wait for 1 seconds Thread.sleep(1000); // Close the driver session driver.quit(); } }Launch Appium Desktop & Appium InspectorNow, we need to find the id associated with the element that we want to click. To locate the id element or other useful selectors, ensure that the emulator or device is booted up. First, launch the Appium Server and Inspector. Open the Appium Desktop Server and click the startServer button. Next, launch the Appium Inspector, making sure that the server port matches the Appium server’s port. Appium Desktop with Emulator Appium Server Is Running Appium Inspector + Setting Up Desired Capabilities Ensure the Remote Port is set to 4273 Set the Remote Path to /wd/hub Click the pencil & Copy the Desired Capabilities from below and paste in the JSON Representation box Click the Save button Click the Start Session button Desired Capabilities JSON DesiredCapabilities.json{ "appium:platformName": "Android", "appium:platformVersion": "13.0", "appium:deviceName": "emulator-5554", "appium:automationName": "UiAutomator2", "appium:app": "/Users/{userName}/eclipse-workspace/test-todo-app/src/test/resources/app/app-debug.apk", "appium:appWaitForLaunch": true }Inspecting The App ElementsWith the Appium Inspector now launched, you should see a preview of your application. In the preview window, locate and click on the element for which you want to retrieve attributes. For the purposes of this guide, we will click on the Add Task button. By doing so, the attributes related to this specific element will be displayed, allowing you to inspect and utilize them as needed. Appium Inspector Viewing The Element's Properties The accessibility id: value is to Add Task The id: value is to com.example.todoapp:id/fab The xpath: value is to //android.widget.ImageButton[@content-desc="Add Task"]You have the flexibility to use any available selector, but for this guide, we will select the second option, which represents the id attribute with the value com.example.todapp:id/fab. This value will be used in our code. Close the Appium Inspector Session to free up the Appium Server 4273 port.Copy this id value and navigate to the FirstAppiumTest class in your project. Find the element designated for adding a task and paste the copied id value into the addTaskButton element. This ensures that our test script can correctly interact with the element. Finding Add Task Button Element By IDJavaWebElement addTaskButton = driver.findElement(By.id("com.example.todoapp:id/fab")); addTaskButton.click(); // quick debug wait for 1 seconds Thread.sleep(1000); System.out.println("TEST PASSED!"); // Close the driver session driver.quit();Run Your TestFinally, it’s time to run the script. Yay! In the FirstAppiumTest class, right-click to open the context menu. Navigate to Run As and select Java Application. Now, sit back and watch as your script takes off for the first time! If everything goes well, you will see TEST PASSED in the console. Appium Run As Java ApplicationAnalyze the Test ResultsAfter the test execution is complete, review the results to ensure your test case ran successfully. Later in this series, we will learn how to implement TestNG to organize tests based on industry standards. Test Results - Passed Running Appium Test ScriptConclusionYou have now learned how to perform basic UI testing with Appium. This includes finding elements and performing actions. In the next guide, we’ll take a closer look at Organizing Multiple Test Scenarios In Appium with TestNG.Tagged:Appium Appium - Previous Setting Up Appium on Local Machine Next - Appium Organizing Multiple Test Scenarios in Appium Using TestNG