松勤-以工匠精神,专注IT在线教育,打造中国互联网教育品牌。
【松勤软件自动化测试】Appium移动端UI自动化中,两个APP交互操作的实践经验
发布时间:2018-10-25

前段时间在用Appium做UI自动化时,碰到一个原来没有碰到的问题,原来做的项目都是单独的APP操作。在目前的项目中,有两个APP,两个APP需要交互操作,一个发单,一个接单,这边发单以后,另外个APP需要接单。

想了两种方法:

一、Two App in one device 两个app在同一个设备上运行


1.启动device,安装一个app


@BeforeTest
  public void beforeTest() throws Exception{
      logger.info("***********************************************************************");
      logger.info("Test begin:");
      logger.info("发单接单,整车,测试");
       
      //获取app路径
      File classpathRoot = new File(System.getProperty("user.dir"));
      File appDir = new File(classpathRoot, "apps");
      File app_carrier = new File(appDir, "carrier.apk");
      File app_shipper = new File(appDir, "shipper.apk");
      logger.info("获取app path:"+app_carrier.getAbsolutePath());
      //set up Appium
      DesiredCapabilities capabilities1 = new DesiredCapabilities();
      capabilities1.setCapability("device","Android");
      capabilities1.setCapability(MobileCapabilityType.DEVICE_NAME, "HC35NW906179");
      capabilities1.setCapability(MobileCapabilityType.UDID, "HC35NW906179");
      capabilities1.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4.4");
      capabilities1.setCapability(MobileCapabilityType.APP, app_carrier.getAbsolutePath());
      capabilities1.setCapability(MobileCapabilityType.APP_PACKAGE, "com.kuaihuoyun.driver");
      capabilities1.setCapability(MobileCapabilityType.APP_ACTIVITY, ".KDLaunch");
      //for input Chinese
      capabilities1.setCapability("unicodeKeyboard", "True"); 
      capabilities1.setCapability("resetKeyboard", "True");
      try {
          driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities1);
        logger.info("carrier app启动成功");
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

 

2.安装另外一个app


//安装货主端
driver.installApp(app_shipper.getAbsolutePath());
logger.info(app_shipper.getAbsolutePath());
if(driver.isAppInstalled("com.kuaihuoyun.freight")==true){
    logger.info("货主端安装成功");
}
else{
    logger.info("货主端安装失败");        
}


3.操作时通过startActivity切换不同app 


//切换到货主端
driver.startActivity("com.kuaihuoyun.freight", ".KDLaunch");


二、two devices two apps 两个设备,每个设备单独一个app

 

  //set up Appium
    //司机端
  DesiredCapabilities capabilities1 = new DesiredCapabilities();
  capabilities1.setCapability("device","Android");
  capabilities1.setCapability(MobileCapabilityType.DEVICE_NAME, "7N2MYN14BK036952");
  capabilities1.setCapability(MobileCapabilityType.UDID, "7N2MYN14BK036952");
  capabilities1.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4.3");
  capabilities1.setCapability(MobileCapabilityType.APP, app_carrier.getAbsolutePath());
  capabilities1.setCapability(MobileCapabilityType.APP_PACKAGE, "com.kuaihuoyun.driver");
  capabilities1.setCapability(MobileCapabilityType.APP_ACTIVITY, ".KDLaunch");
  //for input Chinese
  capabilities1.setCapability("unicodeKeyboard", "True"); 
  capabilities1.setCapability("resetKeyboard", "True");
   
  try {
      driver__carrier = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities1);
    logger.info("carrier app启动成功");
} catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
   
    //货主端
  DesiredCapabilities capabilities2 = new DesiredCapabilities();
  capabilities2.setCapability("device","Android");
  capabilities2.setCapability(MobileCapabilityType.DEVICE_NAME, "HC35NW906179");
  capabilities2.setCapability(MobileCapabilityType.UDID, "HC35NW906179");
  capabilities2.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4.4");
  capabilities2.setCapability(MobileCapabilityType.APP, app_shipper.getAbsolutePath());
  capabilities2.setCapability(MobileCapabilityType.APP_PACKAGE, "com.kuaihuoyun.freight");
  capabilities2.setCapability(MobileCapabilityType.APP_ACTIVITY, ".KDLaunch");
  //for input Chinese
  capabilities2.setCapability("unicodeKeyboard", "True"); 
  capabilities2.setCapability("resetKeyboard", "True");
 
  try {
      driver__shipper = new AndroidDriver<>(new URL("http://127.0.0.1:4725/wd/hub"), capabilities2);
    logger.info("shipper app启动成功");
} catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}


启动两个appium代理,设置不同port,在不同设备中安装不同app


总结:Two App in one device的方式运行起来不是很稳定,目前采用two devices two apps的方式

联系地址:南京市雨花台区花神大道23号京妆商务5幢505
咨询电话:025-86331019
授课课程:软件测试自动化全栈课程性能测试全栈课程
QQ交流群
官方微信公众号
扫码领取公开课视频
025-86331019
Copyright ©2014-2019 南京松勤网络科技有限公司 .All Rights Reserved
苏ICP备14047045号-1
本网站部分图片来源于网络,如有任何疑问请第一时间与我们联系,未经沟通本站不承担相关法律责任!