移动端测试5-第一个自动化测试demo

概述

这里采用了以下几点做测试

  1. python的unittest做为校验
  2. appium

apk

测试一个apk,我们需要知道一些配置信息,如下所示:

1
2
3
4
5
'platformName'
'platformVersion'
'deviceName'
'appPackage'
'appActivity'

我们需要aapt这个工具

1
2
3
cd $ANDROID_HOME/build-tools/28.0.1 

./aapt dump badging ~/Downloads/apk/vipkid_v1.5.5_17106180_default_defaultChannel.apk

然后我们就可以看到如下的appPackage 和 appActicity

image
image

接着我们要获取deviceName,可以使用以下命令

1
./abd devices

platformVersion 需要我们从虚拟机看,或者实际手机查看

定位元素

使用./uiautomation 进行元素捕获

image

代码

目录结构

1
2
3
4
5
6
7
8
--android
|TestCase
--|common
----| Base.py
----| config.py
|TestCase1.py
|TestCase2.py
|TestCase3.py

Base.py

1
2
3
4
5
6
7
8
9
10
11
12
import unittest
from config import Config

class BaseTest(unittest.TestCase):
def setUp(self):
desired_caps = {}
desired_caps['platformName'] = Config['platformName']
desired_caps['platformVersion'] = Config['platformVersion']
desired_caps['deviceName'] = Config['deviceName']
desired_caps['appPackage'] = Config['appPackage']
desired_caps['appActivity'] = Config['appActivity']
self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

config.py

1
2
3
4
5
6
7
Config = {
'platformName':"Android",
'platformVersion':'6.0',
'deviceName':'Android Emulator',
'appPackage':'com.android.calculator2',
'appActivity':'.Calculator'
}

TestCase1.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17


import unittest
from common.Base import BaseTest
import time


class TestCase1(BaseTest):
def test_register(self):
self.driver.find_element_by_id('com.vipkid.app:id/mSignUpLayout').click()
self.driver.find_element_by_xpath('//android.widget.EditText[@content-desc="请输入您的手机号码"]').set_value('18850523947')
self.driver.find_element_by_xpath('//android.widget.EditText[@content-desc="请输入验证码"]').send_keys('12345')
self.driver.find_element_by_xpath('//android.widget.EditText[@content-desc="请输入推荐人手机号(选填)"]').send_keys('1234567890')
self.driver.find_element_by_class_name('android.view.View').click()

if __name__ == "__main__":
unittest.main()

图示

image

至此,完成了一个小demo,虽然还称不上是一个测试,后续需要补上selenium的一些操作知识点。这样的框架无法用于真实生产环境,有几个缺点

  1. 测试用例无法复用
  2. 一个脚本无法用于多个环境

就当做初学的一个尝试,其代码地址: https://github.com/TheFifthMan/python_modules/tree/master/android_appium/TestCaseSuites

参考

https://blog.csdn.net/greta_guo/article/details/71635965