Exemplo n.º 1
0
 def test_adb_cmd(self):
     adb = Adb()
     adb.device_serial = MagicMock()
     adb.device_serial.return_value = "ANDROID_SERIAL"
     adb.raw_cmd = MagicMock()
     args = ["a", "b", "c"]
     adb.cmd(*args)
     adb.raw_cmd.assert_called_once_with("-s", "'%s'" % adb.device_serial(), *args)
Exemplo n.º 2
0
 def test_adb_cmd(self):
     adb = Adb()
     adb.device_serial = MagicMock()
     adb.device_serial.return_value = "ANDROID_SERIAL"
     adb.raw_cmd = MagicMock()
     args = ["a", "b", "c"]
     adb.cmd(*args)
     adb.raw_cmd.assert_called_once_with("-s", adb.device_serial(), *args)
Exemplo n.º 3
0
    def test_adb_cmd_server_host(self):
        adb = Adb(adb_server_host="localhost", adb_server_port=5037)
        adb.device_serial = MagicMock()
        adb.device_serial.return_value = "ANDROID_SERIAL"
        adb.raw_cmd = MagicMock()
        args = ["a", "b", "c"]
        adb.cmd(*args)
        adb.raw_cmd.assert_called_once_with("-H", "localhost", "-P", "5037", "-s", "%s" % adb.device_serial(), *args)

        adb = Adb(adb_server_host="localhost")
        adb.device_serial = MagicMock()
        adb.device_serial.return_value = "ANDROID_SERIAL"
        adb.raw_cmd = MagicMock()
        args = ["a", "b", "c"]
        adb.cmd(*args)
        adb.raw_cmd.assert_called_once_with("-H", "localhost", "-s", "%s" % adb.device_serial(), *args)
Exemplo n.º 4
0
    def test_adb_cmd_server_host(self):
        adb = Adb(adb_server_host="localhost", adb_server_port=5037)
        adb.device_serial = MagicMock()
        adb.device_serial.return_value = "ANDROID_SERIAL"
        adb.raw_cmd = MagicMock()
        args = ["a", "b", "c"]
        adb.cmd(*args)
        adb.raw_cmd.assert_called_once_with("-H", "localhost", "-P", "5037",
                                            "-s", "%s" % adb.device_serial(),
                                            *args)

        adb = Adb(adb_server_host="localhost")
        adb.device_serial = MagicMock()
        adb.device_serial.return_value = "ANDROID_SERIAL"
        adb.raw_cmd = MagicMock()
        args = ["a", "b", "c"]
        adb.cmd(*args)
        adb.raw_cmd.assert_called_once_with("-H", "localhost", "-s",
                                            "%s" % adb.device_serial(), *args)
Exemplo n.º 5
0
class BaseClient(object):

    def __init__(self, device_id):
        self._device_id = device_id
        self._adb_commander = Adb(self._device_id)
        self._logcat_thread = threading.Thread(target=self._start_logcat)
        self._logcat_thread.start()
        self._device = Device(self._device_id)

    def open_app(self):
        self._device.screen.on()
        _package_name, _main_activity_name = utils.get_package_and_main_activity_name()
        os.system('adb -s %s shell am force-stop %s' % (self._device_id, _package_name))
        self._adb_commander.cmd('shell am start -W %s/%s' % (_package_name, _main_activity_name))

    def _start_logcat(self):
        log_file_path = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir, 'log'))
        self._adb_commander.cmd('logcat -c' % self._device_id)
        self._adb_commander.cmd('logcat> %s/%s.log' % (log_file_path, self._device_id))

    def stop_logcat(self):
        os.system('ps -e | grep "adb -s %s logcat" | xargs kill' % self._device_id)
Exemplo n.º 6
0
 def test_forward(self):
     adb = Adb()
     adb.cmd = MagicMock()
     adb.forward(90, 91)
     adb.cmd.assert_called_once_with("forward", "tcp:90", "tcp:91")
     adb.cmd.return_value.wait.assert_called_once_with()
Exemplo n.º 7
0
 def test_forward(self):
     adb = Adb()
     adb.cmd = MagicMock()
     adb.forward(90, 91)
     adb.cmd.assert_called_once_with("forward", "tcp:90", "tcp:91")
     adb.cmd.return_value.wait.assert_called_once_with()
Exemplo n.º 8
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
Exemplo n.º 9
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(self, 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" |
			"""
			if appname.find('/') > 0:
				appname = 'shell am start -n ' + appname
			else:
				appname = 'shell am start -a android.intent.action.MAIN -c android.intent.category.LAUNCHER ' + appname
			print 'Open Application:', appname
			self._result = self.exe_adb_command(appname)
Exemplo n.º 10
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