配置Android sdk(software development kit)环境
官网地址:https://www.androiddevtools.cn/
选择SDK Tools,下载对应的系统版本
step1:将下载的包解压到本地
step2:配置Android SDK环境变量在系统变量中,新建ANDROID_HOME变量名,变量值:Android SDK软件包所在的路径在系统变量中,找到Path追加:
- %ANDROID_HOME%\platform-tools
- %ANDROID_HOME%\tools
- %ANDROID_HOME%\build-tools\30.0.2
- 检测,打开命令提示符(CMD),输入adb version去验证
安装模拟器&准备真机
- 安装夜神模拟器https://www.yeshen.com/
- 华为/oppo/vivo/小米
注一下:(肥肠重要,要不然测试无法进行)
- 测试App安装到了设备中
- 在启动会话之前,一定要确保你的设备是能够检测到(adb devices)
做法:把Android SDK包里面的adb.exe文件替换到夜神模拟器里面去,夜神模拟器bin目录下adb.exe以及nox_adb.exe文件都需要进行替换,名字需要同原来的保持一致,做了替换之后每一次夜神模拟器启动之后设备自动连上
安装Appium-desktop(Appium桌面版)
- 官网地址:http://appium.io/
- Github地址:https://github.com/appium/appium-desktop/releases(使用V1.17.0版本)
开启appium server,填写Appium server的本地IP和端口
出现下面画面即为开启appium server成功
检查器会话:识别App页面元素,录制/回放
检查器会话三个配置:
platformName值为Android or IOS
appPackage App包名值通过aapt工具来查询
appActivity App启动入口值通过aapt工具来查询(要进去aapt所在的目录输入cmd执行)
aapt dump badging D:\apk\apple_release_v2.1.2_finally.apk | findstr packag
aapt dump badging D:\apk\apple_release_v2.1.2_finally.apk | findstr launchable-activity
点击上图中的检查器,来到下面这个页面,通过下面页面填写app的包名和启动入口和平台名,即可来到定位元素页面,平时可以当作定位元素的工具用
- {
"platformName": "Android",
"appPackage": "tv.danmaku.bili",
"appActivity": "tv.danmaku.bili.ui.splash.SplashActivity"
}
怎样录制一条简单的用例(登录流程)
由步骤3结束后,代码就是这个样子
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import junit.framework.TestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities;public class SampleTest {
private AndroidDriver driver;
@Before
public void setUp() throws MalformedURLException {
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setCapability("platformName", "Android");
desiredCapabilities.setCapability("appPackage", "com.apple,apple2");
desiredCapabilities.setCapability("appActivity", "com.apple.apple2.activity.WelcomeActivity"); URL remoteUrl = new URL("http://127.0.0.1:4723/wd/hub");
driver = new AndroidDriver(remoteUrl, desiredCapabilities);
} @Test
public void sampleTest() {
MobileElement el1 = (MobileElement) driver.findElementByXPath("//android.widget.FrameLayout[@content-desc=\"购物车\"]/android.widget.ImageView");
el1.click();
MobileElement el2 = (MobileElement) driver.findElementById("com.apple.apple2:id/button_go_login");
el2.click();
MobileElement el3 = (MobileElement) driver.findElementById("com.apple.apple2:id/et_mobile");
el3.sendKeys("13245653456");
MobileElement el4 = (MobileElement) driver.findElementById("com.apple.apple2:id/et_password");
el4.sendKeys("34567");
MobileElement el5 = (MobileElement) driver.findElementById("com.apple.apple2:id/btn_login");
el5.click();
} @After
public void tearDown() {
driver.quit();
}
}
录制回放脚本
Step1:通过Appium检查器录制对应操作的脚本,获取到完整的代码
Step2:在intellij创建maven工程
Step3:把录制的代码放到maven工程
Step4:引入第三方的依赖,包括java-client以及TestNG
<dependencies>
<!-- https://mvnrepository.com/artifact/io.appium/java-client -->
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>7.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.0.0</version>
<scope>test</scope>
</dependency>
Step5:调试录制的脚本(页面切换的时候加等待)
录制结束之后,去掉junit,改用testng管理用例,appium部分需要java-client的依赖,点击运行
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class SampleTest {
private AndroidDriver driver;
@BeforeTest
public void setUp() throws MalformedURLException {
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setCapability("platformName", "Android");
desiredCapabilities.setCapability("appPackage", "com.apple.apple2");
desiredCapabilities.setCapability("appActivity", "com.apple.apple2.activity.WelcomeActivity");
URL remoteUrl = new URL("http://127.0.0.1:4723/wd/hub");
driver = new AndroidDriver(remoteUrl, desiredCapabilities);
}
@Test
public void sampleTest() throws InterruptedException {
//在定位元素之前要确保页面有加载结束
Thread.sleep(5000);
MobileElement el1 = (MobileElement) driver.findElementByAccessibilityId("购物车");
el1.click();
MobileElement el2 = (MobileElement) driver.findElementById("com.apple.apple2:id/button_go_login");
el2.click();
Thread.sleep(2000);
MobileElement el3 = (MobileElement) driver.findElementById("com.apple.apple2:id/et_mobile");
el3.sendKeys("13245434536");
MobileElement el4 = (MobileElement) driver.findElementById("com.apple.apple2:id/et_password");
el4.sendKeys("123456");
MobileElement el5 = (MobileElement) driver.findElementById("com.apple.apple2:id/btn_login");
el5.click();
Thread.sleep(8000);
}
@AfterTest
public void tearDown() {
driver.quit();
}
}
总结
- App自动化环境部署Android SDK环境配置模拟器安装安装Appium-desktop
- Appium-desktop工具的使用启动参数的配置,包括启动主机地址、端口Appium检查器,提供了查看App页面元素信息的功能、录制功能,前提是填写所需功能参数启动测试App
- 录制的脚本放到Java环境运行App自动化测试环境
- 使用录制功能,将录制代码放到Java工程里面去执行
在此声明:此文不针对任何一款APP,请自己选择一款合适的apk练习