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()