class Mobile():
    """
    """

    def __init__(self, android_serial = None):
#         logger.info("Importing Android library")
#         print "Importing Android library"
#         clm.message("Importing Android library")
        self.adb = ADB(android_serial)
        self.device = Device(android_serial)
        self.test_helper = TestHelper(android_serial)

    def set_serial(self, android_serial):
        """
        Set device serial
        """
        self.adb = ADB(android_serial)
        self.device = Device(android_serial)
        self.test_helper = TestHelper(android_serial)

    def get_info(self):
        """
        Retrieve the device info
        """
        return self.device.info

#Key Event Actions of the device
    """
    Turn on/off screen
    """
    def turn_on_screen(self):
        """
        Turn on screen
        """
        self.device.screen.on()

    def turn_off_screen(self):
        """
        Turn off screen
        """
        self.device.screen.off()

    def wakeup_the_device(self):
        """
        wakeup the device
        """
        self.device.wakeup()

    """
    Press hard/soft key
    """

    def press_key(self, *key):
        """
        press *key* keycode
        """
        self.device.press(*key)

    def press_home(self):
        """
        press home key
        """
        self.device.press.home()

    def press_back(self):
        """
        press back key
        """
        self.device.press.back()

    def press_left(self):
        """
        press left key
        """
        self.device.pres.left()

    def press_right(self):
        """
        press right key
        """
        self.device.press.right()

    def press_up(self):
        """
        press up key
        """
        self.device.press.up()

    def press_down(self):
        """
        press down key
        """
        self.device.press.down()

    def press_center(self):
        """
        press center key
        """
        self.device.press.center()

    def press_menu(self):
        """
        press menu key
        """
        self.device.press.menu()

    def press_search(self):
        """
        press search key
        """
        self.device.press.search()

    def press_enter(self):
        """
        press enter key
        """
        self.device.press.enter()

    def press_delete(self):
        """
        press delete key
        """
        self.device.press.delete()

    def press_recent(self):
        """
        press recent key
        """
        self.device.press.recent()

    def press_volume_up(self):
        """
        press volume up key
        """
        self.device.press.volume_up()

    def press_volume_down(self):
        """
        press volume down key
        """
        self.device.press.volume_down()

    def press_camera(self):
        """
        press camera key
        """
        self.device.press.camera()

    def press_power(self):
        """
        press power key
        """
        self.device.press.power()

#Gesture interaction of the device

    def click(self, x, y):
        """
        click (x, y) on screen
        """
        self.device.click(x, y)

    def swipe(self, sx, sy, ex, ey, steps=10):
        """
        swipe from (sx, sy) to (ex, ey) with steps
        """
        self.device.swipe(sx, sy, ex, ey, steps)

# Swipe from the center of the ui object to its edge

    def swipe_left(self, obj, steps=10):
        """
        swipe the *obj* from center to left
        """
        obj.swipe.left(steps=steps)

    def swipe_right(self, obj, steps=10):
        """
        swipe the *obj* from center to right
        """
        obj.swipe.right(steps=steps)

    def swipe_top(self, obj, steps=10):
        """
        swipe the *obj* from center to top
        """
        obj.swipe.top(steps=steps)

    def swipe_bottom(self, obj, steps=10):
        """
        swipe the *obj* from center to bottom
        """
        obj.swipe.bottom(steps=steps)

    def drag(self,sx, sy, ex, ey, steps=10):
        """
        drag from (sx, sy) to (ex, ey) with steps
        """
        self.device.drag(sx, sy, ex, ey, steps)

    #Wait until the specific ui object appears or gone

    # wait until the ui object appears
    def wait_for_exists(self, timeout=0, *args, **attribute):
        """
        true means the object which has *attribute* exist
        false means the object does not exist
        in the given timeout
        """
        return self.device(**attribute).wait.exists(timeout=timeout)

    # wait until the ui object gone
    def wait_until_gone(self, timeout=0, *args, **attribute):
        """
        true means the object which has *attribute* disappear
        false means the object exist
        in the given timeout
        """
        return self.device(**attribute).wait.gone(timeout=timeout)

    def wait_for_object_exists(self, obj, timeout=0):
        """
        true means the object exist
        false means the object does not exist
        in the given timeout
        """
        return obj.wait.exists(timeout=timeout)

    # wait until the ui object gone
    def wait_until_object_gone(self, obj, timeout=0):
        """
        true means the object disappear
        false means the object exist
        in the given timeout
        """
        return obj.wait.gone(timeout=timeout)


    # Perform fling on the specific ui object(scrollable)
    def fling_forward_horizontally(self, obj):
        """
        return whether the object can be fling or not
        """
        return obj.fling.horiz.forward()

    def fling_backward_horizontally(self, obj):
        """
        return whether the object can be fling or not
        """
        return obj.fling.horiz.backward()

    def fling_forward_vertically(self, obj):
        """
        return whether the object can be fling or not
        """
        return obj.fling.vert.forward()

    def fling_backward_vertically(self, obj):
        """
        return whether the object can be fling or not
        """
        return obj.fling.vert.backward()

    # Perform scroll on the specific ui object(scrollable)

    def scroll_to_beginning_vertically(self, obj, steps=10):
        """
        """
        return obj.scroll.vert.toBeginning(steps=steps)

    def scroll_forward_horizontally(self, obj, steps=10):
        """
        return whether the object can be fling or not
        """
        return obj.scroll.horiz.forward(steps=steps)

    def scroll_backward_horizontally(self, obj, steps=10):
        """
        return whether the object can be fling or not
        """
        return obj.scroll.horiz.backward(steps=steps)

    def scroll_to_horizontally(self, obj, *args,**attribute):
        """
        return whether the object can be fling or not
        """
        return obj.scroll.horiz.to(**attribute)

    def scroll_forward_vertically(self, obj, steps=10):
        """
        return whether the object can be fling or not
        """
        return obj.scroll.vert.forward(steps=steps)

    def scroll_backward_vertically(self, obj, steps=10):
        """
        return whether the object can be fling or not
        """
        return obj.scroll.vert.backward(steps=steps)

    def scroll_to_vertically(self, obj, *args, **attribute):
        """
        return whether the object can be fling or not
        """
        msg = ''
        for key, value in attribute.iteritems():
            msg += ' %s=%s ' % (key, value)
        print msg
#         print 'args:' + args[0]
        return obj.scroll.vert.to(**attribute)

#Screen Actions of the device

    def screenshot(self, scale=None, quality=None):
        """
        Take a screenshot of device and log in the report with timestamp
        """
        output_dir = BuiltIn().get_variable_value('${OUTPUTDIR}')
        ts = time.time()
        st = datetime.datetime.fromtimestamp(ts).strftime('%Y%m%d%H%M%S')
        screenshot_path = '%s%s%s.png' % (output_dir, os.sep, st)
        self.device.screenshot(screenshot_path, scale, quality)
        logger.info('\n<a href="%s">%s</a><br><img src="%s">' % (screenshot_path, st, screenshot_path), html=True)

#Watcher
#     def register_click_watcher(self, watcher_name, attributes, *condition_list):
#         """
#         The watcher click on the object which has the attributes when conditions match
#         """
#         print type(attributes)
#         watcher = self.device.watcher(watcher_name)
#         for condition in condition_list:
#             watcher.when(**condition)
#         watcher.click(**attributes)
#         self.device.watchers.run()
#         print 'register watcher:%s' % watcher_name
#         return

    def __unicode_to_dict(self, a_unicode):
        a_dict = dict()
        dict_item_count = a_unicode.count('=')
        for count in range(dict_item_count):
            equal_sign_position = a_unicode.find('=')
            comma_position = a_unicode.find(',')
            a_key = a_unicode[0:equal_sign_position]
            if comma_position == -1:
                a_value = a_unicode[equal_sign_position + 1:]
            else:
                a_value = a_unicode[equal_sign_position + 1:comma_position]
                a_unicode = a_unicode[comma_position + 1:]
            a_dict[a_key] = a_value
        return a_dict

    def register_click_watcher(self, watcher_name, attributes, *condition_list):
        """
        The watcher click on the object which has the *attributes* when conditions match
        """
        watcher = self.device.watcher(watcher_name)
        for condition in condition_list:
            watcher.when(**self.__unicode_to_dict(condition))
        watcher.click(**self.__unicode_to_dict(attributes))
        self.device.watchers.run()

    def register_press_watcher(self, watcher_name, press_keys, *condition_list):
        """
        The watcher perform *press_keys* action sequentially when conditions match
        """
        def unicode_to_list(a_unicode):
            a_list = list()
            comma_count = a_unicode.count(',')
            for count in range(comma_count + 1):
                comma_position = a_unicode.find(',')
                if comma_position == -1:
                    a_list.append(str(a_unicode))
                else:
                    a_list.append(a_unicode[0:comma_position])
                    a_unicode = a_unicode[comma_position + 1:]
            return a_list

        watcher = self.device.watcher(watcher_name)
        for condition in condition_list:
            watcher.when(**self.__unicode_to_dict(condition))
        watcher.press(*unicode_to_list(press_keys))
        self.device.watchers.run()

    def remove_watchers(self, watcher_name = None):
        """
        remove watcher with *watcher_name* or remove all watchers
        """
        if watcher_name == None:
            self.device.watchers.remove()
        else:
            self.device.watchers.remove(watcher_name)

    def list_all_watchers(self):
        """
        return the watcher list
        """
        return self.device.watchers

#Selector

    def get_object(self, *args, **attribute):
        """
        get the ui object with attribute *attribute*
        """
        msg = ''
        for key, value in attribute.iteritems():
            msg += ' %s=%s ' % (key, value)
        print msg
        return self.device(*args, **attribute)

    def get_info_of_object(self, obj):
        """
        return info dictionary of the *obj*
        The info example:
        {
         u'contentDescription': u'',
         u'checked': False,
         u'scrollable': True,
         u'text': u'',
         u'packageName': u'com.android.launcher',
         u'selected': False,
         u'enabled': True,
         u'bounds': 
                   {
                    u'top': 231,
                    u'left': 0,
                    u'right': 1080,
                    u'bottom': 1776
                   },
         u'className': u'android.view.View',
         u'focusable': False,
         u'focused': False,
         u'clickable': False,
         u'checkable': False,
         u'chileCount': 1,
         u'longClickable': False,
         u'visibleBounds':
                          {
                           u'top': 231,
                           u'left': 0,
                           u'right': 1080,
                           u'bottom': 1776
                          }
        }
        """
        return obj.info

    def click_on(self, *args, **attribute):
        """
        click on the object with *attribute*
        """
        self.device(**attribute).click()

    def long_click_on(self, *args, **attribute):
        """
        click on the object with *attribute*
        """
        self.device(**attribute).long_click()

    def call(self, obj, method, *args, **attribute):
        func = getattr(obj, method)
        return func(**attribute)

    def set_text(self, text, *args, **attribute):
        """
        set *text* to the Component which has the *attribute* 
        """
        self.device(**attribute).set_text(text)

    def clear_text(self, *args, **attributes):
        """
        Clear text of the component  with *attributes*
        """
        while True:
            target = self.device(**attributes)
            text = target.info['text']
            target.clear_text()
            remain_text = target.info['text']
            if text == ''  or remain_text == text:
                break

# Other feature

    def sleep(self, time):
        """
        sleep(no action) for *time* (in millisecond)
        """
        target = 'wait for %s' % str(time)
        self.device(text=target).wait.exists(timeout=time)

    def install(self, apk_path):
        self.adb.cmd('install "%s"' % apk_path)

    def uninstall(self, package_name):
        self.adb.cmd('uninstall %s' % package_name)

    def type(self, text):
        """
        Type *text* at current focused component
        """
        self.test_helper.send_set_text_cmd(text)

    def foo(self):
        pass
#         logger.info('\nGot arg %s %s' % (output_dir, st), also_console=True)
#         clm = CommandLineWriter()
        # output some messages on console
#         clm.message(' ')
#         clm.message(u'中文')
#         clm.message(u'2----------2')

    def test(self):
        pass
