Appium 和 Selenium Grid
使用 Selenium Grid 4+¶
Grid 4 中的 relay 功能允许您将 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 中心。最好在单独的终端窗口中运行这些进程,以避免日志混淆。以下是如何启动每个进程
appium --config appium1.yml
appium --config appium2.yml
java -jar /path/to/selenium.jar node --config node1.toml
java -jar /path/to/selenium.jar node --config node2.toml
java -jar /path/to/selenium.jar hub
等待片刻,让节点检测到它们的 Appium 服务器并向中心注册后,您就可以通过单个中心端点(默认为 http://localhost:4444
)发送所有 Appium 流量。
当然,您也可以将网络中运行在不同机器上的 Appium 服务器和节点链接起来,以形成更大的网格。
使用 Selenium Grid 3¶
可以使用 --nodeconfig
服务器参数将您的 Appium 服务器注册到本地 Selenium Grid 3 (设置文档) 实例。
在引用的配置文件中,您必须定义 browserName
、version
和 platform
功能,并且根据这些参数,网格将重新将您的测试定向到正确的设备。您还需要配置您的主机详细信息和 Selenium Grid 详细信息。有关所有参数和描述的完整列表,请参阅 此处。
启动 Appium 服务器后,它将向网格注册,您将在网格控制台页面上看到您的设备
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>"
}
}
如果未给出 url
、host
和 port
,则配置将自动更新以指向 localhost:<appium-port>
。
如果您的 Appium 服务器运行在与 Selenium Grid 服务器不同的机器上,请确保在 host
和 url
配置中使用外部名称/IP 地址;localhost
和 127.0.0.1
将阻止 Selenium Grid 正确连接。