利用Robot Framework和Python wrapper of Android uiautomator的Android测试自动化

来源:互联网 发布:医院预约管理系统php 编辑:程序博客网 时间:2024/05/01 13:48

现在有很多工具可以实现BDD,比如Cucumber(http://cukes.info/),用文本来描述行为和结果

不会编程的人看得懂,当然了,底下的工作并没有减少。

这个Robot框架好处有两点,1. 适用广泛,可配置高  2. 报告可读性高

今天我们来弄一下这个过程,比较简单,错误之处希望海函。


1. Robot framework 安装

下载:http://robotframework.org/

安装:sudo python setup.py install


2. 安装https://github.com/xiaocong/uiautomato

sudo pip install uiautomator


echo "export ANDROID_HOME=/your_android_home" > > ~./bashrc

source ~/.bashrc


3. 写用例文本(宏文本)

比如我们这个keyword-driven.txt,按照keyword来写的,注意要用很多tab来分割函数和参数

*** Settings ***Documentation     Example test cases using the keyword-driven testing approach.......               All tests contain a workflow constructed from keywords in...               `UiTestLib`. Creating new tests or editing existing...               is easy even for people without programming skills.Library           UiTestLib*** Test Cases ***点亮屏幕    Light screen    Result should be    True打开声音   Open application                   "com.android.settings/com.android.settings.Settings"   Click text                         声音   Click text                         音量   Text display on screen contains    铃声和通知   Result should be                   True



4. 写底层解析脚本

用来解析上面的宏文本,可以看到它是以下划线来命名解析的,只有这样才能达到一一对应。

#!/usr/bin/env python# -*- coding: utf-8 -*-import uiautomatorfrom uiautomator import Adb, Deviceclass UiTestLib(object):    """Test library for testing Calculator business logic.    Interacts with the calculator directly using its `push` method.    """    def __init__(self):        self.d = Device('CB512611AE')        self.adb = Adb('CB512611AE')        self._result = ''    def light_screen(self):        """Light screen by wakeup.        Examples:        |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:        | Open application | "com.android.settings/com.android.settings.Settings" |        """        appname = 'shell am start -n ' + appname        print appname        self._result = self.adb.cmd(appname).wait()    def click_text(self, text):        """Click text label on screen        Example:        | Click text | text |        """        self.d(text = text).click.wait()    def text_display_on_screen_contains(self, text):        """Verify text display on screen        Example:        |Text display on screen is| text |        """        if self.d(text=text).exists:            self._result = True    def result_should_be(self, expected):        """Verifies that the current result is `expected`.        Example:        |  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))

5. 运行

pybot keyword_driven.txt

结果就出来了,可读性很强,报告很强大。



0 0