基于unittest框架和python-uiautomator实现ui自动化测试(包含常用adb命令)

功能描述

1、基于unittest框架安排整个测试工程的目录结构
2、测试执行前需要先检测屏幕是否亮屏,清除android对应的ap-log、isp-log等相关log路径的已有log,应用初始化;
3、基于python-uiautomator2实现ui操作;
3.1、基于控件的resource-id实现前后置相机的切换、点击拍照等操作
3.2、基于text实现ui界面的文本点击
3.3、双指zoom操作
4、测试执行后导出log到指定路径,log目录以当前格式化的时间命名,并列出log目录中需要解压的文件进行解压,最后读取解压后的文件查找异常的关键字

unittest框架

参考网址:https://www.cnblogs.com/yufeihlf/p/5707929.html

工程项目基于如下目录结构安排aw、测试用例等。

目录结构:
在这里插入图片描述

百度搜索测试用例Test Case:

# coding=utf-8
'''
Created on 2016-7-22
@author: Jennifer
Project:登录百度测试用例
'''
from selenium import webdriver
import unittest, time

class BaiduTest(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30) #隐性等待时间为30秒
        self.base_url = "https://www.baidu.com"
    
    def test_baidu(self):
        driver = self.driver
        driver.get(self.base_url + "/")
        driver.find_element_by_id("kw").clear()
        driver.find_element_by_id("kw").send_keys("unittest")
        driver.find_element_by_id("su").click()
        time.sleep(3)
        title=driver.title
        self.assertEqual(title, u"unittest_百度搜索") 

    def tearDown(self):
        self.driver.quit()

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

有道翻译测试用例Test Case:

# coding=utf-8
'''
Created on 2016-7-22
@author: Jennifer
Project:使用有道翻译测试用例
'''
from selenium import webdriver
import unittest, time

class YoudaoTest(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30) #隐性等待时间为30秒
        self.base_url = "http://www.youdao.com"
    
    def test_youdao(self):
        driver = self.driver
        driver.get(self.base_url + "/")
        driver.find_element_by_id("translateContent").clear()
        driver.find_element_by_id("translateContent").send_keys(u"你好")
        driver.find_element_by_id("translateContent").submit()
        time.sleep(3)
        page_source=driver.page_source
        self.assertIn( "hello",page_source) 

    def tearDown(self):
        self.driver.quit()

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

web测试用例:通过测试套件TestSuite来组装多个测试用例。

# coding=utf-8
'''
Created on 2016-7-26
@author: Jennifer
Project:编写Web测试用例
'''
import unittest
from test_case import test_baidu
from test_case import test_youdao

#构造测试集
suite = unittest.TestSuite()
suite.addTest(test_baidu.BaiduTest('test_baidu'))
suite.addTest(test_youdao.YoudaoTest('test_youdao'))

if __name__=='__main__':
    #执行测试
    runner = unittest.TextTestRunner()
    runner.run(suite)

测试结果
在这里插入图片描述

suite.addTest(Test())在Test()里面要写名字,才能找到要执行的函数.suite.addTest(Test(‘test1’))

参考网址:https://www.e-learn.cn/topic/634418

用 adb 判断屏幕是否唤醒

以下是java实现adb判断屏幕是否唤醒的代码

  /** * 判断设备是否休眠 
  * @return 
  * @throws IOException 
  */
  public boolean isScreenLock() throws IOException {
            Runtime rt = Runtime.getRuntime(); 
            Process p = rt.exec("cmd.exe /c adb shell dumpsys power | findstr \"Display Power:state=\""); 
            BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); 
            String line; 
            String content = ""; 
            boolean flag = false; 
            while ((line = in.readLine()) != null)
            content = content + line; 
            if (content.contains("Display Power: state=OFF"))
                    flag = true;
            p.destroy();
            return flag;
  }

唤醒屏幕

  if(getUrlFile.isScreenLock()){
        // 模拟Power键 
        Runtime.getRuntime().exec("adb shell input keyevent 26");
       // 模拟Home键
        Runtime.getRuntime().exec("adb shell input keyevent 3");
}

因为整个工程是基于python的,所以做了python的改写。

先用os.popen(‘adb shell dumpsys power’)获取控制台输出的内容;然后检查Display Power:state对应的值,如果是false就是灭屏状态需要唤醒;

adb 常用命令

rm -rf : 强制删除文件,不提示,直接删除
pull: 导出文件到本地
push:导入文件
adb shell am start -n 包名/类名:启动应用程序
adb devices:查看当前连接的所有设备名称
ll ls:
pwd:

参考网址:https://www.cnblogs.com/fengliting/p/13161812.html

python uiautomator2环境搭建

安装uiautomator2

pip install --pre uiautomator2

初始化

部署相关的守护进程。

1、确保adb已经添加到环境变量中

2、使用uiautomator2进行自动化测试时需要在手机中安装app-uiautomator.apk、app-uiautomator-test.apk、atx-agent

执行下面的命令会自动安装本库所需要的设备端程序:uiautomator-server 、atx-agent、openstf/minicap、openstf/minitouch

python -m uiautomator2 init

安装完成,设备上会多一个uiautomator的应用。

3、配置手机设备参数

3.1 使用WIFI连接

手机获取到手机的IP,并确保电脑可以PING通手机。手机的IP可以在设置-WIFI设置里面获取到。比如手机的IP是192.168.0.100,连接设备的代码为

import uiautomator2 as u2

d = u2.connect('192.168.0.100')

3.2 使用USB连接

手机的序列号可以通过adb devices获取到,假设序列号是123456f,连接代码为

import uiautomator2 as u2

d = u2.connect_usb('123456f')

参考网址:https://www.jianshu.com/p/1456d4e0647b
https://www.cnblogs.com/fnng/p/8486863.html

uiautomator2常用操作

通过usb获取设备
d = u2.connect(‘123456f’)#u2.connect_usb(‘123456f’)

有5种定位方式:
d(text =“设置”)

d(resourceId=“com.ruguoapp.jike:id/tv_title”, className=“android.widget.TextView”)

常用操作:
click()
swipe()
……

参考网址:https://www.jb51.net/article/205942.htm

python os

os.system:执行cmd命令,但是没有控制台的返回值
os.popen:获取控制台输出的内容

result = os.popen('命令')
res = result.read()
for line in res.splitlines():
print line

参考网址:https://www.cnblogs.com/yoyoketang/p/9083932.html

os.mkdir(path[, mode]):以数字mode的mode创建一个名为path的文件夹.默认的 mode 是 0777 (八进制)

os.rename(src, dst):重命名文件或目录,从 src 到 dst

os.getcwd():返回当前工作目录

os.path.split():区分文件的名字和后缀

import os
  
file_path = "D:/test/test.py"
(filepath, tempfilename) = os.path.split(file_path)
(filename, extension) = os.path.splitext(tempfilename)

参考网址:https://www.jb51.net/article/149239.htm

os.path.abspath(os.path.join(os.getcwd(), “…”)):获取上一级目录

rootPath = os.path.abspath(os.path.join(os.getcwd(), ".."))

os.listdir(path):列出当前目录的文件

import os

path = 'mypath/path'
files = os.listdir(path)

files_txt = [i for i in files if i.endswith('.txt')]

python gzip

解压缩.gz文件

#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
import gzip

def un_gz(file_name):
    
    # 获取文件的名称,去掉后缀名
    f_name = file_name.replace(".gz", "")
    # 开始解压
    g_file = gzip.GzipFile(file_name)
    #读取解压后的文件,并写入去掉后缀名的同名文件(即得到解压后的文件)
    open(f_name, "wb+").write(g_file.read())
    g_file.close()
    
un_gz('D:\\python36\\config.gz')

参考网址:https://blog.csdn.net/weixin_37830912/article/details/100159324

python time

利用localtime()转换为时间数组,然后格式化为需要的格式,如

timeArray = time.localtime(time.time())
timeStr = time.strftime("%y%m%d_%H%M%S", timeArray)

python 线程

setDaemon()方法

主线程A中,创建了子线程B,并且在主线程A中调用了B.setDaemon(),这个的意思是,把主线程A设置为守护线程,这时候,要是主线程A执行结束了,就不管子线程B是否完成,一并和主线程A退出.这就是setDaemon方法的含义,这基本和join是相反的。此外,还有个要特别注意的:必须在start() 方法调用之前设置,如果不设置为守护线程,程序会被无限挂起,只有等待了所有线程结束它才结束。

Pycharm运行成功,cmd上执行Python出现导入包找不到

pydev在运行时会把当前工程的所有文件夹路径都作为包的搜索路径,而命令行默认只是搜索当前路径。也于是乎,xx_package也就不可能会被找到,因为它是在上一级目录中。

参考网址: https://blog.csdn.net/u010565765/article/details/84928947

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值