Beispiel #2
0
class UiTestLib(object):
    """Ui Test Lib

    """
    def __init__(self, serial=None):
        """
        """
        logger.info('<p>Device=%s>' % serial, html=True)
        print '<p>Device=%s>' % serial
        self._result = ''
        self.starttime = 0
        self.d = Device(serial)
        self.adb = Adb(serial)
        self.debug = 'True'

    def set_debugable(flag):
        self.debug = flag

    def set_serial(self, serial):
        """Specify given *serial* device to perform test.
        or export ANDROID_SERIAL=CXFS42343 if you have many devices connected but you don't use this
        interface

        When you need to use multiple devices, do not use this keyword to switch between devices in test execution.
        And set the serial to each library.
        Using different library name when importing this library according to
        http://robotframework.googlecode.com/hg/doc/userguide/RobotFrameworkUserGuide.html?r=2.8.5.

        Examples:
        | Setting | Value |  Value |  Value |
        | Library | UiTestLib | WITH NAME | Mobile1 |
        | Library | UiTestLib | WITH NAME | Mobile2 |

        And set the serial to each library.
        | Test Case        | Action             | Argument           |
        | Multiple Devices | Mobile1.Set Serial | device_1's serial  |
        |                  | Mobile2.Set Serial | device_2's serial  |
        """
        self.d = Device(serial)
        self.adb = Adb(serial)

    def logmsg(self, msg):
        if self.debug == 'True':
            print msg

    def exe_adb_command(self, cmd):
        """ Execute adb *cmd*
         Examples:
        | Exe Adb Command | shell getprop  |
        """
        return self.adb.cmd(cmd).wait()

    def exe_adb_and_result(self, cmd):
        """Execute adb *cmd* and return lines of the command"""
        lproc = self.adb.cmd(cmd)
        lproc.poll()
        lines = lproc.stdout.readlines()
        return lines

    def get_device_info(self):
        """Get Device information
        return info dictionary
        """
        return self.d.info

    def light_screen(self):
        """Light screen by wakeup.

        Examples:
        | Action     |
        |Light screen|

        Use `Light screen` to light screen.
        """

        self.d.wakeup()
        self._result = self.d.press.home()

    def open_application(self, appname):
        """Open application by it name `appname`.

        Example:
        | Action           | Argument      |
        | Open application | "com.android.settings/com.android.settings.Settings" |
        """
        appname = 'shell am start -n ' + appname
        print 'Open Application:', appname
        self._result = self.exe_adb_command(appname)

    def click_text(self, text, instance=0):
        """Click text label on screen
        instance=0 is default, change when you needed.

        Example:
        | Action     | Argument   |  Argument  |
        | Click Text | text | instance |
        """

        return self.d(text=text, instance=instance).click.wait()

    def long_click_text(self, text, instance=0):
        """
        Long Click text label on screen, *text* and *instance=0*

        Example:
        | Action     | Argument   |  Argument  |
        | Long Click Text | text | instance |
        """

        return self.d(text=text, instance=instance).long_click()

    def long_click_ui(self, **selectors):
        """
        Long Click on **selectors**
        Selector supports below parameters. Refer to UiSelector java doc for detailed information.

            text, textContains, textMatches, textStartsWith
            className, classNameMatches
            description, descriptionContains, descriptionMatches, descriptionStartsWith
            checkable, checked, clickable, longClickable
            scrollable, enabled,focusable, focused, selected
            packageName, packageNameMatches
            resourceId, resourceIdMatches
            index, instance
        Example:
        | Action     | Argument   |  Argument  |  Argument  |
        | Long Click UI | text=XXXX | className=XXX.xxxx | resourceId=xxxxxx |
        """

        return self.d(**selectors).long_click()

    def text_display_on_screen_contains(self, text):
        """Verify text display on screen

        Example:
        | Action     | Argument   |
        |Text display on screen is| text |
        """

        if self.d(text=text).exists:
            self._result = True
            return True
        else:
            self._result = False
            return False

    def object_display_on_screen(self, obj, timeout=5000):
        """ Verify *obj* UiObject display on screen
        return to self._result

        Example:
        | Action     | Argument   |
        |Object display on screen | obj |
        """
        if obj.wait.exists(timeout):
            self._result = True
            return True
        else:
            self._result = False
            return False

    def assert_expectation(self, expect, actual):
        """
        Assert Expectation and actual value
        Example:
        | Action             |   args   |   args      |
        | Assert Expectation |   324324 |  ${actual}  |
        """
        if str(expect) != str(actual):
            raise AssertionError(
                'Actual result is = %s, but expectation is: %s' %
                (str(actual), str(expect)))

    def assert_true(self, condition):
        """
        Assert True of *condition

        Example:
        |Assert True | condition |
        """
        if str(condition) != 'True':  #because only string from robotframework
            raise AssertionError('Result is = %s' % str(condition))

    def assert_result_true(self):
        """
        Assert True of *self._result

        Example:
        |Assert True |
        """
        if self._result != True:
            raise AssertionError('Result is = %s' % str(self._result))

    def wait_for_ui_exists(self, timeout, **selectors):
        """
        Return True if
          Selector is to identify specific ui object in current window.

        # To seleted the object ,text is 'Clock' and its className is 'android.widget.TextView'
        wait_for_ui_exists(text='Clock', className='android.widget.TextView')

        Selector supports below parameters. Refer to UiSelector java doc for detailed information.

            text, textContains, textMatches, textStartsWith
            className, classNameMatches
            description, descriptionContains, descriptionMatches, descriptionStartsWith
            checkable, checked, clickable, longClickable
            scrollable, enabled,focusable, focused, selected
            packageName, packageNameMatches
            resourceId, resourceIdMatches
            index, instance

        Examples:
        | Action     | Argument   |  Argument  |  Argument  |  Argument  |
        |Wait For UI Exists | timeout | text=XXXX | className=XXX.xxxx | resourceId=xxxxxx |
        """
        self._result = self.d(**selectors).wait.exists(timeout=int(timeout))
        return self._result

    def wait_and_click(self, timeout, **selectors):
        """Wait for uiselector and click"""
        if self.d(**selectors).wait.exists(timeout=int(timeout)):
            self.d(**selectors).click()

    def assert_ui_exists(self, **selectors):
        """
        Assert UiObject appear on the screen

        Examples:
        | Action     | Argument   |  Argument  |  Argument  |  Argument  |
        |Assert UI Exists | timeout | text=XXXX | className=XXX.xxxx | resourceId=xxxxxx |
        """
        if not self.d(**selectors).wait.exists():
            raise AssertionError('UiObject does not exists %s' %
                                 selectors.items())

    def result_should_be(self, expected):
        """Verifies that the current result is `expected`.

        Example:
        | Action     | Argument   |
        |  Open application    | com.android.settings/com.android.settings.Settings |
        |  Result Should Be    | 0       |
        """
        print('result is: %s\n', self._result)
        print('ex is: %s\n', expected)
        if str(self._result) != expected:
            raise AssertionError('%s != %s' % (self._result, expected))

    def click_at_coordinates(self, x, y):
        """ Click at (x,y) coordinates.
        Example:
        | Action     | Argument   |  Argument  |
        | Click At Corrdinates | x | y |
        """
        return self.d.click(int(x), int(y))

    def long_click_at_coordinates(self, x, y):
        """
        # long click (x, y) on screen
        """
        return self.d.long_click(int(x), int(y))

    def swipe(self, sx, sy, ex, ey, steps=20):
        """
        Swipe from (sx,sy) to (ex,ey)
        """
        return self.d.swipe(int(sx), int(sy), int(ex), int(ex), int(steps))

    def drag(self, sx, sy, ex, ey, steps=20):
        """
        Drag from (sx,sy) to (ex,ey)
        """
        return self.d.drag(int(sx), int(sy), int(ex), int(ex), int(steps))

    def freeze_rotation(self, rotation=True):
        """
        Freeze rotation,
        *rotation*, True is default,
        """
        return self.d.freeze_rotation(rotation)

    def set_rotation(self, rotation):
        """
        # retrieve orientation,
        # it should be "natural" or "left" or "right" or "upsidedown"

        Example:
        | Action       | Argument   |
        | Set Rotation | nature      |
        | Set Rotation | left        |
        | Set Rotation | right       |
        | Set Rotation | upsidedown  |

        """
        orientation = self.d.orientation
        if rotation == "nature":
            self.d.orientation = "n"  # or "natural"
        elif rotation == "left":
            self.d.orientation = "l"  # or "left"
        elif rotation == "right":
            self.d.orientation = "r"  # or "right"
        elif rotation == "upsidedown":
            self.d.orientation = "upsidedown"  # or "upsidedown"
        else:
            self.d.rotation = "n"

    def take_screenshot(self, scale=None, quality=None):
        """
        Take a screenshot of device and log in the report with timestamp, scale for screenshot size and quality for screenshot quality
        default scale=1.0 quality=100

        Example:
        | Action     | Argument   |  Argument  |
        | Take Screenshot | 0.5 | 80 |
        """
        output_dir = BuiltIn().get_variable_value('${OUTPUTDIR}')
        ts = time.time()
        st = datetime.datetime.fromtimestamp(ts).strftime('%Y%m%d%H%M%S')
        screenshot_path = '%s%s%s.png' % (output_dir, os.sep, st)
        screenshot_name = '%s%s.png' % (os.sep, st)
        self.d.screenshot(screenshot_path, scale, quality)
        logger.info('\n<a href="%s">%s</a><br><img src="%s">' %
                    (screenshot_path, st, screenshot_name),
                    html=True)

    def open_notification(self):
        """
        Open notification of the phone
        """
        self.d.open.notification()

    def wait_window_update(self):
        """
        wait for window update
        """
        return self.d.wait.update()

    def wait_window_idle(self):
        """
        wait for window idle
        """
        return self.d.wait.idle()

    def remove_watchers(self):
        """
        Remove UI watchers
        """
        self.d.watchers.remove()

    def get_device(self):
        """
        Get device object, you can do any command using this
        """
        return self.d

    def click_id(self, id, instance=0):
        """
        Click *id* with *instance*=0
        """
        return self.d(resourceId=id, instance=instance).click.wait()

    def long_click_id(self, id, instance=0):
        """
        Long click *id* with *instance*=0
        """
        return self.d(resourceId=id, instance=0).long_click()

    def click_description(self, description, instance=0):
        """
        Click *description* with *instance*=0
        """
        return self.d(description=description, instance=instance).long_click()

    def click_class(self, className, instance=0):
        """
        Click *className* with *instance*=0
        """
        return self.d(className=className, instance=instance).long_click()

    def type_text(self, textStr, **selectors):
        """
        type text on selectors
        like text=EditName
        """
        self.d(**selectors).set_text(textStr)

    def press_key(self, key):
        """ Press Key of following value
            home
            back
            left
            right
            up
            down
            center
            menu
            search
            enter
            delete(or del)
            recent(recent apps)
            volume_up
            volume_down
            volume_mute
            camera
            power

            Examples:
            | Action     | Argument   |
            | Press Key | home |
            | Press Key | back |
            | Press Key | left |
            | Press Key | right |
            | Press Key | recent |
            | Press Key | volume_up |
            | Press Key | camera |
        """
        if key.isdigit():
            return self.d.press(int(key))
        return self.d.press(key)

    def phone_sleep(self, timeout):
        """
        android device sleep with timeout in ms, don't use for executor sleep,
        """
        return self.d.wait(int(timeout))

    def execute_command(self, cmd, block_parent_process=True):
        """
        Execute shell *cmd* command, with block_parent_process = True

        If block_parent_process = False, kill_command is needed to terminal the child process

        Example:
        | Execute Command | ping -c 5 127.0.0.1 |
        """
        if str(block_parent_process) == str(True):
            return subprocess.Popen(cmd,
                                    shell=True,
                                    stdout=subprocess.PIPE,
                                    stderr=subprocess.PIPE).communicate()
        else:
            return subprocess.Popen(cmd,
                                    shell=True,
                                    stdout=subprocess.PIPE,
                                    stderr=subprocess.PIPE)

    def kill_command(self, process, timeout=0):
        """ Kill child *process* in timeout seconds
        Some bugs after process teminated
        """
        time.sleep(float(timeout))
        process.terminate()

    def wait_for_logcat(self, log, timeout=30):
        """ Wait log exists in given timeout, return True if have, otherwise will return False
        log is your want to search, log can be a regular expression
        time in seconds, default value is 30 seconds

        Example:
        | Wait For Logcat | .*ActivityManager.*cmp=com.sonyericsson.album/com.sonyericsson.album.Main.* |
        """
        start = time.time()
        while (time.time() - start) < int(timeout):
            log_proc = self.adb.cmd("logcat -d -v time")
            returncode = log_proc.poll()
            lines = log_proc.stdout.readlines()
            for line in lines:
                rlt = re.search(log, line.rstrip())
                if rlt is not None:
                    print rlt.group(0)
                    self._result = True
                    return True
            time.sleep(2)  #sleep 2s to wait
        self._result = False
        return False

    def clear_logcat(self):
        """
        Clear logcat, it's often used before you want to use *wait_for_logcat()*
        """
        print "Clear logcat before test"
        return self.exe_adb_command("logcat -c")

    def click_object(self, obj):
        """ Click UiObject *obj*

        Exmaple:
        | ${result}    | Get UI Object | text=XXX | className=xxxx |
        | Click Object |   ${result}   |
        """
        obj.click()

    def click_ui(self, **selectors):
        """
        Click selector

        click_selector(text="Home", resourceId = "android:id/title")
        for **selector, please refer *get_ui_object()*
        Selector supports below parameters. Refer to UiSelector java doc for detailed information.

            text, textContains, textMatches, textStartsWith
            className, classNameMatches
            description, descriptionContains, descriptionMatches, descriptionStartsWith
            checkable, checked, clickable, longClickable
            scrollable, enabled,focusable, focused, selected
            packageName, packageNameMatches
            resourceId, resourceIdMatches
            index, instance
        Operation for a UiObjects:
        click, clear_text, drag(obj).to(**selector), gesture, exists, set_text, long_click
        pinch.In(percent=100, steps=10), pinch.Out(percent=100, steps=100),.swipe.right(),
        .swipe.left(steps=10),.swipe("right", steps=20),.wait.gone(),.wait.exists()
        """
        print "selectors:", selectors
        return self.d(**selectors).click()

    def get_ui_object(self, **selectors):
        """
        Get UI object with *selectors*
        you can do anything on UiObject, like click(), long_click(),wait.exists(timeout)
        examples: get_ui_object(text="Home", resourceId = "android:id/title")

        Selector supports below parameters. Refer to UiSelector java doc for detailed information.

            text, textContains, textMatches, textStartsWith
            className, classNameMatches
            description, descriptionContains, descriptionMatches, descriptionStartsWith
            checkable, checked, clickable, longClickable
            scrollable, enabled,focusable, focused, selected
            packageName, packageNameMatches
            resourceId, resourceIdMatches
            index, instance
        Operation for a UiObjects:
        click, clear_text, drag(obj).to(**selector), gesture, exists, set_text, long_click
        pinch.In(percent=100, steps=10), pinch.Out(percent=100, steps=100),.swipe.right(),
        .swipe.left(steps=10),.swipe("right", steps=20),.wait.gone(),.wait.exists()

        Exmaple:
        | ${result}    | Get UI Object | text=XXX | className=xxxx |

        """
        self._result = self.d(**selectors)
        return self._result

    def wait_for_object_exists(self, obj, timeout=0):
        """
        Wait for UiObject *obj* exists in *timeout*= 0

        Example:
        | ${result}    | Get UI Object | text=XXX | className=xxxx |
        | Wait For Object Exists | ${result} | timeout |
        """
        self._result = obj.wait.exists(timeout=int(timeout))
        return self._result

    def wait_for_ui_gone(self, timeout=0, **selectors):
        """
        Wait for UiObject *obj* gone in *timeout*= 0

        Example:
        | ${result}    | Get UI Object | text=XXX | className=xxxx |
        | Wait For Object Gone | ${result} | timeout |

        """
        self._result = self.d(**selectors).wait.gone(timeout=int(timeout))
        return self._result

    def get_key_value(self, key, dictionary):
        """
        Get key value of dictionary
        """
        return dictionary(key)

    def get_tts(self, input_text, delete_file=False, **args):
        """
        Get TTS voice mp3 from Google
        get_tts(input_text='tunnel snakes rule apparently', args = {'language':'en','output':'outputto.mp3'})

        Robot Framework:
        Examples:
        | Action     | Argument   |  Argument  |  Argument  |  Argument  |
        | UiTestLib.Get TTS    |   Hello world    |   False   |    output=ooo.mp3   |   language=en |
        """
        print "Get text to speech: ", input_text
        downloaded = False
        if str(delete_file) == 'True':
            if os.path.exists(args['output']):
                os.remove(args['output'])
        if os.path.exists(args['output']):
            if os.path.getsize(args['output']) <= 0:
                os.remove(args['output'])
        if args['output'] is not None:
            if not os.path.exists(args['output']):
                print 'Generating mp3 file......', args['output']
                downloaded = GoogleTTS.audio_extract(input_text, args)
            else:
                print 'Have same local file'
                downloaded = True
        if not downloaded:
            print "Downloaded TTS from Google failed, trying to download from local FTP..."
            mp3file = open(args['output'], 'w')
            mp3url = 'ftp://cnbjlx9548/atautomation/Test-Content/Croft/' + args[
                'output']
            try:
                resp = urllib2.urlopen(mp3url, timeout=40)
                mp3file.write(resp.read())
                time.sleep(.05)
            except Exception, e:
                print e
                return False
            mp3file.close()
        return True
# accelerated location
d(text="NEXT").click.wait()

# open settings
d(resourceId=
  "com.google.android.googlequicksearchbox:id/search_widget_google_logo"
  ).wait.exists(timeout=30)
d.open.quick_settings()
d(resourceId="com.android.systemui:id/settings_button").click.wait()

# turn off WiFi
d(scrollable=True).scroll.to(text="Network & internet")
d(text="Network & internet").click.wait()
if d(text="ON", className="android.widget.Switch").exists:
    d(text="ON", className="android.widget.Switch").click.wait()
d.press("back")

# turn off bt
d(scrollable=True).scroll.to(text="Connected devices")
d(text="Connected devices").click.wait()
d(text="Connection preferences").click.wait()
d(text="Bluetooth").click.wait()
if d(text="ON", className="android.widget.Switch").exists:
    d(text="ON", className="android.widget.Switch").click.wait()
d.press("back")
d.press("back")
d.press("back")

# Display -> Choose "Never"
d(scrollable=True).scroll.to(text="Display")
d(text="Display").click.wait()
Beispiel #4
0
class Mobile():
    def __init__(self):
        self.set_serial(None)

    def set_serial(self, android_serial):
        """
        Specify given *android_serial* device to perform test.

        You do not have to specify the device when there is only one device connects to the computer.

        When you need to use multiple devices, do not use this keyword to switch between devices in test execution.

        Using different library name when importing this library according to http://robotframework.googlecode.com/hg/doc/userguide/RobotFrameworkUserGuide.html?r=2.8.5.

        | Setting | Value  | Value     | Value   | 
        | Library | Mobile | WITH NAME | Mobile1 |
        | Library | Mobile | WITH NAME | Mobile2 |

        And set the serial to each library.

        | Test Case        | Action             | Argument           |
        | Multiple Devices | Mobile1.Set Serial | device_1's serial  |
        |                  | Mobile2.Set Serial | device_2's serial  |

        """
        self.adb = ADB(android_serial)
        self.device = Device(android_serial)
        self.test_helper = TestHelper(self.adb)

    def get_device_info(self):
        """
        Retrieve the device info.

        The keyword will return a dictionary.

        You can log the information by using the log dictionary keyword in build in Collections library(http://robotframework.googlecode.com/hg/doc/libraries/Collections.html?r=2.8.4).

        Example:
        | ${device_info} | Get Device Info |
        | Log Dictionary | ${device_info}  |

        =>

        Dictionary size is 9 and it contains following items:\n
        currentPackageName: com.android.keyguard\n
        displayHeight: 1776\n
        displayRotation: 0\n
        displaySizeDpX: 360\n
        displaySizeDpY: 640\n
        displayWidth: 1080\n
        naturalOrientation: True\n
        productName: hammerhead\n
        sdkInt: 19\n

        Or get specific information of the device by giving the key.

        | ${device_info}  | Get Device Info |   |                |
        | ${product_name} | Get From Dictionary | ${device_info} | productName |

        =>

        ${product_name} = hammerhead

        """
        return self.device.info

