Flutter iOS App Test Automation Using Appium Java Client

Sandeep Dinesh
2 min readAug 22, 2022

--

This is a follow up of my flutter Android automation post using appium java client — https://sandeepqaops.medium.com/flutter-app-test-automation-using-appium-java-client-78e1a90702ff

As it is the case usually, things are straight forward when it comes to Android, but iOS needs more hacks, tips & tricks to get things working. Good news is that Java client for Appium works for iOS Flutter app automation as well. Everything mentioned in my Android post holds good, and here I am not going to repeat them again..

Let’s get straight to the cheese..

Pre-requisite Steps

  1. Keep your Apple developer certificate handy and add to the XCode project settings of the sample Runner.app
  2. Build using XCode as by default the app generated by flutter build ios is for physical devices and won’t work in the iOS simulators, and Appium won’t be able to locate mobile elements.
  1. Identify the debug app location, it’s usually at my_app/build/ios/Debug-iphonesimulator/Runner.app
  2. Start your default appium server

Elements Layout in Flutter App — iOS

As you can see in the example above, the mobile elements come in with, Guess What — ‘id’ parameters which can be leveraged for mobile element identification. So, we don’t need to run behind developers to add test-ids ;-)

Putting everything together

//pseudo code, modify according to your setup..
...
public class Sample {private final String URL_STRING = "http://127.0.0.1:4723/wd/hub";
private URL url;
private DesiredCapabilities capabilities;
private AppiumDriver driver;public void iosTest() throws MalformedURLException, InterruptedException {

System.out.println("iOS Test");
url = new URL(URL_STRING);
capabilities =new DesiredCapabilities();

capabilities.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 300);
capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "XCUITest");
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME,"iPhone12_15.4");
capabilities.setCapability(MobileCapabilityType.PLATFORM,"ios");
capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION,"15.4");
capabilities.setCapability(MobileCapabilityType.NO_RESET, "false");
capabilities.setCapability(MobileCapabilityType.UDID, "<YOUR_DEVICE_UUID>");

capabilities.setCapability(MobileCapabilityType.APP,
"my_app/build/ios/Debug-iphonesimulator/Runner.app");

driver = new IOSDriver(url, capabilities);
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
System.out.println("iOS Driver Initialized");

//System.out.println(driver.getPageSource());

MobileElement pageHeading = (MobileElement) driver.findElement(By.
id("Flutter Demo Home Page"));
MobileElement countText = (MobileElement) driver.findElement(By.
id("You have pushed the button this many times:"));
MobileElement countValue = (MobileElement) driver.findElement(By.
xpath("//*[@name='You have pushed the button this many times:']/following-sibling::XCUIElementTypeStaticText"));
MobileElement incrementButton = (MobileElement) driver.findElement(By.
id("Increment"));

Assert.assertTrue(pageHeading.isDisplayed());
Assert.assertTrue(countText.isDisplayed());
Assert.assertEquals(countValue.getText(), "0");
System.out.println("Asserted Initial Count: 0");
Assert.assertTrue(incrementButton.isDisplayed());

incrementButton.click();
System.out.println("Clicked Increment");
Thread.sleep(5*1000);

Assert.assertEquals(countValue.getText(), "1");
System.out.println("Asserted Updated Count: 1");
System.out.println("iOS Test: Success");

}
...}

Happy Test Automation!

--

--