Appium+python测试app实例
Appium和selenium差不到,只是一个用于测web,一个用于测APP。下面记录一下我搭的测试框架,同样是基于PO模式,用的unittest.
最后测试报告如下:

1.1 代码结构

这个结构是不是很熟悉,都是基于PO模式,用的是unittest框架。
1.2 配置文件globalparameter.py
# coding:utf-8
__author__ = 'Helen'
'''
description:配置全局参数
'''
import time,os # 获取项目路径
# project_path = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)[0]), '.'))
project_path = os.path.abspath(os.path.join(os.path.dirname(os.path.split(os.path.realpath(__file__))[0]), '.'))
# 测试用例代码存放路径(用于构建suite,注意该文件夹下的文件都应该以test开头命名)
test_case_path = project_path+"\\src\\test_case"
# print u'日志路径:'+log_path
# 测试报告存储路径,并以当前时间作为报告名称前缀
report_path = project_path+"\\report\\"
report_name = report_path+time.strftime('%Y%m%d%H%S', time.localtime())
# 设置发送测试报告的公共邮箱、用户名和密码
smtp_sever = 'smtp.exmail.qq.com' # 邮箱SMTP服务,各大运营商的smtp服务可以在网上找,然后可以在foxmail这些工具中验正
email_name = "888@x88.com" # 发件人名称
email_password = "*888" # 发件人登录密码
email_To = '888@qq.com;88@88.com' # 收件人
1.3 配置文件driver_configure.py
# coding:utf-8
__author__ = 'Helen'
'''
description:driver配置
'''
from appium import webdriver class driver_configure():
def get_driver(self):
'''获取driver'''
try:
self.desired_caps = {}
self.desired_caps['platformName'] = 'Android' # 平台
self.desired_caps['platformVersion'] = '6.0' # 系统版本
# self.desired_caps['app'] = 'E:/autotestingPro/app/UCliulanqi_701.apk' # 指向.apk文件,如果设置appPackage和appActivity,那么这项会被忽略
self.desired_caps['appPackage'] = 'com.xsteach.appedu' # APK包名
self.desired_caps['appActivity'] = 'com.qihoo.util.StartActivity' # 被测程序启动时的Activity
self.desired_caps['unicodeKeyboard'] = 'true' # 是否支持unicode的键盘。如果需要输入中文,要设置为“true”
self.desired_caps['resetKeyboard'] = 'true' # 是否在测试结束后将键盘重轩为系统默认的输入法。
self.desired_caps['newCommandTimeout'] = '' # Appium服务器待appium客户端发送新消息的时间。默认为60秒
self.desired_caps['deviceName'] = '612QKBQD225A2' # 手机ID
self.desired_caps['noReset'] = True # true:不重新安装APP,false:重新安装app
self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub",self.desired_caps)
return self.driver
except Exception,e:
raise e
1.4 公共类Base_page.py
# coding:utf-8
__author__ = 'Helen'
'''
description:UI页面公共类
'''
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC class Base_page:
def __init__(self,driver):
self.driver = driver def find_element(self,*loc):
'''重写find_element方法,显式等待'''
try:
WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(loc))
return self.driver.find_element(*loc)
except Exception as e:
raise e def send_keys(self,value,*loc):
try:
self.find_element(*loc).clear()
self.find_element(*loc).send_keys(value)
except AttributeError,e:
raise e
1.5 公共类gesture_mainpulation
# coding:utf-8
__author__ = 'Helen'
'''
description:手势操作
'''
class gesture_mainpulation:
def swipe_left(self,driver):
'''左滑'''
x = driver.get_window_size()['width']
y = driver.get_window_size()['height']
driver.swipe(x*3/4,y/4,x/4,y/4) def swipe_right(self,driver):
'''右滑'''
x = driver.get_window_size()['width']
y = driver.get_window_size()['height']
driver.swipe(x/4,y/4,x*3/4,y/4) def swipe_down(self,driver):
'''下滑'''
x = driver.get_window_size()['width']
y = driver.get_window_size()['height']
driver.swipe(x/2,y*3/4,x/2,y/4) def swipe_up(self,driver):
'''上滑'''
x = driver.get_window_size()['width']
y = driver.get_window_size()['height']
driver.swipe(x/2,y/4,x/2,y*3/4)
1.6 公共类send_email
# coding:utf-8
__author__ = 'Helen'
'''
description:邮件发送最新的测试报告
'''
import os,smtplib,os.path
from config import globalparameter as gl
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText class send_email:
# 定义邮件内容
def email_init(self,report,reportName):
with open(report,'rb')as f:
mail_body = f.read() # 创建一个带附件的邮件实例
msg = MIMEMultipart()
# 以测试报告作为邮件正文
msg.attach(MIMEText(mail_body,'html','utf-8'))
report_file = MIMEText(mail_body,'html','utf-8')
# 定义附件名称(附件的名称可以随便定义,你写的是什么邮件里面显示的就是什么)
report_file["Content-Disposition"] = 'attachment;filename='+reportName
msg.attach(report_file) # 添加附件
msg['Subject'] = '88APP自动化测试报告:'+reportName # 邮件标题
msg['From'] = gl.email_name #发件人
msg['To'] = gl.email_To #收件人列表
try:
server = smtplib.SMTP(gl.smtp_sever)
server.login(gl.email_name,gl.email_password)
server.sendmail(msg['From'],msg['To'].split(';'),msg.as_string())
server.quit()
except smtplib.SMTPException:
self.mylog.error(u'邮件发送测试报告失败 at'+__file__) def sendReport(self):
# 找到最新的测试报告
report_list = os.listdir(gl.report_path)
report_list.sort(key=lambda fn: os.path.getmtime(gl.report_path+fn) if not os.path.isdir(gl.report_path+fn) else 0)
new_report = os.path.join(gl.report_path,report_list[-1])
# 发送邮件
self.email_init(new_report,report_list[-1])
1.7 页面事件login_page.py(只记录一个,其他的都一样)
# coding:utf-8
__author__ = 'Helen'
'''
description:登录页
'''
from src.common import Base_page
from appium.webdriver.common import mobileby class login_page(Base_page.Base_page):
by = mobileby.MobileBy()
etUser_loc = (by.ID,"com.xsteach.appedu:id/etUser")
etPws_loc = (by.ID,"com.xsteach.appedu:id/etPwd")
btnLogin_loc = (by.ID,"com.xsteach.appedu:id/btnLogin") def input_user(self,username):
self.send_keys(username,*self.etUser_loc) def input_Pws(self,password):
self.send_keys(password,*self.etPws_loc) def click_btnLogin(self):
self.find_element(*self.btnLogin_loc).click()
1.8 测试用例test_appium.py
# coding:utf-8
__author__ = 'Helen'
'''
description:测试登录和退出功能
'''
import unittest from src.pages import index_page,myInfo_page,login_page,relative_page
from src.common import gesture_manipulation
from config import driver_configure class test_appium(unittest.TestCase):
@classmethod
def setUpClass(cls):
dconfigur = driver_configure.driver_configure()
cls.driver = dconfigur.get_driver()
cls.GM = gesture_manipulation.gesture_mainpulation() def test_01login(self):
'''测试登录功能'''
# 主页面
self.index_page = index_page.index_page(self.driver)
self.index_page.click_btnCancel()
self.GM.swipe_down(self.driver)
self.index_page.click_inXSTeach()
# 我在邢帅
self.myInfo_page = myInfo_page.myInfo_page(self.driver)
self.myInfo_page.click_login_btn()
# 登录页
self.login_page = login_page.login_page(self.driver)
self.login_page.input_user("lihailing@xsteqach.com")
self.login_page.input_Pws("")
self.login_page.click_btnLogin()
self.assertEqual(u'签到',self.myInfo_page.getText_signHint()) def test_02loginOut(self):
'''测试退出功能'''
self.myInfo_page = myInfo_page.myInfo_page(self.driver)
self.myInfo_page.click_relative()
self.relative = relative_page.relative_page(self.driver)
self.relative.click_tvLoginOut()
self.relative.click_btnYes()
self.assertEqual(u'点击登录',self.myInfo_page.getText_login()) @classmethod
def tearDownClass(cls):
cls.driver.quit() if __name__=='__main__':
unittest.main()
1.9 执行测试runtest.py
# coding:utf-8
__author__ = 'Helen'
'''
description:执行测试
'''
import unittest,time,HTMLTestRunner
from config.globalparameter import test_case_path,report_name
from src.common import send_mail # 构建测试集,包含src/test_case目录下的所有以test开头的.py文件
suite = unittest.defaultTestLoader.discover(start_dir=test_case_path,pattern='test*.py') # 执行测试
if __name__=="__main__":
report = report_name+"Report.html"
fb = open(report,'wb')
runner = HTMLTestRunner.HTMLTestRunner(
stream=fb,
title=u'88APP自动化测试报告',
description=u'项目描述:生产环境'
)
runner.run(suite)
fb.close()
# 发送邮件
time.sleep(10) # 设置睡眠时间,等待测试报告生成完毕(这里被坑了==)
email = send_mail.send_email()
email.sendReport()
Appium+python测试app实例的更多相关文章
- appium+python测试app使用相对坐标定位元素
		