#Key Event Actions of the device

    """
    Turn on/off screen
    """
    def turn_on_screen(self):
        """
        Turn on the screen.
        """
        self.device.screen.on()

    def turn_off_screen(self):
        """
        Turn off the screen.
        """
        self.device.screen.off()

    """
    Press hard/soft key
    """

    def press_key(self, *keys):
        """
        Press *key* keycode.

        You can find all keycode in http://developer.android.com/reference/android/view/KeyEvent.html

        """
        #not tested
        self.device.press(*keys)

    def press_home(self):
        """
        Press home key.
        """
        self.device.press.home()

    def press_back(self):
        """
        Press back key.
        """
        self.device.press.back()

    def press_left(self):
        """
        Press left key.
        """
        self.device.pres.left()

    def press_right(self):
        """
        Press right key.
        """
        self.device.press.right()

    def press_up(self):
        """
        Press up key.
        """
        self.device.press.up()

    def press_down(self):
        """
        Press down key.
        """
        self.device.press.down()

    def press_center(self):
        """
        Press center key.
        """
        self.device.press.center()

    def press_menu(self):
        """
        Press menu key.
        """
        self.device.press.menu()

    def press_search(self):
        """
        Press search key.
        """
        self.device.press.search()

    def press_enter(self):
        """
        Press enter key.
        """
        self.device.press.enter()

    def press_delete(self):
        """
        Press delete key.
        """
        self.device.press.delete()

    def press_recent(self):
        """
        Press recent key.
        """
        self.device.press.recent()

    def press_volume_up(self):
        """
        Press volume up key.
        """
        self.device.press.volume_up()

    def press_volume_down(self):
        """
        Press volume down key.
        """
        self.device.press.volume_down()

    def press_camera(self):
        """
        Press camera key.
        """
        self.device.press.camera()

    def press_power(self):
        """
        Press power key.
        """
        self.device.press.power()

#Gesture interaction of the device

    def click_at_coordinates(self, x, y):
        """
        Click at (x,y) coordinates.
        """
        self.device.click(int(x), int(y))

    def swipe_by_coordinates(self, sx, sy, ex, ey, steps=10):
        """
        Swipe from (sx, sy) to (ex, ey) with *steps* .

        Example:
        | Swipe By Coordinates | 540 | 1340 | 940 | 1340 |     | # Swipe from (540, 1340) to (940, 100) with default steps 10 |
        | Swipe By Coordinates | 540 | 1340 | 940 | 1340 | 100 | # Swipe from (540, 1340) to (940, 100) with steps 100        |
        """
        self.device.swipe(sx, sy, ex, ey, steps)

# Swipe from the center of the ui object to its edge

    def swipe_left(self, steps=10, *args, **selectors):
        """
        Swipe the UI object with *selectors* from center to left.

        Example:

        | Swipe Left | description=Home screen 3 |                           | # swipe the UI object left              |
        | Swipe Left | 5                         | description=Home screen 3 | # swipe the UI object left with steps=5 |

        See `introduction` for details about Identified UI object.
        """
        self.device(**selectors).swipe.left(steps=steps)

    def swipe_right(self, steps=10, *args, **selectors):
        """
        Swipe the UI object with *selectors* from center to right

        See `Swipe Left` for more details.
        """
        self.device(**selectors).swipe.right(steps=steps)

    def swipe_top(self, steps=10, *args, **selectors):
        """
        Swipe the UI object with *selectors* from center to top

        See `Swipe Left` for more details.
        """
        self.device(**selectors).swipe.up(steps=steps)

    def swipe_bottom(self, steps=10, *args, **selectors):
        """
        Swipe the UI object with *selectors* from center to bottom

        See `Swipe Left` for more details.
        """
        self.device(**selectors).swipe.down(steps=steps)

    def object_swipe_left(self, obj, steps=10):
        """
        Swipe the *obj* from center to left

        Example:

        | ${object}         | Get Object | description=Home screen 3 | # Get the UI object                     |
        | Object Swipe Left | ${object}  |                           | # Swipe the UI object left              |
        | Object Swipe Left | ${object}  | 5                         | # Swipe the UI object left with steps=5 |
        | Object Swipe Left | ${object}  | steps=5                   | # Swipe the UI object left with steps=5 |

        See `introduction` for details about identified UI object.
        """
        obj.swipe.left(steps=steps)

    def object_swipe_right(self, obj, steps=10):
        """
        Swipe the *obj* from center to right

        See `Object Swipe Left` for more details.
        """
        obj.swipe.right(steps=steps)

    def object_swipe_top(self, obj, steps=10):
        """
        Swipe the *obj* from center to top

        See `Object Swipe Left` for more details.
        """
        obj.swipe.up(steps=steps)

    def object_swipe_bottom(self, obj, steps=10):
        """
        Swipe the *obj* from center to bottom

        See `Object Swipe Left` for more details.
        """
        obj.swipe.down(steps=steps)

    def drag_by_coordinates(self, sx, sy, ex, ey, steps=10):
        """
        Drag from (sx, sy) to (ex, ey) with steps

        See `Swipe By Coordinates` also.
        """
        self.device.drag(sx, sy, ex, ey, steps)

    #Wait until the specific ui object appears or gone

    # wait until the ui object appears
    def wait_for_exists(self, timeout=0, *args, **selectors):
        """
        Wait for the object which has *selectors* within the given timeout.

        Return true if the object *appear* in the given timeout. Else return false.
        """
        return self.device(**selectors).wait.exists(timeout=timeout)

    # wait until the ui object gone
    def wait_until_gone(self, timeout=0, *args, **selectors):
        """
        Wait for the object which has *selectors* within the given timeout.

        Return true if the object *disappear* in the given timeout. Else return false.
        """
        return self.device(**selectors).wait.gone(timeout=timeout)

    def wait_for_object_exists(self, obj, timeout=0):
        """
        Wait for the object: obj within the given timeout.

        Return true if the object *appear* in the given timeout. Else return false.
        """
        return obj.wait.exists(timeout=timeout)

    # wait until the ui object gone
    def wait_until_object_gone(self, obj, timeout=0):
        """
        Wait for the object: obj within the given timeout.

        Return true if the object *disappear* in the given timeout. Else return false.
        """
        return obj.wait.gone(timeout=timeout)

    # Perform fling on the specific ui object(scrollable)
    def fling_forward_horizontally(self, *args, **selectors):
        """
        Perform fling forward (horizontally)action on the object which has *selectors* attributes.

        Return whether the object can be fling or not.
        """
        return self.device(**selectors).fling.horiz.forward()

    def fling_backward_horizontally(self, *args, **selectors):
        """
        Perform fling backward (horizontally)action on the object which has *selectors* attributes.

        Return whether the object can be fling or not.
        """
        return self.device(**selectors).fling.horiz.backward()

    def fling_forward_vertically(self, *args, **selectors):
        """
        Perform fling forward (vertically)action on the object which has *selectors* attributes.

        Return whether the object can be fling or not.
        """
        return self.device(**selectors).fling.vert.forward()

    def fling_backward_vertically(self, *args, **selectors):
        """
        Perform fling backward (vertically)action on the object which has *selectors* attributes.

        Return whether the object can be fling or not.
        """
        return self.device(**selectors).fling.vert.backward()

    # Perform scroll on the specific ui object(scrollable)

    # horizontal
    def scroll_to_beginning_horizontally(self, steps=10, *args, **selectors):
        """
        Scroll the object which has *selectors* attributes to *beginning* horizontally.

        See `Scroll Forward Vertically` for more details.
        """
        return self.device(**selectors).scroll.horiz.toBeginning(steps=steps)

    def scroll_to_end_horizontally(self, steps=10, *args, **selectors):
        """
        Scroll the object which has *selectors* attributes to *end* horizontally.

        See `Scroll Forward Vertically` for more details.
        """
        return self.device(**selectors).scroll.horiz.toEnd(steps=steps)

    def scroll_forward_horizontally(self, steps=10, *args, **selectors):
        """
        Perform scroll forward (horizontally)action on the object which has *selectors* attributes.

        Return whether the object can be Scroll or not.

        See `Scroll Forward Vertically` for more details.
        """
        return self.device(**selectors).scroll.horiz.forward(steps=steps)

    def scroll_backward_horizontally(self, steps=10, *args, **selectors):
        """
        Perform scroll backward (horizontally)action on the object which has *selectors* attributes.

        Return whether the object can be Scroll or not.

        See `Scroll Forward Vertically` for more details.
        """
        return self.device(**selectors).scroll.horiz.backward(steps=steps)

    def scroll_to_horizontally(self, obj, *args, **selectors):
        """
        Scroll(horizontally) on the object: obj to specific UI object which has *selectors* attributes appears.

        Return true if the UI object, else return false.

        See `Scroll To Vertically` for more details.
        """
        return obj.scroll.horiz.to(**selectors)

    # vertical
    def scroll_to_beginning_vertically(self, steps=10, *args, **selectors):
        """
        Scroll the object which has *selectors* attributes to *beginning* vertically.

        See `Scroll Forward Vertically` for more details.
        """
        return self.device(**selectors).scroll.vert.toBeginning(steps=steps)

    def scroll_to_end_vertically(self, steps=10, *args, **selectors):
        """
        Scroll the object which has *selectors* attributes to *end* vertically.

        See `Scroll Forward Vertically` for more details.
        """
        return self.device(**selectors).scroll.vert.toEnd(steps=steps)

    def scroll_forward_vertically(self, steps=10, *args, **selectors):
        """
        Perform scroll forward (vertically)action on the object which has *selectors* attributes.

        Return whether the object can be Scroll or not.

        Example:
        | ${can_be_scroll} | Scroll Forward Vertically | className=android.widget.ListView       |                                   | # Scroll forward the UI object with class name |
        | ${can_be_scroll} | Scroll Forward Vertically | 100                                     | className=android.widget.ListView | # Scroll with steps |
        """
        return self.device(**selectors).scroll.vert.forward(steps=steps)

    def scroll_backward_vertically(self, steps=10, *args, **selectors):
        """
        Perform scroll backward (vertically)action on the object which has *selectors* attributes.

        Return whether the object can be Scroll or not.

        See `Scroll Forward Vertically` for more details.
        """
        return self.device(**selectors).scroll.vert.backward(steps=steps)

    def scroll_to_vertically(self, obj, *args, **selectors):
        """
        Scroll(vertically) on the object: obj to specific UI object which has *selectors* attributes appears.

        Return true if the UI object, else return false.

        Example:

        | ${list}        | Get Object           | className=android.widget.ListView |              | # Get the list object     |
        | ${is_web_view} | Scroll To Vertically | ${list}                           | text=WebView | # Scroll to text:WebView. |
        """
        return obj.scroll.vert.to(**selectors)

#Screen Actions of the device

    def get_screen_orientation(self):
        """
        Get the screen orientation.

        Possible result: natural, left, right, upsidedown

        See for more details: https://github.com/xiaocong/uiautomator#screen-actions-of-the-device
        """
        return self.device.orientation

    def set_screen_orientation(self, orientation):
        """
        Set the screen orientation.

        Input *orientation* : natural or n, left or l, right or r, upsidedown (support android version above 4.3)

        The keyword will unfreeze the screen rotation first.

        See for more details: https://github.com/xiaocong/uiautomator#screen-actions-of-the-device

        Example:

        | Set Screen Orientation | n       | # Set orientation to natural |
        | Set Screen Orientation | natural | # Do the same thing          |
        """
        self.device.orientation = orientation

    def freeze_screen_rotation(self):
        """
        Freeze the screen auto rotation
        """
        self.device.freeze_rotation()

    def unfreeze_screen_rotation(self):
        """
        Un-Freeze the screen auto rotation
        """
        self.device.freeze_rotation(False)

    def screenshot(self, scale=None, quality=None):
        """
        Take a screenshot of device and log in the report with timestamp, scale for screenshot size and quality for screenshot quality
        default scale=1.0 quality=100
        """
        output_dir = BuiltIn().get_variable_value('${OUTPUTDIR}')
        ts = time.time()
        st = datetime.datetime.fromtimestamp(ts).strftime('%Y%m%d%H%M%S')
        screenshot_path = '%s%s%s.png' % (output_dir, os.sep, st)
        self.device.screenshot(screenshot_path, scale, quality)
        logger.info('\n<a href="%s">%s</a><br><img src="%s">' %
                    (screenshot_path, st, screenshot_path),
                    html=True)

#Watcher

    def __unicode_to_dict(self, a_unicode):
        a_dict = dict()
        dict_item_count = a_unicode.count('=')
        for count in range(dict_item_count):
            equal_sign_position = a_unicode.find('=')
            comma_position = a_unicode.find(',')
            a_key = a_unicode[0:equal_sign_position]
            if comma_position == -1:
                a_value = a_unicode[equal_sign_position + 1:]
            else:
                a_value = a_unicode[equal_sign_position + 1:comma_position]
                a_unicode = a_unicode[comma_position + 1:]
            a_dict[a_key] = a_value
        return a_dict

    def register_click_watcher(self, watcher_name, selectors, *condition_list):
        """
        The watcher click on the object which has the *selectors* when conditions match.
        """
        watcher = self.device.watcher(watcher_name)
        for condition in condition_list:
            watcher.when(**self.__unicode_to_dict(condition))
        watcher.click(**self.__unicode_to_dict(selectors))
        self.device.watchers.run()

    def register_press_watcher(self, watcher_name, press_keys,
                               *condition_list):
        """
        The watcher perform *press_keys* action sequentially when conditions match.
        """
        def unicode_to_list(a_unicode):
            a_list = list()
            comma_count = a_unicode.count(',')
            for count in range(comma_count + 1):
                comma_position = a_unicode.find(',')
                if comma_position == -1:
                    a_list.append(str(a_unicode))
                else:
                    a_list.append(a_unicode[0:comma_position])
                    a_unicode = a_unicode[comma_position + 1:]
            return a_list

        watcher = self.device.watcher(watcher_name)
        for condition in condition_list:
            watcher.when(**self.__unicode_to_dict(condition))
        watcher.press(*unicode_to_list(press_keys))
        self.device.watchers.run()

    def remove_watchers(self, watcher_name=None):
        """
        Remove watcher with *watcher_name* or remove all watchers.
        """
        if watcher_name == None:
            self.device.watchers.remove()
        else:
            self.device.watchers.remove(watcher_name)

    def list_all_watchers(self):
        """
        Return the watcher list.
        """
        return self.device.watchers

#Selector

    def get_object(self, *args, **selectors):
        """
        Get the UI object with selectors *selectors*

        See `introduction` for details about identified UI object.
        
        Example:
        | ${main_layer} | Get Object | className=android.widget.FrameLayout | index=0 | # Get main layer which class name is FrameLayout |
        """
        return self.device(*args, **selectors)

    def get_child(self, object, *args, **selectors):
        """
        Get the child or grandchild UI object from the *object* with *selectors*
        Example:
        | ${root_layout}   | Get Object | className=android.widget.FrameLayout |
        | ${child_layout}  | Get Child  | ${root_layout}                       | className=LinearLayout |
        """
        return object.child(*args, **selectors)

    def get_sibling(self, object, *args, **selectors):
        """
        Get the sibling or child of sibling UI object from the *object* with *selectors*
        Example:
        | ${root_layout}     | Get Object   | className=android.widget.FrameLayout |
        | ${sibling_layout}  | Get Sibling  | ${root_layout}                       | className=LinearLayout |
        """
        return object.sibling(*args, **selectors)

    def get_count(self, *args, **selectors):
        """
        Return the count of UI object with *selectors*

        Example:

        | ${count}              | Get Count           | text=Accessibility    | # Get the count of UI object text=Accessibility |
        | ${accessibility_text} | Get Object          | text=Accessibility    | # These two keywords combination                |
        | ${count}              | Get Count Of Object | ${accessibility_text} | # do the same thing.                            |

        """
        obj = self.get_object(**selectors)
        return self.get_count_of_object(obj)

#     def get_count_of_object(self, obj):
#         """
#         Return the count of given UI object
#
#         See `Get Count` for more details.
#         """
#         return len(obj)

    def get_info_of_object(self, obj, selector=None):
        """
        return info dictionary of the *obj*
        The info example:
        {
         u'contentDescription': u'',
         u'checked': False,
         u'scrollable': True,
         u'text': u'',
         u'packageName': u'com.android.launcher',
         u'selected': False,
         u'enabled': True,
         u'bounds': 
                   {
                    u'top': 231,
                    u'left': 0,
                    u'right': 1080,
                    u'bottom': 1776
                   },
         u'className': u'android.view.View',
         u'focusable': False,
         u'focused': False,
         u'clickable': False,
         u'checkable': False,
         u'chileCount': 1,
         u'longClickable': False,
         u'visibleBounds':
                          {
                           u'top': 231,
                           u'left': 0,
                           u'right': 1080,
                           u'bottom': 1776
                          }
        }
        """
        if selector:
            return obj.info.get(selector)
        else:
            return obj.info

    def click(self, *args, **selectors):
        """
        Click on the UI object with *selectors*

        | Click | text=Accessibility | className=android.widget.Button | # Click the object with class name and text |
        """
        self.device(**selectors).click()

    def click_on_object(self, object):
        """
        Click on the UI object which is gained by `Get Object`.

        Example:
        | ${button_ok}    | text=OK      | className=android.widget.Button |
        | Click on Object | ${button_ok} |
        """
        return object.click()

    def long_click(self, *args, **selectors):
        """
        Long click on the UI object with *selectors*

        See `Click` for more details.
        """
        self.device(**selectors).long_click()

    def call(self, obj, method, *args, **selectors):
        """
        This keyword can use object method from original python uiautomator

        See more details from https://github.com/xiaocong/uiautomator

        Example:

        | ${accessibility_text} | Get Object            | text=Accessibility | # Get the UI object                        |
        | Call                  | ${accessibility_text} | click              | # Call the method of the UI object 'click' |
        """
        func = getattr(obj, method)
        return func(**selectors)

    def set_text(self, input_text, *args, **selectors):
        """
        Set *input_text* to the UI object with *selectors* 
        """
        self.device(**selectors).set_text(input_text)

    def set_object_text(self, input_text, object):
        """
        Set *input_text* the *object* which could be selected by *Get Object* or *Get Child*
        """
        object.set_text(input_text)

