iOS 測試 WebDriverAgent 簡介


WebDriverAgent 是什么

 

 

 

去年的 SeleniumConf 上,Facebook 推出了一款新的iOS移動測試框架 —— WebDriverAgent,當時的推文上,寫的還只支持模擬器。半年過過去了,如今在看官方github —— https://github.com/facebook/WebDriverAgent,已經宣稱支持真機測試了(親測的確支持)。我把它的 ReadMe 搬運過來:

WebDriverAgent 在 iOS 端實現了一個 WebDriver server ,借助這個 server 我們可以遠程控制 iOS 設備。你可以啟動、殺死應用,點擊、滾動視圖,或者確定頁面展示是否正確。This makes it a perfect tool for application end-to-end testing or general purpose device automation.(它說它是iOS上一個完美的e2e的自動化解決方案) It works by linking XCTest.framework and calling Apple's API to execute commands directly on a device.(鏈接XCTest.framework調用蘋果的API直接在設備上執行命令) WebDriverAgent is developed and used at Facebook for end-to-end testing and is successfully adopted by Appium. (Appium封裝工作正在進行中,如果一旦封裝好,那么以后就可以直接用Appium提供的binding了。)It is currently maintained by Marek Cirkos and Mehdi Mulani.

它有如下特性:

  • 真機和模擬器都支持
  • 實現了大部分的 WebDriver spec
  • USB support for devices,所謂的usb支持,指的是設備不需要上網,目前client binding 還沒有。
  • 提供了一個 Inspector
  • Easy development cycle as it can be launched & debugged directly via Xcode
  • Unsupported yet, but works with tvOS & OSX

 

 

 

WebDriverAgent 的使用

To get the project set up just run bootstrap script:

./Scripts/bootstrap.sh

It will:

  • fetch all dependencies with Carthage
  • build Inspector bundle using npm

After it is finished you can simply open WebDriverAgent.xcodeproj and start WebDriverAgentRunner test and start sending requests.

More about how to start WebDriverAgent here.

官方的文檔講的很清晰,基本沒有什么坑。Carthage 是用來解決 xcode project 依賴的,大家可能知道 cocoapod,那你就把 Carthage 理解成和 cocoapod 一樣的東西就可以了,可以通過 brew install carthage 安裝。npm 是用來構建 Inspector, Inspector 是一個 react 的應用,通過往 WebDriverAgent 發 HTTP 請求,返回頁面的結構。

下面是我運行 ./Scripts/bootstrap.sh 的結果:

WebDriverAgent git:(master) ./Scripts/bootstrap.sh
Fetching dependencies
*** Checking out KissXML at "5.0.3"
*** Checking out peertalk at "5a0495a7b7d81b68e25ea1933c755b677cb040da"
*** Checking out RoutingHTTPServer at "v1.0.0"
*** Downloading ocmock.framework binary at "OCMock 3.2.2"
*** Checking out ocmock at "v3.2.2"
*** xcodebuild output can be found in /var/folders/11/wh7ypz2s7q17lxxjphxd4z6h0000gn/T/carthage-xcodebuild.XhkH5T.log
*** Building scheme "OCMock iOS" in OCMock.xcodeproj
2016-05-14 17:45:36.012 xcodebuild[14231:270807] [MT] DVTPlugInManager: Required plug-in compatibility UUID ACA8656B-FEA8-4B6D-8E4A-93F4C95C362C for KSImageNamed.ideplugin (com.ksuther.KSImageNamed) not present
2016-05-14 17:45:36.159 xcodebuild[14231:270807] [MT] PluginLoading: Required plug-in compatibility UUID ACA8656B-FEA8-4B6D-8E4A-93F4C95C362C for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/UncrustifyPlugin.xcplugin' not present in DVTPlugInCompatibilityUUIDs
2016-05-14 17:45:36.160 xcodebuild[14231:270807] [MT] PluginLoading: Required plug-in compatibility UUID ACA8656B-FEA8-4B6D-8E4A-93F4C95C362C for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/Alcatraz.xcplugin' not present in DVTPlugInCompatibilityUUIDs
2016-05-14 17:45:46.318 xcodebuild[14494:271598] [MT] DVTPlugInManager: Required plug-in compatibility UUID ACA8656B-FEA8-4B6D-8E4A-93F4C95C362C for KSImageNamed.ideplugin (com.ksuther.KSImageNamed) not present
2016-05-14 17:45:46.376 xcodebuild[14494:271598] [MT] PluginLoading: Required plug-in compatibility UUID ACA8656B-FEA8-4B6D-8E4A-93F4C95C362C for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/UncrustifyPlugin.xcplugin' not present in DVTPlugInCompatibilityUUIDs
2016-05-14 17:45:46.377 xcodebuild[14494:271598] [MT] PluginLoading: Required plug-in compatibility UUID ACA8656B-FEA8-4B6D-8E4A-93F4C95C362C for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/Alcatraz.xcplugin' not present in DVTPlugInCompatibilityUUIDs
Building Inspector
Creating bundle directory...
Building inspector.js...

> web-driver-inspector@1.0.0 build /Users/lihuazhang/code/WebDriverAgent/Inspector
> webpack --progress --colors

