自动化测试对程序的回归测试更方便。 由于回归测试的动作和用例是完全设计好的,测试期望的结果也是完全可以预料的,将回归测试自动运行...
可以运行更加繁琐的测试 自动化测试的一个明显好处就是可以在很短的时间内运行更多的测试。学习自动化测试最终目的是应用到实际项目中,本篇将介绍大家自动化测试框架:
- 项目目录结构:
- 基本类模块代码
from Common.Log import framelog
class base():def __init__(self,driver):self.driver = driverself.log = framelog().log()self.log.info("info")#把八大定位放在一个函数里面def find_ele(self,dic):#传递过来字典第一个即为定位方式by =list(dic.keys())[0];print("by"+by)#传递过来字典第二个为具体的元素ele=list(dic.values())[0];self.log.info("id")self.log.info("元素"+ele)try:if by == 'id':return self.driver.find_element_by_id(ele)elif by == 'name':return self.driver.find_element_by_name(ele)elif by == 'className':return self.driver.find_element_by_class_name(ele)elif by== 'linktext':return self.find_element_by_link_text(ele)elif by == 'partial':return self.find_element_by_partial_link_text(ele)elif by == "css":return self.driver.find_element_by_css_selector(ele)elif by == "xpath":return self.driver.find_element_by_xpath(ele)else:return self.driver.find_element_by_tag_name(ele)except:return None#输入值def send_key(self,ele):print(ele)return self.find_ele(ele)#后退def back(self):self.driver.back()#前进def forword(self):self.driver.forward()#当前窗口urldef url(self):self.driver.current_url
- 数据模块-读取excel操作:
import xlrd,os
#读excel操作、所有数据存放字典中
#filename为文件名
#index为excel sheet工作簿索引
def read_excel(filename,index):xls = xlrd.open_workbook(filename)sheet = xls.sheet_by_index(index)print(sheet.nrows)print(sheet.ncols)dic={}for j in range(sheet.ncols):data=[]for i in range(sheet.nrows):data.append(sheet.row_values(i)[j])dic[j]=datareturn dic
- log模块日志封装:
import logging,tim
from Common.function import projectPath
class framelog():def __init__(self, logger=None):# 创建一个loggerself.logger = logging.getLogger(logger)self.logger.setLevel(logging.DEBUG)# 创建一个handler,用于写入日志文件self.log_time = time.strftime("%Y_%m_%d_")#路径需要修改self.log_path = projectPath()+"/Logs/"self.log_name = self.log_path + self.log_time + 'log.log'print(self.log_name)fh = logging.FileHandler(self.log_name, 'a', encoding='utf-8')fh.setLevel(logging.INFO)# 定义handler的输出格式formatter = logging.Formatter('[%(asctime)s] %(filename)s->%(funcName)s line:%(lineno)d [%(levelname)s]%(message)s')fh.setFormatter(formatter)self.logger.addHandler(fh)# 添加下面一句,在记录日志之后移除句柄# self.logger.removeHandler(fh)# 关闭打开的文件fh.close()def log(self):return self.logger
- PO车次预定模块实现:
from Base.base import base
import time
class bookPage(base):#预定车票def book(self):return self.by_xpath("//*[@id='tbody-01-K5260']/div[1]/div[6]/div[4]/a")#动车def book_typeD(self):return self.by_css("#resultFilters01 > dl:nth-child(1) > dd.current > label > i")# 关闭浮层def book_close(self):return self.by_css("#appd_wrap_close")def book_nologin(self):return self.by_css("#btn_nologin")def bookBtn(self):try:time.sleep(7)self.book_close().click()time.sleep(2)self.book().click()time.sleep(2)self.book_nologin().click()except:self.log.error("车次查询失败")Nonereturn self.dr_url()
- 测试配置文件:
[testUrl]
url = “测试环境url"
[productUrl]
url = "生产环境url"
- 测试用例管理模块
- 测试套件管理:
import unittest
import HTMLTestRunner
import time
from Common.function import projectPathif __name__ == '__main__':test_dir=projectPath()+"TestCases"tests=unittest.defaultTestLoader.discover(test_dir,pattern ='test*.py',top_level_dir=None)now = time.strftime("%Y-%m-%M-%H_%M_%S",time.localtime(time.time()))filepath=projectPath()+"/Reports/"+now+'.html'fp=open(filepath,'wb')#定义测试报告的标题与描述runner = HTMLTestRunner.HTMLTestRunner(stream=fp,title=u'自动化测试报告',description=u'测试报告')runner.run(tests)fp.close()
2.测试脚本:
import os,sys
sys.path.append(os.path.split(os.getcwd())[0])
import time,unittest,HTMLTestRunner
from PageObject.bookPage import bookPage
from PageObject.orderPage import orderPage
from PageObject.searchPage import searchPagefrom Base.baseUnit import unitBase
from selenium import webdriverclass SearchTest(unitBase):def test_02(self):search = searchPage(self.driver)res =search.searchTrain("杭州","上海","2019-05-10")#本例断言是根据当前页面的url去判断的self.assertIn('TrainBooking',res)def test_03(self):book = bookPage(self.driver)res=book.bookBtn()self.assertIn("InputPassengers",res)def test_04(self):order = orderPage(self.driver)order.userInfo("小王")
- 项目格式:
以上为代码级web自动化测试框架,后续将分享无代码自动化测试框架
最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:
这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!