跳至內容

Appium 和 Selenium Grid

使用 Selenium Grid 4+

Grid 4 中的 中繼 功能可讓您將 Appium 要求代理至 Appium 伺服器執行個體。以下是如何將兩個不同的 Appium 執行個體連線至 Selenium Grid 的範例說明。

定義 Appium 設定

每個 Appium 執行個體都應該有一個容易更新的設定檔。它應該包含任何需要對該特定伺服器獨有的資訊(例如,其驅動程式應該使用而其他驅動程式不應該使用的埠)。我們將會有 2 個 Appium 伺服器,因此我們需要 2 個設定檔

# appium1.yml
server:
  port: 4723
  use-drivers:
    - xcuitest
  default-capabilities:
    wdaLocalPort: 8100
    mjpegServerPort: 9100
    mjpegScreenshotUrl: "http://localhost:9100"

在上述 YAML 設定檔中,我們指定 Appium 伺服器埠、使用的驅動程式,以及將作為預設功能傳入的驅動程式參數。我們的目標是確保在此主機上執行的任何其他驅動程式不會與系統埠或其他資源競爭。第二個設定檔可能如下所示,我們只需調整幾個埠以防止衝突

# appium2.yml
server:
  port: 4733
  use-drivers:
    - xcuitest
  default-capabilities:
    wdaLocalPort: 8110
    mjpegServerPort: 9110
    mjpegScreenshotUrl: "http://localhost:9110"

定義 Grid 節點設定

我們將為每個 Appium 伺服器啟動一個 Grid「節點」,以管理中繼命令並確定容量和線上狀態等。因此,我們也應該為每個 Grid 節點設定一個設定檔。每個節點設定都應該包含其將鎖定的 Appium 伺服器的位址,以及它應該接受以中繼會話請求至 Appium 的功能「設定」清單。以下是兩個節點的設定可能樣貌

# node1.toml
[server]
port = 5555

[node]
detect-drivers = false

[relay]
url = "http://localhost:4723"
status-endpoint = "/status"
configs = [
    "1", "{\"platformName\": \"iOS\", \"appium:platformVersion\": \"15.5\", \"appium:deviceName\": \"iPhone 13\", \"appium:automationName\": \"XCUITest\"}"
]
# node2.toml
[server]
port = 5565

[node]
detect-drivers = false

[relay]
url = "http://localhost:4733"
status-endpoint = "/status"
configs = [
    "1", "{\"platformName\": \"iOS\", \"appium:platformVersion\": \"15.5\", \"appium:deviceName\": \"iPhone 12\", \"appium:automationName\": \"XCUITest\"}"
]

請注意,每個節點設定也為節點本身指定一個不同的埠以執行。

整合

Grid 節點還不夠,您還需要一個 Grid「樞紐」,作為所有節點的負載平衡器和管理員。因此,最後我們將同時執行 5 個程序:2 個 Appium 伺服器、2 個 Grid 節點和 1 個 Grid 樞紐。最好在不同的終端機視窗中執行這些程序,以避免記錄混淆。以下是您啟動每個程序的方式

  1. appium --config appium1.yml
  2. appium --config appium2.yml
  3. java -jar /path/to/selenium.jar node --config node1.toml
  4. java -jar /path/to/selenium.jar node --config node2.toml
  5. java -jar /path/to/selenium.jar hub

一旦您等待幾分鐘讓節點偵測其 Appium 伺服器,並向樞紐註冊,您將能夠透過單一樞紐端點(預設為 http://localhost:4444)傳送所有 Appium 流量。

當然,您能夠連結在網路中不同機器上執行的 Appium 伺服器和節點,以形成更大的網格。

使用 Selenium Grid 3

您可以使用 --nodeconfig 伺服器參數,將您的 Appium 伺服器註冊到本機 Selenium Grid 3 (設定文件) 執行個體。

appium server --nodeconfig /path/to/nodeconfig.json --base-path=/wd/hub

在引用的組態檔中,您必須定義 browserNameversionplatform 功能,而 Grid 會根據這些參數將您的測試重新導向到正確的裝置。您還需要設定您的主機詳細資料和 Selenium Grid 詳細資料。如需所有參數和說明的完整清單,請參閱此處

一旦您啟動 Appium 伺服器,它就會註冊到 Grid,您會在 Grid 主控台頁面上看到您的裝置

http://**\<grid-ip-adress\>**:**\<grid-port\>**/grid/console

範例 Grid 節點組態 JSON

{
  "capabilities":
      [
        {
          "browserName": "<e.g._iPhone5_or_iPad4>",
          "version":"<version_of_iOS_e.g._7.1>",
          "maxInstances": 1,
          "platform":"<platform_e.g._MAC_or_ANDROID>"
        }
      ],
  "configuration":
  {
    "cleanUpCycle":2000,
    "timeout":30000,
    "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
    "url":"http://<host_name_appium_server_or_ip-address_appium_server>:<appium_port>/wd/hub",
    "host": "<host_name_appium_server_or_ip-address_appium_server>",
    "port": <appium_port>,
    "maxSession": 1,
    "register": true,
    "registerCycle": 5000,
    "hubPort": <grid_port>,
    "hubHost": "<Grid_host_name_or_grid_ip-address>",
    "hubProtocol": "<Protocol_of_Grid_defaults_to_http>"
  }
}

如果未提供 urlhostport,組態會自動更新,指向 localhost:<appium-port>

如果您的 Appium 伺服器在與您的 Selenium Grid 伺服器不同的機器上執行,請務必在您的 hosturl 組態中使用外部名稱/IP 位址;localhost127.0.0.1 會阻止 Selenium Grid 正確連線。