UIAutomator使用android timepicker

时间:2016-02-17 13:09:52

标签: android uiautomator ui-testing

我正在尝试为Android时间选择器执行UI测试。

我使用的espresso无法连接到此类型的组件,根据文档我应该使用UIAutomator。我可以使用以下API

来捕获“确定/取消”按钮
mDevice.findObject(new UiSelector().text("OK")).click();

但是,当我尝试设置时间和小时时,我会继续UiObjectNotFoundException

有没有人设法使用此API设置时间? 感谢

enter image description here

1 个答案:

答案 0 :(得分:0)

我将举例说明如何使用AndroidViewClient/culebra来实现这一点,Culebra GUI反过来间接使用UiAutomator。

我开始使用culebra生成一个基本测试脚本,我后来将其自定义(设备与您在屏幕截图中显示的屏幕相同)。遗憾的是,Inheritance and Overriding __init__ in python并未产生如此复杂的测试,但自动生成的测试是一个非常好的起点。

$ culebra -d true -i true -CVLU -o ~/tmp/time-picker.py

您可以找到有关执行culebra --help的选项的更多信息,但基本上-d告诉它使用内容描述,-i使用ID以及输出文件后面的一些日志记录选项。

然后我编辑了自动生成的文件,看起来像这样(我添加了一些注释,使其自我解释)

#! /usr/bin/env python
# -*- coding: utf-8 -*-
'''
Copyright (C) 2013-2014  Diego Torres Milano
Created on 2016-02-19 by Culebra v11.5.1
                      __    __    __    __
                     /  \  /  \  /  \  /  \ 
____________________/  __\/  __\/  __\/  __\_____________________________
___________________/  /__/  /__/  /__/  /________________________________
                   | / \   / \   / \   / \   \___
                   |/   \_/   \_/   \_/   \    o \ 
                                           \_____/--<
@author: Diego Torres Milano
@author: Jennifer E. Swofford (ascii art snake)
'''


import re
import sys
import os


import unittest


try:
    sys.path.insert(0, os.path.join(os.environ['ANDROID_VIEW_CLIENT_HOME'], 'src'))
except:
    pass

from com.dtmilano.android.viewclient import ViewClient, CulebraTestCase

TAG = 'CULEBRA'


class CulebraTests(CulebraTestCase):

    @classmethod
    def setUpClass(cls):
        cls.kwargs1 = {'ignoreversioncheck': False, 'verbose': True, 'ignoresecuredevice': False}
        cls.kwargs2 = {'forceviewserveruse': False, 'useuiautomatorhelper': False, 'ignoreuiautomatorkilled': True, 'autodump': False, 'startviewserver': True, 'compresseddump': True}
        cls.options = {'start-activity': None, 'concertina': False, 'device-art': None, 'use-jar': False, 'multi-device': False, 'unit-test-class': True, 'save-screenshot': None, 'use-dictionary': False, 'glare': False, 'dictionary-keys-from': 'id', 'scale': 0.5, 'find-views-with-content-description': True, 'window': -1, 'orientation-locked': None, 'save-view-screenshots': None, 'find-views-by-id': True, 'log-actions': True, 'use-regexps': False, 'null-back-end': False, 'auto-regexps': None, 'do-not-verify-screen-dump': False, 'verbose-comments': True, 'gui': False, 'find-views-with-text': True, 'prepend-to-sys-path': True, 'install-apk': None, 'drop-shadow': False, 'output': '/home/diego/tmp/time-picker.py', 'unit-test-method': None, 'interactive': False}
        cls.sleep = 5

    def setUp(self):
        super(CulebraTests, self).setUp()

    def tearDown(self):
        super(CulebraTests, self).tearDown()

    def preconditions(self):
        if not super(CulebraTests, self).preconditions():
            return False
        return True

    def testSomething(self):
        if not self.preconditions():
            self.fail('Preconditions failed')

        _s = CulebraTests.sleep
        _v = CulebraTests.verbose

        self.device.Log.d(TAG, "dumping content of window='-1'",  _v)
        self.vc.dump(window='-1')


        # class=android.widget.TextView  text=u'10'
        self.device.Log.d(TAG, "finding view with id=android:id/hours",  _v)
        android___id_hours = self.vc.findViewByIdOrRaise("android:id/hours")

        # class=android.widget.TextView  text=u'30'
        self.device.Log.d(TAG, "finding view with id=android:id/minutes",  _v)
        android___id_minutes = self.vc.findViewByIdOrRaise("android:id/minutes")

        # class=android.view.View
        #self.device.Log.d(TAG, "finding view with id=android:id/radial_picker",  _v)
        #android___id_radial_picker = self.vc.findViewByIdOrRaise("android:id/radial_picker")

        # class=android.widget.RadialTimePickerView$RadialPickerTouchHelper  cd='0'
        hs = {}
        for h in range(0, 24):
            self.device.Log.d(TAG, "finding view with cd %s" % h,  _v)
            hs[h] = self.vc.findViewWithContentDescriptionOrRaise(u'''%s''' % h)

        # class=android.widget.Button  text=u'Cancel'
        self.device.Log.d(TAG, "finding view with text Cancel",  _v)
        android___id_button2 = self.vc.findViewWithTextOrRaise(u'Cancel')

        # class=android.widget.Button  text=u'OK'
        self.device.Log.d(TAG, "finding view with text OK",  _v)
        android___id_button1 = self.vc.findViewWithTextOrRaise(u'OK')

        # Set time (for example 12:15)
        self.device.Log.d(TAG, "setting hour to 12",  _v)
        hs[12].touch()

        # Verify hour
        self.device.Log.d(TAG, "dumping content of window='-1'",  _v)
        self.vc.dump(window='-1')

        # class=android.widget.TextView  text=u'12'
        self.device.Log.d(TAG, "finding view with id=android:id/hours",  _v)
        android___id_hours = self.vc.findViewByIdOrRaise("android:id/hours")

        self.device.Log.d(TAG, "checking hour set to 12",  _v)
        self.assertEqual(android___id_hours.getText(), u'12')

        # update
        self.device.Log.d(TAG, "dumping content of window='-1'",  _v)
        self.vc.dump(window='-1')


        ms = {}
        for m in range(0, 60, 5):
            m0 = '%02d' % m
            self.device.Log.d(TAG, "finding view with cd %s" % m0,  _v)
            ms[m] = self.vc.findViewWithContentDescriptionOrRaise(u'''%s''' % m)

        ms[15].touch()

        # Verify minute
        self.device.Log.d(TAG, "dumping content of window='-1'",  _v)
        self.vc.dump(window='-1')

        # class=android.widget.TextView  text=u'15'
        self.device.Log.d(TAG, "finding view with id=android:id/minutes",  _v)
        android___id_minutes = self.vc.findViewByIdOrRaise("android:id/minutes")
        self.assertEqual(android___id_minutes.getText(), u'15')


        self.device.Log.d(TAG, "finding view with text Cancel",  _v)
        android___id_button2 = self.vc.findViewWithTextOrRaise(u'Cancel')

        # let's cancel, so time is not set
        android___id_button2.touch()

