Learning and Simulation of Services for Mobile Application Testing
Development and testing of mobile applications that communicate with back-end services over REST services, or that connect to physical devices over a Bluetooth interface or with NFC, is burdened by the complexity of setting up the testing environment. Having the REST service return 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 reproducability 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 runs on the SV Lab server embedded in the UFT Mobile connector during the test or debugging session. The mobile application launched on the mobile device connected to the UFT Mobile connector is instrumented with an SV hook code configuring it to communicate with virtual services in the embedded virtual lab.
This enables REST services and Bluetooth LE and NFC devices to 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 that demonstrate a simulation of a REST back-end service used by an Android mobile application during the test. They show the integration of SV lab with the UFT Developer test and with the Appium test.
Our scenario focuses on the flow of using SV Lab with the UFT Developer test described in the introduction.
We demonstrate the simulation of services for a 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 an application involves testing the application with various weather conditions, which may be difficult to achieve using the real, constantly changing data. The simulation helps to provide well defined weather conditions, including extremes, for good test coverage.
Embedded simulation in a UFT Developer mobile application test
The demo comes with pre-recorded models so that it is ready to run the test using virtual services. Below, we describe the configuration steps needed to prepare and run the test environment.
Installing the UFT Developer Java libraries
The UFT Developer libraries must be installed in a local Maven repository prior to running the demo.
Navigate to the SDK/Maven
subdirectory in the UFT Developer installation
directory (for example, "c:\Program Files (x86)\Micro Focus\UFT Developer\SDK\Maven"
) and
then run the 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
After installing the UFT Developer Java libraries, you need to configure the Android phone to connect to the PC
running the simulation. Most of the configuration is automatic
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
. For more information,
see UFT Mobile documentation).
You can verify this step by locating the SV Connector Configuration utility installed on the phone by UFT Mobile and checking the connection to the HTTP Proxy connector:
To virtualize REST services on an Android phone, you must make it communicate with the
HTTP proxy connector. To do this, configure the proxy settings of your
current WiFi connection on the phone. Within the WiFi settings, enable proxy,
select 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.
When prompted, select to use the certificate for VPN and apps.
Use one of the following methods:
* Upload the file over a USB cable and navigate to
Settings/Security/Credential storage and perform Install from device
storage.
* Send the certificate by e-mail to a phone account and open the attachment in the e-mail app.
Installing the Forecastie application
Download the "Forecastie - Weather app" from F-Droid to your hard disk and then upload it to UFT Mobile. Install the packaged version of the application on Android 7.0+ (required because of server certificate trust instrumentation). You can use a non-packaged version on older Android versions.
Running the automation test
Edit the demo.properties
file and enter the device ID of the Android phone
connected to UFT Mobile to use in the 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();
}
Subsequently, 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 back-end service simulation model
The setup of a mobile phone and instrumented application for learning simulation models from the real service is exactly the same as for 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 the following information:
* Your UFT Mobile server URL with ms
argument
* The access key for execution created in Settings/Access Keys in UFT Mobile with -ma
argument
* The mobile phone name with -mn
argument or the 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 back-end services are recorded, as you can see in the sv-capture console output.
Stop the learning by pressing Enter within the console running the sv-capture tool.
The learned model is located in 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.