基於 Appium 的自動化測試

介紹

Appium是適用於本機,混合和移動Web和桌面應用程序的開源,跨平臺測試自動化工具。我們支持模擬器(iOS),模擬器(Android)和真實設備(iOSAndroidWindowsMac)。

 

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

安裝設置

  1. 安裝Java SDK(8以上)

地址: https://www.oracle.com/java/technologies/javase-downloads.html

設置 JAVA_HOME 和PATH

測試 java –version   輸出java版本信息

 

  1. 安裝android SDK,和模擬機或者真機版本匹配

地址: https://dl.google.com/android/installer_r24.4.1-windows.exe

設置 ANDROID_HOME 和PATH

測試 adb version   輸出adb版本信息

 

  1. 安裝android 模擬機(我測試的是 mumu 模擬器)

  http://mumu.163.com/

 

  1. 安裝工具 appium

http://appium.io/

 

Develop

  1. 啓動 mumu 模擬器, 安裝微信

  1. 使用 adb 橋接虛擬機

常用命令 : https://adbshell.com/

 

adb connect 127.0.0.1:7555    adb 連接虛擬機

adb devices                 查看 adb 連接的設備

 

  1. 啓動 appium

 

  1. 默認選項, 點擊 Start Server

瀏覽器 打開 http://127.0.0.1:4723/  確認可以正常訪問

 

  1. 新建連接

 

  1. 設置連接微信的參數

 

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
}

 

  1. 測試連接

 

 

 

如有錯誤,觀察日誌調整參數設置

正常可以看到微信的界面

 

  1. 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();

         }

 

}

 

 

  1. 結合 appium的 inspect 功能, 定位元素,獲取元素,操作元素
  2. Tips
  1. 測試建議採用虛擬機
  2. 多觀察日誌
  3. Android SDK 和 模擬器的版本一致

 

 

 

 

 

 

 

Appium + Windows APP

Start

  1. 安裝設置
  1. 安裝Java SDK(8以上)

地址: https://www.oracle.com/java/technologies/javase-downloads.html

設置 JAVA_HOME 和PATH

測試 java –version   輸出java版本信息

 

  1. 安裝 WinAppDriver

https://github.com/Microsoft/WinAppDriver/releases

 

  1. 安裝工具 appium

http://appium.io/

 

Develop

 

  1. 啓動 appium

 

  1. 默認選項, 點擊 Start Server

瀏覽器 打開 http://127.0.0.1:4723/  確認可以正常訪問

 

  1. 新建連接

 

  1. 設置連接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
}

 

  1. 測試連接

 

 

 

如有錯誤,觀察日誌調整參數設置

正常可以看到Notepad的界面

 

  1. 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");

         }

 

}

 

 

  1. 結合 appium的 inspect 功能, 定位元素,獲取元素,操作元素
  2. Tips
  1. 測試建議採用虛擬機
  2. 多觀察日誌
  3. Win inspect

https://github.com/appium/appium-desktop#the-new-session-window

  1. 打開本地應用不要手動干預, 不能刷新、點擊等操作,否則 Inspect 界面渲染失敗

 

源碼和詳細文檔:  https://gitee.com/xinyuan.com/sample-appium

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章