Appium TesterHome 重庆 WorkShop 技术贴

思寒_seveniruby · 2019年06月02日 · 2176 次阅读

此贴用于 TesterHome 重庆 Workshop

Appium 安装准备

npm install -g cnpm --registry=https://registry.npm.taobao.org
cnpm install -g appium

模拟器准备

任何一个即可

  • 网易 mumu 模拟器
  • android studio 自带模拟器

自动遍历工具

  • maxim 自动遍历工具
  • appcrawler 自动遍历工具

演练 app

  • 雪球
adb devices
adb install -r ~/Downloads/xueqiu_wxdialog.apk
adb logcat -s ActivityManager:I
06-02 15:01:20.704   551   569 I ActivityManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.xueqiu.android/.view.WelcomeActivityAlias bnds=[43,131][170,328]} from uid 10031 on display 0
06-02 15:01:20.756   551   735 I ActivityManager: Start proc 1911:com.xueqiu.android/u0a35 for activity com.xueqiu.android/.view.WelcomeActivityAlias
06-02 15:01:21.785   551  1793 I ActivityManager: Start proc 1971:com.xueqiu.android:pushservice/u0a35 for service com.xueqiu.android/com.xiaomi.push.service.XMPushService
06-02 15:01:22.083   551   595 I ActivityManager: Displayed com.xueqiu.android/.view.WelcomeActivityAlias: +1s346ms
06-02 15:01:26.181   551   569 I ActivityManager: START u0 {flg=0x100000 pkg=com.xueqiu.android cmp=com.xueqiu.android/.common.MainActivity} from uid 10035 on display 0
06-02 15:01:27.259   551   595 I ActivityManager: Displayed com.xueqiu.android/.common.MainActivity: +1s48ms

Appium Desktop

TODO 2:00-3:00

{
  "platformName": "android",
  "deviceName": "chongqing",
  "appPackage": "com.xueqiu.android",
  "appActivity": ".view.WelcomeActivityAlias"
}

解决权限弹框

{
  "platformName": "android",
  "deviceName": "chongqing",
  "appPackage": "com.xueqiu.android",
  "appActivity": ".view.WelcomeActivityAlias",
  "autoGrantPermissions": "true"
}

常见问题

  • adb 找不到 adb 必须在 PATH 变量下,配置完成后需要重启 appium desktop 以及所有的 command 窗口。
  • ANDROID_HOME 变量最好配置,如果他不配置,必须把 adb 放到 PATH 下,理论上第一条满足就可以了。
  • adb devices 找不到,通常是因为有多个 adb 进程已经启动,而且是不同的版本。
  • 个别手机比如华为特别的型号不允许重置 app 的数据,这个时候需要住家 noReset=true

录制与手工编写用例

# This sample code uses the Appium python client
# pip install Appium-Python-Client
# Then you can paste this into a file and simply run with Python

from appium import webdriver

caps = {}
caps["platformName"] = "android"
caps["deviceName"] = "chongqing"
caps["appPackage"] = "com.xueqiu.android"
caps["appActivity"] = ".view.WelcomeActivityAlias"
caps["autoGrantPermissions"] = "true"

driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
#人工追加保证稳定西行
driver.implicitly_wait(10)

el1 = driver.find_element_by_id("com.xueqiu.android:id/user_profile_icon")
el1.click()
el2 = driver.find_element_by_id("com.xueqiu.android:id/tv_login")
el2.click()
el3 = driver.find_element_by_id("com.xueqiu.android:id/tv_login_by_phone_or_others")
el3.click()
el4 = driver.find_element_by_id("com.xueqiu.android:id/register_phone_number")
el4.send_keys("15600534760")
el5 = driver.find_element_by_id("com.xueqiu.android:id/register_code")
el5.send_keys("123456")
el6 = driver.find_element_by_id("com.xueqiu.android:id/button_next")
el6.click()

driver.quit()

XPATH

/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.support.v4.view.ViewPager/android.widget.RelativeLayout/android.widget.ListView/android.widget.LinearLayout[1]/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.RelativeLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout/android.widget.TextView
//*[@text='BABA']
(//*[@resource-id='com.xueqiu.android:id/stockCode'])[1]
//*[@text='01810']/../../..//*[@resource-id='com.xueqiu.android:id/followed_btn']
//*[@text='01810']/../../..//*[contains(@resource-id, '_btn')]

Page Object 原则


PO 用例实战

def test_po(self):
    main_page=MainPage(self.driver)
    search_page=main_page.gotoSearch()
    search_page.search("xiaomi")

def test_po(self):
    main_page=MainPage(self.driver)
    search_page=main_page.gotoSearch()
    search_page.search("alibaba")

def test_tsl(self):
    MainPage(self.driver).gotoSearch().search("tesla")

自动遍历

git clone https://github.com/zhangzhao4444/Maxim
cd Maxim
adb push framework.jar /sdcard
adb push monkey.jar /sdcard
adb shell CLASSPATH=/sdcard/monkey.jar:/sdcard/framework.jar exec app_process /system/bin tv.panda.test.monkey.Monkey -p com.xueqiu.anroid --uiautomatormix --running-minutes 60 -v -v
java -jar appcrawler-2.4.0-jar-with-dependencies.jar --capability "appPackage=com.xueqiu.android,appActivity=.view.WelcomeActivityAlias"

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册