📢 Are you a TestProject user looking to migrate to an open source alternative?

Learn more
Automating Flutter Apps with Appium Flutter Driver

Automating Flutter Apps with Appium Flutter Driver using Appium Java Client

In today’s world, mobile applications are essential for any business to thrive, and almost all developed products offer web and mobile support. This has made mobile application testing an integral part of the QA life cycle, and adding to it. The onus is on the test automation engineers to develop an E2E automation suite for mobile application testing.

In this blog, I will explain how test automation engineers can start using Appium to automate mobile applications developed using Flutter.

“Appium is a well-known open-source mobile automation framework through which we can automate Android and iOS mobile applications.”

About Flutter: 

  • An open-source UI development framework created by Google
  • Using Flutter, it is now possible for developers to develop cross-platform applications for Android, iOS, Linux, macOS, Windows, Google Fuchsia, and the web from a single codebase.
  • Flutter apps are written in Dart programming language and run on Dart virtual machines.

How can we Automate Flutter Apps?

Flutter framework, by default, comes up with flutter_driver. But using flutter_driver for UI automation has its limitations, and one of the main limitations is that we can write automation tests only in the Dart programming language. This can be quite tedious for test automation engineers without exposure to the Dart programming language.

Appium Flutter Driver:

The Appium developer community has created a new driver, appium_flutter_driver.This has enabled test automation engineers to automate flutter-based apps just like how they would automate android and iOS mobile apps. 

Appium Set Up and Walkthrough:

I assume you have already set up Appium on your machine. 

Installing Appium Flutter Driver:

  • If you are using Appium 2, use the command “appium driver install flutter” to install appium-flutter-driver.

 Flutter Finder Library:

The Appium developer community has also developed a flutter finder library through which we can interact with flutter-based elements. Below are the few locator strategies available in the flutter finder library.

  • bySemanticslabel
  • byValuekey
  • byType
  • ancestor
  • byTooltip
  • descendant
  • pageBack
  • getRenderTree
  • text

“You can also go through common finders class documentation to understand more about the locator strategies”

How to Generate the Jar for Flutter Finder Library:

  • Pull the latest from the repo: here to your system.
  • Go to the respective folder where the repo is placed and from the terminal, run the command: “gradle clean build”
  • Once the build is over, inside the build directory of the project, i.e., “build/libs/appium-flutter-finder-0.0.6.jar”, you can find the jar.
  • You can add this jar to your automation project.

How to Inspect Elements in the Flutter App?

“Before trying to identify/inspect elements in the flutter app, request you to quickly go through the Introduction to widgets section in the flutter documentation to get an idea of how the UI is basically built out of Widgets.”

Using Flutter Inspector, one can inspect the widgets and try to identify the elements, but it might not be as easy compared to inspecting elements using Appium Inspector. So here is what you can do: take help from the developers to get all the required locators.

Let’s now try to automate the login flow in a flutter app. 

Sample App:

Pre-Requisites:

After downloading a sample flutter app, we need to do certain prerequisites so that the flutter app becomes feasible to automate.

Browserstack has elaborately mentioned the prerequisites here. You can go through this to complete the pre-requisite and to build the app.

IDE:

  • IntelliJ IDEA

Programming Language:

  • Java: Open JDK 17.0.3v

Mobile Device:

  • Android Emulator

Have added the following dependencies in my pom.xml file

  • Appium java client: 8.1.1
  • TestNG: 7.5
  • Kotlin standard library for JVM: 1.3.40
  • Kotlin multiplatform serialization runtime library: 0.14.0
  • And do not forget to add the flutter finder jar to your project.


public class FlutterAndroidShopper {

   private AndroidDriver driver;
   private FlutterFinder find;

