自动化测试框架开发python selenium excel POM
2022-10-29 19:58 清风软件测试开发 阅读(342) 评论(1) 编辑 收藏 举报自动化测试框架开发python selenium excel POM
本文主要是采用python selenium excel POM 做UI自动化测试
测试框架选用的是unittest
测试框架用的是POM分层设计
测试数据用的是excel
测试报告用的是excel
测试日志用的是logging模块
分层结构主要分为:base层,page层,data层,case层,logging层
首先是base层:
#!/usr/bin/env python # -*- coding: utf-8 -*- class Base(): def __init__(self,driver): self.driver = driver def open_url(self,url): self.driver.get(url) def click(self,locator): self.driver.find_element(*locator).click() def input_text(self,locator,text): self.driver.find_element(*locator).send_keys(text) def element_text(self,locator): return self.driver.find_element(*locator).text
page层:
from selenium.webdriver.common.by import By from ui_auto_test001.base import Base class HomePage(Base): goto_login_page = (By.ID, 'xxxxxxx') def __init__(self, driver): super().__init__(driver) def click_login(self,url): self.open_url(url) self.click(self.goto_login_page)
class LoginPage(Base): input_phoneNumber = (By.ID, 'xxxxxxx') input_Password = (By.ID, 'xxxxxxx') login_Button = (By.ID, 'xxxxxxx') locate_mark = (By.ID, 'xxxxxxx') login_failed_reason = (By.ID, 'xxxxxxx') def __init__(self, driver): super().__init__(driver) def input_phone_number(self, phone_number): self.input_text(self.input_phone_number, phone_number) def input_password(self, password): self.input_text(self.input_Password, password) def click_login_button(self): self.click(self.login_Button) return self.get_actual_login_status() def get_login_failed_reason(self): return self.element_text(self.login_failed_reason) def get_actual_login_status(self): if self.is_element_displayed(self.locate_mark): return self.get_login_failed_reason() else: return '登入成功'
case层:
from ui_auto_test001.HomePage import HomePage from ui_auto_test001.LoginPage import LoginPage from ui_auto_test001.TestData import GetTestData from ui_auto_test001.TestResult import TestResult from ui_auto_test001.baselog import get_logger log = get_logger() get_test_data = GetTestData() write_test_result = TestResult() test_data_list = get_test_data.get_test_data @ddt class TestLogin(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.implicitly_wait(4) self.driver.maximize_window() self.home_page = HomePage(self.driver) self.login_page = LoginPage(self.driver) def tearDown(self): self.driver.quit() @data(*test_data_list) @unpack def test_login_001(self, url, phone_number, password, expext_result): try: self.home_page.click_login(url) self.login_page.input_phone_number(phone_number) self.login_page.input_password(password) actual_login_result = self.login_page.click_login_button() if expext_result.encode('utf-8') == actual_login_result.encode('utf-8'): write_test_result.write_test_result('Pass') else: write_test_result.write_test_result('Fail') except Exception as e: log.info(e) actual_login_result = 'Fail,something wrong' write_test_result.write_test_result('Fail:{}'.format(actual_login_result)) write_test_result.write_actual_result(actual_login_result) self.driver.quit() if __name__ == '__main__': unittest.main()
data层:
class GetTestData(): def __init__(self): self.excel = xlrd.open_workbook('../test_report/test_report.xlsx') self.sheet = self.excel.sheet_by_name('test_data') self.rows = self.sheet.nrows self.cols = self.sheet.ncols @property def get_test_data(self): data_list = [] data = [] for row in range(1 ,self.rows): for i in self.sheet.row_values(row ,1 ,5): if type(i) == float: data.append(int(i)) else: data.append(i) data_list.append(data) data =[] # 移除测试数据 return data_list def test_result(self): for row in range(1, self.rows): self.sheet.cell_value(row, 7)
import xlrd import openpyxl class TestResult(): def __init__(self): self.excel = openpyxl.load_workbook('../test_report/test_report.xlsx') self.sheet = self.excel['test_data'] self.num = 2 self.result = 2 def write_actual_result(self,actual_result): self.sheet.cell(row=self.num,column=6,value=actual_result) self.num +=1 self.close_excel() def write_test_result(self,test_result): self.sheet.cell(row=self.result,column=7,value=test_result) self.result +=1 self.close_excel() def close_excel(self): self.excel.save('../test_report/test_report.xlsx')
logging层:
import logging import os import sys import time from logging.handlers import RotatingFileHandler try: iteration_time = os.environ['WEBSERVICE_ITERATION_RUN_TIME'] except KeyError: iteration_time = time.strftime("%Y-%m-%d_%H_%M_%S") # 如果去掉下面这一句,生成的日志就和testcase处于同一个文件夹下面 ROOT_DIR = os.path.abspath(os.path.curdir) ROOT_DIR = os.path.dirname(ROOT_DIR) # sys.path.append(ROOT_DIR) logFile = os.path.join(ROOT_DIR, 'logs') logFile = os.path.join(logFile, iteration_time) logLevel = 3 _logLevel = { 1: logging.NOTSET, 2: logging.DEBUG, 3: logging.INFO, 4: logging.WARNING, 5: logging.ERROR, 6: logging.CRITICAL } def get_logger(): funcName = sys._getframe().f_back.f_code.co_filename script_name = funcName.split(os.sep)[-1].split('.')[0] log_level = logLevel log_path = logFile if os.path.exists(log_path): log_file = os.path.join(log_path, script_name + '.log') else: os.makedirs(r'%s' % log_path) log_file = os.path.join(log_path, script_name + '.log') logger = logging.getLogger() logger.setLevel(_logLevel[log_level]) if not logger.handlers: ch = logging.StreamHandler() ch.setLevel(_logLevel[log_level]) rh = RotatingFileHandler(log_file, maxBytes=10 * 1024 * 1024, backupCount=5, encoding='utf-8') rh.setLevel(_logLevel[log_level]) formatter = logging.Formatter('%(asctime)s [%(filename)s][line:%(lineno)d][%(name)s][%(levelname)s] %(message)s') ch.setFormatter(formatter) rh.setFormatter(formatter) logger.addHandler(ch) logger.addHandler(rh) return logger