阅读 139

Appium实战汽车订票App

目录结构

目录结构.png

结构说明:

  1. fujian文件夹: 存放图片,文档之类,供发送邮件之类,在发送测试报告邮件的同时,附带上功能需求文档,功能测试用例、功能测试Bug汇总之类的文档;

  2. insert_img文件夹: 存放断言截图;

  3. Public文件夹: 存放公共调用封装的脚本;

  4. TestCase文件夹: 存放测试用例脚本,例如dingpiao.py脚本文件,严格命名上test_dingpiao.py比较合理;

  5. result_report文件夹: 存放测试报告;

    存放测试报告.jpg

  6. dingpiao.py: 订票App页面操作脚本,例如查询、下订单提交等操作;

  7. function.py: 封装页面断言截图脚本;

  8. myunit.py: 封装设备信息、App包名、启动程序脚本;

  9. Send_mail_report: 封装邮件发送、执行测试用例、抓取最新测试报告、添加附件脚本;

脚本参考

1.myunit.py

'''
Created on 2017年10月7日
@author: Yvon_早安阳光
'''
#coding:utf-8
from appium import webdriver
import unittest

class MyTest(unittest.TestCase):
    def setUp(self):
        desired_caps = {
          'platformName': 'Android',
#           'deviceName': 'YVF4C15727002844',#华为平板
        'deviceName': 'a8e52329',#x5play
          'platformVersion': '6.0',
          'appPackage': 'com.Qunar', # apk包名
          'appActivity': 'com.mqunar.splash.SplashActivity', # apk的launcherActivity
#           '''输入法 '''
          'unicodeKeyboard':True,
          'resetKeyboard':True,
          'newCommandTimeout':3000 #超时等待
        }
        self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
        return self.driver

2.function.py

断言截图.jpg
#coding:utf-8
import time

#封装截图函数
'''操作失败截图'''
def insert_img(driver,filename):
    file_path = 'insert_img/'
    now = time.strftime("%Y-%m-%d %H_%M_%S")
    screen_name = file_path + '\\' + now + filename
    driver.get_screenshot_as_file(screen_name)
    
'''操作成功截图'''    
def success_img(driver,file_name):
    filepath = 'success_img/'
    now = time.strftime("%Y-%m-%d %H_%M_%S")
    screenname = filepath + '\\' + now + file_name
    driver.get_screenshot_as_file(screenname)   

3.dingpiao.py

import time,unittest,myunit,function

class DingPiaoTest(myunit.MyTest):
          
    def test_dingpiao(self):
        driver = self.driver
        time.sleep(20)
        # 点击首页汽车票
        driver.find_element_by_id("com.mqunar.atom.alexhome:id/atom_alexhome_mod_bus_ticket").click()
        time.sleep(2)
        function.insert_img(self.driver,"首页汽车票.png")
        time.sleep(2)
        #点击出发地
        driver.find_element_by_id("com.mqunar.atom.bus:id/atom_bus_tv_dep_city").click()
        #出发地搜索
        time.sleep(2)
        driver.find_element_by_id("com.mqunar.patch:id/pub_pat_title_etSearch").click()
        time.sleep(2)
        driver.find_element_by_id("com.mqunar.patch:id/pub_pat_title_etSearch").send_keys(u"杭州")
        #点击确定按钮,返回汽车票订购页面
        driver.find_element_by_id("com.mqunar.patch:id/pub_pat_title_btnSearch").click()
        time.sleep(2)
        #汽车票订购页面,点击到达地
        driver.find_element_by_id("com.mqunar.atom.bus:id/atom_bus_tv_arr_city").click()
        #到达地搜索
        time.sleep(2)
        driver.find_element_by_id("com.mqunar.patch:id/pub_pat_title_etSearch").click()
        time.sleep(2)
        driver.find_element_by_id("com.mqunar.patch:id/pub_pat_title_etSearch").send_keys(u"上海")
        time.sleep(2)
        #点击确定按钮,返回汽车票订购页面
        driver.find_element_by_id("com.mqunar.patch:id/pub_pat_title_btnSearch").click()
        time.sleep(2)
        
        #搜索订票
        driver.find_element_by_id("com.mqunar.atom.bus:id/atom_bus_bt_search").click()
        time.sleep(3)
        list2 = driver.find_elements_by_xpath("//android.widget.ListView[@resource-id=\"android:id/list\"]/android.widget.LinearLayout[4]")
        list2.pop(0).click()
        time.sleep(3)
        function.insert_img(self.driver,"搜索到汽车票.png")
        #预定
        time.sleep(2)
        driver.find_element_by_id("com.mqunar.atom.bus:id/atom_bus_tv_booking").click()
        time.sleep(2)
        driver.find_element_by_id("com.mqunar.atom.bus:id/atom_bus_order_ib_passenger_add").click()
        #勾选乘客,确定选择
        time.sleep(2)
        '''取消勾选'''
        driver.find_element_by_id("com.mqunar.atom.bus:id/cb").click()
        time.sleep(2)
        '''勾选一个客户'''
        driver.find_element_by_id("com.mqunar.atom.bus:id/cb").click()
        driver.find_element_by_id("com.mqunar.patch:id/pub_pat_ll_right_function_area").click()
        time.sleep(2)
        function.insert_img(self.driver,"自己.png")
        #支付页面,向上滑并且点击支付
        time.sleep(2)
        # driver.swipe(300, 874, 300, 300, 500)
        '''优化滑屏'''
        s = driver.get_window_size()
        print (s['width'])
        print(s['height'])
        '''向上滑x轴不变,y轴变小'''
        driver.swipe(s['width']*0.3, s['height']*0.7, s['width']*0.3, s['height']*0.3, 500)
        time.sleep(2)
        '''向下滑x轴不变,y轴变大'''
        driver.swipe(s['width']*0.3, s['height']*0.4, s['width']*0.3, s['height']*0.8, 500)
        time.sleep(2)
        driver.find_element_by_id("com.mqunar.atom.bus:id/atom_bus_order_bottom_bt_submit").click()
        time.sleep(2)
        function.insert_img(self.driver,"支付页面.png")

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

