androidpythonautomated-testsandroidviewclient

How to find out the state of a checkbox using AndroidViewClient


I'm using androidviewclient for automated black-box level testing of an android GUI, I have two checkboxes (YES and NO) with YES checked by default, I use vc.touch() to select NO and I need to know whether NO has been selected.The way I do it now is by taking screenshot before and after a touch and compare them. Is there any easier way to find the state of the button/check-box. It seems touch() is a void function outputting nothing.


Solution

  • As an example, I'm using Clock and the Vibrate checkbox in Alarms

    enter image description here

    Then, I generated the script using culebra and manually added the assert at the end (it could be done in one line but I added 2 for clarity)

    #! /usr/bin/env python
    # -*- coding: utf-8 -*-
    '''
    Copyright (C) 2013-2016  Diego Torres Milano
    Created on 2016-06-03 by Culebra v11.5.6
                          __    __    __    __
                         /  \  /  \  /  \  /  \ 
    ____________________/  __\/  __\/  __\/  __\_____________________________
    ___________________/  /__/  /__/  /__/  /________________________________
                       | / \   / \   / \   / \   \___
                       |/   \_/   \_/   \_/   \    o \ 
                                               \_____/--<
    @author: Diego Torres Milano
    @author: Jennifer E. Swofford (ascii art snake)
    '''
    
    
    import re
    import sys
    import os
    
    
    import unittest
    
    from com.dtmilano.android.viewclient import ViewClient, CulebraTestCase
    
    TAG = 'CULEBRA'
    
    
    class CulebraTests(CulebraTestCase):
    
        @classmethod
        def setUpClass(cls):
            cls.kwargs1 = {'ignoreversioncheck': False, 'verbose': False, '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': False, 'use-regexps': False, 'null-back-end': False, 'auto-regexps': None, 'do-not-verify-screen-dump': True, 'verbose-comments': False, 'gui': True, 'find-views-with-text': True, 'prepend-to-sys-path': False, 'install-apk': None, 'drop-shadow': False, 'output': '/home/diego/tmp/culebra/checbox.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.vc.dump(window=-1)
            checkbox = self.vc.findViewWithTextOrRaise(u'Vibrate')
            self.assertTrue(checkbox.isChecked())
    
    if __name__ == '__main__':
        CulebraTests.main()