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

现在有很多工具可以实现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 uiautomator
from uiautomator import Adb, Device


class 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

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



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值