Appuim源碼剖析(Bootstrap)

來源:互聯網
上載者:User

標籤:觸控   開源   zhang   ios平台   static   android   window   啟動   參數   

Appuim源碼剖析(Bootstrap) 

SkySeraph Jan. 26th 2017

Email:[email protected]

更多精彩請直接存取SkySeraph個人網站:www.skyseraph.com 

 

AboutAppuim

Appium 是一個自動化測試開源工具,支援 iOS 平台和 Android 平台上的原生應用,web 應用和混合應用。

這裡有很關鍵一點,跨平台。更多瞭解Appuim多平台支援相關資訊,參考官方platform-support

 

相關概念
  • C/S 架構
    Appium 的核心是一個 網頁伺服器,它提供了一套 REST 的介面,接收用戶端的串連,監聽到命令,接著在行動裝置上執行這些命令,然後將執行結果放在 HTTP 響應中返還給用戶端。
  • Session機制
    Appuim的自動化測試是在一個 session 的上下文中運行,可以理解成Appuim會話。
    每一次當Appium server成功啟動後,用戶端的測試庫(client library)會要求與Server建立一個會話(session)。
    會話的作用是為了確保能區別不同的用戶端請求與不同的被測應用,每個特定的會話都有一個特定的sessionId參數。每次測試開始時,用戶端將初始化一個session會話,雖然不同的語言初始化的方式不同,但是他們都要發送POST/session請求到伺服器端,這些請求裡面都會帶有一個對象:desired capabilities ,這個時候伺服器端會啟動自動化session然後返回一個session ID,以後的命令都會用這個seesion ID去匹配。

  • Appuim服務端
    包含眾多語言庫(Java, Ruby, Python, PHP, JavaScript,C#等),都實現了 Appium 對 WebDriver 協議的擴充。

  • JSON wire protocol
    Appuim中非常重要的協議

Appuim 架構

Appuim基於Nodejs編寫,基於HTTP協議,可以看成一個類似selenium webdriver的基於移動平台的webdriver,遵循RESTful設計風格web伺服器,接受用戶端的串連然後在手機裝置上執行命令,最後通過HTTP的響應收集命令執行的結果。

如下為我整理的Appuim Android平台下架構原理圖,iOS也類似,只是Bootstrap部分由Instruments替換,UiAutomator由UIAutomation替換。

 

如下兩圖參考testerhome PPT的Appuim Android和iOS平台下資料流程圖。

其中,Android平台下,Android API 17+,底層調用android平台內建的UI測試架構Uiautomator;反之,調用的selendroid測試架構來完成。

 

 

 

 

Bootstrap源碼剖析源碼結構

如下所示,Appuim Bootstrap部分源碼結構,分UiWatchers、Bootstrap和UIAutomator三部分,非常清晰。

啟動時序

Bootstrap入口類為Bootstrap.java,繼承自UiAutomatorTestCase,然後開啟Socket接收命令,時序如下。

 

 

 

類別關係圖

類別關係如所示,很簡單。

 

 

源碼分析

Bootstrap整體分SocketServer部分,CommandHandler部分,Watchers部分和UiAutomator四部分。

  • SocketServer。完成PC Server端命令接收和解析,再通過CommandHandler的execute操作調用UiAutomator實現觸控操作。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
try {
client = server.accept();
Logger.debug("Client connected");
in = new BufferedReader(new InputStreamReader(client.getInputStream(), "UTF-8"));
out = new BufferedWriter(new OutputStreamWriter(client.getOutputStream(), "UTF-8"));
while (keepListening) {
handleClientData();
}
in.close();
out.close();
client.close();
Logger.debug("Closed client connection");
} catch (final IOException e) {
throw new SocketServerException("Error when client was trying to connect");
}
  • CommandHandler,虛基類,功能類都整合自該類完成execute操作,通過HashMap映射,如下。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
private static HashMap<String, CommandHandler> map = new HashMap<String, CommandHandler>();

static {
map.put("waitForIdle", new WaitForIdle());
map.put("clear", new Clear());
map.put("orientation", new Orientation());
map.put("swipe", new Swipe());
map.put("flick", new Flick());
map.put("drag", new Drag());
map.put("pinch", new Pinch());
map.put("click", new Click());
map.put("touchLongClick", new TouchLongClick());
map.put("touchDown", new TouchDown());
map.put("touchUp", new TouchUp());
map.put("touchMove", new TouchMove());
map.put("getText", new GetText());
map.put("setText", new SetText());
map.put("getName", new GetName());
map.put("getAttribute", new GetAttribute());
map.put("getDeviceSize", new GetDeviceSize());
map.put("scrollTo", new ScrollTo());
map.put("find", new Find());
map.put("getLocation", new GetLocation());
map.put("getSize", new GetSize());
map.put("wake", new Wake());
map.put("pressBack", new PressBack());
map.put("pressKeyCode", new PressKeyCode());
map.put("longPressKeyCode", new LongPressKeyCode());
map.put("takeScreenshot", new TakeScreenshot());
map.put("updateStrings", new UpdateStrings());
map.put("getDataDir", new GetDataDir());
map.put("performMultiPointerGesture", new MultiPointerGesture());
map.put("openNotification", new OpenNotification());
map.put("source", new Source());
map.put("compressedLayoutHierarchy", new CompressedLayoutHierarchy());
map.put("configurator", new ConfiguratorHandler());
}

具體映射通過AndroidCommandExecutor中的execute實現。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public AndroidCommandResult execute(final AndroidCommand command) {
try {
Logger.debug("Got command action: " + command.action());

if (map.containsKey(command.action())) {
return map.get(command.action()).execute(command);
} else {
return new AndroidCommandResult(WDStatus.UNKNOWN_COMMAND,
"Unknown command: " + command.action());
}
} catch (final JSONException e) {
Logger.error("Could not decode action/params of command");
return new AndroidCommandResult(WDStatus.JSON_DECODER_ERROR,
"Could not decode action/params of command, please check format!");
}
}
  • Watchers,Android ANR 和 Crash,如下代碼。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public void registerAnrAndCrashWatchers() {
UiDevice.getInstance().registerWatcher("ANR", new UiWatcher() {
@Override
public boolean checkForCondition() {
UiObject window = new UiObject(new UiSelector()
.className("com.android.server.am.AppNotRespondingDialog"));
String errorText = null;
if (window.exists()) {
try {
errorText = window.getText();
} catch (UiObjectNotFoundException e) {
Log.e(LOG_TAG, "dialog gone?", e);
}
onAnrDetected(errorText);
postHandler();
return true; // triggered
}
return false; // no trigger
}
});
  • UiAutomator,通過UiAutomator執行觸控操作。
Refs
    • 官方Links
      首頁
      源碼首頁
      Appuim源碼
      Bootstrap源碼

    • 《Appium Essential》

    • testerhome PPT

    • Appuim介紹

 

 

文章更新, 請移步個人網站查看. 

SYNC POST

 

========  

By SkySeraph-2017

www.skyseraph.com 

 

Appuim源碼剖析(Bootstrap)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.