美文网首页
使用appium远程并发操作安卓测试机

使用appium远程并发操作安卓测试机

作者: 测试老杨 | 来源:发表于2019-01-17 15:31 被阅读51次

安卓自动化环境准备

1、客户端电脑(PC)需要安装的软件主要包括:JDK8, eclipse, maven, testng, appium
2、服务端电脑(PC)需要安装的软件主要包括:JDK8, Android-SDK, appium-desktop
3、移动端设备(Mobiles):
1)打开USB调试,并且连接对应的服务端(PC)
2)安装app客户端(比如:搜狗搜索)
3)安装包名查看器
4、使用appium-desktop录制脚本
5、优化脚本(参数化,添加验证),调试脚本

测试执行步骤

1、通过appium-desktop启动appium-server
2、客户端电脑上运行appium脚本
3、appium-server接收指令并驱动对应的移动设备

设计appium脚本

完整代码如下:

package day09;

import java.net.URL;
import java.time.Duration;
import java.util.concurrent.TimeUnit;

import org.hamcrest.Matchers;
import org.junit.Assert;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;



/**
 * appium客户端程序
 * @author yangzc
 *
 */
public class SearchTest4 {
    
  @DataProvider(name="ds",parallel=true)
  public Object[][] myData(){
      Object[][] arr = {
              {"Android","4.4.2","192.168.0.131:62001","com.sogou.activity.src","com.sogou.search.entry.EntryActivity","http://192.168.0.131:4723/wd/hub","赵薇","黄有龙"},
              {"Android","6.0.1","4201cada","com.sogou.activity.src","com.sogou.search.entry.EntryActivity","http://192.168.0.114:4723/wd/hub","赵薇","黄有龙"}
      };
      return arr;
  }
    
    
  @Test(dataProvider="ds")
  public void f(String platform,String platformVersion,String deviceName,String appPackage,String appActivity,String appiumServer,String keyword,String expected) throws Exception {
        //获取配置工具
        DesiredCapabilities capabilities = new DesiredCapabilities();
        //设置移动设备的平台
        capabilities.setCapability("platformName", platform);
        //设置移动设备的连接名
        capabilities.setCapability("deviceName", deviceName); 
        //设置平台的版本
        capabilities.setCapability("platformVersion", platformVersion); 
        //设置app的包名
        capabilities.setCapability("appPackage", appPackage);
        //设置app的启动页面
        capabilities.setCapability("appActivity", appActivity);
        //
        capabilities.setCapability("unicodeKeyboard", true);
        capabilities.setCapability("resetKeyboard", true);
        //创建一个驱动
        AppiumDriver<MobileElement> driver = 
                new AndroidDriver<MobileElement>(new URL(appiumServer), capabilities);
        //设置默认的等待时长
        driver.manage().timeouts().implicitlyWait(15,TimeUnit.SECONDS);
        //
        //Thread.sleep(10000);
        //找到文本框
        MobileElement el5 = (MobileElement) driver.findElementById("com.sogou.activity.src:id/entry_text");
        //清空文本
        el5.clear();
        //
        int width = driver.manage().window().getSize().width;
        //
        int height = driver.manage().window().getSize().height;
        //System.out.println("宽度:"+width);
        //System.out.println("高度:"+height);
        //找到关键字搜索框
        MobileElement el6 = (MobileElement) driver.findElementById("com.sogou.activity.src:id/sugg_search_edittext");
        //输入关键字
        el6.sendKeys(keyword);
        //找到搜索的按钮
        MobileElement el7 = (MobileElement) driver.findElementById("com.sogou.activity.src:id/search_button");
        //点击搜索
        el7.click();
        //
        Thread.sleep(5000);
        //
        //driver.swipe(75, 500, 75, 0, 800);
        TouchAction tAction = new TouchAction(driver);
        tAction.press(width/2,height*3/4).waitAction(Duration.ofMillis(200)).moveTo(width/2,0).release().perform();
        //模拟滑动
        Thread.sleep(2000);
        tAction.press(width/2,height*3/4).waitAction(Duration.ofMillis(200)).moveTo(width/2,0).release().perform();
        //模拟滑动
        Thread.sleep(2000);     
        //模拟滑动
        tAction.press(width/2,height*3/4).waitAction(Duration.ofMillis(200)).moveTo(width/2,0).release().perform();     
        //验证搜索结果页面的源代码是否包含黄有龙
        Assert.assertThat(driver.getPageSource(),Matchers.containsString(expected));    
        //
        Thread.sleep(2000);
        //关闭app
        driver.quit();
  }
}

运行appium脚本

appium190117.gif

扫码关注本人公众号

image.png

相关文章

网友评论

      本文标题:使用appium远程并发操作安卓测试机

      本文链接:https://www.haomeiwen.com/subject/rpuidqtx.html