4.Send_mail_report.py

'''
#coding:utf-8 
import unittest,time,os,smtplib
from HTMLTestRunner import HTMLTestRunner
from email.mime.multipart import MIMEMultipart
from email.header import Header
from email.mime.application import MIMEApplication
from email.mime.text import MIMEText

#======================定义发送邮件======================

def send_mail(file_new,jpg_new):
    '''file_new是指最新的测试报告'''
    f = open(file_new,'rb')
    mail_body = f.read()
    f.close()
    
    '''jpg_new是指最新的登录成功截图'''
    j = open(jpg_new,'rb')
    mail_body1 = j.read()
    j.close()
    
    msg = MIMEMultipart()
    '''邮件标题'''
    msg['Subject'] = Header('汽车票订购测试报告' ,'utf-8')
    
    '''邮件正文'''
    zhengwen = "各位,详情测试结果请见附件文档"
    msg.attach(MIMEText(zhengwen, 'plain')) 
    
    '''附件测试报告 '''
    att= MIMEText(mail_body,'html','utf-8')
    att.add_header('Content-Disposition', 'attachment', filename='New_TestReport.html')
    msg.attach(att)
    
    '''发送成功登陆的图片'''
    att1= MIMEText(mail_body1,'png','utf-8')
    att1.add_header('Content-Disposition', 'attachment', filename='乘客订票.png')
    msg.attach(att1)
   
    '''附件图片'''
    jpgpart = MIMEApplication(open('fujian/测试计划.jpg','rb').read())
    jpgpart.add_header('Content-Disposition', 'attachment', filename='测试计划.jpg')
    msg.attach(jpgpart)
    
    '''附件PDF'''
    pdfpart = MIMEApplication(open('fujian/功能需求.pdf','rb').read())
    pdfpart.add_header('Content-Disposition', 'attachment', filename='功能需求.pdf')
    msg.attach(pdfpart)
    
    msg['from'] = 'zhangfajinjiayou@yeah.net'
    msg['to'] = 'huiselanse@yeah.net'

    #发送邮件
    smtp = smtplib.SMTP()
    smtp.connect('smtp.yeah.net')
    smtp.login('zhangfajinjiayou@yeah.net','36#20de@36$20A4p')
    smtp.sendmail('zhangfajinjiayou@yeah.net','huiselanse@yeah.net',msg.as_string())
    print('email has send out !')
    smtp.quit()
    
#======================查找测试报告的目录,找到最新生成的测试报告======================
'''按时间获取最新的测试报告'''
def new_report(testreport):
    lists = os.listdir(testreport)
    lists.sort(key = lambda fn: os.path.getatime(testreport + '\\' + fn))
    file_new = os.path.join(testreport,lists[-1])
    print(file_new)
    return file_new

'''按时间获取最新的登录成功的图片'''
def new_success_img(testimg):
    lists = os.listdir(testimg)
    lists.sort(key = lambda fn: os.path.getatime(testimg + '\\' + fn))
    jpg_new = os.path.join(testimg,lists[-1])
    print(jpg_new)
    return jpg_new

if __name__ == '__main__':
    
    #指定测试用例为当前文件夹下的目录
    test_dir = './'
    discover = unittest.defaultTestLoader.discover(test_dir, pattern='dingpiao*.py')
    
    #报告存放的路径
    result_report_dir= 'result_report/'
    #登录成功截图存放的路径
    success_img_dir= 'insert_img/'
    '''实时当前的时间'''
    now = time.strftime("%Y-%m-%d %H_%M_%S")
    file_name = result_report_dir + '\\' + now +'result.html'
    fp = open(file_name,'wb')    
    #定义测试报告
    runner =HTMLTestRunner(stream = fp,title ='自动化测试报告',
                           description='用例执行情况:')
    runner.run(discover)
    fp.close() #关闭报告文件
 
    new_report = new_report(result_report_dir)
    new_success_img = new_success_img(success_img_dir)
    send_mail(new_report,new_success_img)

发送测试邮件.png
最新测试报告.png

作者:寒殇

原文链接:https://www.jianshu.com/p/364971b0422b

文章分类
后端
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