Flutter Android App Test Automation Using Appium Java Client

Sandeep Dinesh
3 min readAug 21, 2022

It’s been about 3 months since I am back to Java for test automation and well, it’s a refreshing change as most of my recent experiences were in Javascript. To say the least, Java of 2022, is way different of what it was in 2017, and don’t we love it for all of that!

I do maintain that Javascript (and of course the Typescript too) is pretty awesome to do test automation; mainly because a lot of packages are readily there, right there & right now, to do any level of white box testing. But, the flip side, as with all things in trends, is that the newer ones get all the attention but the old, the prominent, and in this case, the original test automation language Java, got all the FOMO.

In fact, the truth is Java client for Appium still holds good for React-Native as well as Flutter, for which I am going to share my experience trying to get the automation working for the sample app they ship as part of Flutter installation.

So without further ado, let’s clear our certain questions in Java client, when you are coming from all the web literature about Flutter automation in Javascript.

  1. Do we need to have ‘appium-flutter-driver’ integrated somehow? Answer: No, you don’t. It’s really for white box testing in Javascript/ Typescript. It’s not required for latest appium to locate or perform actions on mobile elements.
  2. Do we need to switch the appium driver to ‘FLUTTER-CONTEXT’ ?
    Answer: No, Same explanation as above.
  3. Do we need to call execute_script every single time to get small things working? Answer:Mostly No, For simple mobile element operations like identifying, click, getText etc. Appium, out of the box, works well.

Elements Layout in Flutter App — Android

As you can see in the example above, the mobile elements come in with ‘content-desc’ parameters which can be leveraged for mobile element identification

Pre-requisite Steps

  1. Build your flutter app for your Android Emulator
  2. Identify the debug apk location, it’s usually at my_app/build/app/outputs/apk/debug/app-debug.apk
  3. Start your default appium server

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 androidTest() throws MalformedURLException, InterruptedException { System.out.println("Android Test");
url = new URL(URL_STRING);
capabilities =new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 300);
capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator2");
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME,"emulator-5554");
capabilities.setCapability(MobileCapabilityType.PLATFORM,"android");
capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION,"12");
capabilities.setCapability(MobileCapabilityType.NO_RESET, "false");
capabilities.setCapability(MobileCapabilityType.APP,
"my_app/build/app/outputs/apk/debug/app-debug.apk");
driver = new AndroidDriver(url, capabilities);
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
System.out.println("Android Driver Initialized");
MobileElement pageHeading = (MobileElement) driver.findElement(By.
xpath("//*[@content-desc='Flutter Demo Home Page']"));
MobileElement countText = (MobileElement) driver.findElement(By.
xpath("//*[@content-desc='You have pushed the button this many times:']"));
MobileElement countValue = (MobileElement) driver.findElement(By.
xpath("//*[@content-desc='You have pushed the button this many times:']/following-sibling::android.view.View"));
MobileElement incrementButton = (MobileElement) driver.findElement(By.
xpath("//*[@content-desc='Increment']"));
Assert.assertTrue(pageHeading.isDisplayed());
Assert.assertTrue(countText.isDisplayed());
Assert.assertEquals(countValue.getAttribute("content-desc"), "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.getAttribute("content-desc"), "1");
System.out.println("Asserted Updated Count: 1");
System.out.println("Android Test: Success");
} ...}

Stay Tuned for Part 2, the iOS story, and here it is — https://sandeepqaops.medium.com/flutter-ios-app-test-automation-using-appium-java-client-61d1364d4562

Happy Test Automation!

--

--