当前位置: 移动技术网 > IT编程>脚本编程>Python > 移动端自动化测试框架搭建

移动端自动化测试框架搭建

2019年03月07日  | 移动技术网IT编程  | 我要评论

上海美罗城柯达影院,李璐禕,岑溪人家

搭建框架的几点思考:

  • 需要框架实现什么功能
  • 前期需要做数据与业务分离,方便维护
  • 测试用例集管理要方便,便于异常时重新run单条用例
  • 用例设计需要尽可能简单,多提供公共方法

 

 

  1. 目前设计框架如下图:

  

  • commonlibrary 用于存放公共库
  • testcasesrepository 用于存放测试用例
  • testdata 用于存放与测试用例有关的测试数据文档
  • testrun_xxx 是测试过程中生成的测试结果,内含测试报告和日志
  • autoruntest.py 用于从用例列表中读取需要执行的测试用例文件,及测试结束后测试报告的发送
  • testcases.txt 用于存放用例表

  2. 其中公共库中大致有appium的操作、excel档的操作、手机信息的配置、测试用例信息的收集、测试结果文件夹的生产 、测试报告的写入、邮件发送等公共方法

目录如下:

 

如appiumserver大致如下设计:

#! /usr/bin/env python
#coding=utf-8
import os,time

def start_appiumserver(port11,port12,deviceuuid):
 
    os.system("cd c:\\program files (x86)\\appium\\  && start node node_modules\\appium\\lib\\server\\main.js --address 127.0.0.1 --port "+port11+ " -bp "+port12+" -u "+deviceuuid)
    time.sleep(2)

def kill_appiumserver(port13):
    # 查找对应端口的pid
    cmd_find = 'netstat -aon | findstr %s' % port13
    print(cmd_find)

    result = os.popen(cmd_find)
    text = result.read()
    pid = text[-5:-1]

    # 执行被占用端口的pid
    cmd_kill = 'taskkill -f -pid %s' % pid
    print(cmd_kill)
    os.popen(cmd_kill)

if __name__ == '__main__':
    #start_appiumserver('4729','4728','bibi5leu6prcdiiv')
    #start_appiumserver('4727','4724','75a2daf1')
    #time.sleep(10)
    kill_appiumserver(4729)
    kill_appiumserver(4727)

 

emailutils结构如下:

#! /usr/bin/env python
#coding=utf-8

import smtplib
from os.path import basename
from email.mime.application import mimeapplication
from email.mime.multipart import mimemultipart
from email.mime.text import mimetext
from email.utils import commaspace,formatdate
from datetime import datetime
import resultfolder
from email.header import header
import zipfile,os
import decode_pwd



def send_email(send_from,send_to,subject,text,files1,files2,server="smtp.163.com"):

    #assert(isinstance(send_to,list),"send to email should be a list")

    msg = mimemultipart()
    msg['from'] = send_from
    msg['to'] = send_to
    msg['date'] = formatdate(localtime = true)
    msg['subject'] = header(subject, 'utf-8')
    my_password = "7a68656e676a696e6731313238"
    my_pass = decode_pwd.decode_pwd(my_password)

    msg.attach(mimetext(text,'html'))

    with open(files1,"rb") as f:
        part1 = mimeapplication(f.read())
        part1["content-type"] = 'application/octet-stream'
        part1["content-disposition"] = 'attachment; filename="testresult.html"'
        msg.attach(part1)
		
    with open(files2,"rb") as g:
        part2 = mimeapplication(g.read())
        part2["content-type"] = 'application/octet-stream'
        part2["content-disposition"] = 'attachment; filename="test.log"'
        msg.attach(part2)
    smtp = smtplib.smtp_ssl(server,465)
    smtp.login(send_from,my_pass)
    smtp.sendmail(send_from,send_to,msg.as_string())
    smtp.quit()

def zip_report(startdir,file_news):
    print startdir
    z = zipfile.zipfile(file_news,'w',zipfile.zip_deflated)
    for dirpath, dirnames, filenames in os.walk(startdir):
        fpath = dirpath.replace(startdir, '')  # 这一句很重要,不replace的话,就从根目录开始复制
        fpath = fpath and fpath + os.sep or ''  # 这句话理解我也点郁闷,实现当前文件夹以及包含的所有文件的压缩
        for filename in filenames:
            z.write(os.path.join(dirpath, filename), fpath + filename)
    print ('压缩成功')
    z.close()


def send_report():
    send_f = "xxxxx@163.com"
    send_t = "xxxxxx@163.com"
    subject = "software test report_"+ str(datetime.today())

    files1 = resultfolder.getrundirectory()+"\\testresult.html"

    print "zip log is start"
    zipdir = resultfolder.getrundirectory()+"\\"
    file2_news = zipdir + 'testlog.zip'
    zip_report(zipdir,file2_news)
    files2 = resultfolder.getrundirectory() + "\\testlog.zip"


    with open(files1,'r') as f:
        text = f.read()

    send_email(send_f,send_t,subject,text,files1,files2)

if __name__=='__main__':
    send_report()

  

其他模块就不一一列举。

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网