Writing Your First Android Appium Script


Appium Driver的生命旅程就像鬼怪一樣燦爛~

讓我們來寫第一隻Appium script, 體驗它的春夏秋冬吧...


一切都是那麼低簡單與美麗

每個Appium script都會經過這些節序

啟動 > 建立 > 執行 > 關閉

執行script之前要先把測試環境都先啟動完成
  1. 第一步當時就是要先由command line啟動Appium Server
    輸入以下指令加上自訂參數
    appium -p 4725 --address 127.0.0.1 --chromedriver-port 9516 --bootstrap-port 4755 --no-reset
    這些參數分別代表的是
    • -p: Appium server port number
    • --address: Appium server IP
    • --chromedriver: mobile chrome port number
    • --bootstrap-port or -bp: bootstrap proxy port number
    • --no-reset: Don't reset app for this run

想了解更多Appium Server 啟動參數可以看Appium的官方文件

https://appium.io/docs/en/writing-running-appium/caps/
  1. 啟動模擬器或是將Android手機接上電腦
    記得用adb devices 檢查狀態
  2. 在程式碼script中加上Desired Capabilies
    desired_caps = {
    'platformName': 'Android',
    'platformVersion': '6.0',
    'deviceName': 'emulator-5554',
    'noReset': 'true',
    'autoAcceptAlerts': 'true',
    'command-timeout': '60',
    'app': '.../AndroidTestApk/ApiDemos-debug-2014-08.apk'
    }
    這些分別代表的是
    • platformName: 作業系統類型
    • platformVersion: 作業系統版本
    • deviceName: 裝置id或名稱
    • noReset: 不Reset app
    • autoAcceptAlerts: 自動允許所有通知
    • command-timeout: 每個動作幾秒以後沒有反應就停止
    • app: 測試app的路徑
    上面使用的Desired Capabilities內容與Appium Server的參數是共用的
    我的建議是將相同的放在Appium參數啟動, 而將那些會變動的放在Capablities
  3. 這些都設定好就可以建立Appium Driver來建立Session
    driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps)
  4. 美麗的鬼怪終究還是會消逝... Appium也不例外
    Script執行完畢要停止Driver
    driver.quit()
  5. 最後要記得把門關上 把Appium Server也停止喔
    Mac環境可以在terminal輸入
    killall node

常用的Appium Driver Function

這裡列出常用的Python Appium API

App Interaction

  • 安裝 App
    driver.install_app('string')
  • 啟動 App
    driver.install_app('string')
  • 關閉 App
    driver.closeApp()

Find UI element

  • Find single WebElement
    driver.find_element_by_id('string')
    driver.find_element_by_xpath('string')
    driver.find_element_by_class_name('string')
    driver.find_element_by_link_text('string') driver.find_element_by_android_uiautomator('string') driver.find_element_by_tag_name('string')
  • Find multiple WebElements
    driver.find_elements_by_accessibility_id('string')

Basic Actions

  • 點擊
    driver.click()
  • 輸入字串
    driver.send_keys('string')
  • 清除內容
    driver.clear()

Get Attributes

  • 取得元件字串
    driver.text()
  • 取得元件指定屬性
    driver.get_attribute('content-desc')
  • 檢查元件是否enabled
    driver.is_enabled()
  • 檢查元件可否被選擇
    driver.is_selected()

Touch Action

  • 單擊
    actions.tap(element).perform()
  • 雙擊
    actions.double_tap(element).perform()
  • 長按
    actions.double_tap(element).long_press().perform()
  • 滑動
    actions.scroll(element, 10, 100).perform()

Wait Action

  • 設定頁面Timeout
    driver.set_page_load_timeout(5000)
  • 設定Implicit Wait
    driver.implicitly_wait(5000)

想了解更多Appium Python API可以參考官方文件

https://appium.readthedocs.io/en/stable/en/about-appium/appium-clients/#list-of-client-libraries-with-appium-server-support

一些常見的Appium Driver錯誤排解

  1. A new session could not be created
    可能原因:
    • Desicred Capabilties 資料有錯誤
    • Appium Server沒有正確啟動
    • Appium server session正被佔用或沒有正確關閉
  2. No such element found...
    可能原因:
    • 指定Locator在頁面上找不到任何元件
    • 沒有設定等待timeout
    • 元件存在, 但是被設定成disable or invisble
  3. Error: Permission to start activity denied.
    可能原因:
    • appPackage and appActivity錯誤
  4. Failed to start an Appium session, err was:INSTALL_FAILED_ALREADY_EXISTS:
    可能原因:
    • io.appium.settings 重複安裝造成


Comments

  1. 這篇文章對此應用的描述讓整體體驗變得簡單易懂。我很欣賞這篇文章,因為它提供了實用訊息,且講解過程毫不冗長重複。關於 Freecine APK 的部分讓我更深入地了解了該應用的主要功能及其在日常瀏覽中的使用方式。我特別喜歡關於「收藏」功能的內容,因為將感興趣的電影或劇集集中保存,能讓日後的查找變得輕鬆許多。此外,文章對介面佈局的介紹也十分直觀明了。總的來說,我認為內容既實用又詳盡,對於初次接觸 Freecine https://freecine.com.br/ 的用戶來說尤其有幫助。

    ReplyDelete
  2. This Appium guide provides a simple introduction to writing an Appium script by presenting the driver lifecycle as a clear sequence: starting the environment, creating the driver, executing the script, and closing it. The command-line setup and explanation of parameters such as the server port, IP address, ChromeDriver port, bootstrap port, and no-reset option make the initial configuration easier to understand.

    The step-by-step approach is particularly useful for beginners because it shows that mobile automation starts with preparing the testing environment before the actual script execution. Developers exploring mobile application development can also benefit from a React Native Course to understand how mobile applications are structured and developed.

    ReplyDelete
  3. Practical automation exercises can help students connect mobile development concepts with real testing workflows. Working on Android Projects for Final Year can provide an opportunity to apply mobile development and testing ideas in hands-on projects.

    ReplyDelete

Post a Comment