# Other feature

    def clear_text(self, *args, **selectors):
        """
        Clear text of the UI object  with *selectors*
        """
        while True:
            target = self.device(**selectors)
            text = target.info['text']
            target.clear_text()
            remain_text = target.info['text']
            if text == '' or remain_text == text:
                break

    def open_notification(self):
        """
        Open notification

        Built in support for Android 4.3 (API level 18)

        Using swipe action as a workaround for API level lower than 18

        """
        sdk_version = self.device.info['sdkInt']
        if sdk_version < 18:
            height = self.device.info['displayHeight']
            self.device.swipe(1, 1, 1, height - 1, 1)
        else:
            self.device.open.notification()

    def open_quick_settings(self):
        """
        Open quick settings

        Work for Android 4.3 above (API level 18)

        """
        self.device.open.quick_settings()

    def sleep(self, time):
        """
        Sleep(no action) for *time* (in millisecond)
        """
        target = 'wait for %s' % str(time)
        self.device(text=target).wait.exists(timeout=time)

    def install(self, apk_path):
        """
        Install apk to the device.

        Example:

        | Install | ${CURDIR}${/}com.hmh.api_4.0.apk | # Given the absolute path to the apk file |
        """
        self.adb.cmd('install "%s"' % apk_path)

    def uninstall(self, package_name):
        """
        Uninstall the APP with *package_name*
        """
        self.adb.cmd('uninstall %s' % package_name)

    def execute_adb_command(self, cmd):
        """
        Execute adb *cmd*
        """
        return self.adb.cmd(cmd)

    def execute_adb_shell_command(self, cmd):
        """
        Execute adb shell *cmd*
        """
        return self.adb.shell_cmd(cmd)

    def type(self, input_text):
        """
        [IME]

        Type *text* at current focused UI object
        """
        self.test_helper.send_set_text_cmd(input_text)

    def start_test_agent(self):
        """
        [Test Agent]

        Start Test Agent Service
        """
        cmd = 'am start edu.ntut.csie.sslab1321.testagent/edu.ntut.csie.sslab1321.testagent.DummyActivity'
        self.adb.shell_cmd(cmd)

    def stop_test_agent(self):
        """
        [Test Agent]

        Stop Test Agent Service
        """
        cmd = 'am broadcast -a testagent -e action STOP_TESTAGENT'
        self.adb.shell_cmd(cmd)

    def connect_to_wifi(self, ssid, password=None):
        """
        [Test Agent]

        Connect to *ssid* with *password*
        """
        cmd = 'am broadcast -a testagent -e action CONNECT_TO_WIFI -e ssid %s -e password %s' % (
            ssid, password)
        self.adb.shell_cmd(cmd)

    def clear_connected_wifi(self):
        """
        [Test Agent]

        Clear all existed Wi-Fi connection
        """
        cmd = 'am broadcast -a testagent -e action CLEAR_CONNECTED_WIFIS'
        self.adb.shell_cmd(cmd)
Beispiel #5
0
class AndroidDevice(object):
    '''
    wrapper for android uiautomator(pip install uiautomator) and image comparision(pip install imglib)
    to provide android device event inject and ui object inspect and image comparison.
    '''

    def __init__(self):
        self.serial = configer.env[ANDROID_SERIAL] if configer.env.has_key(ANDROID_SERIAL) else None
        self.d = Device(self.serial)
    
    def __getattr__(self, method):
        '''
        forward method to uiautomator device if method support by uiautomator.
        '''
        if hasattr(self.d, method):
            def wrapper(*args, **kwargs):
                return getattr(self.d, method)(*args, **kwargs)
            return wrapper
        raise AttributeError(method)


    def serial(self):
        '''device serial number from $ANDROID_SERIAL '''
        return self.serial

    def info(self):
        '''retrieve the device info'''
        return self.d.info
   
    def sleep(self, seconds):
        time.sleep(seconds)
        return self

    #device event inject
    def start_activity(self, **kwargs):
        '''launch application from android shell am start: component, flag
        // from adb docs:
        //<INTENT> specifications include these flags:
        //    [-a <ACTION>] [-d <DATA_URI>] [-t <MIME_TYPE>]
        //    [-c <CATEGORY> [-c <CATEGORY>] ...]
        //    [-e|--es <EXTRA_KEY> <EXTRA_STRING_VALUE> ...]
        //    [--esn <EXTRA_KEY> ...]
        //    [--ez <EXTRA_KEY> <EXTRA_BOOLEAN_VALUE> ...]
        //    [-e|--ei <EXTRA_KEY> <EXTRA_INT_VALUE> ...]
        //    [-n <COMPONENT>] [-f <FLAGS>]
        //    [<URI>]
        '''
        #d.server.adb.cmd('shell','am', 'start', '-a', 'android.intent.action.DIAL','tel:13581739891').communicate()
        #sys.stderr.write(str(kwargs))
        keys = kwargs.keys()
        shellcmd = ['shell', 'am', 'start']
        if 'component' in keys:
            shellcmd.append('-n')
            shellcmd.append(kwargs['component'])

        if 'action' in keys:  
            shellcmd.append('-a')
            shellcmd.append(kwargs['action'])

        if 'data' in keys:
            shellcmd.append('-d')
            shellcmd.append(kwargs['data'])

        if 'mimetype' in keys:
            shellcmd.append('-t')
            shellcmd.append(kwargs['mimetype'])

        if 'categories' in keys:
            for category in kwargs['categories']:
                shellcmd.append('-c')
                shellcmd.append(category)
        
        if 'extras' in keys:
            for extra_key, extra_value in kwargs['extras'].items():
                str_value = ''
                arg = ''
                if isinstance(extra_value, types.IntType):
                    str_value = str(extra_value)
                    arg = '--ei'
                elif isinstance(extra_value, types.BooleanType):
                    str_value = str(extra_value)
                    arg = '--ez'
                else:
                    str_value = str(extra_value)
                    arg = '--es'
                shellcmd.append(arg)
                shellcmd.append(extra_key)
                shellcmd.append(str_value)
                
        if 'flags' in keys:
            shellcmd.append('-f')
            shellcmd.append(str(kwargs['flags']))

        if 'uri' in keys:
            shellcmd.append(kwargs['uri'])
        #sys.stderr.write(str(shellcmd))            
        self.d.server.adb.cmd(*shellcmd).communicate()
        return self

    def instrument(self, **kwargs):
        keys = kwargs.keys()
        shellcmd = ['shell', 'am', 'instrument', '-w', '-r']
        pkgname = kwargs.pop('packagename')
        for k, v in kwargs.items():
            if k and v:
                shellcmd.append('-e')
                shellcmd.append(k)
                shellcmd.append(str(v))
        shellcmd.append(pkgname)
        result = self.d.server.adb.cmd(*shellcmd).communicate()
        return result

    def TODOinstallPackage(self, **kwargs):
        pass

    def TODOremovePackage(self, **kwargs):
        pass

    def press(self, keyname, waittime=1):
        #hard, soft key: home,back,up,down,right,left,center,menu,power or ANDROID_KEYEVENT
        self.d.press(keyname)
        time.sleep(waittime)
        return self

    def click(self, x, y, waittime=1):
        self.d.click(x, y)
        time.sleep(waittime)
        return self

    def click_image(self, imagename, waittime=1, threshold=0.01):
        '''
        if the wanted image found on current screen click it.
        if the wanted image not found raise exception and set test to be failure.
        '''
        expect_image_path = os.path.join(configer['right_dir_path'], imagename)
        assert os.path.exists(expect_image_path), 'the local expected image %s not found!' % imagename
        current_image_path = os.path.join(configer['report_dir_path'], imagename)
        self.d.screenshot(current_image_path)
        assert os.path.exists(current_image_path), 'fetch current screen shot image %s failed!' % imagename
        pos = getMatchedCenterOffset(expect_image_path, current_image_path, threshold)
        assert pos, 'Fail Reason: The wanted image \'%s\' not found on screen!' % imagename
        self.d.click(pos[0], pos[1])
        time.sleep(waittime)
        return self

    def swipe(self, sx, sy, ex, ey, steps=100, waittime=1):
        self.d.swipe(sx, sy, ex, ey, steps)
        time.sleep(waittime)
        return self

    def drag(self, sx, sy, ex, ey, steps=100, waittime=1):
        self.d.drag(sx, sy, ex, ey, steps)
        time.sleep(waittime)
        return self

    #inspect
    def exists(self, **kwargs):
        '''
        if the expected component exists on current screen layout return true else return false.
        '''
        return self.d.exists(**kwargs)

    #device snapshot
    def screenshot(self, filename, waittime=1):
        path = os.path.join(configer['report_dir_path'], filename)
        self.d.screenshot(path)
        return self

    def expect(self, imagename, interval=2, timeout=4, threshold=0.01, msg=''):
        '''
        if the expected image found on current screen return self 
        else raise exception. set test to be failure.
        '''
        expect_image_path = os.path.join(configer['right_dir_path'], imagename)
        assert os.path.exists(expect_image_path)
        current_image_path = os.path.join(configer['report_dir_path'], imagename)
        begin = time.time()
        while (time.time() - begin < timeout):
            self.d.screenshot(current_image_path)
            if isMatch(expect_image_path , current_image_path , threshold):
                return self
            time.sleep(interval)
        name, ext = os.path.splitext(os.path.basename(imagename))
        shutil.copyfile(expect_image_path, os.path.join(configer['report_dir_path'], '%s%s%s' % (name, '_expect', ext)))
        reason = msg if not msg else 'Fail Reason: Image \'%s\' not found on screen!' % imagename
        assert False, reason

    def find(self, imagename, interval=2, timeout=4, threshold=0.01):
        '''
        if the expected image found on current screen return true else return false
        '''
        expect_image_path = os.path.join(configer['right_dir_path'], imagename)
        assert os.path.exists(expect_image_path)
        current_image_path = os.path.join(configer['report_dir_path'], imagename)
        begin = time.time()
        isExists = False
        while (time.time() - begin < timeout):
            time.sleep(interval)
            self.d.screenshot(current_image_path)
            isExists = isMatch(expect_image_path , current_image_path , threshold)
            if not isExists:
                time.sleep(interval)
                continue
        return isExists
Beispiel #6
0
import time
import os
import requests
import re
import random
import time
import sys
from pymongo import MongoClient
from uiautomator import JsonRPCError


conn = MongoClient('mongodb://192.168.1.66:27017/')
db = conn.yandex
yandex = db.account
d = Device('4d51746e')
d.press('home')
d.press('home')


try:         
    if d(text="确定").exists:
        d(text="确定").click()
    #d.press('menu')
    #d(text="设置").click()
    d(text="设置").click()
    time.sleep(1)
    d(text="其他连接方式").click()
    time.sleep(2)
    d(text="飞行模式").click()
    time.sleep(2)
    d(text="飞行模式").click()
Beispiel #7
0
class Mobile():

    def __init__(self):
        pass

    def set_serial(self, android_serial):
        """
        Specify given *android_serial* device to perform test.

        You do not have to specify the device when there is only one device connects to the computer.

        When you need to use multiple devices, do not use this keyword to switch between devices in test execution.

        Using different library name when importing this library according to http://robotframework.googlecode.com/hg/doc/userguide/RobotFrameworkUserGuide.html?r=2.8.4#setting-custom-name-to-test-library.

        | ==Setting== | ==Value== |  ==Value== |  ==Value== | 
        | Library | Mobile | WITH NAME | Mobile1 |
        | Library | Mobile | WITH NAME | Mobile2 |

        And set the serial to each library.

        | Test Case        | Action             | Argument           |
        | Multiple Devices | Mobile1.Set Serial | device_1's serial  |
        |                  | Mobile2.Set Serial | device_2's serial  |

        """
        self.adb = ADB(android_serial)
        self.device = Device(android_serial)
        self.test_helper = TestHelper(self.adb)

    def get_device_info(self):
        """
        Retrieve the device info.

        The keyword will return a dictionary.

        You can log the information by using the log dictionary keyword in build in Collections library(http://robotframework.googlecode.com/hg/doc/libraries/Collections.html?r=2.8.4).

        Example:
        | ${device_info} | Get Device Info |
        | Log Dictionary | ${device_info}  |

        =>

        Dictionary size is 9 and it contains following items:\n
        currentPackageName: com.android.keyguard\n
        displayHeight: 1776\n
        displayRotation: 0\n
        displaySizeDpX: 360\n
        displaySizeDpY: 640\n
        displayWidth: 1080\n
        naturalOrientation: True\n
        productName: hammerhead\n
        sdkInt: 19\n

        Or get specific information of the device by giving the key.

        | ${device_info}  | Get Device Info | | |
        | ${product_name} | Get From Dictionary | ${device_info} | productName |

        =>

        ${product_name} = hammerhead

        """
        return self.device.info

#Key Event Actions of the device
    """
    Turn on/off screen
    """
    def turn_on_screen(self):
        """
        Turn on screen
        """
        self.device.screen.on()

    def turn_off_screen(self):
        """
        Turn off screen
        """
        self.device.screen.off()

    """
    Press hard/soft key
    """

    def press_key(self, *keys):
        """
        Press *key* keycode.

        You can find all keycode in http://developer.android.com/reference/android/view/KeyEvent.html

        """
        #not tested
        self.device.press(*keys)

    def press_home(self):
        """
        Press home key
        """
        self.device.press.home()

    def press_back(self):
        """
        Press back key
        """
        self.device.press.back()

    def press_left(self):
        """
        Press left key
        """
        self.device.pres.left()

    def press_right(self):
        """
        Press right key
        """
        self.device.press.right()

    def press_up(self):
        """
        Press up key
        """
        self.device.press.up()

    def press_down(self):
        """
        Press down key
        """
        self.device.press.down()

    def press_center(self):
        """
        Press center key
        """
        self.device.press.center()

    def press_menu(self):
        """
        Press menu key
        """
        self.device.press.menu()

    def press_search(self):
        """
        Press search key
        """
        self.device.press.search()

    def press_enter(self):
        """
        Press enter key
        """
        self.device.press.enter()

    def press_delete(self):
        """
        Press delete key
        """
        self.device.press.delete()

    def press_recent(self):
        """
        Press recent key
        """
        self.device.press.recent()

    def press_volume_up(self):
        """
        Press volume up key
        """
        self.device.press.volume_up()

    def press_volume_down(self):
        """
        Press volume down key
        """
        self.device.press.volume_down()

    def press_camera(self):
        """
        Press camera key
        """
        self.device.press.camera()

    def press_power(self):
        """
        Press power key
        """
        self.device.press.power()

#Gesture interaction of the device

    def click_at_coordinates(self, x, y):
        """
        Click at (x,y) coordinates.
        """
        self.device.click(x, y)

    def swipe_by_coordinates(self, sx, sy, ex, ey, steps=10):
        """
        Swipe from (sx, sy) to (ex, ey) with *steps* .

        Example:
        | Swipe By Coordinates | 540 | 1340 | 940 | 1340 | | # Swipe from (540, 1340) to (940, 100) with default steps 10 |
        | Swipe By Coordinates | 540 | 1340 | 940 | 1340 | 100 | # Swipe from (540, 1340) to (940, 100) with steps 100 |
        """
        self.device.swipe(sx, sy, ex, ey, steps)

