Your First Appium Test Case: Writing and Running
Sushma Kannedari
In this article, we will examine test cases for both IOS and Android platforms. Even if you are not currently testing on a specific platform, it’s beneficial to review both sections. Each example showcases different scenarios and features, enriching your understanding of Appium’s capabilities.
Let’s make our test case a little more sophisticated, while also looking at how we can work with iOS. If you are not planning on using iOS, we still suggest you read this section as we’ll be introducing new concepts applicable to both iOS and Android. For our iOS sample test case we will create a separate Test Case file named SampleIOS.java.
As we discussed above we need to put the iOS capabilities instead of Android capabilities, and define an IOSDriver class instead of an AndroidDriver class.

After specifying the desired capabilities, we can write the Automation test case. We have a sample app (.app file, which will work on iOS Simulator only) for automation. In this app, there is a feature where you can add 2 integer numbers and can get the results. So, we will automate this feature.

The steps to automate this would be:
1. Find the locator of TextField A and enter the value (ie. Send keys) from the keyboard.
|
1 |
[crayon-692449f4bc3fc385003319 inline="true" ]<span class="line"><span style="color: #9EFFFF">driver</span><span style="color: #E1EFFF">.</span><span style="color: #FAD000">findElement</span><span style="color: #E1EFFF">(</span><span style="color: #9EFFFF">AppiumBy</span><span style="color: #E1EFFF">.</span><span style="color: #FAD000">id</span><span style="color: #E1EFFF">(</span><span style="color: #92FC79">"</span><span style="color: #A5FF90">IntegerA</span><span style="color: #92FC79">"</span><span style="color: #E1EFFF">)).</span><span style="color: #FAD000">sendKeys</span><span style="color: #E1EFFF">(</span><span style="color: #FF628C">5</span><span style="color: #FFFFFF"> </span><span style="color: #FF9D00">+</span><span style="color: #FFFFFF"> </span><span style="color: #92FC79">""</span><span style="color: #E1EFFF">);</span></span> |
NOTE: The sendKeys() method accepts only String parameter, so we have converted the Integer value to a String by appending a blank String value.
2. Find the locator of TextField B and enter the second value from the keyboard.
|
1 |
[crayon-692449f4bc402854974227 inline="true" ]<span class="line"><span style="color: #9EFFFF">driver</span><span style="color: #E1EFFF">.</span><span style="color: #FAD000">findElement</span><span style="color: #E1EFFF">(</span><span style="color: #9EFFFF">AppiumBy</span><span style="color: #E1EFFF">.</span><span style="color: #FAD000">id</span><span style="color: #E1EFFF">(</span><span style="color: #92FC79">"</span><span style="color: #A5FF90">IntegerB</span><span style="color: #92FC79">"</span><span style="color: #E1EFFF">)).</span><span style="color: #FAD000">sendKeys</span><span style="color: #E1EFFF">(</span><span style="color: #FF628C">10</span><span style="color: #FFFFFF"> </span><span style="color: #FF9D00">+</span><span style="color: #FFFFFF"> </span><span style="color: #92FC79">""</span><span style="color: #E1EFFF">);</span></span> |
3. Find the locator of ‘Compute Sum’ and click on it, so the result would be displayed below the ‘Compute Sum’ textview.
|
1 2 3 |
[crayon-692449f4bc407383290413 inline="true" ]<span class="line"><span style="color: #9EFFFF">driver</span><span style="color: #E1EFFF">.</span><span style="color: #FAD000">findElement</span><span style="color: #E1EFFF">(</span><span style="color: #9EFFFF">AppiumBy</span><span style="color: #E1EFFF">.</span><span style="color: #FAD000">id</span><span style="color: #E1EFFF">(</span><span style="color: #92FC79">"</span><span style="color: #A5FF90">ComputeSumButton</span><span style="color: #92FC79">"</span><span style="color: #E1EFFF">)).</span><span style="color: #FAD000">click</span><span style="color: #E1EFFF">();</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF">String</span><span style="color: #FFFFFF"> </span><span style="color: #9EFFFF">answer</span><span style="color: #FFFFFF"> </span><span style="color: #FF9D00">=</span><span style="color: #FFFFFF"> </span><span style="color: #9EFFFF">driver</span><span style="color: #E1EFFF">.</span><span style="color: #FAD000">findElement</span><span style="color: #E1EFFF">(</span><span style="color: #9EFFFF">AppiumBy</span><span style="color: #E1EFFF">.</span><span style="color: #FAD000">id</span><span style="color: #E1EFFF">(</span><span style="color: #92FC79">"</span><span style="color: #A5FF90">Answer</span><span style="color: #92FC79">"</span><span style="color: #E1EFFF">)).</span><span style="color: #FAD000">getText</span><span style="color: #E1EFFF">();</span></span> |
NOTE: The getText() method is used to get the Text(in String format) from UI Elements.
4. Get the text of the result and compare it with the expected result, so if you enter 5 into TextField A, 10 into TextField B and when you click on ‘Compute Sum’ textview the result 15 should be displayed under ‘Compute Sum’.
|
1 |
[crayon-692449f4bc40e481397029 inline="true" ]<span class="line"><span style="color: #9EFFFF">Assert</span><span style="color: #E1EFFF">.</span><span style="color: #FAD000">assertEquals</span><span style="color: #E1EFFF">(</span><span style="color: #9EFFFF">answer</span><span style="color: #E1EFFF">,</span><span style="color: #FFFFFF"> </span><span style="color: #FF628C">15</span><span style="color: #FFFFFF"> </span><span style="color: #FF9D00">+</span><span style="color: #FFFFFF"> </span><span style="color: #92FC79">""</span><span style="color: #E1EFFF">,</span><span style="color: #FFFFFF"> </span><span style="color: #92FC79">"</span><span style="color: #A5FF90">Expected and Actual Result didn't match!</span><span style="color: #92FC79">"</span><span style="color: #E1EFFF">);</span></span> |
NOTE: Assert.assertEquals(expected, actual, error_message) is a TestNG method used to compare the Expected and Actual values. This is the most important step of any test case, because this is how an automation test will know whether values being rendered on UI are correct and as expected or not. You will see us using Assertions throughout this guide.
TestNG is the Testing framework and works best with Appium (Mobile Automation) and Selenium (Website Automation), you can learn more about the TestNG Annotations and methods here.
5. Below is the full code of our test which will enter 2 values into text fields. Click on ‘Compute Result’, get the result from the app, and compare it with the expected result.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
[crayon-692449f4bc416463907364 inline="true" ]<span class="line"><span style="color: #FF9D00">package</span><span style="color: #FFFFFF"> </span><span style="color: #9EFFFF">com</span><span style="color: #E1EFFF">.</span><span style="color: #FFEE80">example</span><span style="color: #E1EFFF">.</span><span style="color: #FAD000">appium</span><span style="color: #E1EFFF">;</span></span> <span class="line"></span> <span class="line"><span style="color: #FF9D00">import</span><span style="color: #9EFFFF"> io.appium.java_client.AppiumBy;</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF">import io.appium.java_client.ios.IOSDriver;</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF">import io.appium.java_client.ios.options.XCUITestOptions;</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF">import org.testng.Assert;</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF">import org.testng.annotations.BeforeTest;</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF">import org.testng.annotations.Test;</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF">import java.net.MalformedURLException;</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF">import java.net.URL;</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF">public class SampleIOS </span><span style="color: #E1EFFF">{</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> public IOSDriver driver;</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> @BeforeTest</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> public void setUp() throws MalformedURLException {</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> </span><span style="color: #B362FF; font-style: italic">// Set the desired capabilities for the iOS simulator</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> XCUITestOptions options = new XCUITestOptions();</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> options.setPlatformName("iOS");</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> options.setAutomationName("XCUITest"); </span><span style="color: #B362FF; font-style: italic">// Use XCUITest for iOS automation</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> options.setDeviceName("iPhone 16 Plus"); </span><span style="color: #B362FF; font-style: italic">// Change this if you're using a different simulator</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> options.setPlatformVersion("18.0"); </span><span style="color: #B362FF; font-style: italic">// Your iOS version</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> options.setApp("/Users/sushmakannedari/Library/Developer/Xcode/DerivedData/TestApp-drwfzxxnlrpirqhhppcktmmkzdgy/Build/Products/Debug-iphonesimulator/TestApp.app");</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> options.setNoReset(true); </span><span style="color: #B362FF; font-style: italic">// Prevents resetting the app state between sessions</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> </span><span style="color: #B362FF; font-style: italic">// Appium server URL</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> URL appiumServerURL = new URL("http:</span><span style="color: #B362FF; font-style: italic">//127.0.0.1:4723"); // Update the URL if Appium runs elsewhere</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> </span><span style="color: #B362FF; font-style: italic">// Initialize the iOSDriver</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> driver = new IOSDriver(appiumServerURL</span><span style="color: #E1EFFF">,</span><span style="color: #9EFFFF"> options);</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> </span><span style="color: #E1EFFF">}</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> @Test</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> public void computeSumTest() </span><span style="color: #E1EFFF">{</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> </span><span style="color: #B362FF; font-style: italic">// Step 1: Enter value in TextField A using Appium 2.0 syntax</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> driver.findElement(AppiumBy.id("IntegerA")).sendKeys(5 + "");</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> </span><span style="color: #B362FF; font-style: italic">// Step 2: Enter value in TextField B using Appium 2.0 syntax</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> driver.findElement(AppiumBy.id("IntegerB")).sendKeys(10 + "");</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> </span><span style="color: #B362FF; font-style: italic">// Step 3: Click on 'Compute Sum' using Appium 2.0 syntax</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> driver.findElement(AppiumBy.id("ComputeSumButton")).click();</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> </span><span style="color: #B362FF; font-style: italic">// Step 4: Get the result and compare with expected value using Appium 2.0 syntax</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> String answer = driver.findElement(AppiumBy.id("Answer")).getText();</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> Assert.assertEquals(answer</span><span style="color: #E1EFFF">,</span><span style="color: #9EFFFF"> 15 + ""</span><span style="color: #E1EFFF">,</span><span style="color: #9EFFFF"> "Expected and Actual Result didn't match!");</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> </span><span style="color: #B362FF; font-style: italic">// Print success message</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> System.out.println("Test completed successfully. The computed sum is: " + answer);</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> </span><span style="color: #E1EFFF">}</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF">}</span></span> |
You can get this example code on our github page.
After setting the valid Desired Capabilities, the next step is to pass them to the AndroidDriver class along with the Appium server URL (by default, it is http://127.0.0.1:4723/wd/hub).
The AndroidDriver is the primary class you will work with in your tests. Here’s how to set it up:
Here’s a code snippet demonstrating how to set this up:

Now let’s create the first sample Appium Test Case.
So let’s automate a simple scenario. In the below screen we want to click(tap) on the App Screen item from the list.

After writing the test case, we need to add the Appium logic to interact with the UI elements. In Appium, each element’s locator is essential for interaction. For example, if you want to tap on a button, you first need to find the locator of that button and then perform a click() action on it. We will explore locators in detail in a subsequent chapter.
For a deeper understanding of how to locate elements in Appium, you can refer to our blog on Appium Element Locator Strategies. This resource will provide you with various strategies and techniques for identifying UI elements effectively.
This code will find the Login Screen textview locator and simply click on it:
|
1 2 3 |
[crayon-692449f4bc420217010493 inline="true" ]<span class="line"><span style="color: #9EFFFF">driver</span><span style="color: #E1EFFF">.</span><span style="color: #FAD000">findElement</span><span style="color: #E1EFFF">(</span><span style="color: #9EFFFF">AppiumBy</span><span style="color: #E1EFFF">.</span><span style="color: #FAD000">accessibilityId</span><span style="color: #E1EFFF">(</span><span style="color: #92FC79">"</span><span style="color: #A5FF90">App</span><span style="color: #92FC79">"</span><span style="color: #E1EFFF">));</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF">appScreen</span><span style="color: #E1EFFF">.</span><span style="color: #FAD000">click</span><span style="color: #E1EFFF">();</span></span> |
Now our First Appium Automation Script is ready to execute, below is the complete code:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
[crayon-692449f4bc428553020996 inline="true" ]<span class="line"><span style="color: #FF9D00">package</span><span style="color: #FFFFFF"> </span><span style="color: #9EFFFF">com</span><span style="color: #E1EFFF">.</span><span style="color: #FFEE80">example</span><span style="color: #E1EFFF">.</span><span style="color: #FAD000">appium</span><span style="color: #E1EFFF">;</span></span> <span class="line"></span> <span class="line"><span style="color: #FF9D00">import</span><span style="color: #9EFFFF"> io.appium.java_client.AppiumBy;</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF">import io.appium.java_client.android.AndroidDriver;</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF">import io.appium.java_client.android.options.UiAutomator2Options;</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF">import org.openqa.selenium.WebElement;</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF">import org.testng.annotations.BeforeTest;</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF">import org.testng.annotations.Test;</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF">import java.net.MalformedURLException;</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF">import java.net.URL;</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF">public class SampleTest </span><span style="color: #E1EFFF">{</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> public AndroidDriver driver;</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> @BeforeTest</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> public void setUp() throws MalformedURLException {</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> </span><span style="color: #B362FF; font-style: italic">// Initialize UiAutomator2Options</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> UiAutomator2Options options = new UiAutomator2Options();</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> </span><span style="color: #B362FF; font-style: italic">// Set the desired capabilities for the Api Demos app</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> options.setPlatformName("Android");</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> options.setDeviceName("9B151FFAZ004ZQ"); </span><span style="color: #B362FF; font-style: italic">// Replace with your emulator/device name</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> </span><span style="color: #B362FF; font-style: italic">// Api Demos app package and activity</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> options.setAppPackage("io.appium.android.apis"); </span><span style="color: #B362FF; font-style: italic">// Package name of the app</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> options.setAppActivity("io.appium.android.apis.ApiDemos"); </span><span style="color: #B362FF; font-style: italic">// Main activity of the app</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> </span><span style="color: #B362FF; font-style: italic">// Initialize the AndroidDriver with the Appium server URL and options</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> String appiumServerURL = "http:</span><span style="color: #B362FF; font-style: italic">//127.0.0.1:4723";</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> driver = new AndroidDriver(new URL(appiumServerURL)</span><span style="color: #E1EFFF">,</span><span style="color: #9EFFFF"> options);</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> </span><span style="color: #E1EFFF">}</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> @Test</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> public void openAppTest() </span><span style="color: #E1EFFF">{</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> </span><span style="color: #B362FF; font-style: italic">// Locate the "App" element using Accessibility ID and click it</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> WebElement appScreen = driver.findElement(AppiumBy.accessibilityId("App"));</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> </span><span style="color: #B362FF; font-style: italic">// Click on the "App" element</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> appScreen.click();</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> System.out.println("Clicked on the App screen successfully!");</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF"> </span><span style="color: #E1EFFF">}</span></span> <span class="line"></span> <span class="line"><span style="color: #9EFFFF">}</span></span> |
Now you are ready to execute your test on a real device, so follow these steps:
Launch your terminal and type “appium” and Start the Server.

Connect your Android Mobile device to your computer and check that it is connected properly by executing the $ adb devices command. And also check the deviceName capability has the same name of the device which is showing up in the terminal.

Please make sure that device screen is unlocked and that it’s connected properly. Now, move to intelliJ Idea and select the test case name > Right click on it > Run ‘firstTest()’
Observe the Test Result and confirm the navigation on your device. It was a simple test case but you’ve actually accomplished a lot! From here, you get to explore all the cool features that Appium offers.

Congratulations on successfully completing your first Appium test case on a real device. This concludes our three part series on writing your first Appium test case. Along the way you learned a little bit about desired capabilities, locators and assertions. All of this is a great grounding to continue your education into the world of Automated testing and Appium.
Check out the rest of our Appium Test Case series:
Your First Appium Test Case: Setting up the IDE
Your First Appium Test Case: Writing and Running
To learn more, download our free eBook, Make the Move to Automation With Appium.
