Appium code Android example with NV - Java

The following Java example shows you how to use Appium code for testing your app on UFT Mobile with Network Virtualization.

To see the effect of the network on your test, add your application test code under the Write your Appium test here and Repeat your Appium tests here sections. At the end of the run, a report is generated along with a *.har (HTTP Archive) file which can be used for load testing.

You can copy this code and use it in your test, after modifying the UFTM_SERVER, UFTM_SERVER_CLIENT_ID, and UFTM_SERVER_CLIENT_SECRET variables.

Note: Before running your Appium test with NV, make sure that the device proxy settings are configured to use the NV proxy and that an NV proxy certificate is installed. For details, see Set up Network Virtualization integration in UFT Mobile (on-premises).

The first section contains all of the required import statements.

import java.io.File;

import java.io.FileOutputStream;

import java.net.URL;

import java.util.Base64;

import java.util.HashMap;

import java.util.Map;

 

import org.apache.commons.io.IOUtils;

import org.openqa.selenium.Platform;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.remote.DesiredCapabilities;

 

import io.appium.java_client.AppiumDriver;

import io.appium.java_client.android.AndroidDriver;

import io.appium.java_client.ios.IOSDriver;

import io.appium.java_client.remote.MobileCapabilityType;

The next section contains the set up of the Network Virtualization.

public class AMB_NV_Android_Example {

 

public static final String UFTM_SERVER_IP = "127.0.0.1";

public static final String UFTM_SERVER_CLIENT_ID = "oauth2-…";

public static final String UFTM_SERVER_CLIENT_SECRET = "ke1SU…";

public static final String UFTM_SERVER_TENANT_ID = "999999999";

public static final String UFTM_SERVER_WORKSPACE_NAME = "Default workspace";

 

public static final String NET_PROFILE_NO_EMULATION = "No emulation";

public static final String NET_PROFILE_4G_TYPICAL = "4G typical";

public static final String NET_PROFILE_4G_TYPICAL_INTL = "4G typical - intercontinental";

public static final String NET_PROFILE_4G_POOR = "4G poor";

public static final String NET_PROFILE_4G_POOR_INTL = "4G poor - intercontinental";

public static final String NET_PROFILE_3G_TYPICAL = "3G typical";

public static final String NET_PROFILE_3G_TYPICAL_INTL = "3G typical - intercontinental";

public static final String NET_PROFILE_3G_POOR = "3G poor";

public static final String NET_PROFILE_3G_POOR_INTL = "3G poor - intercontinental";

public static final String NET_PROFILE_2_5G_POOR = "2.5G poor";

public static final String NET_PROFILE_DISCONNECT = "Network disconnection";

 

public static enum PlatformName {Android, iPhone}

public static PlatformName platform = PlatformName.Android; /*PlatformName.iPhone;*/

 

public static void main(String[] args) {

 

AppiumDriver<WebElement> driver = null;

 

try {

// Connect and start NV test

URL mcServerUrl = new URL("http://" + UFTM_SERVER_IP + ":8080/wd/hub");

 

DesiredCapabilities capabilities = new DesiredCapabilities();

capabilities.setCapability("oauthClientId", UFTM_SERVER_CLIENT_ID);

capabilities.setCapability("oauthClientSecret", UFTM_SERVER_CLIENT_SECRET);

capabilities.setCapability("tenantId", UFTM_SERVER_TENANT_ID);

capabilities.setCapability("mcWorkspaceName", UFTM_SERVER_WORKSPACE_NAME);

 

// At first, select a network profile that doesn't impair network traffic.

capabilities.setCapability("mc:net:profileName", NET_PROFILE_NO_EMULATION);

 

switch (platform) {

case iPhone:

capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "iOS");

capabilities.setCapability("app", "com.apple.settings");

capabilities.setCapability("bundleId", "com.apple.Preferences");

driver = new IOSDriver<WebElement>(mcServerUrl, capabilities);

break;

case Android:

default:

capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, Platform.ANDROID);

capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, ">=4.4.2");

capabilities.setCapability("appPackage", "com.Advantage.aShopping");

capabilities.setCapability("appActivity", "com.Advantage.aShopping.SplashActivity");

driver = new AndroidDriver<WebElement>(mcServerUrl, capabilities);

break;

}

 

// Make sure the device proxy settings are configured to use

// the NV proxy and that an NV proxy certificate is installed.

// This can be done through a script using UI automation.

 

The following section of the code should contain the actions that you want to perform with Network Virtualization.

 

// ****************************************************************

// **************** Write your Appium test here *****************

// ****************************************************************

 

// Change profile

Map<String,String> map = new HashMap<>();

map.put("profileName", NET_PROFILE_3G_TYPICAL);

driver.executeScript("mc:net:changeProfile", map);

 

Object response= null;

 

The following section of the code should contain the same actions that you performed earlier, repeated, in order to generate data for comparison. It also contains the code that generates a report and a *.har file.

 

// ****************************************************************

// **************** To see the effect of the network *************

// **************** repeat your Appium tests here *************

// ****************************************************************

// Stop test and get report

String fileName = "C:\\report.zip";

response = driver.executeScript("mc:net:getReport");

String base64Str = (String) response;

byte[] bytes = Base64.getDecoder().decode(base64Str);

IOUtils.write(bytes, new FileOutputStream(new File(fileName)));

 

// get har file

fileName = "C:\\nv_har.zip";

response = driver.executeScript("mc:net:getHAR");

base64Str = (String) response;

bytes = Base64.getDecoder().decode(base64Str);

IOUtils.write(bytes, new FileOutputStream(new File(fileName)));

}

catch (Exception e) {

e.printStackTrace();

}

finally {

if (driver != null) {

driver.quit();

}

}

}

}

Back to top

See also: