Skip to content

3 MobileApps

Martin Großmann edited this page Jun 20, 2024 · 2 revisions

Automate mobile apps

Use a Android app

Setup a Android app with AppiumDriverRequest

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");

Use a iOS app

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