Skip to content

Create an Appium UI Test Automation Project

HydraXMan edited this page Dec 26, 2022 · 1 revision

Develop

Import HydraLab Sdk

  1. implementation 'com.microsoft.hydralab:appium-sdk:1.0.0' link
  2. implementation 'io.appium:java-client:8.0.0'

PS. The parameters in SDK are ThreadLocal, you need get them in main Thread.

Test Case for Android App

  1. Methods of test cases should be annotated with @Test.
  2. Add a method annotated with @Before.
    • Get DeviceId from SDK method ThreadParam.getDeviceId()
    • Init Appium Service and Android driver
  @Before
  public void beforeTest() {
      DesiredCapabilities capabilities = new DesiredCapabilities();
      // Init and start AppiumService
      service = AppiumDriverLocalService.buildService(new AppiumServiceBuilder().usingPort(4723).withArgument(GeneralServerFlag.BASEPATH, "/wd/hub/").withArgument(GeneralServerFlag.RELAXED_SECURITY).withArgument(GeneralServerFlag.LOG_LEVEL, "error").withArgument(GeneralServerFlag.ALLOW_INSECURE, "adb_shell"));
      service.start();

      // Get DeviceID and config from ThreadParam
      AppiumParam ap = ThreadParam.getAppiumParam();
      String deviceID = ap.getDeviceId();
      String configValue = ThreadParam.getConfigString("configKey");
      //Set the target device of AndroidDriver
      if (deviceID != null) {
          capabilities.setCapability("udid", deviceID);
      }`
      capabilities.setCapability("newCommandTimeout", 4000);
      capabilities.setCapability("clearDeviceLogonStart", true);
      // Init AndroidDriver
      androidDriver = new AndroidDriver(service.getUrl(), capabilities);
  }
  1. Add a method annotated with @After.
    • Destroy Appium Service and Android driver
    @After
    public void afterTest() {
        if (androidDriver != null) {
            androidDriver.quit();
        }
        if (service != null) {
            service.stop();
        }
    }

Test Case for iOS App

  1. Methods of test cases should be annotated with @Test.
  2. Add a method annotated with @Before.
    @Before
    public void beforeTest() {
        service = AppiumDriverLocalService.buildService(new AppiumServiceBuilder().usingPort(4723).withArgument(GeneralServerFlag.BASEPATH, "/wd/hub/").withArgument(GeneralServerFlag.RELAXED_SECURITY).withArgument(GeneralServerFlag.LOG_LEVEL, "error").withArgument(GeneralServerFlag.ALLOW_INSECURE, "adb_shell"));
        service.start();
        AppiumParam ap = ThreadParam.getAppiumParam();

        String udid = ap.getDeviceId();
        String deviceName = ap.getDeviceName();
        String osVersion = ap.getDeviceOsVersion();
        int wdaPort = ap.getWdaPort();

        // These capabilities are important to the iOS Test
        DesiredCapabilities caps = new DesiredCapabilities();

        caps.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 4000);
        caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "iOS");
        caps.setCapability(MobileCapabilityType.AUTOMATION_NAME, "XCUITest");
        caps.setCapability(IOSMobileCapabilityType.XCODE_SIGNING_ID, "iPhone Developer");

        caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, osVersion);
        caps.setCapability(MobileCapabilityType.DEVICE_NAME, deviceName);
        caps.setCapability(MobileCapabilityType.UDID, udid);

        caps.setCapability(IOSMobileCapabilityType.WEB_DRIVER_AGENT_URL, "http://127.0.0.1:" + wdaPort);
        caps.setCapability(IOSMobileCapabilityType.USE_PREBUILT_WDA, false);
        caps.setCapability("useXctestrunFile", false);
        caps.setCapability("skipLogCapture", true);

        try {
            iosDriver = new IOSDriver(new URL("http://127.0.0.1:4723/wd/hub"), caps);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
    }
  1. Add a method annotated with @After.
    @After
    public void afterTest() {
        if (iosDriver != null) {
            iosDriver.quit();
        }
        if (service != null) {
            service.stop();
        }
    }

Test Suite

  1. The class should be annotated with @@RunWith(Suite.class) and @Suite.SuiteClasses({}).
  2. Put the test case classes into SuiteClasses.
   @RunWith(Suite.class)
   @Suite.SuiteClasses({
        BaseTest1.class,
        BaseTest2.class,
   })
   public class AppiumTestSuite {
   }

Main Method

  1. This class is used to debug when developing test case.
  2. Invoke test suite by JuitCore.
  3. Init ThreadParam brfore starting test.
    public class AppiumTestEntry {
        public static void main(String[] args) {
            Map<String,String> configMap= new HashMap<>();
            configMap.put("dbUrl",".....");
            configMap.put("dbPwd",".....");
            AppiumParam ap = new AppiumParam(<deviceId>, <deviceName>, <osVersion>, <wdaPort>, <apkPath>, <outputDir>);
            ThreadParam.init(ap, configMap);
            JUnitCore junit = new JUnitCore();
            junit.run(AppiumTestSuite.class);
        }
    }

Generate and Test Jar

  1. Use shadow to generate Jar
plugins {
    id 'java'
    id 'com.kncept.junit.reporter' version '2.1.0'
    id 'com.github.johnrengelman.shadow' version '6.1.0'
}
shadowJar {
    classifier = 'test'
    version = null
    zip64 true

    manifest {
        attributes 'Main-Class': 'com.appium.demo.AppiumTestEntry'
    }
}
  1. Test Jar in local PC
java -cp path_to_jar com.appium.demo.AppiumTestEntry

Demo Project

AppiumDemo.zip

Clone this wiki locally