app自动化训练营

开营仪式

image

ppt截图

adb

adb shell 'uiautomator dump && cat /sdcard/window_dump.xml'
seveniruby:~ seveniruby$ adb logcat | grep Displayed
05-27 21:14:19.140   553   578 I ActivityTaskManager: Displayed com.xueqiu.android/.view.WelcomeActivityAlias: +707ms
05-27 21:14:23.324   553   578 I ActivityTaskManager: Displayed com.xueqiu.android/.common.MainActivity: +3s206ms

微信自动化小工具

demo() {
    find_element f_l | awk -F\" '{print $4}' | while read e; do
        click "$e"
        location=$(get_text b26)
        echo $e $location
        back
    done
}

appium desktop

学院内部下载地址:http://download.testing-studio.com/appium-desktop/
雪球财经app:https://sj.qq.com/myapp/detail.htm?apkName=com.xueqiu.android

desktop的配置

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

第一个测试用例

# 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 time import sleep

from appium import webdriver


class TestDemo:
    def setup(self):
        caps = {}
        caps["platformName"] = "android"
        caps["deviceName"] = "hogwarts"
        caps["appPackage"] = "com.xueqiu.android"
        caps["appActivity"] = ".view.WelcomeActivityAlias"

        self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", caps)
        self.driver.implicitly_wait(10)

    def test_search(self):
        self.driver.find_element_by_id("tv_agree").click()
        self.driver.find_element_by_id("tv_search").click()
        self.driver.find_element_by_id("search_input_text").send_keys("alibaba")

        assert self.driver.find_element_by_id("name").text == "阿里巴巴"

    def teardown(self):
        sleep(10)
        self.driver.quit()

Appium安装

  • 安装appium,推荐nodejs的LTS版本v10
  • 官方文档的安装方式:
    • 官方方式:npm install -g appium 基本安装不上
    • 使用代理安装:http_proxy=[代理地址] https_proxy=$http_proxy npm install -g appium
  • 淘宝cnpm (最稳定的方法)
    • npm install -g cnpm --registry=https://registry.npm.taobao.org
    • cnpm install -g appium

Appium自动化

capability介绍

  • unicodeKeyboard + resetKeyboard 要输入中文等多国文字
  • autoGrantPermissions 解决app的权限弹框
  • noReset = True 可以保留app的数据,也可以加速
  • dontStopAppOnReset 启动应用的时候,不杀掉已有的app进程

虽然很多人都会提到不建议使用xpath,其实是一种落后的看法。使用相对定位是很容易维护的。很多控件因为没有id,所以xpath是离不开的。xpath只有一个缺点,会比正常的定位慢1-3s

  • 绝对定位xpath:hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.view.ViewGroup/android.widget.LinearLayout/android.widget.RelativeLayout[2]/android.widget.LinearLayout/android.widget.RelativeLayout/android.widget.ImageButton
  • 相对路径xpath://*[@resource-id=‘com.xueqiu.android:id/action_message’]
  • 改进版本的相对定位xpath://*[contains(@resource-id, ‘message’)]

定位符的使用

  • id定位
  • accessibility id简称aid
  • xpath

改进版的普通测试用例

# 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 time import sleep

from appium import webdriver
from selenium.webdriver.common.keys import Keys


class TestDemo:
    def setup(self):
        caps = {}
        caps["platformName"] = "android"
        caps["deviceName"] = "hogwarts"
        caps["appPackage"] = "com.xueqiu.android"
        caps["appActivity"] = ".view.WelcomeActivityAlias"
        caps['unicodeKeyboard'] = True
        caps['resetKeyboard'] = True
        caps['autoGrantPermissions'] = True
        caps['noReset'] = True

        self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", caps)
        self.driver.implicitly_wait(10)
        # self.driver.find_element_by_id("tv_agree").click()

    def test_search(self):
        self.driver.find_element_by_id("home_search").click()
        self.driver.find_element_by_id("search_input_text").send_keys("alibaba")
        assert self.driver.find_element_by_id("name").text == "阿里巴巴"
        self.driver.find_element_by_id('name').click()
        price = self.driver.find_element_by_id('current_price').text
        assert float(price) > 200

    def teardown(self):
        sleep(5)
        self.driver.quit()