   @BeforeTest
   public void setUp() throws Exception {
       DesiredCapabilities caps = new DesiredCapabilities();
       caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "android");
       caps.setCapability(MobileCapabilityType.AUTOMATION_NAME, "Flutter");
       caps.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, "3000");
       caps.setCapability(MobileCapabilityType.DEVICE_NAME, "emulator-5554");
       caps.setCapability(MobileCapabilityType.APP, ""); //mention your app path here
       driver =  new AndroidDriver(new URL("http://localhost:4723/wd/hub"), caps);
       find = new FlutterFinder(driver);
   }
   @Test
   public void login(){
       find.bySemanticsLabel("Username").sendKeys("username");
       find.bySemanticsLabel("Password").sendKeys("Password");
       find.text("ENTER").click();
       Assert.assertEquals(find.text("Catalog").getText(),"Catalog");
   }
  @AfterTest
   public void tearDown(){
       if(driver!=null){
           driver.quit();
       }
   }
}



As you can see from the above code, I have created a new object for FlutterFinder class, and with that, I can interact with the elements in the flutter app.

Similarly, you can also run automation on your iOS device as well.

An important point to be considered here is:

For running the automation in Android and iOS mobile applications, AUTOMATION_NAME should be “Flutter”

Can we launch Flutter App in Appium Inspector?

We cannot launch the flutter app in Appium inspector with the automation name: “Flutter,”

But if the app has both native and flutter-based components:

  • We can launch the app in Appium inspector by mentioning the automation name “UIAutomator2” for android and “XCUITest” for iOS.
  • Only native elements can be found in the inspector; we will not be able to find flutter-based elements.
launch Flutter App in Appium inspector

Switching between Native App and Flutter App context:

If the app has both native and flutter-based components, here is what you need to do:

  • Start the Appium session with automation_name as “Flutter”
  • driver.getContextHandles() => will give “NATIVE_APP” and “FLUTTER”
  • You can then set the context accordingly using driver.context(“NATIVE_APP”) and interact with the respective elements.


public class FlutterAndroidShopper {
   private AndroidDriver driver;
   private FlutterFinder find;

   @BeforeTest
   public void setUp() throws Exception {
       DesiredCapabilities caps = new DesiredCapabilities();
       caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "android");
       caps.setCapability(MobileCapabilityType.AUTOMATION_NAME, "Flutter");
       caps.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, "3000");
       caps.setCapability(MobileCapabilityType.DEVICE_NAME, "emulator-5554");
       caps.setCapability(MobileCapabilityType.APP, ""); //mention your app path here
       driver =  new AndroidDriver(new URL("http://localhost:4723/wd/hub"), caps);
       find = new FlutterFinder(driver);
   }

   @Test
   public void login(){
       switchContext("NATIVE_APP");
       RemoteWebElement username = (RemoteWebElement) 
          driver.findElement(By.xpath("/hierarchy/android.widget.FrameLayout/
          android.widget.LinearLayout/android.widget.FrameLayout/
          android.widget.FrameLayout/android.view.View/android.view.View/
          android.view.View/android.view.View/android.widget.EditText[1]"));
       username.click();
       username.sendKeys("Username");
       switchContext("FLUTTER");
       find.bySemanticsLabel("Password").sendKeys("Password");
       find.text("ENTER").click();
       Assert.assertEquals(find.text("Catalog").getText(),"Catalog");
   }

   public void switchContext(String context) {
       Set contexts = driver.getContextHandles();
       for (String appContext : contexts) {
           if (appContext.contains(context)) {
               driver.context(appContext);
               break;
           }
       }
   }

   @AfterTest
   public void tearDown(){
       if(driver!=null){
           driver.quit();
       }
   }
}


Conclusion:

Thus, by using the Appium flutter driver and flutter finder library, you can now start writing automation tests using the Appium Java client for flutter apps and also for flutter+native apps.  Despite the support and the capabilities offered, the Appium flutter driver is still in the early stages of development and due to this, there might be issues and limitations in using the same for flutter app automation.  

So before adopting the appium flutter driver, do thorough research, understand the pros and cons, and then make the call! Happy testing.


Test automation made easy

Start your smart continuous testing journey today with Testsigma.

SHARE THIS BLOG

RELATED POSTS