Kent Chen
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee
  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Appium 101 (1):App 測試自動化框架 ![](https://i.imgur.com/CyhbzpI.png)https://hackmd.io/1jXAFMcDQdup5GD9r8MLyg?both#Appium-101-1%EF%BC%9AApp-%E6%B8%AC%E8%A9%A6%E8%87%AA%E5%8B%95%E5%8C%96%E6%A1%86%E6%9E%B6 > 本文部分翻譯/摘錄自 [Appium 官方文件](http://appium.io/slate/en/master/?javascript#about-appium)以及 nishant@TestVagrant 所著之 [Appium for Android](https://nishantverma.gitbooks.io/appium-for-android/content/appium/why_appium.html),非常感謝他這份鉅細彌遺的導引。 本篇將以簡介 Appium 為主。本系列將分為幾個部分: - [Appium 101 (1):跨平台 App 測試自動化框架](http://logme.logdown.com/posts/1175935/appium-1-app-testing-automation-framework) - [Appium 101 (2):Test For Android]() - [Appium 101 (3):Test For iOS]() - [Appium 101 (4):Test For React Native]() ## 環境 本系列文章所使用之環境 / 軟體版本如下: - MacOS 10.11 - Appium app 1.5.3 - Android Studio 2.2.3 - Xcode - Android 6.0.1 ## Appium 是什麼 **Appium** 是一套開源的應用程式自動測試工具,它允許你在原生(Native),混合(Hybrid)以及 Mobile Web 三種不同類型的應用程式上進行自動測試。 在此順道簡介一下不同應用程式類型間的差異: - **原生(Native)** 指的是以 iOS、Android 或 Windows 提供的 SDK 工具所撰寫的 Apps。例如手機內建的電話 App。 - **混合(Hybrid)** 涵蓋所有主要以 ”webview“ 包裝層、再以原生控制項與 web 內容互動的 App,例如所有以 [PhoneGap](http://phonegap.com/) 技術封裝的專案。 - **Mobile Web** 基本上則是透過手機內瀏覽器呈現的 Web App。Appium 支援以 Safari/Chrome 或是封裝好的 built-in ‘Browser’ App 進行自動化測試。 所以,React Native 以及基於其上的 Exponent 基本上屬於原生(Native)類別,當然支援。而執行環境部分,則可以使用模擬器或是在實機上進行測試。 最重要的是,Appium 是**跨平台**的測試工具:它允許你針對多個平台(iOS / Android / Windows)使用相同的 API 撰寫測試;換句話說,平台間的測試組合能夠有效地重用。 關於更多 Appium 所支援平台的詳細資料,請參考官方的 [Platform Support Doc](http://appium.io/slate/en/master/?javascript#platform-support.md)。 ## Appium 架構與設計哲學 Appium 的自動化設計哲學可以被概括成以下四條原則: - 你不應該為了自動化而修改或重新編譯(recompile)你的應用程式。 - 你不應該被鎖定在特定語言或框架之下撰寫、及進行測試。 - 自動化測試框架的不應該在 APIs 重造輪子,重複已經有的東西。 - 無論在名義上、精神上,或是實做上,自動化測試框架應該是開源的。 基於原則 #1,Appium 實際上使用平台(Android / iOS / Windows)原先就提供的自動化框架進行操作,所以我們不需要把加入某種 Appium 專用的 lib、程式碼,或是任何第三方的框架之類的東西到 App 裡。 基於原則 #2,Appium 使用 WebDriver API(aka “Selenium WebDriver”)作為 client-server 協定,所以只要能夠發送正確 HTTP 請求的語言,通通透過 REST API 拉進來做成大雜燴! 換句話說,`你可以在本機執行測試,由遠端的 Appium Server 替你跑出結果`,而且`你可以用你喜歡的語言寫 test case`;同時`你測試的 App 就是你所發行的 App`。 以下粗列 Appium 所使用的自動化框架及其對應版本: - iOS: Apple’s [UIAutomation](https://developer.apple.com/library/ios/documentation/DeveloperTools/Reference/UIAutomationRef/) - Android 4.2+: Google’s [UiAutomator](http://developer.android.com/tools/help/uiautomator/index.html) - Android 2.3+: Google’s [Instrumentation](http://developer.android.com/reference/android/app/Instrumentation.html). (Selendroid) - Windows: Microsoft’s [WinAppDriver](https://github.com/microsoft/winappdriver) (Supports UWP and Windows 10 PC) Appium 整套架構大致如下圖: ![](https://i.imgur.com/Thz83XJ.png) 基本上 Appium Server 就負責以下幾件事: ``` - 接收 Client 的連線 - 監聽指令 - 執行指令 - 回傳指令執行狀態 ``` ### Client / Server Appium 的核心是一個公開 REST API 的 Web Server。它負責監聽來自 Client 的連線與指令、並且把執行結果以 HTTP 狀態的方式回應。 所以我們得以使用各種不同語言撰寫測試程式 --- 只要該語言擁有 HTTP Client API 即可;當然,還是用現成的 [Appium client libraries](http://appium.io/downloads) 會比較方便。 基於這樣的架構,我們也能把 Appium Server 跟實際執行測試的環境分開,甚至把 Server 託管在類似 [Sauce Labs](https://saucelabs.com/mobile) 這樣的雲端服務上,視需要以提供更多的靈活性。 ### Session 基於每種 client-library 的不同,Client 會以特定的方式始化一段 session 並且把一組叫做 ”desired capabilities“ 的 JSON object 用 POST 的方式丟給 Server。 當收到 JSON 之後,Server 會建立一組對應的 automation-session 並回傳一組用於發送其他指令的 session ID。 ### Desired Capabilities 基本上這是一組 Key-Value 的組合值,用來告訴 Appium Server 要操作怎樣的對象。 Capabilities 會影響 Server 在自動化測試過程中的行為,例如我們可以設定 `platformName` 的值為 `iOS`,告訴 Appium 我們要啟動一組 iOS 的 session。 或是,我們也能把 `safariAllowPopups` 設定為 `true`,確保在進行 Safari automation session 的過程中能夠允許以 javascript 的方是開啟一個新的視窗。 例如,以下定義告訴 Appium 我們要使用 Appium v1.5 啟動一組操作 OS 為 Android 6.0、設備叫做 `device` 的 session(實際上此為 Genymoton 模擬器)。 ``` exports.android23 = { browserName: '', 'appium-version': '1.5', platformName: 'Android', platformVersion: '6.0', deviceName: 'device', app: undefined }; ``` 如果你對其他 capabilities 的有興趣,請參考 [capabilities doc](http://appium.io/slate/en/master/?javascript#caps.md)。 ## 下載 Appium Server Appium Server 是由 NodeJs 撰寫而成,你可以參考[這份文件](https://github.com/appium/appium/blob/master/docs/en/contributing-to-appium/appium-from-source.md),自己 built && install 整個環境。或是直接經由 NPM 安裝: ``` $ npm install -g appium ``` ![](https://i.imgur.com/Go6NJ4z.png) 不過本文統一使用封裝好的 App 版本,該版本已經自己內建了對應的 NodeJs 環境與 GUI。請到以下網址下載 `Appium Desktop App`: ``` http://appium.io/downloads.html ``` ## 啟動 Appium Server 開啟後首先點選齒輪圖案,可以觀察 Appium 使用的 port 以及相關設定。 接下來請點選標有 `appium-doctor` 的按鈕,隨即會啟動一個 CLI 介面顯示目前你的環境還有缺少什麼套件,請按照指示完成安裝。 如果你使用 CLI 套件,也可以直接輸入 `appium-doctor` 啟動環境檢測。 ![](https://i.imgur.com/B9YNJtl.png) 如果沒有更改設定,在瀏覽器輸入以下網址,應該可以看到 Appium server 回傳的 JSON 格式訊息: ``` http://localhost:4723/wd/hub/status ``` ![](https://i.imgur.com/jPu57zc.png) 如果你看到差不多的訊息,那就表示你的 Appium server 應該運作正常,恭喜你! ## 接下來 ... - [Appium 101 (2):Test For Android]() - [Appium 101 (3):Test For iOS]() - [Appium 101 (4):Test For React Native]() 迫不及待想要開始你的自動化測試之旅嗎? 官網文件提到目前有 .NET, Java, Ruby, Python, PHP, JavaScript, C# 等 8 種語言支援 Appium 的 WebDriver 協定,你可以從[這裡](http://appium.io/slate/en/master/?javascript#appium-clients.md)看到更多與 Client 端有關的資訊。 同時 Appium 官方也提供了這些語言的操作範例:[Appium example tests](https://github.com/appium/sample-code/tree/master/sample-code/examples)。 # REF - [Appium - API Reference](http://appium.io/slate/en/master/?javascript#about-appium) - [Gitbook - Appium for Android](https://nishantverma.gitbooks.io/appium-for-android/content/appium/why_appium.html)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully