5v1988 / appium-flutter-client

Appium Flutter Client
MIT License
3 stars 2 forks source link
appium flutter java selenium

Appium Flutter Java Binding

Introduction

This library helps to write Flutter tests in Java for Appium Flutter Driver. It mainly offers binding to find elements on mobile app using various locating strategies, and also executes custom flutter commands This page contains all necessary details in preparing Flutter app under test.

Installation

Pre-requisites:

  1. Install Appium by following the steps mentioned here
  2. Install Appium Flutter Driver from source as below:
    appium driver install --source=npm appium-flutter-driver
  3. Flutter app under test needs to be compiled in debug or profile mode and Flutter Driver extension also needs to be enabled by adding this line enableFlutterDriverExtension() before calling app's run method in main.dart
  4. Include flutter_driver from Flutter SDK as dev dependency

This library, Appium Flutter Binding which is based on Java can be installed by adding the following dependency on the project

<dependency>
    <groupId>io.github.5v1988</groupId>
    <artifactId>appium-flutter-client</artifactId>
    <version>1.0.5</version>
</dependency>
implementation group: 'io.github.5v1988', name: 'appium-flutter-client', version: '1.0.5'

Once the above steps are done, Appium Flutter Driver can be instantiated by passing in necessary mobile capabilities, including AutomationName as Flutter. The below code snippet is one such example for the reference.

    AppiumDriverLocalService service = new AppiumServiceBuilder()
    .usingAnyFreePort().build();
    service.start();
    if (service == null || !service.isRunning()) {
    throw new AppiumServerHasNotBeenStartedLocallyException(
    "An appium server node is not started!");
    }
    BaseOptions options = new BaseOptions()
    .setPlatformName("Android")
    .setAutomationName("Flutter")
    .amend("appium:app", "/path/to/app")
    .amend("autoGrantPermissions", "true");
    AppiumFlutterDriver driver = new AppiumFlutterDriver(service.getUrl(), options);

As for locating an element, there are many ways by which the flutter elements can be located, and the most prominent one is ByValueKey, which is equivalent to id in web context. If in case, you don't see it for the elements, it's better to talk with the developers to add Key for input elements , and all the available locating strategies are listed down as below:

  VALUE_KEY("ByValueKey"),
  TYPE("ByType"),
  TOOL_TIP("ByTooltipMessage"),
  TEXT("ByText"),
  SEMANTICS_LABEL("BySemanticsLabel"),
  ANCESTOR("Ancestor"),
  DESCENDANT("Descendant"),
  PAGE_BACK("PageBack");

Adding some code snippets to locate elements using this library.

//This is by key
private FlutterElement getEmailTextBox() {
  return driver.findElement(FlutterBy.VALUE_KEY, "KEYS.loginTextbox");
}

//This is by descendant
private FlutterElement getFirstItemFromList() {
    return driver.findDescendantElement(
    driver.findElement(FlutterBy.TYPE, "DefaultRefreshIndicator"),
    driver.findElement(FlutterBy.TYPE, "ListItem"),
    false,
    true);
    }

Using FlutterCommand from this library, you could able to execute them by passing in necessary parameters that each command takes in. Status of all implemented Flutter commands can be tracked from here

The below are some example snippets to perform certain interactions on the screen.

//To scroll for an element in Flutter context
protected FlutterCommand command = new FlutterCommand(driver);
command.execute(Command.SCROLL_INFO_VIEW, element, ImmutableMap.of("alignment", 0.1));

// To render the tree for the given screen on the flutter app
String flutterTree = driver.getRenderTree();

//To switch to Native context and find an element using XPath
driver.switchToContext("NATIVE_APP");
WebElement signIn = driver.findElement(By.xpath("//*[@content-desc='Sign in']"));
signIn.click();
driver.switchToContext("FLUTTER");

Please feel free to raise if you come across any issues in using this library.

Veera