Learning And Simulation of Services For Mobile Application Testing

Development and testing of mobile applications that talk to backend services over REST services or that connect to physical devices over Bluetooth interface or with NFC is hurdled by the complexity of setting up the testing environment. Having the REST service to return a predictable responses for the test case is not always an easy task. Test cases with Bluetooth or NFC devices usually require physical manipulation with connected devices. Achieving reproducibility can be challenging and automation of the test cases if often impossible.

Service virtualization helps mobile application testing and development by providing stable virtual environments with well-defined behavior for each test.

The test is extended with simulation models and a virtual lab configuration. The virtual lab is running on the SV Lab server embedded into the UFT Mobile connector during the test or debugging session. The mobile application that was launched on mobile device connected to the UFT Mobile connector had been instrumented with a SV hook code configuring it to talk to virtual services in the embedded virtual lab.

This way, REST services and Bluetooth LE and NFC devices can be simulated in UFT Developer or Appium tests of Android and iOS applications.

The example application

There are two similar demo applications available on GitHub demonstrating a simulation of REST backend service used by Android mobile application during the test. They show the integration of SV lab with the UFT Developer test and with Appium test.

We will follow the flow of the using SV Lab with UFT Developer test in this introduction.

We demonstrate the simulation of services for mobile application test with the Forecastie open source weather forecast application which uses the OpenWeatherMap REST API for obtaining weather data.

Development and testing of such application involves testing the application with various weather conditions which may be hard to achieve using the real, ever changing data. The simulation helps to provide well defined weather conditions including extremes for a good test coverage.

Embedded simulation in UFT Developer mobile application test

The demo comes with pre-recorded models so that it is ready to run the test using virtual services. Lets follow the configuration steps needed to prepare and run the test environment.

Installing the UFT Developer Java libraries

The UFT Developer libraries have to be installed in local Maven repository prior running the demo.

Navigate to the SDK/Maven subdirectory in the UFT Developer installation directory (i.e. "c:\Program Files (x86)\Micro Focus\UFT Developer\SDK\Maven"), then run following commands from there:

mvn install:install-file -DpomFile=com.hp.lft.sdk-15.0.0-pom.xml -Dfile=com.hp.lft.sdk-15.0.0.jar
mvn install:install-file -DpomFile=com.hp.lft.common-15.0.0-pom.xml -Dfile=com.hp.lft.common-15.0.0.jar
mvn install:install-file -DpomFile=com.hp.lft.unittesting-15.0.0-pom.xml -Dfile=com.hp.lft.unittesting-15.0.0.jar 
mvn install:install-file -DpomFile=com.hp.lft.report-15.0.0-pom.xml -Dfile=com.hp.lft.report-15.0.0.jar 
mvn install:install-file -DpomFile=com.hp.lft.reportbuilder-15.0.0-pom.xml -Dfile=com.hp.lft.reportbuilder-15.0.0.jar

Mobile phone configuration

Now you need to configure the Android phone to connect to your PC running the simulation. Most of the configuration is done automatically once you connect the phone to UFT Mobile (the SV integration must be enabled for that particular phone in UFT Mobile/server/conf/connector.properties, see UFT Mobile documentation for more information).

You can verify this step by locating the SV Connector Configuration utility installed on the phone by UFT Mobile and checking the connection to HTTP Proxy connector:

To virtualize REST services on Android phone, you need to make it talk to the HTTP proxy connector. You achieve this by configuring proxy settings of your current WiFi connection on the phone. Within the WiFi settings, enable proxy selecting Manual and set the proxy host name to 127.0.0.1 and port to 6543:

To enable virtualization of services over HTTPS, import the ../lib/sv-capture.pem certificate into User Certificates on the phone. Choose to use the certificate for VPN and apps when asked. * One way is to upload the file over USB cable and go to Settings/Security/Credential storage and perform Install from device storage. * Another option is to send the certificate by e-mail to phone account and open the attachment in e-mail app.

Installing the Forecastie application

Download the "Forecastie - Weather app" from F-Droid to disk and upload to UFT Mobile. Install the packaged version of application on Android 7.0+ (because of server certificate trust instrumentation). You can use non-packaged version on older Android versions.

Running the automation test

Edit the demo.properties file and enter the device ID of Android phone connected to UFT Mobile to use in test, uftm.deviceId=5200....

Then run mvn test within the demo directory.

The setupBeforeClass() method in the test class will compile simulation models located in src/test/resources and deploy and start the virtual lab on the SV Lab server embedded in UFT Mobile:

@BeforeClass
public static void setUpBeforeClass() throws Exception {
    ...
    device = MobileLab.lockDevice(...);

    // create SV Lab
    sv = SvClient.newInstance(device.getSvInfo().getEndpoint());
    sv.loadActiveVirtualLab("classpath:/sv-lab.json", sv.compileModuleFromSources("classpath:/demo/*"), true);
    sv.startActiveVirtualLab();
}

After that, each test can simulate required behavior of virtual services by running the corresponding application scenario from the simulation model:

@Test
public void test() throws GeneralLeanFtException {
    sv.runSimulation("weatherForecast");
    ...
}

Learning the backend service simulation model

The setup of mobile phone and instrumented application for learning of simulation models from the real service is exactly the same as in case of simulation.

Simulation models can be learned either with the UFT Developer GUI or with the sv-capture command line tool available in the SV Lab client distribution that can be downloaded from the Micro Focus marketplace.

To learn using the sv-capture tool, start learning with:

sv-capture.sh -ms http://localhost:8080 -ma "client=oauth2-..." \
              -r https://api.openweathermap.org -as weatherForecast \ 
              -o ./learned-model

In Windows:

sv-capture.bat -ms http://localhost:8080 -ma "client=oauth2-..." ^
               -r https://api.openweathermap.org -as weatherForecast ^
               -o learned-model

Provide your UFT Mobile server URL with ms argument, the access key for execution created in Settings/Access Keys in UFT Mobile with -ma argument, and mobile phone name with -mn argument or mobile phone ID with -mi argument.

Then open the Forecastie app on your smartphone and tap on the Refresh button. Alternatively, you can change your location to display a forecast for another location. The messages to backend services are being recorded as you can see in the sv-capture console output.

Now stop the learning by pressing Enter within the console running the sv-capture tool.

The learned model is located within the learned-model directory. You can check and modify learned scenarios and data.

Simulate the learned model by running:

sv-capture.sh -ms http://localhost:8080 -ma "client=oauth2-..." \
              -m SIMULATE -vsl ./learned-model -as weatherForecast \
              -r https://api.openweathermap.org

In Windows:

sv-capture.bat -ms http://localhost:8080 -ma "client=oauth2-..." ^
               -m SIMULATE -vsl ./learned-model -as weatherForecast ^
               -r https://api.openweathermap.org

Restart the Forecastie app and repeat the actions against the simulation using the learned model.