Page Object模式

用例PO改造

获奖同学

{"pic_url":"//thirdqq.qlogo.cn/g?b=sdk&k=eicw4LeXm1uialZZSum7qwsw&s=140&t=1569308540","nick_name":"醉****","first_comment":"老师讲的很不错,由简单到复杂,循序渐进,不错"}

{"pic_url":"https://wx.qlogo.cn/mmopen/vi_32/PiajxSqBRaELPT2gvXFPthJU12Zaib0mC6cU1M25sVRa1n5iaISo4yEDszOestcib7icggDMhr4NDLkgsfgqTpcpiaxw/132","nick_name":"_****","first_comment":"课程不错,干货很多,老师也很nice"}

total=52 hit=50
{"pic_url":"https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLgSY7N3EeVtKD9wUkHCmhxLxLhOsfaTyH8CEic7EeAvsu35KbCrq0ic1yRokjA0Wy7mamKNe6gKZMw/132","nick_name":"往****","first_comment":"非常棒非常棒,醍醐灌顶,想要代码"}

total=52 hit=4
{"pic_url":"http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83epNOicVKRvDh7zFJI1fgibCy6DPNnv0BdOcPVBjOebIX6QUu0HIzDRiaXKHZdaF6ia4qsWic4M4RMl03Jw/132","nick_name":"雨****","first_comment":"第一节课介绍了一些基础知识,对于我这种小白来说非常实用,加上老师的一些操作加深理解,也更有兴趣了。 非常心动想报一个班去学习一下"}


total=52 hit=52
{"pic_url":"https://wx.qlogo.cn/mmopen/vi_32/DYAIOgq83epc85fHaAMph207tM0ZltPpOOZOPxEg5PmzJEuticLoXFCjsianaNgMH4LtFpw8YMf2qvFOYTO5EQ7A/132","nick_name":"C****","first_comment":"非常有用 引领我再次入门 很好"}

total=52 hit=3
{"pic_url":"http://thirdwx.qlogo.cn/mmopen/vi_32/lmLPMhUJ3ic1oIVBmrhHATY2vhJuO3ZOxfyicNJKo53rIQ8ibFNv2v3ia2cX7RN1e9aykOT9JQkH03vJdrWKiageKWw/132","nick_name":"邱****","first_comment":"思寒老师讲的很好,贴合实际工作,对自己的工作能力提升还是有很大的帮助,以后还是多多向思寒老师学习,不断学习,不断进步,谢谢老师。"}

total=52 hit=49
{"pic_url":"https://wx.qlogo.cn/mmopen/vi_32/Q02yeejica3JYAhrT0vE9Vabp3l7I9e0hmhvvSeyJazHTmqGfUUI8AM3NuBfwEjCgOmg7Gtn3BibcLVzz5MkYiaXQ/132","nick_name":"S****","first_comment":"老师讲解很好,对po模式有了入门"}

total=53 hit=20
{"pic_url":"https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLVdlII1FCYsXsh82iceSXKma6Q1Cq7zeibYNoMerQlyG1fgNGVMtCeynPicMGantGdTl7ZWAxjjiaXMw/132","nick_name":"f****","first_comment":"老师讲的比较详细,学习圈氛围也比较浓郁,希望有更多更深入的学习"}

total=53 hit=37
{"pic_url":"https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKr6PmZuh0a1zyBEVcvAdNtHVictbD0wUvHet0pzNiciaRcrFIJNPxgYgibI4rj9uo9GdKb2b13nZDpjg/132","nick_name":"春****","first_comment":"再听一遍,理解又深刻了一些,思寒老师讲的很棒。"}

total=54 hit=13
{"pic_url":"http://thirdwx.qlogo.cn/mmopen/vi_32/6ATpLy9yackVjaiaicQXqm7X9lt4bKDI8nJicERib9boSsQudrR3vBqXWkiczf9f6LAT8HU9am04ChKqXXLEicNvZshw/132","nick_name":"奔****","first_comment":"老师讲的很好哈哈哈想要书快点中吧中吧啦啦啦"}

限时优惠

image

课程质量挺高,两节课听下来对appium有了新的认识

1 Like

非常实用的课程,硬货多多

1 Like