当前Appium2.1服务器的要求:
系统要求:http://appium.io/docs/en/2.1/intro/requirements/
下载nodejs:https://nodejs.org/zh-cn/download/releases
本文下载:node-v18.17.1-win-x64.zip
解压在D:\Program Files\nodejs\node-v18.17.1-win-x64目录
在PATH环境变量中添加
D:\Program Files\nodejs\node-v18.17.1-win-x64
npm默认使用国外源,网络原因容易出现安装依赖失败,本文设置npm为国内源
npm config set registry https://registry.npmmirror.com
运行NPM命令安装Appium,参考官方安装http://appium.io/docs/en/2.1/quickstart/install/
npm i --location=global appium
图片
安装成功后,输入下边命令可以直接启动服务
appium
查看当前可用驱动appium driver list
C:\Users\admin>appium driver list
✔ Listing available drivers
- uiautomator2 [not installed]
- xcuitest [not installed]
- mac2 [not installed]
- espresso [not installed]
- safari [not installed]
- gecko [not installed]
- chromium [not installed]
appium driver install uiautomator2
官方参考:http://appium.io/docs/en/2.1/quickstart/uiauto2-driver/
下载Android SDK platform tools:Android SDK platform tools
解压目录:D:\ProgramData\android\sdk\platform-tools
在PATH环境变量中添加
D:\ProgramData\android\sdk\platform-tools
如果使用真机测试,只安装platform tools就可以了,如果使用模拟器,需要安装完整在sdk,可以下载Android Studio:https://developer.android.google.cn/studio,使用Android Studio里面的管理SDK工具管理相关SDK和工具。
下载OpenJDK:https://21doc.net/java/awesomejava#jvm-and-jdk
当前版本支持JDK11,本文使用bellsoft-jdk11.0.20+8-windows-amd64.zip,下载地址:https://bell-sw.com/pages/downloads/
下载后解压在:C:\Program Files\Java\bellsoft\jdk-11.0.20
在PATH环境变量中添加
C:\Progra~1\Java\bellsoft\jdk-11.0.20\bin
Appium Inspector官网:https://github.com/appium/appium-inspector
官方介绍Appium Inspector基本上只是一个带有用户界面的Appium客户端(如WebdriverIO、Appium的Java客户端、Appium的Python客户端等)。有一个接口用于指定使用哪个Appium服务器、设置哪些功能,然后在启动会话后与元素和其他Appium命令进行交互。
在1.22版本后appium-deskop不集成Appium Inspector,所以Appium Inspector需要单独打开。
Appium Inspector支持web和客户端版本。
web版本
web版本可以直接访问:https://inspector.appiumpro.com/使用。
客户端版本
下载地址:https://github.com/appium/appium-inspector/releases
参数配置可查看:https://appium.io/docs/en/2.1/guides/caps/
** 启动会话可能会出现下边的错误提示**
Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 500. Message: An unknown server-side error occurred while processing the command. Original error: Check https://forum.xda-developers.com/t/i-cant-enable-write_secure_settings-for-an-app-over-adb.3855596/ for throubleshooting. Error executing adbExec. Original error: 'Command 'D:\\ProgramData\\android\\sdk\\platform-tools\\adb.exe -P 5037 -s 972cac1 shell 'settings delete global hidden_api_policy_pre_p_apps;settings delete global hidden_api_policy_p_apps;settings delete global hidden_api_policy'' exited with code 255'; Command output: Security exception: Permission denial: writing to settings requires:android.permission.WRITE_SECURE_SETTINGS
解决办法
小米设备: 开启 USB调试(安全设置)
(1) 手机-插入 sim卡
(2) 设置 - 更多设置 - 开发者选项 - USB调试(安全设置)- 开启
OPPO设备: 开启 禁止权限监控
开发者选项 - 禁止权限监控 - 开启
使用appium的java-client库(https://github.com/appium/java-client)编写android app自动化测试代码,并使用extentreports生成测试报告
maven工程,pom.xml
4.0.0
com.penngo.app
monitor_app
1.0-SNAPSHOT
11
11
UTF-8
2.17.2
1.7.36
1.18.24
7.4.0
5.8.3
1.2.3
5.0.9
io.appium
java-client
8.5.1
org.seleniumhq.selenium
selenium-java
4.9.1
org.testng
testng
${testng.version}
com.aventstack
extentreports
${extentreports.version}
com.aventstack
extentreports-testng-adapter
${extentreports-testng-adapter.version}
org.apache.logging.log4j
log4j-api
${log4j2.version}
org.apache.logging.log4j
log4j-core
${log4j2.version}
org.slf4j
slf4j-simple
${slf4j_simple.version}
cn.hutool
hutool-all
${hutool.version}
alimaven
Maven Aliyun Mirror
https://maven.aliyun.com/repository/central
org.apache.maven.plugins
maven-compiler-plugin
3.8.1
${maven.compiler.source}
UTF-8
org.apache.maven.plugins
maven-surefire-plugin
2.22.2
true
测试用例java代码
import cn.hutool.core.io.FileUtil;
import com.aventstack.extentreports.testng.listener.ExtentIReporterSuiteClassListenerAdapter;
import io.appium.java_client.AppiumBy;
import io.appium.java_client.android.Activity;
import io.appium.java_client.android.AndroidDriver;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import java.io.File;
import java.io.FileOutputStream;
import java.time.Duration;
import java.util.HashSet;
import java.util.List;
/**
* 例子
* https://github.com/appium/java-client/blob/master/src/test/java/io/appium/java_client/android/AndroidTouchTest.java
*/
@Listeners(ExtentIReporterSuiteClassListenerAdapter.class)
@Slf4j
@Test(description="APP测试", priority = 1)
public class AppTest {
public static final String APP_ID = "com.penngo.xxx";
protected static final int PORT = 4723;
protected static AndroidDriver driver = null;
private static AppiumDriverLocalService service;
@BeforeClass
public void setUpAll(){
service = new AppiumServiceBuilder()
.withIPAddress("127.0.0.1")
.usingPort(PORT)
.build();
service.start();
UiAutomator2Options options = new UiAutomator2Options()
.setDeviceName("972cac1")
.setAppPackage(APP_ID)
.setAppActivity("com.penngo.xxx.activity.MainActivity")
;
driver = new AndroidDriver(service.getUrl(), options);
}
@AfterClass
public void tearDownAll(){
if(driver != null){
driver.quit();
}
}
/**
* app首页
*/
@Test(description="APP首页", priority = 1)
public void testHome() {
String activity = driver.currentActivity();
Assert.assertEquals(activity, ".activity.MainActivity");
}
/**
* app 我首页
*/
@Test(description="关于", priority = 2)
public void testMe() {
String activity = driver.currentActivity();
// 获取底部5个选项
WebElement navTabIndicator = driver.findElement(AppiumBy.id("com.penngo.xxx:id/NavTabIndicator"));
List<WebElement> list = navTabIndicator.findElements(AppiumBy.className("android.widget.TextView"));
System.out.println("list.size====="+ list.size());
HashSet tabs = new HashSet();
WebElement myElement = null;
for(WebElement el:list){
String text = el.getAttribute("text");
if(text.equals("关于")){
myElement = el;
}
}
// 切换到关于选项卡
myElement.click();
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(AppiumBy.id("com.penngo.xxx:id/setting_fragment")));
// 切换到设置
WebElement setting = driver.findElement(AppiumBy.id("com.penngo.xxx:id/setting_fragment"));
setting.click();
new WebDriverWait(driver, Duration.ofSeconds(10)).until(d->{
AndroidDriver driver2 = (AndroidDriver)d;
String currentActive = driver2.currentActivity();
if(!currentActive.equals(activity)){
log.info("设置====" + activity);
return currentActive;
}
else{
return null;
}
});
System.out.println("设置 Activity====" + driver.currentActivity());
log.info("设置 Activity====" + driver.currentActivity());
// 截图
File screenShotFile = driver.getScreenshotAs(OutputType.FILE);
System.out.println("screenShotFile====" + screenShotFile);
FileUtil.copy(screenShotFile.getAbsolutePath(), new File("logs/setting.png").getAbsolutePath(), true);
}
}
更多官方例子:https://github.com/appium/java-client/tree/master/src/test/java/io/appium/java_client/android