pipshowuiautomator2Name:uiautomator2Version:1.2.2Summary:PythonWrapperforAndroidUiAutomator2testtoolHome-page:https://github.com/codeskyblue/uiautomator2Author:codeskyblueAuthor-ema" />

亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

python uiautomator2 watcher的使用方法

系統 1526 0

該方是基于uiautomator2如下版本進行驗證的:

            PS C:\windows\system32> pip show uiautomator2
Name: uiautomator2
Version: 1.2.2
Summary: Python Wrapper for Android UiAutomator2 test tool
Home-page: https://github.com/codeskyblue/uiautomator2
Author: codeskyblue
Author-email: codeskyblue@gmail.com
License: MIT
Location: c:\program files\python36\lib\site-packages
Requires: six, progress, whichcraft, logzero, lxml, adbutils, retry, Pillow, requests, humanize
Required-by: weditor, atx

          

  下面貼出githup上關于該方法的使用

            
               1
            
            
              Watcher

            
            
               2
            
             You can register watchers to perform some actions when a selector does 
            
              not
            
            
               find a match.

            
            
               3
            
            
               4
            
            
              Register Watcher

            
            
               5
            
            
               6
            
             When a selector can 
            
              not
            
            
               find a match, uiautomator2 will run all registered watchers.

            
            
               7
            
            
               8
            
            
              Click target when conditions match

            
            
               9
            
             d.watcher(
            
              "
            
            
              AUTO_FC_WHEN_ANR
            
            
              "
            
            ).when(text=
            
              "
            
            
              ANR
            
            
              "
            
            ).when(text=
            
              "
            
            
              Wait
            
            
              "
            
            
              ) \

            
            
              10
            
                                          .click(text=
            
              "
            
            
              Force Close
            
            
              "
            
            
              )

            
            
              11
            
            
              #
            
            
               d.watcher(name) ## creates a new named watcher.
            
            
              12
            
            
              #
            
            
                .when(condition)  ## the UiSelector condition of the watcher.
            
            
              13
            
            
              #
            
            
                .click(target)  ## perform click action on the target UiSelector.
            
            
              14
            
             There 
            
              is
            
            
               also a trick about click. You can use click without arguments.

            
            
              15
            
            
              16
            
             d.watcher(
            
              "
            
            
              ALERT
            
            
              "
            
            ).when(text=
            
              "
            
            
              OK
            
            
              "
            
            
              ).click()

            
            
              17
            
            
              #
            
            
               Same as
            
            
              18
            
             d.watcher(
            
              "
            
            
              ALERT
            
            
              "
            
            ).when(text=
            
              "
            
            
              OK
            
            
              "
            
            ).click(text=
            
              "
            
            
              OK
            
            
              "
            
            
              )

            
            
              19
            
            
              Press key when a condition becomes true

            
            
              20
            
             d.watcher(
            
              "
            
            
              AUTO_FC_WHEN_ANR
            
            
              "
            
            ).when(text=
            
              "
            
            
              ANR
            
            
              "
            
            ).when(text=
            
              "
            
            
              Wait
            
            
              "
            
            
              ) \

            
            
              21
            
                                          .press(
            
              "
            
            
              back
            
            
              "
            
            , 
            
              "
            
            
              home
            
            
              "
            
            
              )

            
            
              22
            
            
              #
            
            
               d.watcher(name) ## creates a new named watcher.
            
            
              23
            
            
              #
            
            
                .when(condition)  ## the UiSelector condition of the watcher.
            
            
              24
            
            
              #
            
            
                .press(
              
                , ..., 
                
                  .()  ## press keys one by one in sequence.
                
              
            
            
              25
            
             Check 
            
              if
            
            
               the named watcher triggered

            
            
              26
            
            
              27
            
             A watcher 
            
              is
            
             triggered, which means the watcher was run 
            
              and
            
            
               all its conditions matched.

            
            
              28
            
            
              29
            
             d.watcher(
            
              "
            
            
              watcher_name
            
            
              "
            
            
              ).triggered

            
            
              30
            
            
              #
            
            
               true in case of the specified watcher triggered, else false
            
            
              31
            
            
              Remove a named watcher

            
            
              32
            
            
              33
            
            
              #
            
            
               remove the watcher
            
            
              34
            
             d.watcher(
            
              "
            
            
              watcher_name
            
            
              "
            
            
              ).remove()

            
            
              35
            
            
              List all watchers

            
            
              36
            
            
              37
            
            
              d.watchers

            
            
              38
            
            
              #
            
            
               a list of all registered watchers
            
            
              39
            
             Check 
            
              for
            
            
               any triggered watcher

            
            
              40
            
            
              41
            
            
              d.watchers.triggered

            
            
              42
            
            
              #
            
            
                true in case of any watcher triggered
            
            
              43
            
            
              Reset all triggered watchers

            
            
              44
            
            
              45
            
            
              #
            
            
               reset all triggered watchers, after that, d.watchers.triggered will be false.
            
            
              46
            
            
              d.watchers.reset()

            
            
              47
            
            
              Remove watchers

            
            
              48
            
            
              49
            
            
              #
            
            
               remove all registered watchers
            
            
              50
            
            
              d.watchers.remove()

            
            
              51
            
            
              #
            
            
               remove the named watcher, same as d.watcher("watcher_name").remove()
            
            
              52
            
             d.watchers.remove(
            
              "
            
            
              watcher_name
            
            
              "
            
            
              )

            
            
              53
            
            
              Force to run all watchers

            
            
              54
            
            
              55
            
            
              #
            
            
               force to run all registered watchers
            
            
              56
            
             d.watchers.run()
          

注:里面涉及的watcher_name可以自定義,可以做到見名知意即可

watcher的使用是要先注冊(第9行至20行均是注冊watcher的方法),然后激活watcher(第56行),注意這個激活方法只是一個瞬時激活,就是說使用之后即銷毀,不會一直存于后臺。那這樣的話在實際的使用場景中怎么使用這個功能呢,下面看一段腳本 ?1 # -*- coding:utf-8 -*-

            
              
                 2
              
              
                 3
              
              
                import
              
              
                 uiautomator2 as u2

              
              
                 4
              
              
                import
              
              
                 time

              
              
                 5
              
              
                 6
              
              
                 7
              
               d =
              
                 u2.connect()

              
              
                 8
              
               cfg =
              
                 MTBFConfig()

              
              
                 9
              
               package = cfg.getstr(
              
                "
              
              
                Admit
              
              
                "
              
              , 
              
                "
              
              
                pkg
              
              
                "
              
              , 
              
                "
              
              
                config
              
              
                "
              
              
                )

              
              
                10
              
               PACKAGELIST = package.split(
              
                "
              
              
                ,
              
              
                "
              
              
                )

              
              
                11
              
              
                print
              
              
                (PACKAGELIST)

              
              
                12
              
              
                
                  d.watcher("????????????????????????????????????????????????????????????????????????????????????????????????ALLOW??????").when(text="????????????????????????????????????????????????????????????????????????????????????????????????ALLOW??????").click(text="????????????????????????????????????????????????????????????????????????????????????????????????ALLOW??????")

                
              
              
                13
              
              
                #
              
              
                d.watchers.run()
              
              
                14
              
              
                print
              
              
                (d.watchers)

              
              
                15
              
              
                16
              
               time.sleep(2
              
                )

              
              
                17
              
               pkglen =
              
                 len(PACKAGELIST)

              
              
                18
              
              
                print
              
              ((
              
                "
              
              
                There are %d package for test
              
              
                "
              
              ) %
              
                pkglen)

              
              
                19
              
              
                20
              
              
                class
              
              
                 Admit(object):

              
              
                21
              
              
                22
              
              
                def
              
              
                 main(self):

              
              
                23
              
              
                for
              
               i 
              
                in
              
              
                 range(pkglen):

              
              
                24
              
                           k =
              
                 0

              
              
                25
              
              
                for
              
               j 
              
                in
              
               range(5
              
                ):

              
              
                26
              
              
                if
              
               d.info[
              
                '
              
              
                currentPackageName
              
              
                '
              
              ] !=
              
                 PACKAGELIST[i]:

              
              
                27
              
              
                                    d.app_start(PACKAGELIST[i])

              
              
                28
              
              
                print
              
              
                (PACKAGELIST[i])

              
              
                29
              
                                   time.sleep(1
              
                )

              
              
                30
              
                                   k += 1

              
                31
              
              
                if
              
               k == 3
              
                :

              
              
                32
              
              
                print
              
              (
              
                "
              
              
                Can not enter 
              
              
                "
              
              +
              
                 str(PACKAGELIST[i]))

              
              
                33
              
              
                return
              
              
                 False

              
              
                34
              
              
                if
              
               PACKAGELIST[i] == 
              
                '
              
              
                com.google.android.contacts
              
              
                '
              
              
                :

              
              
                35
              
              
                print
              
              (
              
                "
              
              
                hello
              
              
                "
              
              
                )

              
              
                36
              
              
                if
              
               d(description = 
              
                "
              
              
                Open navigation drawer
              
              
                "
              
              ).exists(timeout = 5
              
                ):

              
              
                37
              
                                   d(description = 
              
                "
              
              
                Open navigation drawer
              
              
                "
              
              
                ).click()

              
              
                38
              
              
                39
              
              
                if
              
               d(text = 
              
                "
              
              
                Settings
              
              
                "
              
              ).exists(timeout = 5
              
                ):

              
              
                40
              
                                   d(text = 
              
                "
              
              
                Settings
              
              
                "
              
              
                ).click()

              
              
                41
              
              
                42
              
              
                if
              
               d(resourceId=
              
                "
              
              
                android:id/title
              
              
                "
              
              , text = 
              
                "
              
              
                Import
              
              
                "
              
              ).exists(timeout=5
              
                ):

              
              
                43
              
                                   d(resourceId=
              
                "
              
              
                android:id/title
              
              
                "
              
              , text = 
              
                "
              
              
                Import
              
              
                "
              
              
                ).click()

              
              
                44
              
                                   time.sleep(3
              
                )

              
              
                45
              
              
                46
              
              
                if
              
               d(resourceId = 
              
                "
              
              
                android:id/button1
              
              
                "
              
              , text = 
              
                "
              
              
                OK
              
              
                "
              
              ).exists(timeout = 5
              
                ):

              
              
                47
              
                                   d(resourceId = 
              
                "
              
              
                android:id/button1
              
              
                "
              
              , text = 
              
                "
              
              
                OK
              
              
                "
              
              
                ).click()

              
              
                48
              
                                   time.sleep(1
              
                )

              
              
                49
              
              
                
                                      d.watchers.run()  //在上面OK點擊之后會彈出一個權限訪問的許可,所以這個時候需要激活一次watcher把彈框關掉,以便不影響后續測試,所以就一個原則,哪里可能會有彈框就在哪里激活watcher

                
              
              
                50
              
              
                51
              
              
                52
              
              
                53
              
              
                if
              
              
                __name__
              
              ==
              
                "
              
              
                __main__
              
              
                "
              
              
                :

              
              
                54
              
                   ad =
              
                 Admit()

              
              
                55
              
                   ad.main()
            
          

?


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 欧美一级别| 国产精品6 | 国产精品一区二区不卡 | 亚洲欧美日韩国产一区图片 | 欧美日韩成人在线观看 | 日韩欧美福利 | 俺去鲁婷婷六月色综合 | 色狠狠婷婷97 | 国产久视频观看 | 国产青草视频免费观看97 | 国产亚洲99影院 | 四虎久久久 | 大伊香蕉在线精品不卡视频 | 色偷偷女人的天堂a在线 | 欧美视频免费在线播放 | 国内精品视频一区 | 午夜影院在线 | jizz国产精品免费麻豆 | 国产精品福利视频主播真会玩 | 亚洲精品视频久久 | 最新亚洲国产有精品 | 人人狠狠综合久久亚洲88 | 亚洲成人免费视频在线 | 亚洲精品高清国产一久久 | 97夜夜操 | 四虎影院永久免费 | 亚洲一区二区三区久久久久 | 亚洲久久影院 | 宅男在线影院 | 久久99综合国产精品亚洲首页 | 午夜影院毛片 | 免费在线毛片 | 天天射天天色天天干 | 国产精品九九视频 | 久久91精品国产91久久跳舞 | 久久香蕉国产线看免费 | 黄色综合网 | 国产91精品久久久久久久 | 26uuu欧美视频在线观看 | 国产精品亚洲综合一区在线观看 | 日韩精品片|