本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 Appium 和 AWS Device Farm
本節說明如何設定、封裝 Appium 測試並上傳至 Device Farm。Appium 是用於自動化本地和移動 Web 應用程序的開源工具。有關更多信息,請參閱 Appium 網站上的 Appium 簡介。
有關示例應用程序和工作測試的鏈接,請參閱適用於 Android 的 Device Farm 示例應用程序和適用於 iOS 的 Device Farm 示例應用程序 GitHub。
版本支援
對各種架構和程式設計語言的支援取決於所使用的語言。
Device Farm 支援所有 Appium 1.x 和 2.x 伺服器版本。對於 Android,您可以選擇任何主要的 Appium 版本。devicefarm-cli
例如,要使用 Appium 服務器版本 2,請將以下命令添加到測試規格 YAML 文件中:
phases:
install:
commands:
# To install a newer version of Appium such as version 2:
- export APPIUM_VERSION=2
- devicefarm-cli use appium $APPIUM_VERSION
對於 iOS,您可以使用avm
或npm
命令選擇特定的 Appium 版本。例如,要使用avm
命令將 Appium 服務器版本設置為 2.1.2,請將以下命令添加到測試規格 YAML 文件中:
phases:
install:
commands:
# To install a newer version of Appium such as version 2.1.2:
- export APPIUM_VERSION=2.1.2
- avm $APPIUM_VERSION
使用命npm
令使用 Appium 2 的最新版本,將這些命令添加到測試規格 YAML 文件中:
phases:
install:
commands:
- export APPIUM_VERSION=2
- npm install -g appium@$APPIUM_VERSION
如需有關devicefarm-cli
或任何其他 CLI 命令的詳細資訊,請參閱 AWS CLI 參考資料。
若要使用架構的所有功能 (例如註解),請選擇自訂測試環境,然後使用 AWS CLI 或Device Farm
主控台上傳自訂測試規格。
配置您的 Appium 測試包
使用以下指示來設定您的測試套件。
- Java (JUnit)
-
-
修改pom.xml
以將打包設置為 JAR 文件:
<groupId>com.acme</groupId>
<artifactId>acme-myApp-appium</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
-
修改pom.xml
以使用maven-jar-plugin
將測試構建到 JAR 文件中。
以下插件將您的測試源代碼(src/test
目錄中的任何內容)構建到 JAR 文件中:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
-
修改pom.xml
以用maven-dependency-plugin
來將依賴項構建為 JAR 文件。
以下插件將您的依賴關係複製到dependency-jars
目錄中:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/dependency-jars/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
-
將下列 XML 組件儲存至src/main/assembly/zip.xml
。
下列 XML 是組件定義,設定後會指示 Maven 建置 .zip 檔案,其中包含組建輸出目錄和目錄根目錄中的所有項目dependency-jars
:
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>zip</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>./</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>./</outputDirectory>
<includes>
<include>/dependency-jars/</include>
</includes>
</fileSet>
</fileSets>
</assembly>
-
修改pom.xml
以用maven-assembly-plugin
於將測試和所有依賴關係打包到單個 .zip 文件中。
下列外掛程式會使用上述組件,在每次執行時mvn package建立zip-with-dependencies
在 build 輸出目錄中名為的 .zip 檔案:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<finalName>zip-with-dependencies</finalName>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/assembly/zip.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
如果您收到錯誤,指明 1.3 中不支援註釋,請將以下內容新增至 pom.xml
:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
- Java (TestNG)
-
-
修改pom.xml
以將打包設置為 JAR 文件:
<groupId>com.acme</groupId>
<artifactId>acme-myApp-appium</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
-
修改pom.xml
以使用maven-jar-plugin
將測試構建到 JAR 文件中。
以下插件將您的測試源代碼(src/test
目錄中的任何內容)構建到 JAR 文件中:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
-
修改pom.xml
以用maven-dependency-plugin
來將依賴項構建為 JAR 文件。
以下插件將您的依賴關係複製到dependency-jars
目錄中:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/dependency-jars/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
-
將下列 XML 組件儲存至src/main/assembly/zip.xml
。
下列 XML 是組件定義,設定後會指示 Maven 建置 .zip 檔案,其中包含組建輸出目錄和目錄根目錄中的所有項目dependency-jars
:
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>zip</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>./</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>./</outputDirectory>
<includes>
<include>/dependency-jars/</include>
</includes>
</fileSet>
</fileSets>
</assembly>
-
修改pom.xml
以用maven-assembly-plugin
於將測試和所有依賴關係打包到單個 .zip 文件中。
下列外掛程式會使用上述組件,在每次執行時mvn package建立zip-with-dependencies
在 build 輸出目錄中名為的 .zip 檔案:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<finalName>zip-with-dependencies</finalName>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/assembly/zip.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
如果您收到錯誤,指明 1.3 中不支援註釋,請將以下內容新增至 pom.xml
:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
- Node.JS
-
要打包 Appium Node.js 測試並將其上傳到 Device Farm,您必須在本地計算機上安裝以下內容:
-
確認 nvm 已存在
command -v nvm
您應該會在輸出中看到 nvm
。
如需詳細資訊,請參閱上 GitHub的 nvm。
-
執行此命令以安裝 Node.js:
nvm install node
您可以指定特定版本的 Node.js:
nvm install 11.4.0
-
確認使用的是正確版本的節點:
node -v
-
全域安裝 npm-bundle:
npm install -g npm-bundle
- Python
-
-
強烈建議您設定 Python virtualenv 來進行開發和封裝測試,這樣您的應用程式套件就不會包含不必要的相依性。
$
virtualenv workspace
$
cd workspace
$
source bin/activate
-
將 py.test 安裝到虛擬環境中。
$
pip install pytest
-
在您的虛擬環境中安裝 Appium Python 用戶端。
$
pip install Appium-Python-Client
-
除非您在自訂模式中指定不同的路徑,否則 Device Farm 會預期您的測試儲存在中tests/
。您可以使用 find
來顯示資料夾內的所有檔案:
$
find tests/
確認這些文件包含您要在 Device Farm 上運行的測試套件
tests/
tests/my-first-tests.py
tests/my-second-tests/py
-
從虛擬環境工作區資料夾中執行此命令,以顯示測試的清單而不予以執行。
$
py.test --collect-only tests/
確認輸出顯示您要在 Device Farm 上運行的測試。
-
清理您的測試資料夾下的所有快取檔案:
$
find . -name '__pycache__' -type d -exec rm -r {} +
$
find . -name '*.pyc' -exec rm -f {} +
$
find . -name '*.pyo' -exec rm -f {} +
$
find . -name '*~' -exec rm -f {} +
-
在工作區中執行下列命令,以產生 requirements.txt 檔案:
$
pip freeze > requirements.txt
- Ruby
-
要打包 Appium Ruby 測試並將其上傳到 Device Farm,您必須在本地計算機上安裝以下內容:
-
安裝所需的金鑰、RVM 和 Ruby。如需詳細資訊,請參閱 RVM 網站上的安裝 RVM。
在安裝完成時,藉由登出再重新登入,來重新載入終端機。
RVM 只會做為 bash shell 的函數載入。
-
確認已正確安裝 rvm
command -v rvm
您應該會在輸出中看到 rvm
。
-
如果您想要安裝特定版本的 Ruby,例如 2.5.3
,請執行下列命令:
rvm install ruby 2.5.3 --autolibs=0
確認使用的是要求的 Ruby 版本:
ruby -v
-
配置捆綁器以編譯所需測試平台的軟件包:
bundle config specific_platform true
-
更新您的 .lock 文件以添加運行測試所需的平台。
-
如果您正在編譯測試以在 Android 設備上運行,請運行此命令以配置 Gemfile 以使用 Android 測試主機的依賴關係:
bundle lock --add-platform x86_64-linux
-
如果您正在編譯測試以在 iOS 設備上運行,請運行此命令以配置 Gemfile 以使用 iOS 測試主機的依賴關係:
bundle lock --add-platform x86_64-darwin
-
通常預設為安裝 bundler gem。如果不是,請安裝它:
gem install bundler -v 2.3.26
建立壓縮的測試套件檔案
在 Device Farm 中,壓縮測試包中文件的文件夾結構很重要,並且某些存檔工具將隱式更改 ZIP 文件的結構。我們建議您遵循以下指定的命令列公用程式,而不要使用本機桌面 (例如 Finder 或 Windows 檔案總管) 檔案管理員內建的封存公用程式。
現在,針對 Device Farm 將您的測試綁定在一起。
- Java (JUnit)
-
建置並封裝您的測試:
$ mvn clean package -DskipTests=true
結果會建立檔案 zip-with-dependencies.zip
。這是您的測試套件。
- Java (TestNG)
-
建置並封裝您的測試:
$ mvn clean package -DskipTests=true
結果會建立檔案 zip-with-dependencies.zip
。這是您的測試套件。
- Node.JS
-
-
檢查您的專案。
確定您位於專案的根目錄。您可以在根目錄看到 package.json
。
-
執行此命令來安裝本機相依性。
npm install
此命令也會在目前的目錄中建立 node_modules
資料夾。
-
執行此命令將目前資料夾中的檔案封裝成 *.tgz 檔案。檔案會使用 package.json
檔案中的 name
屬性來命名。
npm-bundle
這個 tarball (.tgz) 檔案包含所有程式碼和相依性。
-
執行此命令將前一步驟所產生的 tarball (*.tgz 檔案) 套裝成單一壓縮的存檔:
zip -r MyTests.zip
*.tgz
這是您在下列程序中上傳至 Device Farm 列的MyTests.zip
檔案。
- Python
-
- Python 2
-
使用 pip 產生所需 Python 套件的存檔 (稱為「wheelhouse」):
$
pip wheel --wheel-dir wheelhouse -r requirements.txt
針對 Device Farm 將您的 wheelhouse、測試和 pip 要求封裝到 zip 存檔中:
$
zip -r test_bundle.zip
tests/ wheelhouse/ requirements.txt
- Python 3
-
將您的測試和 pip 要求封裝到一個 zip 檔案中:
$
zip -r test_bundle.zip
tests/ requirements.txt
- Ruby
-
-
執行此命令來建立虛擬 Ruby 環境:
# myGemset is the name of your virtual Ruby environment
rvm gemset create myGemset
-
執行此命令來使用您剛建立的環境:
rvm gemset use myGemset
-
檢查您的原始程式碼。
確定您位於專案的根目錄。您可以在根目錄看到 Gemfile
。
-
執行此命令從 Gemfile
安裝本機相依性和所有 gem 套件:
bundle install
此時,您應該能夠在本機執行測試。使用此命令從本機執行測試:
bundle exec $test_command
-
將您的 gem 套件封裝在 vendor/cache
資料夾中。
# This will copy all the .gem files needed to run your tests into the vendor/cache directory
bundle package --all-platforms
-
執行以下命令將您的原始程式碼及所有相依性套裝到單一壓縮的存檔:
zip -r MyTests.zip Gemfile vendor/ $(any other source code directory files)
這是您在下列程序中上傳至 Device Farm 列的MyTests.zip
檔案。
將測試套件上傳至裝 Device Farm
您可以使用 Device Farm 主控台來上傳測試。
登入 Device Farm 主控台,網址為 https://console.aws.amazon.com/devicefarm。
-
在 [Device Farm] 導覽面板上,選擇 [行動裝置測試],然後選擇 [專案
-
如果您是新使用者,請選擇「新增專案」,輸入專案名稱,然後選擇「提交」。
如果您已經有一個項目,則可以選擇它將測試上傳到該項目。
-
開啟您的專案,然後選擇 Create a new run (建立新執行)。
-
- 若是原生 Android 和 iOS 測試
-
在 [選擇應用程式] 頁面上,選擇 [行動應用程式],然後選取 [選擇檔案] 以上傳應用程式的可散發套件
檔案必須是 Android .apk
或 iOS .ipa
。iOS 應用程式必須是專為真實裝置建置,而不是專為模擬器建置。
- 若是行動 Web 應用程式測試
在選擇應用程式頁面上,選擇 [Web 應用程式]。
-
為測試指定適當的名稱。這可包含空格或標點符號的任何組合。
-
選擇下一步。
-
在「設定」頁面的「安裝程式測試架構」區段中,選擇「Appium 語言
」,然後選擇「檔案」。
-
瀏覽並選擇包含測試的 .zip 檔案。.zip 檔案必須遵循配置您的 Appium 測試包中所述的格式。
-
選擇在自訂環境中執行測試。此執行環境允許完全控制測試設置,拆卸和調用,以及選擇特定版本的運行時和 Appium 服務器。您可以通過測試規格文件配置您的自定義環境。如需詳細資訊,請參閱在 AWS Device Farm 中使用自訂測試環境。
-
選擇 [下一步],然後依照指示選取裝置並開始執行。如需詳細資訊,請參閱 在 Device Farm 中建立測試回合。
Device Farm 不會修改 Appium 測試。
拍攝測試的屏幕截圖(可選)
您可以在測試時取得螢幕擷取畫面。
Device Farm 會將 DEVICEFARM_SCREENSHOT_PATH
屬性設為本機檔案系統上的完整路徑,這是 Device Farm 預期的 Appium 螢幕擷取畫面儲存位置。用於存放螢幕擷取畫面的測試專用目錄是在執行時間定義。系統會自動將螢幕擷取畫面提取到您的 Device Farm 報告。若要檢視螢幕擷取畫面,在 Device Farm 主控台中選擇 Screenshots (螢幕擷取畫面) 區段。
如需在 Appium 測試中擷取螢幕擷取畫面的詳細資訊,請參閱 Appium API 文件中的 擷取螢幕擷取畫面。