app自动化6-进阶知识

1. 常用设备交付命令

    1> 模拟电话,短信;

    2> 横竖屏切换;

    3> app处理

    4> 键盘,粘贴板

    5> 录屏

具体的api说明字段:http://appium.io/docs/en/writing-running-appium/caps/

 

 2. 获取appn性能数据举例:

from appium import webdriver

class TestDemo():
    def setup(self):
        #参数设置
        caps = {}
        caps["platformName"] = "android"
        caps["deviceName"] = "qiucaixia"
        caps["appPackage"] = "com.xueqiu.android"
        caps["appActivity"] = ".view.WelcomeActivityAlias"
        caps["autoGrantPermissions"] = "true"   #自动赋予app权限
        caps["unicodeKeyboard"]="true"  #支持输入中文

        self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
        self.driver.implicitly_wait(10)  #隐式等待

    def test_performance(self):
        print('###能收集的性能数据类型:###',self.driver.get_performance_data_types())
        for p in self.driver.get_performance_data_types():
            try:
                print("## %s 的使用情况##" % self.driver.get_performance_data('com.xueqiu.android',p,5))
            except:
                pass

    def teardown(self):
        self.driver.quit()
获取app性能数据

   执行结果如下:

 3. XPath高级使用:

 

 4. XPath使用举例:

from appium import webdriver

class TestDemo():
    def setup(self):
        #参数设置
        caps = {}
        caps["platformName"] = "android"
        caps["deviceName"] = "qiucaixia"
        caps["appPackage"] = "com.xueqiu.android"
        caps["appActivity"] = ".view.WelcomeActivityAlias"
        caps["autoGrantPermissions"] = "true"   #自动赋予app权限
        caps["unicodeKeyboard"]="true"  #支持输入中文

        self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
        self.driver.implicitly_wait(10)  #隐式等待

    def test_xpath(self):
        self.driver.find_element_by_xpath("//*[@text='交易' and contains(@resource-id,'tab_name')]").click()

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

5. Toast不属于当前page结构,属于Android系统级别的提醒框,appium在UIAutomator2引擎下支持对toast的捕获,获取原理:

     1> 当界面发生变化时就去捕获信息

     2> 若是toast的,就把toast信息保存起来

    识别原理:捕获到toast信息时

     1> 将toast封装为一个独立节点,储存到出现toast界面的空间树里(XPath)

     2> 把toast信息设置为节点的内容,该节点的类名ClassName设置为"android.widget.Toast",该节点的包名PackageName设置为"com.android.settings",组成了一个toast节点

     3> 故toast只能使用XPath定位

          a> //*[@class='android.widget.Toast']   (推荐方式)

          b> //*[contains(@text,'XXXXX')]

6. 定位策略API说明文档: http://appium.io/docs/en/commands/element/find-elements/index.html

7. UiSelector定位方法(仅对于安卓平台):

    1> API用法说明:http://appium.io/docs/en/writing-running-appium/android/uiautomator-uiselector/

    2> ApiDemos-debug.apk下载连接: http://appium.io/docs/en/commands/element/find-elements/index.html

    3> 安装到手机: self.driver.install_app('D:\work_office\ApiDemos-debug\ApiDemos-debug.apk')

    4> 通过 ApiDemos-debug.apk这个app,来举例使用UiSelector方式定位

from appium import webdriver

class TestApiDemo():
    def setup(self):
        #参数设置
        caps = {}
        caps["platformName"] = "android"
        caps["deviceName"] = "qiucaixia"
        caps["appPackage"] = "io.appium.android.apis"
        caps["appActivity"] = ".ApiDemos"

        self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
        self.driver.implicitly_wait(10)  #隐式等待

    def test_toast(self):
        self.driver.find_element_by_accessibility_id("Views").click() #点击Views

        self.driver.find_element_by_android_uiautomator\
            ('new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text("Popup Menu").instance(0));'
             ).click()  #滑动屏幕到某个元素可见处,再点击该元素

        # self.driver.swipe()
        # self.driver.find_element_by_accessibility_id('Popup Menu').click()  #点击Popup Menu

        self.driver.find_element_by_accessibility_id('Make a Popup!').click()  #点击 Make a Popup
        self.driver.find_element_by_xpath("//*[@text='Add']").click()  #点击Add
        print(self.driver.find_element_by_xpath("//*[@class='android.widget.Toast']").text)

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

   执行结果:

 8.  断言内容:

      1> 元素是否存在: find_elements

      2> 元素属性正确性:get_attribute,可以获取的属性有

           a> checkable、checked

           b> clickable

           c> class

           d> content-desc

           e> enabled

            f> focusable、focused

           g> long-clickable

           h> package

           i> password

           j> resource-id

           k> scrollable

           l> selection-start

          m> selection-end

          n> selected

          o> text

          p> bounds

          q> index

          r> displayed

          s> contentSize

9. 断言属性举例:

 1 from appium import webdriver
 2 
 3 class TestDemo():
 4     def setup(self):
 5         #参数设置
 6         caps = {}
 7         caps["platformName"] = "android"
 8         caps["deviceName"] = "qiucaixia"
 9         caps["appPackage"] = "com.xueqiu.android"
10         caps["appActivity"] = ".view.WelcomeActivityAlias"
11         caps["autoGrantPermissions"] = "true"   #自动赋予app权限
12         caps["unicodeKeyboard"]="true"  #支持输入中文
13 
14         self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
15         self.driver.implicitly_wait(10)  #隐式等待
16     def test_search(self):
17         #点击搜索栏
18         self.driver.find_element_by_id("com.xueqiu.android:id/home_search").click()
19         #输入搜索内容
20         self.driver.find_element_by_id("com.xueqiu.android:id/search_input_text").send_keys('pdd')
21         self.driver.find_element_by_id("name").click()
22         price=self.driver.find_element_by_id("current_price")
23         assert float(price.text) > 20
24         #断言属性- package, resource-id
25         assert 'com.xueqiu.android' in  price.get_attribute('package')
26         assert  'price' in price.get_attribute('resource-id')
27 
28     def teardown(self):
29         self.driver.quit()
断言属性

 10. Hamcrest断言

     1> 官网: http://hamcrest.org/

     2> 安装hamrest: pip install pyhamcrest

 11. Hamcrest断言举例

from appium import webdriver
from hamcrest import  *

class TestDemo():
    def setup(self):
        #参数设置
        caps = {}
        caps["platformName"] = "android"
        caps["deviceName"] = "qiucaixia"
        caps["appPackage"] = "com.xueqiu.android"
        caps["appActivity"] = ".view.WelcomeActivityAlias"
        caps["autoGrantPermissions"] = "true"   #自动赋予app权限
    
        self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
        self.driver.implicitly_wait(10)  #隐式等待

    def test_search(self):
        #点击搜索栏
        self.driver.find_element_by_id("com.xueqiu.android:id/home_search").click()
        #输入搜索内容
        self.driver.find_element_by_id("com.xueqiu.android:id/search_input_text").send_keys('pdd')
        self.driver.find_element_by_id("name").click()
        price=self.driver.find_element_by_id("current_price")
        assert float(price.text) > 20
        #断言属性- resource-id
        assert 'price' in price.get_attribute('resource-id')

        #hamcrest断言
        assert_that(price.get_attribute('package'),equal_to('com.xueqiu.android'))

    def teardown(self):
        self.driver.quit()
Hamcrest断言

 

posted @ 2020-03-31 13:12  宁静~朝夕  阅读(217)  评论(0)    收藏  举报