App自动化测试-Appium的使用
App自动化测试 —— Appium的使用
目录
简介
Appium
是一个开源测试自动化框架,用于原生、 混合和移动 Web 应用程序。
安装
Appium
安装方式有两种,一种是通过
npm
命令行安装,另一种是通过安装可视化工具。
npm
安装npm
安装方式稍微麻烦一点,需要先安装npm
才能进行操作,想了解的可参考:可视化工具安装
可视化工具的下载可前往
本文使用的是 可视化工具安装 方式。
配置 & Run
安装完成后启动
Appium.exe
,点击编辑配置
在弹出的配置界面中填写已提前配置好的环境变量路径并保存重新启动
重启后点击启动服务器
点击右上角的启动检查器会话
点击中间偏下方的 “ + ” 号添加参数
Capability | Description |
---|---|
platformName | 设备操作系统 |
platformVersion | 设备操作系统版本 |
deviceName | 设备名称(可通过执行adb的 adb devies 获取当前连接上的设备) |
appPackage | 应用程序包名 |
appActivity | 从appPackage 中启动的 Android 活动的活动名称 |
noReset | true or false,执行程序的同时是否清空app数据 |
更多Capability可参考:
填写部分参数如下:
点击启动会话后弹出如下窗口:
Appium
可以通过录制操作,生成JS、Java、Python、Ruby等程序可运行的语言。具体操作如:开始操作 → 点击app界面的某一个按钮或输入框 → 点击后会在右侧的 选定元素 中显示相关信息 → 选择 点击 或 发送密钥 的操作 → 刷新屏幕截图。
部分选择元素是无法选中的,需要使用坐标定位所点击位置。录制完相关操作,将录制生成的代码复制到剪贴板,粘贴至
Android Studio
开发工具
@Test
注解的方法里。
run
一下,自动化UI测试的操作步骤就完成了
在
Android Studio
里面执行代码,应提前引入
commons-lang3
、
java-client
、
selenium-server-standalone
三个jar包,下载地址在文章尾部。部分
Java
代码如下:
@Before
public void initAppium() {
File classpathRoot = new File(System.getProperty("user.dir"));
// 获取apps文件,前面测试应用所存放的目录
File appDir = new File(classpathRoot, "/test_app/");
// 获取apk文件
File app = new File(appDir, "AppiumAutoTest.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName", "测试的设备系统");
capabilities.setCapability("platformVersion", "系统平台版本号");
capabilities.setCapability("deviceName", "测试的设备名称");
capabilities.setCapability("app", app.getAbsolutePath());
capabilities.setCapability("appPackage", "被测应用包名");
capabilities.setCapability("appActivity", "app启动的第一个运行界面");
// 连接appium启动相应app
try {
driver = new AndroidDriver<>(new URL("http://192.168.31.98:4723/wd/hub"), capabilities);
} catch (MalformedURLException e) {
e.printStackTrace();
}
System.out.println("App is launched!");
}
@Test
public void startTest() throws InterruptedException {
MobileElement el3 = (MobileElement) driver.findElementByAccessibilityId("Reflow");
el3.click();
sleep(1000);
MobileElement el4 = (MobileElement) driver.findElementByAccessibilityId("Slideshow");
el4.click();
sleep(1000);
MobileElement el5 = (MobileElement) driver.findElementByXPath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/androidx.drawerlayout.widget.DrawerLayout/android.view.ViewGroup/android.widget.LinearLayout[2]/android.widget.FrameLayout[1]/android.widget.FrameLayout/android.view.ViewGroup");
el5.click();
sleep(1000);
execRootCmdSilent("adb shell input tap 958 1588");
sleep(1000);
MobileElement el6 = (MobileElement) driver.findElementByAccessibilityId("更多选项");
el6.click();
sleep(1000);
MobileElement el7 = (MobileElement) driver.findElementById("com.miyue.appiumautotest:id/title");
el7.click();
sleep(1000);
MobileElement el8 = (MobileElement) driver.findElementByAccessibilityId("转到上一层级");
el8.click();
}
private void sleep(int second) throws InterruptedException {
Thread.sleep(second);
}
/**
* 使用adb命令执行点击的坐标点
* @param paramString
*/
public void execRootCmdSilent(String paramString) {
String content = "";
BufferedReader reader = null;
InputStream is = null;
try {
java.lang.Process process = Runtime.getRuntime().exec(paramString);
is = process.getInputStream();
reader = new BufferedReader(new InputStreamReader(is));
StringBuffer output = new StringBuffer();
int read;
char[] buffer = new char[4096];
while ((read = reader.read(buffer)) > 0) {
output.append(buffer, 0, read);
}
content = output.toString();
System.out.println("App execution adb done!" + content);
} catch (Exception e) {
e.printStackTrace();
System.out.println("App issue!" + e.getMessage());
} finally {
if (null != is) {
try {
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 运行完成
* @throws InterruptedException
*/
@After
public void end() throws InterruptedException {
// 退出应用
driver.quit();
}
问题 & 解决方案
no tasks available.
原因 :没有配置
Run/Debug Configurations
。Connection refused: connect.
原因 :
Appium
服务未启动。An unknown server-side error occurred while processing the command. Original error: Unable to find an active device or emulator with OS 11. The following are available: 6HJ4C19A29009173 (10).
原因 :
platformVersion
输入的操作系统与连接到的操作系统版本不对。An unknown server-side error occurred while processing the command. Original error: Could not find a connected Android device in 20088ms.
原因 :未连接到测试设备,需要使用adb或数据线连接上设备,执行adb命令
adb devices
查看目前所连接到的设备,将设备名称拷贝粘贴至deviceName
。重新编写测试步骤的代码,应该提前
clean
运行缓存。
优点 & 缺点
优点
- 支持第三方应用程序。
- 支持同时测试多台设备。
- 支持生成多种语言的测试代码。
- 不需要把第三方代码编译进app中。
- 解放了双手,避免了不必要的重复操作。
- 支持IOS、Android、Windows的应用程序。
- 支持Native apps、 Hybrid apps、Web apps。
缺点
- 暂不支持可用于编写Android程序的Kotlin代码。
- 相对于人工测试而言,Appium自动测试不太适合小规模的测试。
总结
噢,相信你体验之后肯定会刷新你对
Appium
的看法。
若测试的应用不需要进行多次重复性的UI测试,似乎没必要使用
Appium
自动化测试,与手动点击UI测试相比,速度慢的似乎不是一丁半点。
最近了解到一个叫
AccessibilityService
(无障碍服务)的东西,这玩意可以用来开发Application脚本,脚本执行速度那叫一个快。只可惜它不是一个自动化测试的框架,不然用来搞自动化测试应该很牛掰的吧。
本文
Android Studio
代码下载地址:
参考资料
1、
2、
3、
4、
5、
6、