[Technology blog] Android automated testing

[Technology blog] Android automated testing

Andrews automated testing tools and platform to build

Similar to the web-side automation, automated test Andrews is mainly for automated control. The principle is to replace our manual tasks through scripting python (other languages) is. So the tools we need is python (test scripts), Appium (Andrews automated testing tools), uiautomatorviewer.bat (Andrews positioning controls), a virtual machine or a real machine (running Android program).

We follow the process to introduce the use of automated testing and installation tools.

uiautomatorviewer.bat

This is the SDK comes with a tool, the main role is to locate the position of Andrews controls, because these controls is Appium control, or that element. Get element positions There are many ways, similar to the web client 'class', 'xpath', 'id' and the like. About this tool, GitHub has a big God shared a version of the enhanced version of the tool, the portal . Enhanced version can directly locate the full path xpath compared by class wrote a lot easier.

The above example is a view element, it can be very simple access to the element through the lower right path. React-Native especially for this frame type, rather than on the native Andrews, so convenient lot.

Appium automated testing tools

This tool is currently widely used Android automated testing tools. But the official website seems to have stopped updating, and now seemed to GitHub maintenance portal . After downloading directly installed, but looks can only be installed in the system tray. About Appium specific use, you can refer to this blog, portal . Described here is not too much of the specific operation.

After starting like this, and then run python scripts directly, you can see the real machine on the virtual machine or APP automatically run.

Automated test code

Configuring a virtual machine or a real machine

这里准确的来说是,在python 脚本中对虚拟机或者真机的操作。对python 来说,首先安装支持 Appium 运行的包,appium,然后在测试的开始要配置机器,也就是让 Appium 找到机器在哪,链接机器与 Appium,这样就可以通过 Appium来控制 机器了。

self.desired_caps = {}
            self.desired_caps['platformName'] = 'Android'  # 平台
            self.desired_caps['deviceName'] = '192.168.221.101:5555'
            # self.desired_caps['platformVersion'] = '9.0'  # 系统版本
            # self.desired_caps['app'] = 'E:/autotestingPro/app/UCliulanqi_701.apk'   # 指向.apk文件,如果设置appPackage和appActivity,那么这项会被忽略
            # self.desired_caps['deviceName'] = 'Android Emulator'
            self.desired_caps['appPackage'] = 'com.cnblogandroid'  # APK包名
            self.desired_caps['appActivity'] = '.MainActivity'  # 被测程序启动时的Activity
            self.desired_caps['unicodeKeyboard'] = 'true'  # 是否支持unicode的键盘。如果需要输入中文,要设置为“true”
            self.desired_caps['resetKeyboard'] = 'true'  # 是否在测试结束后将键盘重轩为系统默认的输入法。
            self.desired_caps['noReset'] = True  # true:不重新安装APP,false:重新安装app
            self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", self.desired_caps)
            # 构建虚拟机,用于测试

这样配置之后就可以直接开始写测试代码,控制虚拟机或者真机了。但是我们为了方便,写很多个不同的测试,由于有时候测试无法连在一起,所以我们使用 unittest 框架来写测试代码。该方法的好处是,可以写多个相关或者不相关的测试实例,然后将他们组合起来,具体就是:

if __name__ == '__main__':
    suite = unittest.TestSuite() # 一个测试实例
    tests = [Test_Blog_HomeWork("test_blog"), test_borad("test_borad")] # 可以加入其它测试
    suite.addTests(tests)

对于自动化测试,我们通常用于重复的操作,或者覆盖性的测试。那么如何判断测试的完整性呢,及代码中有没有出现未运行到的测试呢? unittest 提供了一个 UnittestTextReport 的方法。可以看到测试的情况,为了美观,unittest 还提供了 HTMLTestRunner 通过html的方式显示测试结果。

Guess you like

Origin www.cnblogs.com/PureMan6/p/10978186.html