Hash: 536587fce0cad79f8558
Version: webpack 1.12.2
Time: 2794ms
Asset Size Chunks Chunk Names
inspector.js 706 kB 0 [emitted] main
[0] multi main 28 bytes {0} [built]
+ 182 hidden modules
Done
WebDriverAgent git:(master)

build 好之后就是啟動 WebDriverAgent,官方提供了四種方式:

  1. 使用 XCode

  2. 用 xcodebuild

  3. Using fbsimctl from FBSimulatorControl framework

  4. Using FBSimulatorControl framework directly

我使用 XCode (如果自動化的話,可以使用 xcodebuild),打開 WebDriverAgent.xcodeproj,IDE 的顯示是這樣的:

 

 

 

先不要糾結其他,我們只看 UITestingUITests.m,這是一個 XCTestCase 的實現類,我們直接運行,真機或者模擬器都可以。我用的模擬器,運行之后,會在模擬器安裝一個 WebDriverAgentRunner 的應用。我們看下運行的日志:

2016-05-15 01:19:43.019 XCTRunner[29300:524372] Running tests...
01:19:43.218 XCTRunner[29300:524495] _XCT_testBundleReadyWithProtocolVersion:minimumVersion: reply received
01:19:43.228 XCTRunner[29300:524495] _IDE_startExecutingTestPlanWithProtocolVersion:16
2016-05-15 01:19:44.732 XCTRunner[29300:524372] Continuing to run tests in the background with task ID 1
Test Suite 'Selected tests' started at 2016-05-15 01:19:45.319
Test Suite 'UITestingUITests' started at 2016-05-15 01:19:45.322
Test Case '-[UITestingUITests testRunner]' started.
2016-05-15 01:19:45.323 XCTRunner[29300:524372] -[_XCTestDriverTestObserver testCaseWillStart:] UITestingUITests testRunner
t = 0.00s Start Test
2016-05-15 01:19:45.324 XCTRunner[29300:524372] -[UITestingUITests testRunner] started activity <XCActivityRecord: 0x7f8acb721660> 2016-05-14 17:19:45 +0000: Start Test
2016-05-15 01:19:45.327 XCTRunner[29300:524372] -[UITestingUITests testRunner] finished activity <XCActivityRecord: 0x7f8acb721660> 2016-05-14 17:19:45 +0000: Start Test (0.00271106s)
t = 0.01s Set Up
2016-05-15 01:19:45.328 XCTRunner[29300:524372] -[UITestingUITests testRunner] started activity <XCActivityRecord: 0x7f8acb7219e0> 2016-05-14 17:19:45 +0000: Set Up
2016-05-15 01:19:45.330 XCTRunner[29300:524372] -[UITestingUITests testRunner] finished activity <XCActivityRecord: 0x7f8acb7219e0> 2016-05-14 17:19:45 +0000: Set Up (0.00130302s)
2016-05-15 01:19:45.331 XCTRunner[29300:524372] Built at May 15 2016 01:19:32
2016-05-15 01:19:45.386 XCTRunner[29300:524372] ServerURLHere->http://192.168.0.105:8100<-ServerURLHere
2016-05-15 01:19:45.386 XCTRunner[29300:524372] Listening on USB

關鍵字是:

2016-05-15 01:19:45.386 XCTRunner[29300:524372] ServerURLHere->http://192.168.0.105:8100<-ServerURLHere

這個意思是告訴我們,設備中起了一個服務器,我們可以用 http://192.168.0.105:8100 訪問到。下面就是在瀏覽器打開返回的內容:

{
value: "Unhandled endpoint: / -- http://192.168.0.105:8100/ with parameters { wildcards = ( "" ); }",
sessionId: null,
status: 1
}

通道找到了,接下來就是看如何造請求了。官方問題提供了文檔:https://github.com/facebook/WebDriverAgent/wiki/Queries。最簡單的 statushttp://192.168.0.105:8100/status

{
value: {
state: "success",
os: {
name: "iPhone OS",
version: "9.3"
},
ios: {
simulatorVersion: "9.3"
},
build: {
time: "May 15 2016 01:19:33"
}
},
sessionId: null,
status: 0
}

其他的 API 有興趣的人可以直接看文檔。

服務端有了,就剩下客戶端了,但是目前為止沒有一個官方的客戶端binding,Facebook 的同學沒有放出來,看這個issue——https://github.com/facebook/WebDriverAgent/issues/115

 

 

 

所以目前為止如果要使用 WebDriverAgent 就要自己實現一套客戶端代碼。

WebDriverAgent Inspector 的使用

直接在瀏覽器端打開 http://192.168.0.105:8100/inspector,就可以看見如下界面:

 

 

 

inspector 不是很強大,無法實現交互,不過大致能幫大家定位元素。

WebDriverAgent 簡單原理

 

 

 

總結

WebDriverAgent 取代 UIAutomation 或者 XCUITesting 做為 Appium 底部驅動看來是時間問題了。原因如下:

  1. 支持真機和模擬器

  2. 不受 instruments 單實例的限制

  3. Facebook 出品

 

轉自:https://testerhome.com/topics/4904

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM