

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 將 Device Farm 與 Gradle 建置系統整合
<a name="aws-device-farm-android-gradle-plugin"></a>

Device Farm Gradle 外掛程式提供 AWS Device Farm 與 Android Studio 中 Gradle 建置系統的整合。如需詳細資訊，請參閱 [Gradle](https://gradle.org)。

**注意**  
若要下載 Gradle 外掛程式，請前往 [GitHub](https://github.com/awslabs/aws-device-farm-gradle-plugin) 並依照 [建置 Device Farm Gradle 外掛程式](#aws-device-farm-gradle-plugin-building) 中的說明操作。

Device Farm Gradle 外掛程式可從 Android Studio 環境提供 Device Farm 功能。您可以在 Device Farm 託管的真實 Android 手機和平板電腦上開始測試。

本節包含一系列設定和使用 Device Farm Gradle 外掛程式的程序。

**Topics**
+ [相依性](#aws-device-farm-gradle-plugin-dependencies)
+ [步驟 1：建置 AWS Device Farm Gradle 外掛程式](#aws-device-farm-gradle-plugin-building)
+ [步驟 2：設定 AWS Device Farm Gradle 外掛程式](#aws-device-farm-gradle-plugin-setting-up)
+ [步驟 3：在 Device Farm Gradle 外掛程式中產生 IAM 使用者](#aws-device-farm-gradle-plugin-generating-iam-user)
+ [步驟 4：設定測試類型](#aws-device-farm-gradle-plugin-configuring-test-types)

## 相依性
<a name="aws-device-farm-gradle-plugin-dependencies"></a>

**執行時間**
+ Device Farm Gradle 外掛程式需要 AWS Mobile SDK 1.10.15 或更新版本。如需安裝軟體開發套件的詳細資訊，請參閱 [AWS Mobile SDK](https://aws.amazon.com/mobile/sdk/)。
+ Android tools builder test api 0.5.2
+ Apache Commons Lang3 3.3.4

**適用於單位測試**
+ Testng 6.8.8
+ Jmockit 1.19
+ Android gradle 工具 1.3.0

## 步驟 1：建置 AWS Device Farm Gradle 外掛程式
<a name="aws-device-farm-gradle-plugin-building"></a>

此外掛程式提供 AWS Device Farm 與 Android Studio 中 Gradle 建置系統的整合。如需詳細資訊，請參閱 [Gradle](https://gradle.org)。

**注意**  
建置外掛程式為選用。外掛程式會透過 Maven Central 發佈。如果您希望允許 Gradle 直接下載外掛程式，請略過此步驟並跳到 [步驟 2：設定 AWS Device Farm Gradle 外掛程式](#aws-device-farm-gradle-plugin-setting-up)。

**建置外掛程式**

1. 前往 [GitHub](https://github.com/awslabs/aws-device-farm-gradle-plugin) 並複製儲存庫。

1. 使用 `gradle install` 建置外掛程式

   外掛程式已安裝至您的本機 Maven 儲存庫。

後續步驟：[步驟 2：設定 AWS Device Farm Gradle 外掛程式](#aws-device-farm-gradle-plugin-setting-up)

## 步驟 2：設定 AWS Device Farm Gradle 外掛程式
<a name="aws-device-farm-gradle-plugin-setting-up"></a>

如果您尚未執行，請使用此處的程序複製儲存庫並安裝外掛程式：[建置 Device Farm Gradle 外掛程式](#aws-device-farm-gradle-plugin-building)。

**設定 AWS Device Farm Gradle 外掛程式**

1. 將外掛程式成品中新增到 `build.gradle` 中的相依性清單。

   ```
       buildscript {
   
           repositories {        
               mavenLocal()            
               mavenCentral()            
           }
   
           dependencies {        
               classpath 'com.android.tools.build:gradle:1.3.0'           
               classpath 'com.amazonaws:aws-devicefarm-gradle-plugin:1.0'            
           }        
       }
   ```

1. 在 `build.gradle` 檔案中設定外掛程式。以下針對測試的組態應做為您的指南：

   ```
   apply plugin: 'devicefarm'
   
   devicefarm {
   
       // Required. The project must already exist. You can create a project in the AWS Device Farm console.
       projectName "My Project" // required: Must already exist.
   
       // Optional. Defaults to "Top Devices"
       // devicePool "My Device Pool Name"
       
       // Optional. Default is 150 minutes
       // executionTimeoutMinutes 150
       
       // Optional. Set to "off" if you want to disable device video recording during a run. Default is "on"
       // videoRecording "on"
       
       // Optional. Set to "off" if you want to disable device performance monitoring during a run. Default is "on"
       // performanceMonitoring "on"
       
       // Optional. Add this if you have a subscription and want to use your unmetered slots
       // useUnmeteredDevices()
       
       // Required. You must specify either accessKey and secretKey OR roleArn. roleArn takes precedence. 
       authentication {
           accessKey "AKIAIOSFODNN7EXAMPLE"
           secretKey "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
           
           // OR
           
           roleArn "arn:aws:iam::111122223333:role/DeviceFarmRole"
       }
   
       // Optionally, you can 
       // - enable or disable Wi-Fi, Bluetooth, GPS, NFC radios
       // - set the GPS coordinates
       // - specify files and applications that must be on the device when your test runs
       devicestate {
           // Extra files to include on the device.
           // extraDataZipFile file("path/to/zip")
           
           // Other applications that must be installed in addition to yours. 
           // auxiliaryApps files(file("path/to/app"), file("path/to/app2"))
           
           // By default, Wi-Fi, Bluetooth, GPS, and NFC are turned on.
           // wifi "off"
           // bluetooth "off"
           // gps "off"
           // nfc "off"
           
           // You can specify GPS location. By default, this location is 47.6204, -122.3491
           // latitude 44.97005
           // longitude -93.28872
       }
    
       // By default, the Instrumentation test is used.
       // If you want to use a different test type, configure it here.
       // You can set only one test type (for example, Calabash, Fuzz, and so on)
    
       // Fuzz
       // fuzz { }
   
       // Calabash
       // calabash { tests file("path-to-features.zip") }
          
   }
   ```

1. 使用以下任務執行 Device Farm 測試：`gradle devicefarmUpload`。

   建置輸出會印出 Device Farm 主控台的連結，您可以在其中監控測試執行。

後續步驟：[在 Device Farm Gradle 外掛程式中產生 IAM 使用者](#aws-device-farm-gradle-plugin-generating-iam-user)

## 步驟 3：在 Device Farm Gradle 外掛程式中產生 IAM 使用者
<a name="aws-device-farm-gradle-plugin-generating-iam-user"></a>

AWS Identity and Access Management (IAM) 可協助您管理使用 AWS 資源的許可和政策。本主題會逐步引導您產生具有存取 AWS Device Farm 資源許可的 IAM 使用者。

如果您尚未這麼做，請先完成步驟 1 和 2，再產生 IAM 使用者。

我們建議您不要使用 AWS 根帳戶來存取 Device Farm。請改為在帳戶中 AWS 建立新的 IAM 使用者 （或使用現有的 IAM 使用者），然後使用該 IAM 使用者存取 Device Farm。

**注意**  
您用來完成下列步驟的 AWS 根帳戶或 IAM 使用者必須具有建立下列 IAM 政策並將其連接至 IAM 使用者的許可。如需詳細資訊，請參閱[使用政策](https://docs.aws.amazon.com/IAM/latest/UserGuide/policies_manage.html)。

**在 IAM 中建立具有適當存取政策的新使用者**

1. 前往 [https://console.aws.amazon.com/iam/](https://console.aws.amazon.com/iam/) 開啟 IAM 主控台。

1. 選擇 **Users** (使用者)。

1. 選擇 **Create New Users (建立新的使用者)**。

1. 輸入您選擇的使用者名稱。

   例如 **GradleUser**。

1. 選擇**建立**。

1. 選擇 **Download Credentials (下載登入資料)** 並將其儲存在您稍後可以輕鬆取得的位置。

1. 選擇 **Close** (關閉)。

1. 在清單中選擇使用者名稱。

1. 在 **Permissions (許可)** 下，按一下右側的向下箭頭展開 **Inline Policies (內嵌政策)** 標頭。

1. 選擇**按一下它所說**的這裡，**沒有要顯示的內嵌政策。若要建立一個，請按一下此處**。

1. 在 **Set Permissions (設定許可)** 畫面上，選擇 **Custom Policy (自訂政策)**。

1. 選擇**選取**。

1. 為您的政策命名，例如 **AWSDeviceFarmGradlePolicy**。

1. 將以下政策貼到 **Policy Document (政策文件)** 中。

------
#### [ JSON ]

****  

   ```
       {
           "Version":"2012-10-17",		 	 	 
           "Statement": [
               {
                   "Sid": "DeviceFarmAll",
                   "Effect": "Allow",
                   "Action": [ "devicefarm:*" ],
                   "Resource": [ "*" ]
               }
           ]
       }
   ```

------

1. 選擇 **Apply Policy** (套用政策) 

後續步驟：[設定測試類型](#aws-device-farm-gradle-plugin-configuring-test-types)。

如需詳細資訊，請參閱[建立 IAM 使用者 (AWS 管理主控台)](https://docs.aws.amazon.com/IAM/latest/UserGuide/Using_SettingUpUser.html#Using_CreateUser_console) 或 [設定](setting-up.md)。

## 步驟 4：設定測試類型
<a name="aws-device-farm-gradle-plugin-configuring-test-types"></a>

根據預設，AWS Device Farm Gradle 外掛程式會執行[適用於 Android 和 AWS Device Farm 的檢測](test-types-android-instrumentation.md)測試。如果您想要執行自己的測試或指定其他參數，您可以選擇設定測試類型。本主題提供有關每個可用的測試類型的資訊，以及您必須在 Android Studio 中所進行的設定。如需 Device Farm 中可用測試類型的詳細資訊，請參閱 [AWS Device Farm 中的測試架構和內建測試](test-types.md)。

如果您尚未這麼做，請先完成步驟 1 – 3，再設定測試類型。

**注意**  
如果您使用的是[裝置插槽](how-to-purchase-device-slots.md)，則裝置插槽功能預設為停用。

### Appium
<a name="configuring-test-types-appium"></a>

Device Farm 支援適用於 Android 的 Appium Java JUnit 和 TestNG。
+ [Appium （在 Java (JUnit) 下）](https://docs.aws.amazon.com//devicefarm/latest/developerguide/test-types-appium.html)
+ [Appium （在 Java (TestNG) 下）](https://docs.aws.amazon.com//devicefarm/latest/developerguide/test-types-appium.html)

您可以選擇 `useTestNG()` 或 `useJUnit()`。預設為 `JUnit` 且不需要明確指定。

```
    appium {
        tests file("path to zip file") // required
        useTestNG() // or useJUnit()
    }
```

### 內建：模糊
<a name="configuring-test-types-built-in-fuzz"></a>

Device Farm 提供內建模糊測試類型，它會將使用者介面事件隨機傳送到裝置，然後報告結果。

```
    fuzz {

       eventThrottle 50 // optional default
       eventCount 6000  // optional default
       randomizerSeed 1234 // optional default blank

     }
```

如需詳細資訊，請參閱[執行 Device Farm 的內建模糊測試 (Android 和 iOS)](test-types-built-in-fuzz.md)。

### 檢測
<a name="configuring-test-types-instrumentation"></a>

Device Farm 支援 Android 的檢測 (JUnit、Espresso、Robotium 或任何檢測型測試）。如需詳細資訊，請參閱[適用於 Android 和 AWS Device Farm 的檢測](test-types-android-instrumentation.md)。

在 Gradle 中執行檢測測試時，Device Farm 會使用從您的 **androidTest** 目錄產生的`.apk`檔案做為測試來源。

```
    instrumentation { 

        filter "test filter per developer docs" // optional

    }
```