How to Access App Store from Appium Script

  February 07, 2017

It’s quite common in mobile app testing that application and test script with a test session needs access to various locations. For example, some test scripts need to access URLs, databases, back-end servers or even App Stores (e.g. Google Play or Apple App Store). With the access, test session can get, download or update certain applications from app stores before the actual test session will begin. In this blog, we’ll take a look at how the Appium test script can access the Apple App Store and how to use certain XCUIElement components to assist your script with that.

Appium is a great choice for native mobile app testing, mobile game testing and of course – for mobile web testing. With the help of test automation, it’s possible to combine different APIs and even frameworks to get applications tested out. We’ve covered some examples of how to use Cucumber with Appium for test automation before and naturally, it’s possible to combine other frameworks too.

In iOS test automation, it’s quite common also to combine higher abstraction frameworks with the API foundation that XCTest provides. Here are a few good examples of using KIF framework on iOS and using XCUITest with test recorder.

how to access app store from appium script

Using XCUIElement for UI Object Identification

One of the greatest things in test automation is how it can be used across different frameworks. In this example as well, we’re using Appium with XCUITest and its API. For instance, XCUIElementTypeSearchField is used to get the class name for Appium’s driver instance:

driver.findElement(By.className("XCUIElementTypeSearchField")).sendKeys("Application's Name");

XCUITest and Xcode Test Recorder offer great help for identifying UI elements and those details that are needed for a test script. In addition, there are a bunch of great tools available for Appium to identify UI elements and yet get the details of Appium automation engine.

Desired Capabilities for Test Session

Naturally, when Appium is used you need to configure desired capabilities to ensure app and test does and access what you want them to do/access. In the case of App Store, bundleId should be and for automation run you should configure it be XCUITest (not actually Appium as we’re using XCUIElement function calls as part of it).

The desired capabilities for a working example could be as follows:

capabilities.setCapability("bundleId", "");
capabilities.setCapability("automationName", "XCUITest");
capabilities.setCapability("deviceName", "device");
capabilities.setCapability("platformName", "iOS");
capabilities.setCapability("udid", deviceUDID);

TIP! If you look for a way to find out what applications have been installed on a device, you can use ideviceinstaller to get that information, plus their bundle ID information. It can be used as follows:

$ ideviceinstaller --list-apps -o list_all

For full detailed instructions on how to create IPA and test package check these verified instructions out!

Code Example for App Store Access from Test Script

And as we’ve gone through the basic things of how to build a test script that can access the app store, interact with its UI elements, here is the AppStoreTest() function for your reference:

    public void AppStoreTest() throws Exception {
        // Search button at bottom menu
        // Search text field
                .sendKeys("Pineapple Pen");
        // driver.hideKeyboard() enclosed within try/catch
        // Find the parent element of the desired app
        MobileElement parent = driver.findElement("Pineapple Pen, Ketchapp"));
        // Try to find and click the GET and then INSTALL buttons under the parent
        try {
            driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
        } catch (NoSuchElementException e) {
            // If GET button wasn't found, the app has been purchased before and there's a Download button instead
        // Increase implicit wait time for the download
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.MINUTES);
        // Open the app from within App Store
        // Put implicit wait back to our default value
        driver.manage().timeouts().implicitlyWait(defaultWaitTime, TimeUnit.SECONDS);
        // Allow notifications pop-up

The logic with the script is that first ‘search’ button is pressed on the App Store and whatever application is searched using .sendKeys. We are also identifying parent window which needs to be in line with what app is meant to be installed.

In iOS, after the search provides results the icon first indicates ‘GET’ and after a click, it turns on ‘INSTALL’ or ‘DOWNLOAD’ depending on whether the app has been installed before. After this, it’s possible to open application for a test run or alternatively just keep it in the background (e.g. if other application needs to interact/share some details with the recently installed app).

That’s it for this time – hopefully you’ll find it useful!

Happy Testing Folks!