# Swipe from the center of the ui object to its edge

    def swipe_left(self, steps=10, *args, **selectors):
        """
        Swipe the UI object with *selectors* from center to left.

        Example:

        | Swipe Left | description=Home screen 3 | | # swipe the UI object left |
        | Swipe Left | 5 | description=Home screen 3 | # swipe the UI object left with steps=5 |

        See `introduction` for details about identified UI object.
        """
        self.device(**selectors).swipe.left(steps=steps)

    def swipe_right(self, steps=10, *args, **selectors):
        """
        Swipe the UI object with *selectors* from center to right

        See `Swipe Left` for more details.
        """
        self.device(**selectors).swipe.right(steps=steps)

    def swipe_top(self, steps=10, *args, **selectors):
        """
        Swipe the UI object with *selectors* from center to top

        See `Swipe Left` for more details.
        """
        self.device(**selectors).swipe.up(steps=steps)

    def swipe_bottom(self, steps=10, *args, **selectors):
        """
        Swipe the UI object with *selectors* from center to bottom

        See `Swipe Left` for more details.
        """
        self.device(**selectors).swipe.down(steps=steps)

    def object_swipe_left(self, obj, steps=10):
        """
        Swipe the *obj* from center to left

        Example:

        | ${object} | Get Object | description=Home screen 3 | # Get the UI object |
        | Object Swipe Left | ${object} | | # Swipe the UI object left |
        | Object Swipe Left | ${object} | 5 | # Swipe the UI object left with steps=5 |
        | Object Swipe Left | ${object} | steps=5 | # Swipe the UI object left with steps=5 |

        See `introduction` for details about identified UI object.
        """
        obj.swipe.left(steps=steps)

    def object_swipe_right(self, obj, steps=10):
        """
        Swipe the *obj* from center to right

        See `Object Swipe Left` for more details.
        """
        obj.swipe.right(steps=steps)

    def object_swipe_top(self, obj, steps=10):
        """
        Swipe the *obj* from center to top

        See `Object Swipe Left` for more details.
        """
        obj.swipe.up(steps=steps)

    def object_swipe_bottom(self, obj, steps=10):
        """
        Swipe the *obj* from center to bottom

        See `Object Swipe Left` for more details.
        """
        obj.swipe.down(steps=steps)

    def drag_by_coordinates(self,sx, sy, ex, ey, steps=10):
        """
        Drag from (sx, sy) to (ex, ey) with steps

        See `Swipe By Coordinates` also.
        """
        self.device.drag(sx, sy, ex, ey, steps)

    #Wait until the specific ui object appears or gone

    # wait until the ui object appears
    def wait_for_exists(self, timeout=0, *args, **selectors):
        """
        true means the object which has *selectors* exist
        false means the object does not exist
        in the given timeout
        """
        return self.device(**selectors).wait.exists(timeout=timeout)

    # wait until the ui object gone
    def wait_until_gone(self, timeout=0, *args, **selectors):
        """
        true means the object which has *selectors* disappear
        false means the object exist
        in the given timeout
        """
        return self.device(**selectors).wait.gone(timeout=timeout)

    def wait_for_object_exists(self, obj, timeout=0):
        """
        true means the object exist
        false means the object does not exist
        in the given timeout
        """
        return obj.wait.exists(timeout=timeout)

    # wait until the ui object gone
    def wait_until_object_gone(self, obj, timeout=0):
        """
        true means the object disappear
        false means the object exist
        in the given timeout
        """
        return obj.wait.gone(timeout=timeout)


    # Perform fling on the specific ui object(scrollable)
    def fling_forward_horizontally(self, *args, **selectors):
        """
        return whether the object can be fling or not
        """
        return self.device(**selectors).fling.horiz.forward()

    def fling_backward_horizontally(self, *args, **selectors):
        """
        return whether the object can be fling or not
        """
        return self.device(**selectors).fling.horiz.backward()

    def fling_forward_vertically(self, *args, **selectors):
        """
        return whether the object can be fling or not
        """
        return self.device(**selectors).fling.vert.forward()

    def fling_backward_vertically(self, *args, **selectors):
        """
        return whether the object can be fling or not
        """
        return self.device(**selectors).fling.vert.backward()

    # Perform scroll on the specific ui object(scrollable)

    def scroll_to_beginning_vertically(self, steps=10, **selectors):
        """
        """
        return self.device(**selectors).scroll.vert.toBeginning(steps=steps)

    def scroll_to_end_vertically(self, steps=10, **selectors):
        """
        """
        return self.device(**selectors).scroll.vert.toEnd(steps=steps)

    def scroll_object_to_beginning_vertically(self, obj, steps=10):
        """
        """
        return obj.scroll.vert.toBeginning(steps=steps)

    def scroll_object_to_end_vertically(self, obj, steps=10):
        """
        """
        return obj.scroll.vert.toEnd(steps=steps)

    def scroll_forward_horizontally(self, obj, steps=10):
        """
        return whether the object can be scroll or not
        """
        return obj.scroll.horiz.forward(steps=steps)

    def scroll_backward_horizontally(self, obj, steps=10):
        """
        return whether the object can be scroll or not
        """
        return obj.scroll.horiz.backward(steps=steps)

    def scroll_to_horizontally(self, obj, *args,**selectors):
        """
        return whether the object can be scroll or not
        """
        return obj.scroll.horiz.to(**selectors)

    def scroll_forward_vertically(self, obj, steps=10):
        """
        return whether the object can be scroll or not
        """
        return obj.scroll.vert.forward(steps=steps)

    def scroll_backward_vertically(self, obj, steps=10):
        """
        return whether the object can be scroll or not
        """
        return obj.scroll.vert.backward(steps=steps)

    def scroll_to_vertically(self, obj, *args, **selectors):
        """
        return whether the object exists or not
        """
        return obj.scroll.vert.to(**selectors)

#Screen Actions of the device

    def get_screen_orientation(self):
        """
        Get the screen orientation.

        Possible result: natural, left, right, upsidedown

        See for more details: https://github.com/xiaocong/uiautomator#screen-actions-of-the-device
        """
        return self.device.orientation

    def set_screen_orientation(self, orientation):
        """
        Set the screen orientation.

        Input *orientation* : natural or n, left or l, right or r, upsidedown (support android version above 4.3)

        The keyword will unfreeze the screen rotation first.

        See for more details: https://github.com/xiaocong/uiautomator#screen-actions-of-the-device

        Example:

        | Set Screen Orientation | n | # Set orientation to natural |
        | Set Screen Orientation | natural | # Do the same thing  |
        """
        self.device.orientation = orientation

    def freeze_screen_rotation(self):
        """
        Freeze the screen auto rotation
        """
        self.device.freeze_rotation()

    def unfreeze_screen_rotation(self):
        """
        Un-Freeze the screen auto rotation
        """
        self.device.freeze_rotation(False)

    def screenshot(self, scale=None, quality=None):
        """
        Take a screenshot of device and log in the report with timestamp, scale for screenshot size and quality for screenshot quality
        default scale=1.0 quality=100
        """
        output_dir = BuiltIn().get_variable_value('${OUTPUTDIR}')
        ts = time.time()
        st = datetime.datetime.fromtimestamp(ts).strftime('%Y%m%d%H%M%S')
        screenshot_path = '%s%s%s.png' % (output_dir, os.sep, st)
        self.device.screenshot(screenshot_path, scale, quality)
        logger.info('\n<a href="%s">%s</a><br><img src="%s">' % (screenshot_path, st, screenshot_path), html=True)

#Watcher
#     def register_click_watcher(self, watcher_name, selectors, *condition_list):
#         """
#         The watcher click on the object which has the selectors when conditions match
#         """
#         print type(selectors)
#         watcher = self.device.watcher(watcher_name)
#         for condition in condition_list:
#             watcher.when(**condition)
#         watcher.click(**selectors)
#         self.device.watchers.run()
#         print 'register watcher:%s' % watcher_name
#         return

    def __unicode_to_dict(self, a_unicode):
        a_dict = dict()
        dict_item_count = a_unicode.count('=')
        for count in range(dict_item_count):
            equal_sign_position = a_unicode.find('=')
            comma_position = a_unicode.find(',')
            a_key = a_unicode[0:equal_sign_position]
            if comma_position == -1:
                a_value = a_unicode[equal_sign_position + 1:]
            else:
                a_value = a_unicode[equal_sign_position + 1:comma_position]
                a_unicode = a_unicode[comma_position + 1:]
            a_dict[a_key] = a_value
        return a_dict

    def register_click_watcher(self, watcher_name, selectors, *condition_list):
        """
        The watcher click on the object which has the *selectors* when conditions match
        """
        watcher = self.device.watcher(watcher_name)
        for condition in condition_list:
            watcher.when(**self.__unicode_to_dict(condition))
        watcher.click(**self.__unicode_to_dict(selectors))
        self.device.watchers.run()

    def register_press_watcher(self, watcher_name, press_keys, *condition_list):
        """
        The watcher perform *press_keys* action sequentially when conditions match
        """
        def unicode_to_list(a_unicode):
            a_list = list()
            comma_count = a_unicode.count(',')
            for count in range(comma_count + 1):
                comma_position = a_unicode.find(',')
                if comma_position == -1:
                    a_list.append(str(a_unicode))
                else:
                    a_list.append(a_unicode[0:comma_position])
                    a_unicode = a_unicode[comma_position + 1:]
            return a_list

        watcher = self.device.watcher(watcher_name)
        for condition in condition_list:
            watcher.when(**self.__unicode_to_dict(condition))
        watcher.press(*unicode_to_list(press_keys))
        self.device.watchers.run()

    def remove_watchers(self, watcher_name = None):
        """
        Remove watcher with *watcher_name* or remove all watchers
        """
        if watcher_name == None:
            self.device.watchers.remove()
        else:
            self.device.watchers.remove(watcher_name)

    def list_all_watchers(self):
        """
        Return the watcher list
        """
        return self.device.watchers

#Selector

    def get_object(self, *args, **selectors):
        """
        Get the UI object with selectors *selectors*

        See `introduction` for details about identified UI object.
        """
        return self.device(*args, **selectors)

    def get_count(self, *args, **selectors):
        """
        Return the count of UI object with *selectors*

        Example:

        | ${count} | Get Count | text=Accessibility | # Get the count of UI object text=Accessibility |
        | ${accessibility_text} | Get Object | text=Accessibility | # These two keywords combination |
        | ${count} | Get Count Of Object | ${accessibility_text} | # do the same thing. |

        """
        obj = self.get_object(**selectors)
        return self.get_count_of_object(obj)

    def get_count_of_object(self, obj):
        """
        Return the count of given UI object

        See `Get Count` for more details.
        """
        return len(obj)

    def get_info_of_object(self, obj, selector=None):
        """
        return info dictionary of the *obj*
        The info example:
        {
         u'contentDescription': u'',
         u'checked': False,
         u'scrollable': True,
         u'text': u'',
         u'packageName': u'com.android.launcher',
         u'selected': False,
         u'enabled': True,
         u'bounds': 
                   {
                    u'top': 231,
                    u'left': 0,
                    u'right': 1080,
                    u'bottom': 1776
                   },
         u'className': u'android.view.View',
         u'focusable': False,
         u'focused': False,
         u'clickable': False,
         u'checkable': False,
         u'chileCount': 1,
         u'longClickable': False,
         u'visibleBounds':
                          {
                           u'top': 231,
                           u'left': 0,
                           u'right': 1080,
                           u'bottom': 1776
                          }
        }
        """
        if selector:
            return obj.info.get(selector)
        else:
            return obj.info

    def click(self, *args, **selectors):
        """
        click on the UI object with *selectors*
        """
        self.device(**selectors).click()

    def long_click(self, *args, **selectors):
        """
        click on the UI object with *selectors*
        """
        self.device(**selectors).long_click()

    def call(self, obj, method, *args, **selectors):
        """
        This keyword can use object method from original python uiautomator

        See more details from https://github.com/xiaocong/uiautomator

        Example:

        | ${accessibility_text} | Get Object | text=Accessibility | # Get the UI object |
        | Call | ${accessibility_text} | click | # Call the method of the UI object 'click' |
        """
        func = getattr(obj, method)
        return func(**selectors)

    def set_text(self, input_text, *args, **selectors):
        """
        Set *input_text* to the UI object with *selectors* 
        """
        self.device(**selectors).set_text(input_text)

# Other feature

    def clear_text(self, *args, **selectors):
        """
        Clear text of the UI object  with *selectors*
        """
        while True:
            target = self.device(**selectors)
            text = target.info['text']
            target.clear_text()
            remain_text = target.info['text']
            if text == ''  or remain_text == text:
                break

    def open_notification(self):
        """
        open notification

        Built in support for Android 4.3 (API level 18)

        Using swipe action as a workaround for API level lower than 18

        """
        sdk_version = self.device.info['sdkInt']
        if sdk_version < 18:
            height = self.device.info['displayHeight']
            self.device.swipe(1, 1, 1, height - 1, 1)
        else:
            self.device.open.notification()

    def open_quick_settings(self):
        """
        open quick settings

        Work for Android 4.3 above (API level 18)

        """
        self.device.open.quick_settings()

    def sleep(self, time):
        """
        Sleep(no action) for *time* (in millisecond)
        """
        target = 'wait for %s' % str(time)
        self.device(text=target).wait.exists(timeout=time)

    def install(self, apk_path):
        """
        Install apk to the device
        """
        self.adb.cmd('install "%s"' % apk_path)

    def uninstall(self, package_name):
        """
        Uninstall the APP with *package_name*
        """
        self.adb.cmd('uninstall %s' % package_name)

    def execute_adb_command(self, cmd):
        """
        Execute adb *cmd*
        """
        return self.adb.cmd(cmd)

    def execute_adb_shell_command(self,cmd):
        """
        Execute adb shell *cmd*
        """
        return self.adb.shell_cmd(cmd)

    def type(self, input_text, **selectors):
        """
        Type *text* at current focused UI object
        """
        self.test_helper.send_set_text_cmd(input_text)

    def start_test_agent(self):
        """
        [Test Agent]
        Start Test Agent Service
        """
        cmd = 'am start edu.ntut.csie.sslab1321.testagent/edu.ntut.csie.sslab1321.testagent.DummyActivity'
        self.adb.shell_cmd(cmd)

    def stop_test_agent(self):
        """
        [Test Agent]
        Stop Test Agent Service
        """
        cmd = 'am broadcast -a testagent -e action STOP_TESTAGENT'
        self.adb.shell_cmd(cmd)

    def connect_to_wifi(self, ssid, password):
        """
        [Test Agent]
        Connect to *ssid* with *password*
        """
        cmd = 'adb shell am start edu.ntut.csie.sslab1321.testagent/edu.ntut.csie.sslab1321.testagent.DummyActivity'
        cmd = 'adb shell am broadcast -a testagent -e action CONNECT_TO_WIFI -e ssid WEP -e password 12345'
        cmd = 'am broadcast -a testagent -e action CONNECT_TO_WIFI -e ssid %s -e password %s' % (ssid, password)
        self.adb.shell_cmd(cmd)

    def clear_connected_wifi(self):
        """
        [Test Agent]
        Clear all existed Wi-Fi connection
        """
        cmd = 'am broadcast -a testagent -e action CLEAR_CONNECTED_WIFIS'
        self.adb.shell_cmd(cmd)

    def foo(self):
        pass
#         logger.info('\nGot arg %s %s' % (output_dir, st), also_console=True)
#         clm = CommandLineWriter()
        # output some messages on console
#         clm.message(' ')
#         clm.message(u'中文')
#         clm.message(u'2----------2')

    def test(self):
        pass
