1. 程式人生 > >selenium+python3+excel測試框架

selenium+python3+excel測試框架

next() ret msl ron __init__ red icm != value

介紹:框架采用excel驅動unittest 完成ui自動化測試

安裝python、selenium的方法就不說了,網上一大把,直接進入正題

技術分享

1.data數據源:

技術分享

動作:封裝selenium各種動作

定位類型:定位元素的方式

定位值:與定位類型匹配使用

輸入值:與動作匹配使用

2.變量文件
技術分享

為了數據後期更好維護,用一個csv文件放置變量

casedata中可直接引用變量值(采用jmeter變量思想)

3.框架介紹

  配置文件:config.conf

#運行模式
[mode]
    mode = normal

#normal模式下各個文件路徑
[normal]
    file 
= ./data/客戶端.xlsx sheet = BB電子 var_file = ./data/變量.csv

  讀取excel文件:excelutil

#-*- coding:utf-8 -*-
import xlrd
import json
import csv
import re
import config
from pylog import Pylog
‘‘‘獲取測試數據‘‘‘

class ExcelUtil:
    def __init__(self,file,sheetname):
        self.sheetname = sheetname
        self.dataSheet 
= xlrd.open_workbook(file).sheet_by_name(sheetname) # get titles self.row = self.dataSheet.row_values(0) # get rows number self.rowNum = self.dataSheet.nrows # get columns number self.colNum = self.dataSheet.ncols # the current column self.curRowNo = 1 #
獲取用例數據 def get_excel(self): var_file = config.get_config("normal","var_file") r = [] while self.hasNext(): s = {} col = self.dataSheet.row_values(self.curRowNo) i = self.colNum for x in range(i): s[self.row[x]] = col[x] #重置用例名稱 s["用例No"] = self.sheetname + str(s["用例No"]) #引入變量 if s["輸入值"][:2] == "${": var = re.findall({(.*)}, s["輸入值"], re.S)[0] vars = self.get_var(var=var,var_file=var_file) s["輸入值"] = vars #引入其他用例集 try: if s["動作"] == "引入": file = ./data/+json.loads(s["輸入值"])["file"] sheet = json.loads(s["輸入值"])["sheet"] aimslist = ExcelUtil(file=file,sheetname=sheet).get_excel() r.extend(aimslist) else: r.append(s) except Exception as e : Pylog.error("引入用例錯誤..." + str(e) + "|"+ s["輸入值"]) self.curRowNo += 1 return r #遍歷 def hasNext(self): if self.rowNum == 0 or self.rowNum <= self.curRowNo: return False else: return True #獲取變量 def get_var(self,var="all_vars",var_file=None): dic = {} try: with open(var_file,r) as file: reader = csv.reader(file) for row in reader: dic[row[0]] = row[1] if var == "all_vars": return dic return dic[var] except Exception as e : Pylog.error("讀取變量錯誤:"+str(e))

  日誌文件:pylog

import logging
import time

class Pylog:
    def __init__(self):
        ##############################日誌配置############################################################
        logging.basicConfig(level=logging.DEBUG,
                            format=%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s,
                            datefmt=%a, %d %b %Y %H:%M:%S,
                            # filename="./log/"+ time.strftime(‘%Y%m%d%H%M%S‘,time.localtime(time.time()))+".log",
                            filename="./Report/test.log",
                            filemode=w)
        console = logging.StreamHandler()
        console.setLevel(logging.INFO)
        formatter = logging.Formatter(%(name)-12s: %(levelname)-8s %(message)s)
        console.setFormatter(formatter)
        logging.getLogger(‘‘).addHandler(console)
        ###################################################################################################

    @staticmethod
    def info(data = None):
        logging.info(data)

    @staticmethod
    def debug(data = None):
        logging.debug(data)

    @staticmethod
    def error(data = None):
        logging.error(data)
    @staticmethod
    def warning(data = None):
        logging.warning(data)

  獲取測試數據:

import config
from pylog import Pylog
from excelutil import ExcelUtil
import json


class Predata:
    def __init__(self):
        pass

    def get_data(self):
        datalist = []
        mode = config.get_config("mode", "mode")
        file = config.get_config("normal","file")
        sheet = config.get_config("normal","sheet")
        if mode == "normal":
            datalist = self.get_exceldata(file,sheet)
        return datalist

    def get_exceldata(self,file,sheet):
        datalist =  ExcelUtil(file,sheet).get_excel()
        n = 0
        for item in datalist:
            item["用例No"] = str(10000+n) + item["用例No"]
            n += 1
        return datalist

  單元測試部分:

import unittest
import config
import ddtSelenium
import time
from pylog import Pylog
from action import Action
from predata import Predata


@ddtSelenium.ddt
class Case(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.action = Action()
        #進入瀏覽器設置
        # options = cls.action.driver.webdriver.ChromeOptions()
        # options.add_argument()

    @classmethod
    def tearDownClass(cls):
        time.sleep(3)
        # cls.action.driver.close()

    @ddtSelenium.data(*Predata().get_data())
    def testcase(self, data):
        Pylog.info(str(data[list(data.keys())[0]]) + "開始")
        if data["動作"] == "等待":
            time.sleep(int(data["輸入值"]))
        else:
            text = self.action.pre_do(data)
        if data["動作"] == "斷言":
            Pylog.info("斷言:"+ str(data[list(data.keys())[0]]))
            if text != data["輸入值"]:
                Pylog.info("斷言截圖:" + str(data[list(data.keys())[0]]))
                self.action.driver.save_png(./Report/+data[用例No]+.jpg)
            self.assertEqual(text, data["輸入值"])

  action部分:(pyse引用蟲師封裝py)

from pylog import Pylog
from pyse import Pyse
import os
import config
from predata import Predata


class Action:
    def __init__(self):
        self.driver = Pyse("chrome")

    def pre_do(self,data):
        if config.get_config("mode","mode") == "normal":
            return self.do_action(data)


    def do_action(self,data):
        try:
            if data["動作"] == "斷言":
                text = self.driver.get_element(data["定位類型"] + => + data["定位值"]).text
                return text
            if data["動作"] == "打開":
                self.driver.open(data["輸入值"])
            if data["動作"] == "點擊":
                self.driver.click(data["定位類型"] + => + data["定位值"])
            if data["動作"] == "清除":
                self.driver.clear(data["定位類型"] + => + data["定位值"])

            # if data["動作"] == "等待":
            #     self.driver.wait(int(data["輸入值"]))

            if data["動作"] == "輸入":
                self.driver.type(data["定位類型"] + => + data["定位值"],data["輸入值"])
            if data["動作"] == "滾動條下拉":
                self.driver.js( "var q=document.body.scrollTop="+data["輸入值"]+";")
            if data["動作"] == "等待元素":
                self.driver.element_wait(data["定位類型"] + => + data["定位值"],10)
            if data["動作"] == "最大化":
                self.driver.max_window()
            if data["動作"] == "關閉瀏覽器":
                self.driver.close()
            if data["動作"] == "外調程序":
                os.system(data["輸入值"])
        except Exception as e :
            Pylog.error("異常截圖-"+data["動作"]+str(e))
            self.driver.save_png(./Report/+data[用例No]+.jpg)

框架源碼:https://github.com/Acguo

selenium+python3+excel測試框架