Skip to content

3 MobileApps

Martin Großmann edited this page Aug 16, 2024 · 4 revisions

Automate mobile apps

You can easily setup your test configuration for testing mobile iOS or Android apps.

Please note, that the Appium Connector only supports the engines UiAutomator2 and XCUITest.

App settings

Global test config with WEB_DRIVER_MANAGER.setUserAgent(..):

(since 2.4)

All new created sessions use this settings depends on the platform of your device.

import eu.tsystems.mms.tic.testframework.mobile.driver.AndroidConfig;
import eu.tsystems.mms.tic.testframework.mobile.driver.IOSConfig;
...

WEB_DRIVER_MANAGER.setUserAgentConfig(Browsers.android, (AndroidConfig) options -> {
    options.setApp("cloud:com.telekom.mms.cqa.mdc.androidapp/.HomeActivity");
    options.setAppPackage("com.telekom.mms.cqa.mdc.androidapp");
    options.setAppActivity(".HomeActivity");
});

WEB_DRIVER_MANAGER.setUserAgentConfig(Browsers.ios, (IOSConfig) options -> {
    options.setApp("cloud:com.telekom.mms.cqa.mdc.iosapp");
    options.setBundleId("com.telekom.mms.cqa.mdc.iosapp");
});

You can switch the platform with the browser property:

tt.browser=android
# or
tt.browser=ios

Local test config with options for AppiumDriverRequest

(since 2.4)

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

AppiumDriverRequest request = new AppiumDriverRequest();

// Excample for an Android app
UiAutomator2Options options = new UiAutomator2Options();
options.setApp("cloud:com.telekom.mms.cqa.mdc.androidapp/.HomeActivity");
options.setAppPackage("com.telekom.mms.cqa.mdc.androidapp");
options.setAppActivity(".HomeActivity");
request.setCapabilities(options);

WebDriver driver = WEB_DRIVER_MANAGER.getWebDriver(request);

Use XCUITestOptions for iOS apps in the same way.

Local config with desired caps

(only 2.3)

Please note, that classes like MobileCapabilityType or AndroidMobileCapabilityType were removed in Appium Client 9.x!

AppiumDriverRequest request = new AppiumDriverRequest();
// Define the Android app, provided by Appium cloud or your developer
request.getDesiredCapabilities().setCapability(AndroidMobileCapabilityType.APP_PACKAGE, "org.example.mobile.app.android");
request.getDesiredCapabilities().setCapability(MobileCapabilityType.APP, "cloud:org.example.mobile.app.android/.HomeActivity");
request.getDesiredCapabilities().setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, ".HomeActivity");
// Set the automation engine
request.setAppiumEngine("UiAutomator2");

Setup a iOS app with AppiumDriverRequest

AppiumDriverRequest request = new AppiumDriverRequest();
// Define the iOS app, provided by Appium cloud or your developer
request.getDesiredCapabilities().setCapability(MobileCapabilityType.APP, "cloud:eu.tsystems.mms.tic.testframework.mobile.mdcapp");
request.getDesiredCapabilities().setCapability(IOSMobileCapabilityType.BUNDLE_ID, "eu.tsystems.mms.tic.testframework.mobile.mdcapp");
// Set the automation engine
request.setAppiumEngine("XCUITest");

Page classes for apps

Classic

Use the same page layout like for web based tests.

public class MainPage extends Page {

    @Check
    private UiElement inputTestButton = find(By.xpath("//*[@name='Input Test']"));

    public MainPage(WebDriver webDriver) {
        super(webDriver);
    }

    ...
}

Pages with multiple locators

If your app looks the same in Android and iOS, you can combine different locators in one page class.

At runtime the correct locator is used for find(By..) to create a new instance of your UiElement.

// Important 1: Extends from 'AppiumPage'
public class MainPage extends AppiumPage {

    // Use the Appium annotations to define os based locators
    // Important 2: Don't use 'find()' method!
    @Check
    @AndroidFindBy(xpath = "//*[contains(@resource-id, 'imageButton_textEdit')]")
    @iOSXCUITFindBy(xpath = "//*[@name='Input Test']")
    private UiElement inputTestButton;

    public MainPage(WebDriver webDriver) {
        super(webDriver);
    }

    ...
}

Please note:

Pages with multiple implementations

If you have different workflows in the Android and iOS app, you can implement OS specific page classes.

At runtime the correct page class is used based on the current WebDriver instance.

// Basic class for both platforms
public class MainPage extends Page {
    public MainPage(WebDriver webDriver) {
        super(webDriver);
    }
    ...
}

// iOS specific page with suffix 'IOS'
public class IOSMainPage extends Mainpage{
    public MainPage(WebDriver webDriver) {
        super(webDriver);
    }
    ...
}

// Android specific page with suffix 'Android'
public class AndroidMainPage extends Mainpage{
    public MainPage(WebDriver webDriver) {
        super(webDriver);
    }
    ...
}

In your testcase you only use MainPage

@Test
public void myTest() {
    final WebDriver driver = WEB_DRIVER_MANAGER.getWebDriver();
    MainPage mainPage = PAGE_FACTORY.createPage(MainPage.class, driver);
}

Notes:

  • If no platform specific implementation was found the default page is used (here: MainPage)
  • The suffixes IOS and Android are case insensitive. You can also use somethign like iOSPage or iosPage