Beispiel #8
0
class UiAutomator(object):
    def __init__(self, serial=None):
        self.device = Device(serial)
        self.selector_mapping = {'text': 'text',
                                 'id': 'resourceId',
                                 'desc': 'description',
                                 'class': 'className',
                                 'index': 'index'}

    def do_action(self, **kwargs):
        action_name = kwargs.get('name').lower()
        if not action_name:
            return False
        elif action_name == 'press':
            return self.__press(**kwargs)
        elif action_name == 'click':
            return self.__click(**kwargs)
        elif action_name == 'random_click':
            return self.__random_click(**kwargs)
        elif action_name == 'edit':
            return self.__edit(**kwargs)
        elif action_name == 'wait':
            return self.__wait(**kwargs)
        elif action_name == 'wakeup':
            return self.__wakeup()
        elif action_name == 'sleep':
            return self.__sleep()
        elif action_name == 'objectinfo':
            return self.__get_object_info(**kwargs)
        elif action_name == 'orientation':
            return self.__orientation(**kwargs)
        elif action_name == 'get_current_package_name':
            return self.__get_current_package_name()
        elif action_name == 'open':
            return self.__open(**kwargs)
        elif action_name == 'exists':
            return self.__exists(**kwargs)
        elif action_name == 'return_to':
            return self.__return_to(**kwargs)

    def __get_selector(self, **kwargs):
        tmp = dict()
        for key in self.selector_mapping.keys():
            parameter_value = kwargs.get(key)
            if parameter_value:
                tmp[self.selector_mapping.get(key)] = parameter_value
        return tmp

    def __click(self, **kwargs):
        selector = self.__get_selector(**kwargs)
        return self.device(**selector).click()

    def __exists(self, **kwargs):
        selector = self.__get_selector(**kwargs)
        return self.device(**selector).exists

    def __random_click(self, **kwargs):
        selector = self.__get_selector(**kwargs)
        count = self.device(**selector).count
        if count <= 0:
            return False
        selector['instance'] = randint(0, count-1)
        self.device(**selector).click()




    def __press(self, **kwargs):
        key = kwargs.get('key')
        if key in ["home", "back", "left", "right", "up", "down", "center", "menu", "search", "enter",
                   "delete", "del", "recent", "volume_up", "volume_down", "volume_mute", "camera", "power"]:
            return self.device.press.__getattr__(key)()
        else:
            try:
                key_code = int(key)
                return self.device.press(key_code)
            except ValueError:
                return False

    def __open(self,**kwargs):
        key = kwargs.get('key')
        if key in ["notification", "quick_settings"]:
            return self.device.open.__getattr__(key)()
        return False

    def __orientation(self, **kwargs):
        # left/l:       rotation=90 , displayRotation=1
        # right/r:      rotation=270, displayRotation=3
        # natural/n:    rotation=0  , displayRotation=0
        # upsidedown/u: rotation=180, displayRotation=2
        value = kwargs.get('value')
        if value in ['l', 'r', 'n', 'u', 'left', 'right', 'natural', 'upsidedown']:
            self.device.orientation = value
            return True
        return False

    def __freeze_rotation(self, **kwargs):
        value = kwargs.get('value', 'true')
        if value in ['False', 'false', 'f']:
            return self.device.freeze_rotation(freeze=False)
        return self.device.freeze_rotation(True)

    def __edit(self, **kwargs):
        selector = self.__get_selector(**kwargs)
        text_input = kwargs.get('input')
        return self.device(**selector).set_text(text=text_input)

    def __get_object_info(self, **kwargs):
        selector = self.__get_selector(**kwargs)
        key = kwargs.get('key')
        if key:
            return self.device(**selector).info.get(key)
        return self.device(**selector).info

    def __wait(self, **kwargs):
        try:
            wait_time = int(kwargs.get('time'))
            sleep(wait_time/1000.0)
        except ValueError:
            sleep(1)
        return True

    def __wakeup(self):
        return self.device.wakeup()

    def __sleep(self):
        return self.device.sleep()


    # def info(self):
    #     return self.device.info
    #
    # def dump(self, filename=None, compressed=True, pretty=True):
    #     Utility.output_msg('Dump device window and pull to \"%s\".' % filename)
    #     return self.device.dump(filename=filename, compressed=compressed, pretty=pretty)
    #
    # def screenshot(self, filename, scale=1.0, quality=100):
    #     Utility.output_msg('Take screenshot and save to \"%s\".' % filename)
    #     return self.device.screenshot(filename=filename, scale=scale, quality=quality)
    #

    #
    # def long_click(self, **kwargs):
    #     try:
    #         return self.device(**kwargs).long_click()
    #     except JsonRPCError, e:
    #         return 'Error'
    #
    # def scroll(self, **kwargs):
    #     return self.device(**kwargs).scroll(steps=steps)
    #
    def __get_current_package_name(self):
        return self.device.info.get('currentPackageName')

    def get_device_info(self):
        return self.device.info

    def __return_to(self, **kwargs):
        selector = self.__get_selector(**kwargs)
        for x in range(10):
            if self.device(**selector).exists:
                break
            self.device.press.back()
Beispiel #9
0
class Mobile():
    """
    robotframework-uiautomatorlibrary is an Android device testing library for Robot Framework.

    It uses uiautomator - Python wrapper for Android uiautomator tool (https://pypi.python.org/pypi/uiautomator/0.1.28) internally.

    *Before running tests*

    You can use `Set Serial` to specify which device to perform the test.


    *Identify UI object*

    There are two kinds of keywords.

    
    """

    __version__ = '0.1'
    ROBOT_LIBRARY_DOC_FORMAT = 'ROBOT'
    ROBOT_LIBRARY_SCOPE = 'GLOBAL'

    def set_serial(self, android_serial):
        """
        Specify given *android_serial* device to perform test.

        You do not have to specify the device when there is only one device connects to the computer.

        When you need to use multiple devices, do not use this keyword to switch between devices in test execution.

        Using different library name when importing this library according to http://robotframework.googlecode.com/hg/doc/userguide/RobotFrameworkUserGuide.html?r=2.8.4#setting-custom-name-to-test-library.

        | Setting | Value |  Value |  Value | 
        | Library | Mobile | WITH NAME | Mobile1 |
        | Library | Mobile | WITH NAME | Mobile2 |

        And set the serial to each library.

        | Test Case        | Action             | Argument           |
        | Multiple Devices | Mobile1.Set Serial | device_1's serial  |
        |                  | Mobile2.Set Serial | device_2's serial  |

        """
        self.adb = ADB(android_serial)
        self.device = Device(android_serial)
        self.test_helper = TestHelper(self.adb)

    def get_device_info(self):
        """
        Retrieve the device info.

        The keyword will return a dictionary.

        You can log the information by using the log dictionary keyword in build in Collections library(http://robotframework.googlecode.com/hg/doc/libraries/Collections.html?r=2.8.4).

        Example:
        | ${device_info} | Get Device Info |
        | Log Dictionary | ${device_info}  |

        =>

        Dictionary size is 9 and it contains following items:\n
        currentPackageName: com.android.keyguard\n
        displayHeight: 1776\n
        displayRotation: 0\n
        displaySizeDpX: 360\n
        displaySizeDpY: 640\n
        displayWidth: 1080\n
        naturalOrientation: True\n
        productName: hammerhead\n
        sdkInt: 19\n

        Or get specific information of the device by giving the key.

        | ${device_info}  | Get Device Info | | |
        | ${product_name} | Get From Dictionary | ${device_info} | productName |

        =>

        ${product_name} = hammerhead

        """
        return self.device.info

#Key Event Actions of the device
    """
    Turn on/off screen
    """
    def turn_on_screen(self):
        """
        Turn on screen
        """
        self.device.screen.on()

    def turn_off_screen(self):
        """
        Turn off screen
        """
        self.device.screen.off()

    """
    Press hard/soft key
    """

    def press_key(self, *keys):
        """
        Press *key* keycode.

        You can find all keycode in http://developer.android.com/reference/android/view/KeyEvent.html

        Example:

        |Press Key | 
        """
        self.device.press(*keys)

    def press_home(self):
        """
        Press home key
        """
        self.device.press.home()

    def press_back(self):
        """
        Press back key
        """
        self.device.press.back()

    def press_left(self):
        """
        Press left key
        """
        self.device.pres.left()

    def press_right(self):
        """
        Press right key
        """
        self.device.press.right()

    def press_up(self):
        """
        Press up key
        """
        self.device.press.up()

    def press_down(self):
        """
        Press down key
        """
        self.device.press.down()

    def press_center(self):
        """
        Press center key
        """
        self.device.press.center()

    def press_menu(self):
        """
        Press menu key
        """
        self.device.press.menu()

    def press_search(self):
        """
        Press search key
        """
        self.device.press.search()

    def press_enter(self):
        """
        Press enter key
        """
        self.device.press.enter()

    def press_delete(self):
        """
        Press delete key
        """
        self.device.press.delete()

    def press_recent(self):
        """
        Press recent key
        """
        self.device.press.recent()

    def press_volume_up(self):
        """
        Press volume up key
        """
        self.device.press.volume_up()

    def press_volume_down(self):
        """
        Press volume down key
        """
        self.device.press.volume_down()

    def press_camera(self):
        """
        Press camera key
        """
        self.device.press.camera()

    def press_power(self):
        """
        Press power key
        """
        self.device.press.power()

#Gesture interaction of the device

    def click_at_coordinates(self, x, y):
        """
        Click at (x,y) coordinates.
        """
        self.device.click(x, y)

    def swipe_by_coordinates(self, sx, sy, ex, ey, steps=100):
        """
        Swipe from (sx, sy) to (ex, ey) with *steps* .
        """
        self.device.swipe(sx, sy, ex, ey, steps)

# Swipe from the center of the ui object to its edge

    def swipe_left(self, steps=100, *args, **attributes):
        """
        Swipe the UI object with *attributes* from center to left.
        """
        self.device(**attributes).swipe.left(steps=steps)

    def swipe_right(self, steps=100, *args, **attributes):
        """
        Swipe the UI object with *attributes* from center to right
        """
        self.device(**attributes).swipe.right(steps=steps)

    def swipe_top(self, steps=100, *args, **attributes):
        """
        Swipe the UI object with *attributes* from center to top
        """
        self.device(**attributes).swipe.up(steps=steps)

    def swipe_bottom(self, steps=100, *args, **attributes):
        """
        Swipe the UI object with *attributes* from center to bottom
        """
        self.device(**attributes).swipe.down(steps=steps)

    def object_swipe_left(self, obj, steps=100):
        """
        Swipe the *obj* from center to left
        """
        obj.swipe.left(steps=steps)

    def object_swipe_right(self, obj, steps=100):
        """
        Swipe the *obj* from center to right
        """
        obj.swipe.right(steps=steps)

    def object_swipe_top(self, obj, steps=100):
        """
        Swipe the *obj* from center to top
        """
        obj.swipe.up(steps=steps)

    def object_swipe_bottom(self, obj, steps=100):
        """
        Swipe the *obj* from center to bottom
        """
        obj.swipe.down(steps=steps)

    def drag(self,sx, sy, ex, ey, steps=100):
        """
        drag from (sx, sy) to (ex, ey) with steps
        """
        self.device.drag(sx, sy, ex, ey, steps)

    #Wait until the specific ui object appears or gone

    # wait until the ui object appears
    def wait_for_exists(self, timeout=0, *args, **attribute):
        """
        true means the object which has *attribute* exist
        false means the object does not exist
        in the given timeout
        """
        return self.device(**attribute).wait.exists(timeout=timeout)

    # wait until the ui object gone
    def wait_until_gone(self, timeout=0, *args, **attribute):
        """
        true means the object which has *attribute* disappear
        false means the object exist
        in the given timeout
        """
        return self.device(**attribute).wait.gone(timeout=timeout)

    def wait_for_object_exists(self, obj, timeout=0):
        """
        true means the object exist
        false means the object does not exist
        in the given timeout
        """
        return obj.wait.exists(timeout=timeout)

    # wait until the ui object gone
    def wait_until_object_gone(self, obj, timeout=0):
        """
        true means the object disappear
        false means the object exist
        in the given timeout
        """
        return obj.wait.gone(timeout=timeout)


    # Perform fling on the specific ui object(scrollable)
    def fling_forward_horizontally(self, obj):
        """
        return whether the object can be fling or not
        """
        return obj.fling.horiz.forward()

    def fling_backward_horizontally(self, obj):
        """
        return whether the object can be fling or not
        """
        return obj.fling.horiz.backward()

    def fling_forward_vertically(self, obj):
        """
        return whether the object can be fling or not
        """
        return obj.fling.vert.forward()

    def fling_backward_vertically(self, obj):
        """
        return whether the object can be fling or not
        """
        return obj.fling.vert.backward()

    # Perform scroll on the specific ui object(scrollable)

    def scroll_to_beginning_vertically(self, steps=10, **attributes):
        """
        """
        return self.device(**attributes).scroll.vert.toBeginning(steps=steps)

    def scroll_to_end_vertically(self, steps=10, **attributes):
        """
        """
        return self.device(**attributes).scroll.vert.toEnd(steps=steps)

    def scroll_object_to_beginning_vertically(self, obj, steps=10):
        """
        """
        return obj.scroll.vert.toBeginning(steps=steps)

    def scroll_object_to_end_vertically(self, obj, steps=10):
        """
        """
        return obj.scroll.vert.toEnd(steps=steps)

    def scroll_forward_horizontally(self, obj, steps=10):
        """
        return whether the object can be scroll or not
        """
        return obj.scroll.horiz.forward(steps=steps)

    def scroll_backward_horizontally(self, obj, steps=10):
        """
        return whether the object can be scroll or not
        """
        return obj.scroll.horiz.backward(steps=steps)

    def scroll_to_horizontally(self, obj, *args,**attribute):
        """
        return whether the object can be scroll or not
        """
        return obj.scroll.horiz.to(**attribute)

    def scroll_forward_vertically(self, obj, steps=10):
        """
        return whether the object can be scroll or not
        """
        return obj.scroll.vert.forward(steps=steps)

    def scroll_backward_vertically(self, obj, steps=10):
        """
        return whether the object can be scroll or not
        """
        return obj.scroll.vert.backward(steps=steps)

    def scroll_to_vertically(self, obj, *args, **attribute):
        """
        return whether the object exists or not
        """
        return obj.scroll.vert.to(**attribute)

#Screen Actions of the device

    def screenshot(self, scale=None, quality=None):
        """
        Take a screenshot of device and log in the report with timestamp, scale for screenshot size and quality for screenshot quality
        default scale=1.0 quality=100
        """
        output_dir = BuiltIn().get_variable_value('${OUTPUTDIR}')
        ts = time.time()
        st = datetime.datetime.fromtimestamp(ts).strftime('%Y%m%d%H%M%S')
        screenshot_path = '%s%s%s.png' % (output_dir, os.sep, st)
        self.device.screenshot(screenshot_path, scale, quality)
        logger.info('\n<a href="%s">%s</a><br><img src="%s">' % (screenshot_path, st, screenshot_path), html=True)

#Watcher
#     def register_click_watcher(self, watcher_name, attributes, *condition_list):
#         """
#         The watcher click on the object which has the attributes when conditions match
#         """
#         print type(attributes)
#         watcher = self.device.watcher(watcher_name)
#         for condition in condition_list:
#             watcher.when(**condition)
#         watcher.click(**attributes)
#         self.device.watchers.run()
#         print 'register watcher:%s' % watcher_name
#         return

    def __unicode_to_dict(self, a_unicode):
        a_dict = dict()
        dict_item_count = a_unicode.count('=')
        for count in range(dict_item_count):
            equal_sign_position = a_unicode.find('=')
            comma_position = a_unicode.find(',')
            a_key = a_unicode[0:equal_sign_position]
            if comma_position == -1:
                a_value = a_unicode[equal_sign_position + 1:]
            else:
                a_value = a_unicode[equal_sign_position + 1:comma_position]
                a_unicode = a_unicode[comma_position + 1:]
            a_dict[a_key] = a_value
        return a_dict

    def register_click_watcher(self, watcher_name, attributes, *condition_list):
        """
        The watcher click on the object which has the *attributes* when conditions match
        """
        watcher = self.device.watcher(watcher_name)
        for condition in condition_list:
            watcher.when(**self.__unicode_to_dict(condition))
        watcher.click(**self.__unicode_to_dict(attributes))
        self.device.watchers.run()

    def register_press_watcher(self, watcher_name, press_keys, *condition_list):
        """
        The watcher perform *press_keys* action sequentially when conditions match
        """
        def unicode_to_list(a_unicode):
            a_list = list()
            comma_count = a_unicode.count(',')
            for count in range(comma_count + 1):
                comma_position = a_unicode.find(',')
                if comma_position == -1:
                    a_list.append(str(a_unicode))
                else:
                    a_list.append(a_unicode[0:comma_position])
                    a_unicode = a_unicode[comma_position + 1:]
            return a_list

        watcher = self.device.watcher(watcher_name)
        for condition in condition_list:
            watcher.when(**self.__unicode_to_dict(condition))
        watcher.press(*unicode_to_list(press_keys))
        self.device.watchers.run()

    def remove_watchers(self, watcher_name = None):
        """
        Remove watcher with *watcher_name* or remove all watchers
        """
        if watcher_name == None:
            self.device.watchers.remove()
        else:
            self.device.watchers.remove(watcher_name)

    def list_all_watchers(self):
        """
        return the watcher list
        """
        return self.device.watchers

