- %ANDROID_HOME%platform-tools
- %ANDROID_HOME% ools
- %ANDROID_HOME%uild-tools30.0.2
- 检测,打开命令提示符(CMD),输入adb version去验证
- 安装夜神模拟器https://www.yeshen.com/
- 华为/oppo/vivo/小米
- 测试App安装到了设备中
- 在启动会话之前,一定要确保你的设备是能够检测到(adb devices)
- 官网地址:http://appium.io/
- Github地址:https://github.com/appium/appium-desktop/releases(使用V1.17.0版本)
开启appium server,填写Appium server的本地IP和端口
出现下面画面即为开启appium server成功
检查器会话三个配置:
platformName值为Android or IOS
appPackage App包名值通过aapt工具来查询
appActivity App启动入口值通过aapt工具来查询(要进去aapt所在的目录输入cmd执行)
aapt dump badging D:apkapple_release_v2.1.2_finally.apk | findstr packag
aapt dump badging D:apkapple_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();
}
}
录制回放脚本
<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>
录制结束之后,去掉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练习