我们获取到的是绝对坐标,如果换一个屏幕分辨率不同的手机那这个坐标自然会发生变化,要实现不同手机均能实现点击同一控件自然要用到相对坐标了,具体方法如下: 1.获取当前空间的绝对坐标(x1,y1),开启指 ...
 - appium+Python 启动app(二)
		
我们上步操作基本完成,下面介绍编写Python脚本启动app 打开我们pycharm新建.py文件 第一步:输入Python脚本代码: #coding=utf-8 from appium import ...
 - appium+Python 启动app(一)
		
当我们appium和Python环境都配置好了,如何启动我们第一个app呢?下面介绍appium+Python启动app的操作步骤,为了能够详细查看,我们这里使用夜游神模拟器进行示范. 测试项目:QQ ...
 - Appium + Python 测试 QQ 音乐 APP的一段简单脚本
		
1. 大致流程 + 程序(Python):打开 QQ 音乐,点击一系列接收按键,进入搜索音乐界面,输入『Paradise』,播放第一首音乐. 2. Python 脚本如下 from appium im ...
 - appium+python的APP自动化(2)
		
上节说到安卓上的测试环境都安装好了,这个时候要安装python了 1python的安装 https://www.python.org/15官网下载python2.7(3.0以上也行,个人爱好),安装也 ...
 - 利用Appium Python测试爱壁纸的登录和设置壁纸
		
设置壁纸: #coding:utf-8 #Import the common package import os import unittest from appium import webdrive ...
 - appium+python的APP自动化(1)
		
写这个东西也是自己喜欢研究些自动化的东西,以下全是自己的经验所得,由于开源的软件对于各版本以及操作系统要求很高,会经常碰到一些不兼容的问题,这个都属于正常的,换版本就对了. 本人的环境搭建都是在win ...
 - appium+Python 启动app(三)登录
		
我们根据前面的知识点,用uiautomatorviewer工具来获取我们当前的元素 (注:uiautomatorviewer 是 android sdk 自带的) 知识点:appium的webdriv ...
 - appium+python,app自动化测试框架
		
目前正在写一个app的自动化UI测试框架,目录结构如, 脚本还在调试,实现的方法是从excel表格读取测试用例,执行完成后会将结果保存到Excel中. 等待.......
 
随机推荐
- c#多线程同步之EventWaitHandle的应用
			
最近在研究前辈写的winform代码,其中有一个功能,前辈用了EventWaitHandle.初读代码,有点不理解,慢慢想来,还是可以理解的.这个功能,就是执行某项比较耗时的任务,需要打开旋转图标,等 ...
 - Java 对IP请求进行限流.
			
高并发系统下, 有三把利器 缓存 降级 限流. 缓存: 将常用数据缓存起来, 减少数据库或者磁盘IO 降级: 保护核心系统, 降低非核心业务请求响应 限流: 在某一个时间窗口内对请求进行限速, 保护系 ...
 - [转]Git教程【译】
			
[转]Git教程[译] http://www.cnblogs.com/zhangjing230/archive/2012/05/09/2489745.html 原文出处:http://www.voge ...
 - postgresql和oracle数据库对比
			
SQL执行计划干预 从使用postgresql来看,想要改变执行计划只能通过対表进行分析,不能通过添加hint的方式来改变执行计划: oracle不仅可以通过对表进行收集统计来改变执行计划,而且很重要 ...
 - 使用Angular CLI进行Build (构建) 和 Serve
			
第一篇文章是: "使用angular cli生成angular5项目" : http://www.cnblogs.com/cgzl/p/8594571.html 第二篇文章是: & ...
 - Download a image 图片另存为
			
点击一个链接,下载图片: JS: 1.找到图片的URL,即src的值: 2.创建一个anchor,将URL赋值给anchor 的 href. 3.将anchor追加到body,并且添加click事件: ...
 - 关于html文档的规范
			
1. <!DOCTYPE html> 告诉浏览器该文档使用哪种html或xhtml的规范 2. 元数据中的X-UA-Compatible <meta http-equiv=" ...
 - 记录一则ASM实例阻塞,rbal进程异常的案例
			
1.故障现象描述 2.确认故障现象 3.排查ASM层面 4.解决问题 1.故障现象描述 环境:AIX 7.1 + Standalone Oracle 11.2.0.4 现象:客户反映某11g版本的AD ...
 - shiro权限框架(五)
			
五.与Spring集成 5.1 环境准备 <dependency> <groupId>org.apache.shiro</groupId> <artifact ...
 - Post Office
			
Post Office poj-1160 题目大意:给你在数轴上的n个村庄,建立m个邮局,使得每一个村庄距离它最近的邮局的距离和最小,求距离最小和. 注释:n<=300,m<=min(n, ...