if __name__ == '__main__':
    CulebraTests.main()

测试有以下主要部分:

  1. 验证起始点是时间选择器(如果没有则会失败)
  2. 获取所有小时视图
  3. 将小时设置为12
  4. 验证已设置12
  5. 获取所有分钟视图
  6. 将分钟设置为15
  7. 验证已设置15
  8. 触摸取消以避免设置时间
  9. 由于日志记录选项,当您使用-v

    运行时,会收到此输出
    $ ~/tmp/time-picker.py -v
    
    testSomething (__main__.CulebraTests) ... Connecting to a device with serialno=.* with a timeout of 60 secs...
    Connected to device with serialno=.*
    Actual device serialno=XXXXXXXXXXX
    CULEBRA: dumping content of window='-1'
    CULEBRA: finding view with id=android:id/hours
    CULEBRA: finding view with id=android:id/minutes
    CULEBRA: finding view with cd 0
    CULEBRA: finding view with cd 1
    CULEBRA: finding view with cd 2
    CULEBRA: finding view with cd 3
    CULEBRA: finding view with cd 4
    CULEBRA: finding view with cd 5
    CULEBRA: finding view with cd 6
    CULEBRA: finding view with cd 7
    CULEBRA: finding view with cd 8
    CULEBRA: finding view with cd 9
    CULEBRA: finding view with cd 10
    CULEBRA: finding view with cd 11
    CULEBRA: finding view with cd 12
    CULEBRA: finding view with cd 13
    CULEBRA: finding view with cd 14
    CULEBRA: finding view with cd 15
    CULEBRA: finding view with cd 16
    CULEBRA: finding view with cd 17
    CULEBRA: finding view with cd 18
    CULEBRA: finding view with cd 19
    CULEBRA: finding view with cd 20
    CULEBRA: finding view with cd 21
    CULEBRA: finding view with cd 22
    CULEBRA: finding view with cd 23
    CULEBRA: finding view with text Cancel
    CULEBRA: finding view with text OK
    CULEBRA: setting hour to 12
    CULEBRA: dumping content of window='-1'
    CULEBRA: finding view with id=android:id/hours
    CULEBRA: checking hour set to 12
    CULEBRA: dumping content of window='-1'
    CULEBRA: finding view with cd 00
    CULEBRA: finding view with cd 05
    CULEBRA: finding view with cd 10
    CULEBRA: finding view with cd 15
    CULEBRA: finding view with cd 20
    CULEBRA: finding view with cd 25
    CULEBRA: finding view with cd 30
    CULEBRA: finding view with cd 35
    CULEBRA: finding view with cd 40
    CULEBRA: finding view with cd 45
    CULEBRA: finding view with cd 50
    CULEBRA: finding view with cd 55
    CULEBRA: dumping content of window='-1'
    CULEBRA: finding view with id=android:id/minutes
    CULEBRA: finding view with text Cancel
    ok
    
    ----------------------------------------------------------------------
    Ran 1 test in 21.399s
    
    OK
    

    希望这可以帮助你