基于python单元测试框架unittest完成appium自动化测试,生成基于html可视化测试报告

代码示例:

 #利用unittest并生成测试报告
class Appium_test(unittest.TestCase):
"""appium测试类"""
def setUp(self):
desired_caps = {
'platformName': 'Android',
'deviceName': 'Android Emulator',#可有可无,这里是指我的模拟器
'platformVersion': '5.0',
# apk包名
'appPackage': 'com.smartisan.notes',
# apk的launcherActivity
'appActivity': 'com.smartisan.notes.NewNotesActivity',
#如果存在activity之间的切换可以用这个
# 'appWaitActivity':'.MainActivity',
'unicodeKeyboard': True,
#隐藏手机中的软键盘
'resetKeyboard': True
}
self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_caps)
time.sleep()
self.verificationErrors = "今天天气不错在家学习!" #设置的断言 def tearDown(self):
time.sleep()
assertt = self.driver.find_element_by_id("com.smartisan.notes:id/list_rtf_view").text
# print(assertt) #调试用
self.assertEqual(assertt,self.verificationErrors,msg="验证失败!")
#断言:实际结果,预期结果,错误信息
self.driver.quit() def test_creat(self):
"""记事本中新增一条记录"""
self.driver.find_element_by_id("com.smartisan.notes:id/add_button").click()
time.sleep()
self.driver.find_element_by_class_name("android.widget.EditText").send_keys("今天天气不错在家学习!")
self.driver.find_element_by_id("com.smartisan.notes:id/send_finish_button").click() suite = unittest.TestSuite()
suite.addTest(Appium_test('test_creat')) report_file = ".\\appium_report.html"
fp = open(report_file,'wb')
runner = HTMLTestRunner.HTMLTestRunner(stream=fp,title="appium测试报告",description='新增一条笔记并保存')
runner.run(suite)
fp.close()

生成测试报告:

Appium自动化测试PO模型:

其中,main.py为框架的主入口,test_creat.py调用creat_page.py,creat_page.py调用base_page.py。

PO代码示例:

main.py

 import unittest
import HTMLTestRunner #相对路径
testcase_path = ".\\testcase"
report_path = ".\\report\\appium_report.html"
def creat_suite():
uit = unittest.TestSuite()
discover = unittest.defaultTestLoader.discover(testcase_path,pattern="test_*.py")
for test_suite in discover:
# print(test_suite)
for test_case in test_suite:
uit.addTest(test_case)
return uit suite = creat_suite()
fp = open(report_path,"wb")
runner = HTMLTestRunner.HTMLTestRunner(stream=fp,title="测试结果",description="appium新建笔记测试结果")
runner.run(suite)
fp.close()

test_creat.py

 from appium import webdriver
import unittest
from appiumframework.PO.creat_page import CreatPage
import time class Test(unittest.TestCase):
"""自动化"""
def setUp(self):
desired_caps = {
'platformName': 'Android',
'deviceName': 'Android Emulator',#可有可无
'platformVersion': '5.0',
# apk包名
'appPackage': 'com.smartisan.notes',
# apk的launcherActivity
'appActivity': 'com.smartisan.notes.NewNotesActivity',
#如果存在activity之间的切换可以用这个
# 'appWaitActivity':'.MainActivity',
'unicodeKeyboard': True,
#隐藏手机中的软键盘
'resetKeyboard': True
}
self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_caps)
time.sleep(5)
self.verificationErrors = "今天天气不错在家学习!" def tearDown(self):
time.sleep(10)
self.driver.quit() def test_saveedittext(self):
"""保存编辑的文本"""
sp = CreatPage(self.driver)
sp.add_button_link()
sp.run_case("今天天气不错在家学习!")
#断言:实际结果,预期结果,错误信息
self.assertEqual(sp.get_finish_button_text(),self.verificationErrors,msg="验证失败!")

creat_page.py

 from appiumframework.PO import base_page
import time class CreatPage(base_page.Action):
add_button_loc = ("com.smartisan.notes:id/add_button")
edittext_loc = ("com.smartisan.notes:id/list_rtf_view")
finish_button_loc = ("com.smartisan.notes:id/send_finish_button") def add_button_link(self):
self.find_element(self.add_button_loc).click()
time.sleep(3) #等待3秒,等待登录弹窗加载完成 def run_case(self,value):
self.find_element(self.edittext_loc).send_keys(value)
time.sleep(5)
self.find_element(self.finish_button_loc).click()
time.sleep(2) def get_finish_button_text(self):
return self.find_element(self.edittext_loc).text

base_page.py

 class Action(object):
#初始化
def __init__(self,se_driver):
self.driver = se_driver #重写元素定位的方法
def find_element(self,loc):
try:
return self.driver.find_element_by_id(loc)
except Exception as e:
print("未找到%s"%(self,loc))

测试报告截图:

Appium基于Python unittest自动化测试 & 自动化测试框架 -- PO并生成html测试报告的更多相关文章

  1. Appium基于python unittest自动化测试并生成html测试报告

    本文基于python单元测试框架unittest完成appium自动化测试,生成基于html可视化测试报告 代码示例: #利用unittest并生成测试报告 class Appium_test(uni ...

  2. 简单实现接口自动化测试(基于python+unittest)

    简单实现接口自动化测试(基于python+unittest) 简介 本文通过从Postman获取基本的接口测试Code简单的接口测试入手,一步步调整优化接口调用,以及增加基本的结果判断,讲解Pytho ...

  3. 基于Python的selenuim自动化测试尝试

    工作这么多年了,终于狠下心好好开始学学自动化测试相关知识,揭开这层神秘的面纱. 困难重重,障碍很多,但好在每天都多少有点小收获. 很感谢一个QQ好友推荐的虫师,也非常感谢在这个契机读到了虫师编著的&l ...

  4. selenium+python+unittest实现自动化测试(入门篇)

    本文主要讲解关于selenium自动化测试框架的入门知识点,教大家如何搭建selenium自动化测试环境,如何用selenium+python+unittest实现web页面的自动化测试,先来看看se ...

  5. Python Unittest 自动化单元测试框架Demo

    python 测试框架(本文只涉及 PyUnit) https://wiki.python.org/moin/PythonTestingToolsTaxonomy 环境准备 首先确定已经安装有Pyth ...

  6. 符号执行-基于python的二进制分析框架angr

    转载:All Right 符号执行概述 在学习这个框架之前首先要知道符号执行.符号执行技术使用符号值代替数字值执行程序,得到的变量的值是由输入变 量的符号值和常量组成的表达式.符号执行技术首先由Kin ...

  7. ShutIt:一个基于 Python 的 shell 自动化框架

    ShutIt是一个易于使用的基于shell的自动化框架.它对基于python的expect库(pexpect)进行了包装.你可以把它看作是“没有痛点的expect”.它可以通过pip进行安装. Hel ...

  8. python用unittest+HTMLTestRunner的框架测试并生成测试报告

    直接贴代码: import unittestfrom selenium import webdriverfrom time import sleepimport osimport time # 定义打 ...

  9. python unittest自动测试框架

    编写函数或者类时进行测试,确保代码正常工作 python  unittest 模块提供了代码测试工具.按照定义测试包括两部分:管理测试依赖库的代码(称为‘固件’)和测试本身. 单元测试用于核实函数的某 ...

随机推荐

  1. asp.net -mvc框架复习(2)-创建ASP.NET MVC 第一个程序以及MVC项目文件夹说明

    建议vs2013或2013以上版本的vs,要是跨平台的话最好用vs2015或vs2017的asp.net mvc core . 1.创建ASP.NET MVC 第一个程序 打开vs2013->文 ...

  2. jquery mobile-按钮控件

    jQuery Mobile 中的按钮会自动获得样式,这增强了他们在移动设备上的交互性和可用性.我们推荐您使用 data-role="button" 的 <a> 元素来创 ...

  3. ajax和跨域

    一.简介 ajax是什么? AJAX(Asynchronous JavaScript and XML) 是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术. AJAX, (异步的JavaSc ...

  4. 开地址哈希表(Hash Table)的原理描述与冲突解决

    在开地址哈希表中,元素存放在表本身中.这对于某些依赖固定大小表的应用来说非常有用.因为不像链式哈希表在每个槽位上有一个"桶"来存储冲突的元素,所以开地址哈希表需要通过另一种方法来解 ...

  5. js/j'query相互转换操作指南

    // jquery对象转js对象 $('#search')[0].checked=true; // js对象转jquery对象 var obj = document.getElementById('s ...

  6. webpack + vue

    开始之前 本文包含以下技术,文中尽量给与详细的描述,并且附上参考链接,读者可以深入学习: 1.webpack2.Vue.js3.npm4.ES6语法 前言 在对着产品高举中指怒发心中之愤后,真正能够解 ...

  7. hadoop - spark on yarn 集群搭建

    一.环境准备 1. 机器: 3 台虚拟机 机器 角色  l-qta3.sp.beta.cn0 NameNode,ResourceManager,spark的master l-querydiff1.sp ...

  8. Android Studio库依赖问题

    Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'. > com.android. ...

  9. openstack开发基础

  10. python selenium+phantomjs alert()弹窗报错

    问题:用selenium+phantomjs 模拟登陆,网页用JavaScript的alert("登陆成功")弹出框,但是用switch_to_alert().accept()报错 ...