Selenium3与Python3实战Web自动化测试框架【日记1】
2020/9/14 3:03:44
本文主要是介绍Selenium3与Python3实战Web自动化测试框架【日记1】,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
课程地址
很多人被这门课的pom模型弄得云里雾里的,根据自己的整理,总结了本课程主要的类和对象的调用顺序:
1、数据驱动:LocalElement.ini
[RegisterElement] user_email=id>register_email user_email_error=id>register_email-error user_name=id>register_nickname user_name_error=id>register_nickname-error password=id>register_password password_error=id>register_password-error code_image=id>getcode_num code_text=id>captcha_code code_text_error=id>captcha_code-error register_button=id>register-btn
2、read_ini.py 读取LocalElement.ini数据信息:
#coding=utf-8 import configparser class ReadIni(object): def __init__(self,file_name=None,node=None): if file_name == None: file_name = "E:/Teacher/Imooc/SeleniumPython/config/LocalElement.ini" if node == None: self.node = "RegisterElement" else: self.node = node self.cf = self.load_ini(file_name) #加载文件 def load_ini(self,file_name): cf = configparser.ConfigParser() cf.read(file_name) return cf #获取value得值 def get_value(self,key): data = self.cf.get(self.node,key) return data #if __name__ == '__main__': # read_init = ReadIni() # print(read_init.get_value('user_name'))
3、find_element.py 【调用read_ini.py,返回元素定位对象】
#coding=utf-8 from util.read_ini import ReadIni from log.user_log import UserLog class FindElement(object): def __init__(self,driver): self.driver = driver get_user_log = UserLog() self.logger = get_user_log.get_log() def get_element(self,key): read_ini = ReadIni() data = read_ini.get_value(key) by = data.split('>')[0] value = data.split('>')[1] self.logger.info("定位方式:"+by+"--->定位值:"+value) try: if by == 'id': return self.driver.find_element_by_id(value) elif by == 'name': return self.driver.find_element_by_name(value) elif by == 'className': return self.driver.find_element_by_class_name(value) else: return self.driver.find_element_by_xpath(value) except: return None
4、register_page.py【调用find_element.py,返回元素定位对象】
#coding=utf-8 from base.find_element import FindElement class RegisterPage(object): def __init__(self,driver): self.fd = FindElement(driver) #获取邮箱元素 def get_email_element(self): return self.fd.get_element("user_email") #获取用户名元素 def get_username_element(self): return self.fd.get_element("user_name") #获取密码元素 def get_password_element(self): return self.fd.get_element("password") #获取验证码元素 def get_code_element(self): return self.fd.get_element("code_text") #获取注册按钮元素 def get_button_element(self): return self.fd.get_element("register_button") #获取邮箱错误元素 def get_email_error_element(self): return self.fd.get_element("user_email_error") #获取用户名错误元素 def get_name_error_element(self): return self.fd.get_element("user_name_error") #获取密码错误元素 def get_password_error_element(self): return self.fd.get_element("password_error") #获取验证码错误元素 def get_code_error_element(self): return self.fd.get_element("code_text_errorr")
5、register_handle.py 【调用register_page.py,通过元素对象输入值或者返回数据】
#coding=utf-8 from page.register_page import RegisterPage import sys from util.get_code import GetCode from log.user_log import UserLog class RegisterHandle(object): def __init__(self,driver): self.driver = driver self.register_p = RegisterPage(self.driver) get_user_log = UserLog() self.loger = get_user_log.get_log() #输入邮箱 def send_user_email(self,email): self.loger.info("输入的邮箱值是:"+email) self.register_p.get_email_element().send_keys(email) #输入用户名 def send_user_name(self,username): self.loger.info("输入的用户名是:"+username) self.register_p.get_username_element().send_keys(username) #输入密码 def send_user_password(self,password): self.loger.info("输入的密码是:"+password) self.register_p.get_password_element().send_keys(password) #输入验证码 def send_user_code(self,file_name): get_code_text = GetCode(self.driver) code = get_code_text.code_online(file_name) self.register_p.get_code_element().send_keys(code) #获取文字信息 def get_user_text(self,info,user_info): try: if info == 'user_email_error': text = self.register_p.get_email_error_element().text elif info == 'user_name_error': text = self.register_p.get_name_error_element().text elif info == 'password_error': text = self.register_p.get_password_element().text else: text = self.register_p.get_code_element().text except: text = None return text #点击注册按钮 def click_register_button(self): self.register_p.get_button_element().click() #获取注册按钮文字 def get_register_text(self): return self.register_p.get_button_element().text
6、register_business.py 【调用register_handle.py的send_user_XXX和get_user_text方法】
– 先调用send_user_XXX将case数据传给register_handle处理,再调用get_user_text查看case是否正确
#coding=utf-8 from handle.register_handle import RegisterHandle class RegisterBusiness: def __init__(self,driver): self.register_h = RegisterHandle(driver) def user_base(self,email,name,password,file_name): self.register_h.send_user_email(email) self.register_h.send_user_name(name) self.register_h.send_user_password(password) self.register_h.send_user_code(file_name) self.register_h.click_register_button() def register_succes(self): if self.register_h.get_register_text() == None: return True else: False ''' 执行操作 def login_email_error(self,email,name,password,file_name): self.user_base(email,name,password,file_name) if self.register_h.get_user_text('email_error',"请输入有效的电子邮件地址") == None: #print("邮箱检验不成功") return True else: return False ''' def register_function(self,email,username,password,file_name,assertCode,assertText): self.user_base(email,username,password,file_name) #输入邮箱(假设错误的邮箱),user_email_error就会有值 if self.register_h.get_user_text(assertCode,assertText) == None: # 表示text没值 #print("邮箱输入正确,检验不成功") return True else: #print("邮箱输入错误,成功检验") return False # 表示text有值,邮箱输入错误被校验住了 ''' #name错误 def login_name_error(self,email,name,password,file_name): self.user_base(email,name,password,file_name) if self.register_h.get_user_text('user_name_error',"字符长度必须大于等于4,一个中文字算2个字符") == None: #print("用户名检验不成功") return True else: return False #密码错误 def login_password_error(self,email,name,password,file_name): self.user_base(email,name,password,file_name) if self.register_h.get_user_text('password_error',"最少需要输入 5 个字符") == None: #print("密码检验不成功") return True else: return False #验证码错误 def login_code_error(self,email,name,password,file_name): self.user_base(email,name,password,file_name) if self.register_h.get_user_text('code_text_error',"验证码错误") == None: #print("验证码检验不成功") return True else: return False '''
7、first_ddt_case.py 【调用register_business.py的register_function 方法】
#coding=utf-8 import ddt import unittest import sys sys.path.append('E:\\Teacher\\Imooc\\SeleniumPython') import traceback from business.register_business import RegisterBusiness from selenium import webdriver import HTMLTestRunner from util.excel_util import ExcelUtil import os from log.user_log import UserLog import time ex = ExcelUtil() # 获取操作Excel的对象 data = ex.get_data() # 根据对象调用获取数据的方法获取数据,存放在data里 log = UserLog() logger = log.get_log() file_name = "E:/Teacher/Imooc/SeleniumPython/Image/test001.png" #邮箱、用户名、密码、验证码、错误信息定位元素、错误提示信息 @ddt.ddt class FirstDdtCase(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get('http://www.5itest.cn/register') self.login = RegisterBusiness(self.driver) def tearDown(self): time.sleep(2) #if sys.exc_info()[0]: for method_name,error in self._outcome.errors: if error: case_name = self._testMethodName file_path = os.path.join(os.getcwd()+"/report/"+case_name+".png") self.driver.save_screenshot(file_path) #print("这个是case的后置调键1") self.driver.close() ''' @ddt.data( ['12','Mushishi01','111111','code','user_email_error','请输入有效的电子邮件地址'], ['@qq.com','Mushishi01','111111','code','user_email_error','请输入有效的电子邮件地址'], ['12@qq.com','Mushishi01','111111','code','user_email_error','请输入有效的电子邮件地址'] ) @ddt.unpack ''' @ddt.data(*data) #data里存放着Excel里的数据 def test_register_case(self,data): #通过list赋值方式,将Excel里数据赋值到各个参数里 email,username,password,self.file_name,assertCode,assertText = data email_error = self.login.register_function(email,username,password,self.file_name,assertCode,assertText) self.assertFalse(email_error,"测试失败") if __name__ == '__main__': file_path = os.path.join(os.getcwd()+"/report/"+"first_case1.html") f = open(file_path,'wb') suite = unittest.TestLoader().loadTestsFromTestCase(FirstDdtCase) runner = HTMLTestRunner.HTMLTestRunner(stream=f,title="This is first report1",description=u"这个是我们第一次测试报告1",verbosity=2) runner.run(suite)
这篇关于Selenium3与Python3实战Web自动化测试框架【日记1】的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-24Python编程基础详解
- 2024-11-21Python编程基础教程
- 2024-11-20Python编程基础与实践
- 2024-11-20Python编程基础与高级应用
- 2024-11-19Python 基础编程教程
- 2024-11-19Python基础入门教程
- 2024-11-17在FastAPI项目中添加一个生产级别的数据库——本地环境搭建指南
- 2024-11-16`PyMuPDF4LLM`:提取PDF数据的神器
- 2024-11-16四种数据科学Web界面框架快速对比:Rio、Reflex、Streamlit和Plotly Dash
- 2024-11-14获取参数学习:Python编程入门教程