介绍
Appium是适用于本机,混合和移动Web和桌面应用程序的开源,跨平台测试自动化工具。我们支持模拟器(iOS),模拟器(Android)和真实设备(iOS,Android,Windows,Mac)。
Appium 官网 :http://appium.io/
https://github.com/appium/appium
Appiu详细介绍: https://github.com/appium/appium
Appium小程序DEMO: https://github.com/appium/appium/tree/master/sample-code
常用Downloads: http://appium.io/downloads.html
Appium + Android APP
Start
安装设置
- 安装Java SDK(8以上)
地址: https://www.oracle.com/java/technologies/javase-downloads.html
设置 JAVA_HOME 和PATH
测试 java –version 输出java版本信息
- 安装android SDK,和模拟机或者真机版本匹配
地址: https://dl.google.com/android/installer_r24.4.1-windows.exe
设置 ANDROID_HOME 和PATH
测试 adb version 输出adb版本信息
- 安装android 模拟机(我测试的是 mumu 模拟器)
- 安装工具 appium
Develop
- 启动 mumu 模拟器, 安装微信
- 使用 adb 桥接虚拟机
常用命令 : https://adbshell.com/
adb connect 127.0.0.1:7555 adb 连接虚拟机
adb devices 查看 adb 连接的设备
- 启动 appium
- 默认选项, 点击 Start Server
浏览器 打开 http://127.0.0.1:4723/ 确认可以正常访问
- 新建连接
- 设置连接微信的参数
Appium 参数设置参考:
https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/caps.md
参数值: 来源于网络 、 反编译软件、 系统默认值
设置sample
微信
{
"platformName": "Android",
"deviceName": "127.0.0.1:7555",
"platformVersion": "6.0.1",
"appPackage": "com.tencent.mm",
"appActivity": ".ui.LauncherUI",
"sessionOverride": true,
"noReset": true,
"fullReset": false,
"unicodeKeyboard": true
}
微博
{
"platformName": "Android",
"deviceName": "127.0.0.1:7555",
"platformVersion": "6.0.1",
"appPackage": "com.sina.weibo",
"appActivity": ".SplashActivity",
"sessionOverride": true,
"noReset": true,
"fullReset": false,
"unicodeKeyboard": true
}
- 测试连接
如有错误,观察日志调整参数设置
正常可以看到微信的界面
- Java 开发
Pom 依赖
<dependency> <groupId>io.appium</groupId> <artifactId>java-client</artifactId> <version>7.3.0</version> </dependency> |
Java 测试
import java.net.MalformedURLException; import java.net.URL; import java.time.Duration; import java.util.List; import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By; import org.openqa.selenium.Dimension; import org.openqa.selenium.WebElement; import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.TouchAction; import io.appium.java_client.android.AndroidDriver; import io.appium.java_client.touch.WaitOptions; import io.appium.java_client.touch.offset.PointOption;
public class AndroidWechat {
public AndroidDriver<WebElement> getDriver() throws MalformedURLException { System.out.println("---getDriver start"); DesiredCapabilities desiredCapabilities = new DesiredCapabilities(); desiredCapabilities.setCapability("platformName", "Android"); desiredCapabilities.setCapability("deviceName", "127.0.0.1:7555"); desiredCapabilities.setCapability("platformVersion", "6.0.1"); desiredCapabilities.setCapability("appPackage", "com.tencent.mm"); desiredCapabilities.setCapability("appActivity", ".ui.LauncherUI"); desiredCapabilities.setCapability("sessionOverride", true); desiredCapabilities.setCapability("noReset", true); desiredCapabilities.setCapability("fullReset", false); desiredCapabilities.setCapability("unicodeKeyboard", true); desiredCapabilities.setCapability("newCommandTimeout", "30000"); desiredCapabilities.setCapability("showChromedriverLog", true); desiredCapabilities.setCapability("recreateChromeDriverSessions", "True");
URL remoteUrl = new URL("http://localhost:4723/wd/hub"); AndroidDriver<WebElement> driver = new AndroidDriver<WebElement>(remoteUrl, desiredCapabilities);
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS); System.out.println("---getDriver end"); return driver; }
public void openMessage() { AndroidDriver<WebElement> driver = null; try { driver = getDriver();
execClick(driver, "com.tencent.mm:id/tb", 3, "我");
execClick(driver, "com.tencent.mm:id/ak2", 0, "支付");
execSwipeDown(driver, -2800); execSwipeDown(driver, 2800); execSwipeDown(driver, 0); execSwipeDown(driver, 500); execSwipeDown(driver, 20);
} catch (Exception e) { e.printStackTrace(); } finally { } System.out.println("---openMessage end"); }
private void execClick(AndroidDriver<WebElement> driver, String id, int index, String comment) throws InterruptedException { List<WebElement> elementList; int elementLength; WebElement element; elementList = driver.findElements(By.id(id)); elementLength = elementList == null ? 0 : elementList.size(); System.out.println(id + " find=" + elementLength); element = elementList.get(index); element.click(); System.out.println("--- click:" + comment); Thread.sleep(2 * 1000); }
private boolean execSwipe(AndroidDriver<WebElement> driver, int startX, int startY, int endX, int endY) throws InterruptedException { Dimension size = driver.manage().window().getSize(); int width = size.getWidth() - 10; int height = size.getHeight() - 10;
if (startX < 0) { startX = 0; } if (startX > width) { startX = width; }
if (startY < 0) { startY = 0; } if (startY > height) { startY = height; }
int relX = startX - endX % width + startX; if (relX < 0) { endX = 0; } else { endX = relX % width; }
int relY = startY - endY % height + startY; if (relY < 0) { endY = 0; } else { endY = relY % height; }
System.out.println("---swipe: width=" + width + ", height=" + height + ", startX=" + startX + ", startY=" + startY + ", endX=" + endX + ", endY=" + endY);
TouchAction action = new TouchAction(driver); action.press(PointOption.point(startX, startY)).waitAction(WaitOptions.waitOptions(Duration.ofMillis(500))); action.moveTo(PointOption.point(endX, endY)); action.release().perform(); Thread.sleep(2 * 1000);
return false; }
private boolean execSwipeDown(AndroidDriver<WebElement> driver, int y) throws InterruptedException { Dimension size = driver.manage().window().getSize(); int width = size.getWidth(); int height = size.getHeight(); return this.execSwipe(driver, width / 2, height, width / 2, y); }
private boolean execSwipeUp(AndroidDriver<WebElement> driver, int y) throws InterruptedException { Dimension size = driver.manage().window().getSize(); int width = size.getWidth(); int height = size.getHeight(); return this.execSwipe(driver, width / 2, 0, width / 2, -y); }
private boolean execSwipeLeft(AndroidDriver<WebElement> driver, int x) throws InterruptedException { Dimension size = driver.manage().window().getSize(); int width = size.getWidth(); int height = size.getHeight(); return this.execSwipe(driver, width, height / 2, x, height / 2); }
private boolean execSwipeRight(AndroidDriver<WebElement> driver, int x) throws InterruptedException { Dimension size = driver.manage().window().getSize(); int width = size.getWidth(); int height = size.getHeight(); return this.execSwipe(driver, 0, height / 2, -x, height / 2); }
public static void main(String[] args) { AndroidWechat model = new AndroidWechat();
model.openMessage(); }
} |
- 结合 appium的 inspect 功能, 定位元素,获取元素,操作元素
- Tips
- 测试建议采用虚拟机
- 多观察日志
- Android SDK 和 模拟器的版本一致
Appium + Windows APP
Start
- 安装设置
- 安装Java SDK(8以上)
地址: https://www.oracle.com/java/technologies/javase-downloads.html
设置 JAVA_HOME 和PATH
测试 java –version 输出java版本信息
- 安装 WinAppDriver
https://github.com/Microsoft/WinAppDriver/releases
- 安装工具 appium
Develop
- 启动 appium
- 默认选项, 点击 Start Server
浏览器 打开 http://127.0.0.1:4723/ 确认可以正常访问
- 新建连接
- 设置连接Notepad的参数
Appium 参数设置参考:
https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/caps.md
参数值: 来源于网络 、 反编译软件、 系统默认值
设置sample
Notepad
{
"platformName": "windows",
"deviceName": "WindowsPC",
"app": "C:/Windows/System32/notepad.exe"
}
电脑版微信
{
"platformName": "windows",
"deviceName": "WindowsPC",
"app": "D:/Program Files (x86)/Tencent/WeChat/WeChat.exe",
"sessionOverride": true,
"noReset": true,
"fullReset": false,
"unicodeKeyboard": true
}
- 测试连接
如有错误,观察日志调整参数设置
正常可以看到Notepad的界面
- Java 开发
Pom 依赖
<dependency> <groupId>io.appium</groupId> <artifactId>java-client</artifactId> <version>7.3.0</version> </dependency> |
Java 测试
import java.net.MalformedURLException; import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebElement;
import io.appium.java_client.windows.WindowsDriver;
public class WindowNotepad {
public static void main(String[] args) {
WindowNotepad model = new WindowNotepad();
try { model.exce(); } catch (Exception e) { e.printStackTrace(); }
}
public void exce() throws MalformedURLException { DesiredCapabilities appCapabilities = new DesiredCapabilities(); appCapabilities.setCapability("app", "C:/Windows/System32/notepad.exe"); appCapabilities.setCapability("deviceName", "WindowsPC"); appCapabilities.setCapability("platformName", "windows"); WindowsDriver<RemoteWebElement> driver = new WindowsDriver<RemoteWebElement>(new URL("http://127.0.0.1:4723/wd/hub"), appCapabilities); // Control the AlarmClock app driver.findElementByClassName("Edit").sendKeys("This is some text"); }
} |
- 结合 appium的 inspect 功能, 定位元素,获取元素,操作元素
- Tips
- 测试建议采用虚拟机
- 多观察日志
- Win inspect
https://github.com/appium/appium-desktop#the-new-session-window
- 打开本地应用不要手动干预, 不能刷新、点击等操作,否则 Inspect 界面渲染失败
源码和详细文档: https://gitee.com/xinyuan.com/sample-appium