appium服务端默认监听8100端口,一个appiun服务端只能监听一台设置,想要并行测试多台设备,需要启动多个appium服务,监听不同的端口,每台设备指定一个唯一端口
配置appium服务初始化参数,打开appium.txt,在[appium_lib]添加参数port=5000,可指定appium监听的端口
例子:
iphone 5s appium.txt:
[caps]
# 模拟器
platformName = "ios"
deviceName = "iPhone 5s"
platformVersion = "11.4"
app = "/Users/developer/Library/Developer/Xcode/DerivedData/BeiJiaFund-eesewjkcukxsbfcerpcnjtyholnv/Build/Products/Debug-iphonesimulator/BeiJiaFund.app"
automationName = "XCUITest"
[appium_lib]
port = "5000"
env.rb
equire 'rspec/expectations'
require 'appium_lib'
require 'cucumber/ast'
# Create a custom World class so we don't pollute `Object` with Appium methods
class AppiumWorld
end
# Load the desired configuration from appium.txt, create a driver then
# Add the methods to the world
# 示例一:cucumber --tags @homepage IDEVICENAME='iPhone_6' (还需要修改对应的端口号,详情见runsimulator文件。)
# 示例二:cucumber --tags @homepage
if ENV['IDEVICENAME']=='iPhone_6'
caps = Appium.load_appium_txt file: File.expand_path("./../iPhone_6/appium.txt", __FILE__), verbose: true
elsif ENV['IDEVICENAME']=='iPhone_8'
caps = Appium.load_appium_txt file: File.expand_path("./../iPhone_8/appium.txt", __FILE__), verbose: true
elsif ENV['IDEVICENAME']=='iPhone_5s'
caps = Appium.load_appium_txt file: File.expand_path("./../iPhone_5s/appium.txt", __FILE__), verbose: true
elsif ENV['IDEVICENAME']=='iPhone_6s_Plus'
caps = Appium.load_appium_txt file: File.expand_path("./../iPhone_6s_Plus/appium.txt", __FILE__), verbose: true
else
caps = Appium.load_appium_txt file: File.expand_path('./../appium.txt', __FILE__), verbose: true
end
Appium::Driver.new(caps, true)
Appium.promote_appium_methods AppiumWorld
World do
AppiumWorld.new
end
Before { $driver.start_driver }
After { $driver.driver_quit }
执行脚本:
1、启动appium服务
# 参数说明:-p指定监听端口
# -bp:指定adb端口,Android真机测试使用
# --webdriveragent-port 指定wda端口,ios真机测试使用
appium -p 5000 -bp 2250 --webdriveragent-port 8100
2、执行测试脚本
cucumber IDEVICENAME='iPhone_5s' -p iPhone_5s -f json -o ./reports/"iPhone_5s.json"
以上是一台设备测试的例子,如果要同时在多台设备并行测试,可以通过shell脚本执行,创建runsimulator shell文件,编写如下的shell脚本:
#!/usr/bin/env bash
# 并行测试脚本
# -bp:指定adb端口,Android多设备测试使用
# --webdriveragent-port 指定wda端口,ios多设备测试使用
appium -p 5000 -bp 2250 --webdriveragent-port 8100 &
appium -p 5001 -bp 2251 --webdriveragent-port 8101 &
appium -p 5002 -bp 2252 --webdriveragent-port 8102 &
appium -p 5003 -bp 2253 --webdriveragent-port 8103 &
sleep 20
echo
if [ ! -d "reports/" ]; then
mkdir "reports/";
fi
cucumber IDEVICENAME='iPhone_8' -p iPhone_8 -f json -o ./reports/"iPhone_8.json" &
cucumber IDEVICENAME='iPhone_6' -p iPhone_6 -f json -o ./reports/"iPhone_6.json" &
cucumber IDEVICENAME='iPhone_5s' -p iPhone_5s -f json -o ./reports/"iPhone_5s.json" &
cucumber IDEVICENAME='iPhone_6s_Plus' -p iPhone_6s_Plus -f json -o ./reports/"iPhone_6s_Plus.json" &
wait %5
wait %6
wait %7
wait %8
kill -9 %1
kill -9 %2
kill -9 %3
kill -9 %4
打开终端输入./runsimulator即可实现并行测试