appium使用教程(三)-------------用例編寫


1. 驅動

import os, time, unittest
from appium import webdriver
PATH = lambda p:os.path.abspath(os.path.join(os.path.dirname(__file__),p))

desired_caps = {}

  1. desired_caps['platformName'] = 'Android'  # 設備系統
  2. desired_caps['platformVersion'] = '4.2.2'  # 設備系統版本
  3. desired_caps['deviceName'] = 'H30-U10'  #  設備名稱
  4. desired_caps["unicodeKeyboard"] = True  #設置輸入法
  5. desired_caps["resetKeyBoard"] = True    #設置輸入法
  6.  
  7.  
  8. # desired_caps['app'] = PATH(r"D:\zhihu.apk")
  9. desired_caps['appPackage'] = 'com.zhihu.android'
  10. desired_caps['appActivity'] = 'com.zhihu.android.app.ui.activity.MainActivity'
  11. def new_driver():
  12.     #設置driver
  13.     driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps)       

    return driver

注意:其中第9行,如果使用appium安裝應用的話則需要,手動安裝則注銷掉

10,11行有幾種方法得到,建議使用解析apk直接得到,方法如下:

[cmd]

aapt dump badging D:\zhihu.apk

 

 

 

 

此處有個坑,開發者選項里面需要勾選一個使用自動化的一個選項,記不清到底是什么了,換了個手機竟然連接不上了…╮(╯▽╰)╭好氣呀

1. 編寫用例.

使用D:\Android\android-sdk\tools下面的一個工具進行定位

 

 

 

 

點擊它

 

 

一般用test定位

 

2. 簡單代碼

driver = self.driver

try:
    time.sleep(10)
    driver.find_element_by_name("搜索").send_keys("xxx")
    driver.find_element_by_name("xxxxx").click()
    str = "xxxx"
    driver.find_element_by_name(str).click()
    time.sleep(5)
     #實例化
    driver.get_screenshot_as_file("new.png")
    print("頁面打開成功")

涉及到一些隱私內容,已經替換成xxx

 

3. 其他

Appium是由selenium繼承來的,所以使用appium的話需要有一些selenium的經驗,畢竟selenium的環境比appium好搭太太太多了,至於再深入的東西,可以使用unittes框架,或者pytest.

 


免責聲明!

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



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