#Selector

    def get_object(self, *args, **attribute):
        """
        Get the ui object with attribute *attribute*
        """
        return self.device(*args, **attribute)

    def get_count_of_object(self, obj):
        """
        Return the count of given *obj*
        """
        return len(obj)

    def get_info_of_object(self, obj, attribute=None):
        """
        return info dictionary of the *obj*
        The info example:
        {
         u'contentDescription': u'',
         u'checked': False,
         u'scrollable': True,
         u'text': u'',
         u'packageName': u'com.android.launcher',
         u'selected': False,
         u'enabled': True,
         u'bounds': 
                   {
                    u'top': 231,
                    u'left': 0,
                    u'right': 1080,
                    u'bottom': 1776
                   },
         u'className': u'android.view.View',
         u'focusable': False,
         u'focused': False,
         u'clickable': False,
         u'checkable': False,
         u'chileCount': 1,
         u'longClickable': False,
         u'visibleBounds':
                          {
                           u'top': 231,
                           u'left': 0,
                           u'right': 1080,
                           u'bottom': 1776
                          }
        }
        """
        if attribute:
            return obj.info.get(attribute)
        else:
            return obj.info

    def click_on(self, *args, **attribute):
        """
        click on the object with *attribute*
        """
        self.device(**attribute).click()

    def long_click_on(self, *args, **attribute):
        """
        click on the object with *attribute*
        """
        self.device(**attribute).long_click()

    def call(self, obj, method, *args, **attribute):
        func = getattr(obj, method)
        return func(**attribute)

    def set_text(self, input_text, *args, **attribute):
        """
        set *text* to the Component which has the *attribute* 
        """
        self.device(**attribute).set_text(input_text)

# Other feature

    def clear_text(self, *args, **attributes):
        """
        Clear text of the component  with *attributes*
        """
        while True:
            target = self.device(**attributes)
            text = target.info['text']
            target.clear_text()
            remain_text = target.info['text']
            if text == ''  or remain_text == text:
                break

    def open_notification(self):
        """
        open notification
        Built in support for Android 4.3 (API level 18)
        Using swipe action as a workaround for API level lower 18
        """
        sdk_version = self.device.info['sdkInt']
        if sdk_version < 18:
            height = self.device.info['displayHeight']
            self.device.swipe(1, 1, 1, height - 1, 1)
        else:
            self.device.open.notification()

    def open_quick_settings(self):
        """
        open quick settings
        Work for Android 4.3 (API level 18)
        """
        self.device.open.quick_settings()

    def sleep(self, time):
        """
        sleep(no action) for *time* (in millisecond)
        """
        target = 'wait for %s' % str(time)
        self.device(text=target).wait.exists(timeout=time)

    def install(self, apk_path):
        """
        Install apk to the device
        """
        self.adb.cmd('install "%s"' % apk_path)

    def uninstall(self, package_name):
        """
        Uninstall the APP with *package_name*
        """
        self.adb.cmd('uninstall %s' % package_name)

    def execute_adb_command(self, cmd):
        """
        Execute adb *cmd*
        """
        self.adb.cmd(cmd)

    def execute_adb_shell_command(self,cmd):
        """
        Execute adb shell *cmd*
        """
        self.adb.shell_cmd(cmd)

    def type(self, text):
        """
        Type *text* at current focused component
        """
        self.test_helper.send_set_text_cmd(text)

    def start_test_agent(self):
        """
        [Test Agent]
        Start Test Agent Service
        """
        cmd = 'am start edu.ntut.csie.sslab1321.testagent/edu.ntut.csie.sslab1321.testagent.DummyActivity'
        self.adb.shell_cmd(cmd)

    def stop_test_agent(self):
        """
        [Test Agent]
        Stop Test Agent Service
        """
        cmd = 'am broadcast -a testagent -e action STOP_TESTAGENT'
        self.adb.shell_cmd(cmd)

    def connect_to_wifi(self, ssid, password):
        """
        [Test Agent]
        Connect to *ssid* with *password*
        """
        cmd = 'adb shell am start edu.ntut.csie.sslab1321.testagent/edu.ntut.csie.sslab1321.testagent.DummyActivity'
        cmd = 'adb shell am broadcast -a testagent -e action CONNECT_TO_WIFI -e ssid WEP -e password 12345'
        cmd = 'am broadcast -a testagent -e action CONNECT_TO_WIFI -e ssid %s -e password %s' % (ssid, password)
        self.adb.shell_cmd(cmd)

    def clear_connected_wifi(self):
        """
        [Test Agent]
        Clear all existed Wi-Fi connection
        """
        cmd = 'am broadcast -a testagent -e action CLEAR_CONNECTED_WIFIS'
        self.adb.shell_cmd(cmd)

    def foo(self):
        pass
#         logger.info('\nGot arg %s %s' % (output_dir, st), also_console=True)
#         clm = CommandLineWriter()
        # output some messages on console
#         clm.message(' ')
#         clm.message(u'中文')
#         clm.message(u'2----------2')

    def test(self):
        pass
Beispiel #10
0
        #     lstResult = tv(resourceId = 'com.xiaomi.voicecontrol:id/lstResult')
        #     result_count = lstResult.childCount
        #     res_tit_list = []
        #     for t in range(result_count):
        #         print t,
        #         res_tit = lstResult.child(resourceId = 'com.xiaomi.voicecontrol:id/titleBar',instance = t).child(resourceId = 'com.xiaomi.voicecontrol:id/title').text
        #         print res_tit,
        #         res_tit_list.append(res_tit)
        #     dict_w['title'] = res_tit_list
        #     dict_w['tag'] = 'result_list'
        #     dict_w['recognition'] = tv(resourceId = 'com.xiaomi.voicecontrol:id/title').text
        #     dict_w['result_msg'] = tv(resourceId = 'com.xiaomi.voicecontrol:id/txtTitle').text
        tv.dump("%s.xml" % (f_name))
        print "\tDump... "
        screencapAndPullOut(f_name + ".png")
        tv.press('back')
        print "Done.\n*-*-*-*-*-*-*-*-*-*-*-*-*-*-"
    except:
        # dict_w['tag'] = 'occur_err'
        print "occur_err"
        pass
    # f_result.write(str(dict_w).encode('utf-8'))
    query = f_query.readline()
    if os.path.exists(f_name + ".png") and os.path.exists(f_name + ".xml"):
        pass
    else:
        f_fail = open(f_name + ".txt", 'w')
        f_fail.close()

print "*** All clear ***"
# f_result.close()
Beispiel #11
0
class UiTestLib(object):
    """Ui Test Lib

    """

    def __init__(self, serial=None):
        """
        """
        logger.info("<p>Device=%s>" % serial, html=True)
        print "<p>Device=%s>" % serial
        self._result = ""
        self.starttime = 0
        self.d = Device(serial)
        self.adb = Adb(serial)
        self.debug = "True"

    def set_debugable(flag):
        self.debug = flag

    def set_serial(self, serial):
        """Specify given *serial* device to perform test.
        or export ANDROID_SERIAL=CXFS42343 if you have many devices connected but you don't use this
        interface

        When you need to use multiple devices, do not use this keyword to switch between devices in test execution.
        And set the serial to each library.
        Using different library name when importing this library according to
        http://robotframework.googlecode.com/hg/doc/userguide/RobotFrameworkUserGuide.html?r=2.8.5.

        Examples:
        | Setting | Value |  Value |  Value |
        | Library | UiTestLib | WITH NAME | Mobile1 |
        | Library | UiTestLib | WITH NAME | Mobile2 |

        And set the serial to each library.
        | Test Case        | Action             | Argument           |
        | Multiple Devices | Mobile1.Set Serial | device_1's serial  |
        |                  | Mobile2.Set Serial | device_2's serial  |
        """
        self.d = Device(serial)
        self.adb = Adb(serial)

    def logmsg(self, msg):
        if self.debug == "True":
            print msg

    def exe_adb_command(self, cmd):
        """ Execute adb *cmd*
         Examples:
        | Exe Adb Command | shell getprop  |
        """
        return self.adb.cmd(cmd).wait()

    def exe_adb_and_result(self, cmd):
        """Execute adb *cmd* and return lines of the command"""
        lproc = self.adb.cmd(cmd)
        lproc.poll()
        lines = lproc.stdout.readlines()
        return lines

    def get_device_info(self):
        """Get Device information
        return info dictionary
        """
        return self.d.info

    def light_screen(self):
        """Light screen by wakeup.

        Examples:
        | Action     |
        |Light screen|

        Use `Light screen` to light screen.
        """

        self.d.wakeup()
        self._result = self.d.press.home()

    def open_application(self, appname):
        """Open application by it name `appname`.

        Example:
        | Action           | Argument      |
        | Open application | "com.android.settings/com.android.settings.Settings" |
        """
        appname = "shell am start -n " + appname
        print "Open Application:", appname
        self._result = self.exe_adb_command(appname)

    def click_text(self, text, instance=0):
        """Click text label on screen
        instance=0 is default, change when you needed.

        Example:
        | Action     | Argument   |  Argument  |
        | Click Text | text | instance |
        """

        return self.d(text=text, instance=instance).click.wait()

    def long_click_text(self, text, instance=0):
        """
        Long Click text label on screen, *text* and *instance=0*

        Example:
        | Action     | Argument   |  Argument  |
        | Long Click Text | text | instance |
        """

        return self.d(text=text, instance=instance).long_click()

    def long_click_ui(self, **selectors):
        """
        Long Click on **selectors**
        Selector supports below parameters. Refer to UiSelector java doc for detailed information.

            text, textContains, textMatches, textStartsWith
            className, classNameMatches
            description, descriptionContains, descriptionMatches, descriptionStartsWith
            checkable, checked, clickable, longClickable
            scrollable, enabled,focusable, focused, selected
            packageName, packageNameMatches
            resourceId, resourceIdMatches
            index, instance
        Example:
        | Action     | Argument   |  Argument  |  Argument  |
        | Long Click UI | text=XXXX | className=XXX.xxxx | resourceId=xxxxxx |
        """

        return self.d(**selectors).long_click()

    def text_display_on_screen_contains(self, text):
        """Verify text display on screen

        Example:
        | Action     | Argument   |
        |Text display on screen is| text |
        """

        if self.d(text=text).exists:
            self._result = True
            return True
        else:
            self._result = False
            return False

    def object_display_on_screen(self, obj, timeout=5000):
        """ Verify *obj* UiObject display on screen
        return to self._result

        Example:
        | Action     | Argument   |
        |Object display on screen | obj |
        """
        if obj.wait.exists(timeout):
            self._result = True
            return True
        else:
            self._result = False
            return False

    def assert_expectation(self, expect, actual):
        """
        Assert Expectation and actual value
        Example:
        | Action             |   args   |   args      |
        | Assert Expectation |   324324 |  ${actual}  |
        """
        if str(expect) != str(actual):
            raise AssertionError("Actual result is = %s, but expectation is: %s" % (str(actual), str(expect)))

    def assert_true(self, condition):
        """
        Assert True of *condition

        Example:
        |Assert True | condition |
        """
        if str(condition) != "True":  # because only string from robotframework
            raise AssertionError("Result is = %s" % str(condition))

    def assert_result_true(self):
        """
        Assert True of *self._result

        Example:
        |Assert True |
        """
        if self._result != True:
            raise AssertionError("Result is = %s" % str(self._result))

    def wait_for_ui_exists(self, timeout, **selectors):
        """
        Return True if
          Selector is to identify specific ui object in current window.

        # To seleted the object ,text is 'Clock' and its className is 'android.widget.TextView'
        wait_for_ui_exists(text='Clock', className='android.widget.TextView')

        Selector supports below parameters. Refer to UiSelector java doc for detailed information.

            text, textContains, textMatches, textStartsWith
            className, classNameMatches
            description, descriptionContains, descriptionMatches, descriptionStartsWith
            checkable, checked, clickable, longClickable
            scrollable, enabled,focusable, focused, selected
            packageName, packageNameMatches
            resourceId, resourceIdMatches
            index, instance

        Examples:
        | Action     | Argument   |  Argument  |  Argument  |  Argument  |
        |Wait For UI Exists | timeout | text=XXXX | className=XXX.xxxx | resourceId=xxxxxx |
        """
        self._result = self.d(**selectors).wait.exists(timeout=int(timeout))
        return self._result

    def wait_and_click(self, timeout, **selectors):
        """Wait for uiselector and click"""
        if self.d(**selectors).wait.exists(timeout=int(timeout)):
            self.d(**selectors).click()

    def assert_ui_exists(self, **selectors):
        """
        Assert UiObject appear on the screen

        Examples:
        | Action     | Argument   |  Argument  |  Argument  |  Argument  |
        |Assert UI Exists | timeout | text=XXXX | className=XXX.xxxx | resourceId=xxxxxx |
        """
        if not self.d(**selectors).wait.exists():
            raise AssertionError("UiObject does not exists %s" % selectors.items())

    def result_should_be(self, expected):
        """Verifies that the current result is `expected`.

        Example:
        | Action     | Argument   |
        |  Open application    | com.android.settings/com.android.settings.Settings |
        |  Result Should Be    | 0       |
        """
        print ("result is: %s\n", self._result)
        print ("ex is: %s\n", expected)
        if str(self._result) != expected:
            raise AssertionError("%s != %s" % (self._result, expected))

    def click_at_coordinates(self, x, y):
        """ Click at (x,y) coordinates.
        Example:
        | Action     | Argument   |  Argument  |
        | Click At Corrdinates | x | y |
        """
        return self.d.click(int(x), int(y))

    def long_click_at_coordinates(self, x, y):
        """
        # long click (x, y) on screen
        """
        return self.d.long_click(int(x), int(y))

    def swipe(self, sx, sy, ex, ey, steps=20):
        """
        Swipe from (sx,sy) to (ex,ey)
        """
        return self.d.swipe(int(sx), int(sy), int(ex), int(ex), int(steps))

    def drag(self, sx, sy, ex, ey, steps=20):
        """
        Drag from (sx,sy) to (ex,ey)
        """
        return self.d.drag(int(sx), int(sy), int(ex), int(ex), int(steps))

    def freeze_rotation(self, rotation=True):
        """
        Freeze rotation,
        *rotation*, True is default,
        """
        return self.d.freeze_rotation(rotation)

    def set_rotation(self, rotation):
        """
        # retrieve orientation,
        # it should be "natural" or "left" or "right" or "upsidedown"

        Example:
        | Action       | Argument   |
        | Set Rotation | nature      |
        | Set Rotation | left        |
        | Set Rotation | right       |
        | Set Rotation | upsidedown  |

        """
        orientation = self.d.orientation
        if rotation == "nature":
            self.d.orientation = "n"  # or "natural"
        elif rotation == "left":
            self.d.orientation = "l"  # or "left"
        elif rotation == "right":
            self.d.orientation = "r"  # or "right"
        elif rotation == "upsidedown":
            self.d.orientation = "upsidedown"  # or "upsidedown"
        else:
            self.d.rotation = "n"

    def take_screenshot(self, scale=None, quality=None):
        """
        Take a screenshot of device and log in the report with timestamp, scale for screenshot size and quality for screenshot quality
        default scale=1.0 quality=100

        Example:
        | Action     | Argument   |  Argument  |
        | Take Screenshot | 0.5 | 80 |
        """
        output_dir = BuiltIn().get_variable_value("${OUTPUTDIR}")
        ts = time.time()
        st = datetime.datetime.fromtimestamp(ts).strftime("%Y%m%d%H%M%S")
        screenshot_path = "%s%s%s.png" % (output_dir, os.sep, st)
        screenshot_name = "%s%s.png" % (os.sep, st)
        self.d.screenshot(screenshot_path, scale, quality)
        logger.info('\n<a href="%s">%s</a><br><img src="%s">' % (screenshot_path, st, screenshot_name), html=True)

    def open_notification(self):
        """
        Open notification of the phone
        """
        self.d.open.notification()

    def wait_window_update(self):
        """
        wait for window update
        """
        return self.d.wait.update()

    def wait_window_idle(self):
        """
        wait for window idle
        """
        return self.d.wait.idle()

    def remove_watchers(self):
        """
        Remove UI watchers
        """
        self.d.watchers.remove()

    def get_device(self):
        """
        Get device object, you can do any command using this
        """
        return self.d

    def click_id(self, id, instance=0):
        """
        Click *id* with *instance*=0
        """
        return self.d(resourceId=id, instance=instance).click.wait()

    def long_click_id(self, id, instance=0):
        """
        Long click *id* with *instance*=0
        """
        return self.d(resourceId=id, instance=0).long_click()

    def click_description(self, description, instance=0):
        """
        Click *description* with *instance*=0
        """
        return self.d(description=description, instance=instance).long_click()

    def click_class(self, className, instance=0):
        """
        Click *className* with *instance*=0
        """
        return self.d(className=className, instance=instance).long_click()

    def type_text(self, textStr, **selectors):
        """
        type text on selectors
        like text=EditName
        """
        self.d(**selectors).set_text(textStr)

    def press_key(self, key):
        """ Press Key of following value
            home
            back
            left
            right
            up
            down
            center
            menu
            search
            enter
            delete(or del)
            recent(recent apps)
            volume_up
            volume_down
            volume_mute
            camera
            power

            Examples:
            | Action     | Argument   |
            | Press Key | home |
            | Press Key | back |
            | Press Key | left |
            | Press Key | right |
            | Press Key | recent |
            | Press Key | volume_up |
            | Press Key | camera |
        """
        if key.isdigit():
            return self.d.press(int(key))
        return self.d.press(key)

    def phone_sleep(self, timeout):
        """
        android device sleep with timeout in ms, don't use for executor sleep,
        """
        return self.d.wait(int(timeout))

    def execute_command(self, cmd, block_parent_process=True):
        """
        Execute shell *cmd* command, with block_parent_process = True

        If block_parent_process = False, kill_command is needed to terminal the child process

        Example:
        | Execute Command | ping -c 5 127.0.0.1 |
        """
        if str(block_parent_process) == str(True):
            return subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
        else:
            return subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    def kill_command(self, process, timeout=0):
        """ Kill child *process* in timeout seconds
        Some bugs after process teminated
        """
        time.sleep(float(timeout))
        process.terminate()

    def wait_for_logcat(self, log, timeout=30):
        """ Wait log exists in given timeout, return True if have, otherwise will return False
        log is your want to search, log can be a regular expression
        time in seconds, default value is 30 seconds

        Example:
        | Wait For Logcat | .*ActivityManager.*cmp=com.sonyericsson.album/com.sonyericsson.album.Main.* |
        """
        start = time.time()
        while (time.time() - start) < int(timeout):
            log_proc = self.adb.cmd("logcat -d -v time")
            returncode = log_proc.poll()
            lines = log_proc.stdout.readlines()
            for line in lines:
                rlt = re.search(log, line.rstrip())
                if rlt is not None:
                    print rlt.group(0)
                    self._result = True
                    return True
            time.sleep(2)  # sleep 2s to wait
        self._result = False
        return False

    def clear_logcat(self):
        """
        Clear logcat, it's often used before you want to use *wait_for_logcat()*
        """
        print "Clear logcat before test"
        return self.exe_adb_command("logcat -c")

    def click_object(self, obj):
        """ Click UiObject *obj*

        Exmaple:
        | ${result}    | Get UI Object | text=XXX | className=xxxx |
        | Click Object |   ${result}   |
        """
        obj.click()

    def click_ui(self, **selectors):
        """
        Click selector

        click_selector(text="Home", resourceId = "android:id/title")
        for **selector, please refer *get_ui_object()*
        Selector supports below parameters. Refer to UiSelector java doc for detailed information.

            text, textContains, textMatches, textStartsWith
            className, classNameMatches
            description, descriptionContains, descriptionMatches, descriptionStartsWith
            checkable, checked, clickable, longClickable
            scrollable, enabled,focusable, focused, selected
            packageName, packageNameMatches
            resourceId, resourceIdMatches
            index, instance
        Operation for a UiObjects:
        click, clear_text, drag(obj).to(**selector), gesture, exists, set_text, long_click
        pinch.In(percent=100, steps=10), pinch.Out(percent=100, steps=100),.swipe.right(),
        .swipe.left(steps=10),.swipe("right", steps=20),.wait.gone(),.wait.exists()
        """
        print "selectors:", selectors
        return self.d(**selectors).click()

    def get_ui_object(self, **selectors):
        """
        Get UI object with *selectors*
        you can do anything on UiObject, like click(), long_click(),wait.exists(timeout)
        examples: get_ui_object(text="Home", resourceId = "android:id/title")

        Selector supports below parameters. Refer to UiSelector java doc for detailed information.

            text, textContains, textMatches, textStartsWith
            className, classNameMatches
            description, descriptionContains, descriptionMatches, descriptionStartsWith
            checkable, checked, clickable, longClickable
            scrollable, enabled,focusable, focused, selected
            packageName, packageNameMatches
            resourceId, resourceIdMatches
            index, instance
        Operation for a UiObjects:
        click, clear_text, drag(obj).to(**selector), gesture, exists, set_text, long_click
        pinch.In(percent=100, steps=10), pinch.Out(percent=100, steps=100),.swipe.right(),
        .swipe.left(steps=10),.swipe("right", steps=20),.wait.gone(),.wait.exists()

        Exmaple:
        | ${result}    | Get UI Object | text=XXX | className=xxxx |

        """
        self._result = self.d(**selectors)
        return self._result

    def wait_for_object_exists(self, obj, timeout=0):
        """
        Wait for UiObject *obj* exists in *timeout*= 0

        Example:
        | ${result}    | Get UI Object | text=XXX | className=xxxx |
        | Wait For Object Exists | ${result} | timeout |
        """
        self._result = obj.wait.exists(timeout=int(timeout))
        return self._result

    def wait_for_ui_gone(self, timeout=0, **selectors):
        """
        Wait for UiObject *obj* gone in *timeout*= 0

        Example:
        | ${result}    | Get UI Object | text=XXX | className=xxxx |
        | Wait For Object Gone | ${result} | timeout |

        """
        self._result = self.d(**selectors).wait.gone(timeout=int(timeout))
        return self._result

    def get_key_value(self, key, dictionary):
        """
        Get key value of dictionary
        """
        return dictionary(key)

    def get_tts(self, input_text, delete_file=False, **args):
        """
        Get TTS voice mp3 from Google
        get_tts(input_text='tunnel snakes rule apparently', args = {'language':'en','output':'outputto.mp3'})

        Robot Framework:
        Examples:
        | Action     | Argument   |  Argument  |  Argument  |  Argument  |
        | UiTestLib.Get TTS    |   Hello world    |   False   |    output=ooo.mp3   |   language=en |
        """
        print "Get text to speech: ", input_text
        downloaded = False
        if str(delete_file) == "True":
            if os.path.exists(args["output"]):
                os.remove(args["output"])
        if os.path.exists(args["output"]):
            if os.path.getsize(args["output"]) <= 0:
                os.remove(args["output"])
        if args["output"] is not None:
            if not os.path.exists(args["output"]):
                print "Generating mp3 file......", args["output"]
                downloaded = GoogleTTS.audio_extract(input_text, args)
            else:
                print "Have same local file"
                downloaded = True
        if not downloaded:
            print "Downloaded TTS from Google failed, trying to download from local FTP..."
            mp3file = open(args["output"], "w")
            mp3url = "ftp://cnbjlx9548/atautomation/Test-Content/Croft/" + args["output"]
            try:
                resp = urllib2.urlopen(mp3url, timeout=40)
                mp3file.write(resp.read())
                time.sleep(0.05)
            except Exception, e:
                print e
                return False
            mp3file.close()
        return True
