Your First Appium Test Case: IOS and Android

Reading Time : < 1min read
IOS and Android

Exploring Test Cases for Both IOS and Android 

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.

  • iOS Test Case: Similarly, we will look at an automation scenario for iOS , demonstrating how to leverage Appium’s capabilities in a different environment.
  • Android Test Case: We will explore a specific automation scenario tailored for Android devices, highlighting unique features and functionalities available on this platform.

iOS

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.

Screenshot of IntelliJ IDEA showing the setup of an iOS test case using the IOSDriver class with XCUITestOptions for iOS automation. The code specifies the iOS platform version, device name, and app path for the test. The project explorer on the left shows the structure of the test files
iOSDriver initialization and Desired Capabilities.

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.

Screenshot of an iOS sample application. The app screen displays various UI elements, including text fields, a 'Compute Sum' button, alerts, labels, a disabled button, a location switch, and other test controls like 'Test Gesture' and 'Crash.
iOS Sample Application

The steps to automate this would be:

1. Find the locator of TextField A and enter the value (ie. Send keys) from the keyboard.

driver.findElement(AppiumBy.id("IntegerA")).sendKeys(5 + "");

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.

driver.findElement(AppiumBy.id("IntegerB")).sendKeys(10 + "");

3. Find the locator of ‘Compute Sum’ and click on it, so the result would be displayed below the ‘Compute Sum’ textview.

driver.findElement(AppiumBy.id("ComputeSumButton")).click();

String answer = driver.findElement(AppiumBy.id("Answer")).getText();

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’.

Assert.assertEquals(answer, 15 + "", "Expected and Actual Result didn't match!");

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.

package com.example.appium;

import io.appium.java_client.AppiumBy;

import io.appium.java_client.ios.IOSDriver;

import io.appium.java_client.ios.options.XCUITestOptions;

import org.testng.Assert;

import org.testng.annotations.BeforeTest;

import org.testng.annotations.Test;

import java.net.MalformedURLException;

import java.net.URL;

public class SampleIOS {

   public IOSDriver driver;

   @BeforeTest

   public void setUp() throws MalformedURLException {

       // Set the desired capabilities for the iOS simulator

       XCUITestOptions options = new XCUITestOptions();

       options.setPlatformName("iOS");

       options.setAutomationName("XCUITest"); // Use XCUITest for iOS automation

       options.setDeviceName("iPhone 16 Plus"); // Change this if you're using a different simulator

       options.setPlatformVersion("18.0"); // Your iOS version

       options.setApp("/Users/sushmakannedari/Library/Developer/Xcode/DerivedData/TestApp-drwfzxxnlrpirqhhppcktmmkzdgy/Build/Products/Debug-iphonesimulator/TestApp.app");

       options.setNoReset(true); // Prevents resetting the app state between sessions

       // Appium server URL

       URL appiumServerURL = new URL("http://127.0.0.1:4723"); // Update the URL if Appium runs elsewhere

       // Initialize the iOSDriver

       driver = new IOSDriver(appiumServerURL, options);

   }

   @Test

   public void computeSumTest() {

       // Step 1: Enter value in TextField A using Appium 2.0 syntax

       driver.findElement(AppiumBy.id("IntegerA")).sendKeys(5 + "");

       // Step 2: Enter value in TextField B using Appium 2.0 syntax

       driver.findElement(AppiumBy.id("IntegerB")).sendKeys(10 + "");

       // Step 3: Click on 'Compute Sum' using Appium 2.0 syntax

       driver.findElement(AppiumBy.id("ComputeSumButton")).click();

       // Step 4: Get the result and compare with expected value using Appium 2.0 syntax

       String answer = driver.findElement(AppiumBy.id("Answer")).getText();

       Assert.assertEquals(answer, 15 + "", "Expected and Actual Result didn't match!");

       // Print success message

       System.out.println("Test completed successfully. The computed sum is: " + answer);

   }

}

You can get this example code on our github page.

Android Test Case

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:

  1. Initialize AndroidDriver: Create an instance of AndroidDriver using the Desired Capabilities and the Appium server URL.
  2. Interact with UI Elements: Once the AndroidDriver instance is created, you can use it to interact with the various UI elements of the application.

Here’s a code snippet demonstrating how to set this up:

Screenshot of a Java code snippet in IntelliJ IDEA demonstrating AndroidDriver initialization and setting desired capabilities using UiAutomator2Options in an Appium test. The test is configured to open the 'API Demos' app on an Android device. The code includes a setup method annotated with @BeforeTest and a test method annotated with @Test to open the app.
AndroidDriver initialization and Desired Capabilities.

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.

Screenshot of the main menu in the 'API Demos' app on an Android device. The menu displays various options such as Accessibility, Animation, App, Content, Graphics, Media, NFC, OS, Preference, Text, and Views
Android – API Demos App

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:

driver.findElement(AppiumBy.accessibilityId("App"));

appScreen.click();

Now our First Appium Automation Script is ready to execute, below is the complete code:

package com.example.appium;

import io.appium.java_client.AppiumBy;

import io.appium.java_client.android.AndroidDriver;

import io.appium.java_client.android.options.UiAutomator2Options;

import org.openqa.selenium.WebElement;

import org.testng.annotations.BeforeTest;

import org.testng.annotations.Test;

import java.net.MalformedURLException;

import java.net.URL;

public class SampleTest {

   public AndroidDriver driver;

   @BeforeTest

   public void setUp() throws MalformedURLException {

       // Initialize UiAutomator2Options

       UiAutomator2Options options = new UiAutomator2Options();

       // Set the desired capabilities for the Api Demos app

       options.setPlatformName("Android");

       options.setDeviceName("9B151FFAZ004ZQ");  // Replace with your emulator/device name

       // Api Demos app package and activity

       options.setAppPackage("io.appium.android.apis");  // Package name of the app

       options.setAppActivity("io.appium.android.apis.ApiDemos");  // Main activity of the app

       // Initialize the AndroidDriver with the Appium server URL and options

       String appiumServerURL = "http://127.0.0.1:4723";

       driver = new AndroidDriver(new URL(appiumServerURL), options);

   }

   @Test

   public void openAppTest() {

       // Locate the "App" element using Accessibility ID and click it

       WebElement appScreen = driver.findElement(AppiumBy.accessibilityId("App"));

       // Click on the "App" element

       appScreen.click();

       System.out.println("Clicked on the App screen successfully!");

   }

}

Running the Tests on Real Devices

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.

Screenshot of a terminal window showing Appium server running on http://0.0.0.0:4723
Appium Server is Running on 0.0.0.0:4723

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.

Screenshot of a terminal window showing the result of the command adb devices, listing a connected Android device . The image confirms that the Android device is successfully connected to the system
Android device is connected.

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()’

Screenshot of IntelliJ IDEA showing the Appium test case ready to be executed. The code editor displays the test class, and the right-click context menu is open, highlighting the option to 'Run SampleTest.' The project structure on the left shows the test files and directories
Run the test case

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.

Screenshot of the test result output in IntelliJ IDEA, showing that one test has passed. The log indicates that the app screen was clicked successfully. The default test suite ran with 1 test, 1 pass, 0 failures, and 0 skips, with the process finishing with exit code 0.
Test Result

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.

Appium eBook

Interested in Learning More?

Subscribe today to stay informed and get regular updates from Kobiton

Ready to accelerate delivery of
your mobile apps?

Request a Demo