Beispiel #12
0
def main():

    port = '5554'

    d = Device('emulator-' + port)

    #d.dump("/home/yu/repeatbugreport/middleResults/result.xml")

    #this is for flash
    d(resourceId="com.example.terin.asu_flashcardapp:id/email").set_text(
        "*****@*****.**")
    d(resourceId="com.example.terin.asu_flashcardapp:id/password").set_text(
        "12331986")
    d(resourceId="com.example.terin.asu_flashcardapp:id/email_sign_in_button"
      ).click()

    time.sleep(4)
    '''
    #this is for yastore
    d(resourceId="android:id/text1").click();
    
    d(resourceId="com.github.yeriomin.yalpstore:id/email").set_text("*****@*****.**")
    d(resourceId="com.github.yeriomin.yalpstore:id/password").set_text("lunar1986")
    d(resourceId="com.github.yeriomin.yalpstore:id/button_ok").click();
    time.sleep(4)
    '''
    '''this is for mifos
    d(resourceId="com.mifos.mifosxdroid:id/et_username").set_text("mifos")    
    d(resourceId="com.mifos.mifosxdroid:id/et_password").set_text("password") 
    
    d(text="LOGIN").click();
    
    
    time.sleep(2)
    '''

    #d(text="ALLOW").click();

    #d.press.back()
    #d.press.home()
    d.orientation = "n"

    d.screen.on()

    #address='/home/yu/repeatbugreport'
    address = '.'

    doc = minidom.parse(address + '/middleResults/run.xml')
    root = doc.documentElement

    for step in root.childNodes:
        if step.nodeType == minidom.Node.ELEMENT_NODE:  ##minidom is a little bit ugly

            textid = ""
            textX = ""
            textY = ""
            textStr = ""

            actionTypes = step.getElementsByTagName("Actiontype")
            if actionTypes[0].firstChild.nodeValue == "Noaction":
                continue
            if actionTypes[0].firstChild.nodeValue == "back":
                d.press.back()
                continue

            if actionTypes[0].firstChild.nodeValue == "enter":
                d.press("enter")
                time.sleep(2)
                continue

            ##########get the source id
            ownText = step.getElementsByTagName("ownText")

            if ownText.length != 0:
                textid = ownText[0].firstChild.nodeValue

            #########get the x-position and y-position
            xPositions = step.getElementsByTagName("xposition")
            yPositions = step.getElementsByTagName("yposition")

            if xPositions.length != 0:
                textX = xPositions[0].firstChild.nodeValue
                textY = yPositions[0].firstChild.nodeValue

            #########get the click text
            clickText = step.getElementsByTagName("clickText")

            if clickText.length != 0:
                if clickText[0].childNodes.length != 0:
                    textStr = clickText[0].firstChild.nodeValue

            #print(actionTypes[0].firstChild.nodeValue)
            if actionTypes[0].firstChild.nodeValue == "EditText":

                typewhat = step.getElementsByTagName("typeWhat")
                textTypeWhat = typewhat[0].firstChild.nodeValue

                if textTypeWhat == "default":
                    continue

                #write text
                d(resourceId=textid).set_text(textTypeWhat)
                #d.press("enter")

                #print(textid)
                #print(textTypeWhat)

                #d(resourceId="com.newsblur:id/login_password").set_text("asd")
                #d.wait.idle()
                time.sleep(0.5)

            else:
                #elif actionTypes[0].firstChild.nodeValue=="ClickList":
                #########get the x-position and y-position

                clicktype = step.getElementsByTagName("clicktype")
                textclicktype = clicktype[0].firstChild.nodeValue

                try:
                    if textclicktype == "short":
                        if not textid == "":
                            d(resourceId=textid).click()

                        if not textStr == "":
                            d(text=textStr).click()
                        else:
                            d.click(int(textX), int(textY))

                    elif textclicktype == "long":
                        if not textid == "":
                            d(resourceId=textid).long_click()

                        elif not textStr == "":
                            d(text=textStr).long_click()
                        else:
                            d.long_click(int(textX), int(textY))
                #d.wait.update()
                except Exception as err:
                    break

            time.sleep(2)

            #d.dump("/home/yu/repeatbugreport/middleResults/result.xml")

    d.dump(address + "/middleResults/result.xml")

    cmd = "adb -s emulator-" + port + " shell dumpsys window windows | grep -E 'mFocusedApp'"
    packInfo = commands.getstatusoutput(cmd)

    fo = open(address + "/middleResults/packInfo", "w")
    fo.write(str(packInfo))
    fo.close()

    if root.getAttribute("Rotate") == "True":

        d.orientation = "l"
        time.sleep(0.5)
        d.orientation = "n"
        time.sleep(0.5)
class InstallUninstallTest(object):
    
    def __init__(self, serial=None):
        self.serial = serial 
        self.device=Device(self.serial)
        self.log_setting()
        
    def log_setting(self):
        logfilepath = os.path.abspath("C:\Temp")
        if not os.path.exists(logfilepath):
            os.mkdir(logfilepath)
        self.logger = logging.getLogger('mylogger')    
        self.logger.setLevel(logging.INFO) 
        #create handler for writting log into log file.
        fh = logging.FileHandler(os.path.join(logfilepath,"InstallSAPAnywhere.log")) 
        #create handler for writting log into console.
        ch = logging.StreamHandler() 
        formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') 
        fh.setFormatter(formatter) 
        ch.setFormatter(formatter) 
        self.logger.addHandler(fh)
        self.logger.addHandler(ch)   
   

    def check_device_attached(self):
        out=subprocess.Popen("adb devices",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE).communicate()[0]
        match="List of devices attached"
        index=out.find(match)
        target_device_index=out.find(self.serial)
        if index<0:
            raise EnvironmentError("adb is not working.")
        elif target_device_index>0:
            self.logger.info("Device with serial %s attached"%self.serial)
        else:
            raise EnvironmentError("Device with serial %s is not attached"%self.serial)  
        
    def switch_network_to_corporate(self):
        try:
            self.swipe_down_notification_bar()
            self.device(text="WLAN").long_click()
            if not self.device(text="SAP-Corporate").sibling(text="Connected").exists:
                self.device(text="SAP-Corporate").click()
                self.device(text="Connect",className="android.widget.Button").click()
            if self.device(text="Notice").exists:
                self.device(text="connect",className="android.widget.Button").click()
            if self.device(text="SAP-Corporate").sibling(text="Connected").wait.exists(timeout=10000):
                self.logger.info("Network is switched to SAP-Corporate successfully")
            else:
                self.logger.error("Network is switched to SAP-Corporate timeout in 10s") 
        except:
            self.logger.error("Switch network to corporate failed with below %s"%traceback.format_exc()) 
     
    def check_app_installed(self):
        out=subprocess.Popen("adb shell pm list packages sap.sfa.container",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE).communicate()[0]
        if len(out)>0:
            self.logger.info("SAP Anywhere is installed alreadlly.")
        else:
            self.logger.info("SAP Anywhere is not installed.")
        return len(out)>0
    
    def uninstall_app(self,appName):
        try:
            self.device.press("home")
            self.device.press("menu")
            self.device(text="Settings",className="android.widget.TextView").click()
            self.device(scrollable=True).scroll.to(text="Application manager")
            self.device(text="Application manager",className="android.widget.TextView").click()
            self.device(scrollable=True).scroll.to(text=appName)
            self.device(text=appName,className="android.widget.TextView").click()
            self.device(text="Uninstall").click()
            self.device(text="OK").click()
            if self.device(text="Uninstalled").wait.exists(timeout=10000)==True:
                self.logger.info("SAP Anywhere is uninstalled successfully.")    
                self.device(text="OK").click()
            else:
                self.logger.error("SAP Anywhere is uninstalled timeout in 10s.")
  
        except:
            self.logger.error("SAP Anywhere is uninstalled failed with below %s"%traceback.format_exc())      
              
    def install_app(self,appName):
        try:
            self.device(textContains=appName).click()
            self.device(text="Install").click()
            if self.device(text="Application installed").wait.exists(timeout=15000)==True:
                self.logger.info("%s is installed successfully."%appName)
                self.device(text="Done").click()
            else:
                self.logger.error("%s is installed timeout in 15s."%appName)                
        except:
            self.logger.error("%s is installed faield with below %s"%(traceback.format_exc(),appName))    
       

    def launch_chromedriver_servie(self):
        try:
            subprocess.Popen("start cmd /k C:\\temp\\chromedriver.exe",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
            self.logger.info("Launch chromedriver service successfully.")
        except:
            self.logger.error("Launch chromedriver service failed with below %s"%traceback.format_exc()) 
            
    def kill_chromedriver_servie(self):
        try:
            
            subprocess.Popen("start cmd /k taskkill /f /im chromedriver.exe",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
            self.logger.info("Terminate chromedriver service successfully.")
        except:
            self.logger.error("Terminate chromedriver service failed with below %s"%traceback.format_exc())  
                
    def download_app_from_appStore(self):
        try:
            capabilities = {
              'chromeOptions': {
                'androidPackage': 'com.android.chrome',
                "androidDeviceSerial":self.serial,
              }
            }
            driver = webdriver.Remote('http://*****:*****@class="ref"]//a[contains(text(),"Android")])[1]').click()
            self.device(text="OK").click()
            driver.quit()
            self.swipe_down_notification_bar()
            if self.device(text="Download complete").wait.exists(timeout=60000)==True:
                self.logger.info("SAP Anywhere is downloaded successfully from internal app store.")
            else:
                self.logger.error("SAP Anywhere is downloaded timeout in 60s from internal app store.")
        except:
            self.logger.error("SAP Anywhere is downloaded failed from internal app store with below %s"%traceback.format_exc())     
         
        
    def swipe_down_notification_bar(self):
        self.device.swipe(self.device.width/2, 0 ,self.device.width/2, self.device.height,10)
                
    def test_install_uninstall_SAPAnywhere(self):
        self.logger.info("Log start----------------------------------------")
        self.check_device_attached()
        self.device.screen.on()
        self.device.swipe(self.device.width-10,self.device.height/2,0,self.device.height/2)
        self.switch_network_to_corporate()
        if self.check_app_installed()==True:
            self.uninstall_app("SAP Anywhere")  
        self.kill_chromedriver_servie()
        sleep(2)
        self.launch_chromedriver_servie()            
        self.download_app_from_appStore()
        self.install_app("SAPAnywhere")             
        self.logger.info("Log end----------------------------------------")