本文为Python自动化测试框架基础入门篇,主要帮助会写基本selenium测试代码又没有规划的同仁。

本文应用到POM模型、selenium、unittest框架、configparser配置文件、smtplib邮件发送、HTMLTestRunner测试报告模块结合登录案例实现简单自动化测试框架

项目主要包括以下几个部分

conif.ini 放置配置文件

例如:

myunit.py文件放置的浏览器操作代码

import unittest

from selenium import webdriver

class MyTest(unittest.TestCase):

def setUp(self):

self.driver = webdriver.Chrome()

self.driver.implicitly_wait(10)

self.driver.maximize_window()

def tearDown(self):

self.driver.quit()

if __name__=='__main__':

unittest.main()

base.py中放置浏览器对象操作代码

from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.support.wait import WebDriverWait

import os,configparser

class Page(object):

path = os.path.dirname(os.path.abspath("."))

cfpath = os.path.join(path, 'autoparker\config\conf.ini')

conf = configparser.ConfigParser()

conf.read(cfpath)

url=conf.get('base','url')

def __init__(self,driver,url=url):

self.driver=driver

self.url=url

def open(self):

self.driver.get(self.url)

def find_element(self,*loc):#传入参数为元组需要加*,本身就是元组的不需要*

#print(*loc)

try:

WebDriverWait(self.driver,10).until(EC.visibility_of_element_located(loc))

return self.driver.find_element(*loc)

except:

print('页面中未找到 %s 元素'%(self,loc))

def find_elements(self,*loc):

return self.driver.find_elements(*loc)

def send_keys(self,loc,value):

self.find_element(*loc).send_keys(value)

def click(self,loc):

self.find_element(*loc).click()

def clear(self,loc):

self.find_element(*loc).clear()

loginpage.py中放置通用登录模块代码(尽量避免重复代码)

from selenium.webdriver.common.by importByfrom time importsleepfrom objpage.base importPageclasslogin(Page):

username_loc=(By.NAME,'accounts')

password_loc=(By.NAME,'pwd')

login_button_loc=(By.XPATH,'/html/body/div[5]/div/form/fieldset/p/button')

login_error_loc=(By.XPATH,'//*[@id="common-prompt"]/p')deflogin_username(self,username):

self.find_element(*self.username_loc).clear()

self.find_element(*self.username_loc).send_keys(username)deflogin_password(self,password):

self.find_element(*self.password_loc).clear()

self.find_element(*self.password_loc).send_keys(password)deflogin_button(self):

self.find_element(*self.login_button_loc).click()#统一登录入口

defuser_login(self,username,password):

self.open()

self.login_username(username)

self.login_password(password)

self.login_button()

sleep(2)#登录提示信息

deflogin_error_text(self):return self.find_element(*self.login_error_loc).text

parker.py中放置公共元素操作代码(parker是我随便命名的,不纠结)

from selenium importwebdriverfrom selenium.webdriver.common.action_chains importActionChainsfrom selenium.webdriver.support.select importSelectclassParker(object):def __init__(self,browser='chrome'):if browser=='ie' or browser=='internet explorer':

driver=webdriver.Ie()elif browser=='firefox' or browser=='ff':

driver=webdriver.Firefox()elif browser=='chrome':

driver=webdriver.Chrome()try:

self.driver=driverexceptException:raise NameError('没有找到浏览器,请输入"ie","chrome","ff"')def wait(self,secs=5): #隐式等待

self.driver.implicitly_wait(secs)def to_element(self,key):#元素定位

if '->' not in key: #如果key里面不包含=就执行下列语句

raise NameError('参数类型输入错误')

by=key.split('->')[0] #通过分隔获取[0]对应的值

val=key.split('->')[1]#通过分隔获取[1]对应的值

if by=='id':

element=self.driver.find_element_by_id(val)elif by=='name':

element=self.driver.find_element_by_name(val)elif by=='class':

element=self.driver.find_element_by_class_name(val)elif by=='link_text':

element=self.driver.find_element_by_link_text(val)elif by=='xpath':

element=self.driver.find_element_by_xpath(val)elif by=='css':

element=self.driver.find_element_by_css_selector(val)else:raise NameError('请输入正确的定位方式:id,name,class,link_text,xpath,css')returnelementdef open(self,url):#打开一个URL

self.driver.get(url)def max_window(self):#最大化窗口(浏览器)

self.driver.maximize_window()def set_windows(self,wide,high):#设置窗口大小

self.driver.set_window_size(wide,high)def input(self,key,text):#对文本框进行输入

el=self.to_element(key)

el.send_keys(text)def click(self,key):#点击

el=self.to_element(key)

el.click()def clear(self,key):#清除文本框内容

el=self.to_element(key)

el.clear()def right_click(self,key):#右键操作

el=self.to_element(key)

ActionChains(self.driver).context_click(el).perform()def move_to_element(self,key):#鼠标悬停

el=self.to_element(key)

ActionChains(self.driver).move_to_element(el).perform()def drag_and_drop(self,el_key,ta_key):#拖拽 从一个元素拖到另外一个元素

el=self.to_element(el_key)

target=self.to_element(ta_key)

ActionChains(self.driver).drag_and_drop(el,target).perform()defclick_text(self,text):

self.driver.find_element_by_partial_link_text(text).click()def close(self):#关闭当前浏览器窗口

self.driver.close()def quit(self):#退出浏览器

self.driver.quit()def submit(self,key):#提交事件

el=self.to_element(key)

el.submit()def F5(self):#刷新

self.driver.refresh()def js(self,script):#执行js

self.driver.execute_script(script)def get_attribute(self,key,attribute):#获取元素属性

el=self.to_element(key)returnel.get_attribute(attribute)def get_text(self,key):#获取text

el=self.to_element(key)returnel.textdef get_title(self):#获取title

returnself.driver.titledef get_url(self):#获取url

returnself.driver.current_urldef to_frame(self,key):#窗口切换

el=self.to_element(key)

self.driver.switch_to.frame(el)def alert_accept(self):#对话框确认操作

self.driver.switch_to.alert.accept()def alert_dismiss(self):#对话框取消操作

self.driver.switch_to.alert.dismiss()def img(self,fp):#截图

self.driver.get_screenshot_as_file(fp)def select_by_value(self,key,value):#下拉框操作

el=self.to_element(key)

Select(el).select_by_value(value)

send_email.py放置邮件发送代码

importsmtplibfrom email.mime.text importMIMETextfrom email.mime.multipart importMIMEMultipartimportconfigparserimportosdefsendEmail(file_path):

path=os.path.dirname(os.path.abspath("."))

cfpath=os.path.join(path,'autoparker\config\conf.ini')

conf=configparser.ConfigParser()

conf.read(cfpath)

smtpserver= conf.get('emailqq','smtpserver')

sender= conf.get('emailqq','sender')

pwd= conf.get('emailqq','pwd')

receiver=[]

email_to=conf.get('emailqq','receiver')

email_array=email_to.split(';')for i inrange(len(email_array)):

receiver.append(email_array[i])print(receiver)

with open(file_path,'rb') as fp:

mail_boby=fp.read()

msg=MIMEMultipart()

msg['From']=sender

msg['To']=",".join(receiver)

msg['Subject']='我曾把完整的镜子打碎'body=MIMEText(mail_boby,'html','utf-8')

msg.attach(body)

att=MIMEText(mail_boby,'html','utf-8')

att['Content-Type']='application/octet-stream'att['Content-Disposition']='attachment;filename="test_reuslt.html"'msg.attach(att)try:

smtp=smtplib.SMTP()

smtp.connect(smtpserver)

smtp.login(sender,pwd)except:

smtp=smtplib.SMTP_SSL(smtpserver,465)

smtp.login(sender,pwd)

smtp.sendmail(sender,receiver,msg.as_string())

smtp.quit()

sendEmail('D:\\report.html')

最后main.py文件放置的就是运行代码(执行这个文件进行测试就可以)

importHTMLTestRunnerimportunittestfrom test_case.login importloginTestfrom public.send_email importsendEmailif __name__=='__main__':

testunit=unittest.TestLoader().loadTestsFromTestCase(loginTest)

suite=unittest.TestSuite(testunit)

file_path="D:\\html_report.html"fp=open(file_path,'wb')

runner=HTMLTestRunner.HTMLTestRunner(stream=fp,title='登录测试',description='测试执行结果')

runner.run(suite)

fp.close()

sendEmail(file_path)

今天就先到这里,一个入门级的自动化测试框架,由于是非专业开发自学,拿出来给大家分享,编程代码可能有点不规范,命名也很随意。大家先将就着看吧!回头慢慢完善。

还要多向大佬们学习。

备注:有想一起自学的朋友可以看配置文件代码,一起交流。三人行必有我师焉

python自动化测试登录_Python selenium自动化测试框架入门实战--登录测试案例相关推荐

  1. element 登录_Python selenium自动化测试框架入门实战--登录测试案例

    本文为Python自动化测试框架基础入门篇,主要帮助会写基本selenium测试代码又没有规划的同仁. 本文应用到POM模型.selenium.unittest框架.configparser配置文件. ...

  2. selenium python自动化测试教程_Python selenium自动化测试模型图解

    1.线性测试 优势:每一个脚本都是完整独立的,每一个脚本对应一个测试用例 缺点:开发成本高,会有重复操作重复脚本:维护成本也高,修改重复操作的脚本时,要逐一进行修改. 2.模块化驱动测试 把重复的操作 ...

  3. python写界面输入测试脚本_python+Selenium自动化测试——输入,点击操作

    这是我的第一个真正意思上的自动化脚本. 1.练习的测试用例为: 打开百度首页,搜索"胡歌",然后检索列表,有无"胡歌的新浪微博"这个链接 2.在写脚本之前,需要 ...

  4. python自动化验证部署_Python selenium自动化测试环境安装部署

    1. Windows系统下python环境搭建 1.1首先访问http://www.python.org/download/去下载最新的python版本.本人使用的是2.7的python版本 1.2安 ...

  5. python教程点击器_python+Selenium自动化测试——输入,点击操作

    这是我的第一个真正意思上的自动化脚本. 1.练习的测试用例为: 打开百度首页,搜索"胡歌",然后检索列表,有无"胡歌的新浪微博"这个链接 2.在写脚本之前,需要 ...

  6. python基础课件ppt_Python+selenium自动化测试入门【PPT课件】

    [导读]Selenium是一款基于web应用程序的开源测试工具.它支持Firefox.ie.Mozilla等众多浏览器.简单灵活.支持很多种语言.SeleniumCore:支持DHTML的测试案例(效 ...

  7. python selenium框架_python+selenium之框架设计

    一.自动化测试框架 1.什么是自动化测试框架 简单来说,自动化测试框架就是由一些标准,协议,规则组成,提供脚本运行的环境.自动化测试框架能够提供很多便利给用户高效完成一些事情,例如,结构清晰开发脚本, ...

  8. python通过cookie绕过验证码_Python Selenium Cookie 绕过验证码实现登录示例代码

    之前介绍过通过cookie 绕过验证码实现登录的方法.这里并不多余,会增加分析和另外一种方法实现登录. 1.思路介绍 1.1.直接看代码,内有详细注释说明 # FileName : Wm_Cookie ...

  9. python模拟qq空间登录_python selenium模拟登录163邮箱和QQ空间

    最近在看python网络爬虫,于是我想自己写一个邮箱和QQ空间的自动登录的小程序, 下面以登录163邮箱和QQ空间和为例: 了解到在Web应用中经常会遇到frame/iframe 表单嵌套页面的应用, ...

最新文章

  1. Mat 类常用函数用法示例
  2. 前沿 | DeepMind改进超参数优化:遗传算法效果超越贝叶斯
  3. python每隔2s执行一次hello world!
  4. Silverlight视频教程、资源下载。如果你觉得看图文不够形象,不够生动,那就看看视频吧。...
  5. 基于c语言单片机秒表课程设计,基于c语言单片机秒表课程设计要点.doc
  6. mysql 非空语法_mysql从入门到优化(1)基本操作上
  7. [html] HTML5如何使用音频和视频?
  8. docker代理设置ssl证书_docker - 设置HTTP/HTTPS 代理
  9. Oracle 12.2新特性 | 基于权重的节点驱逐
  10. Boxes in a Line UVA - 12657 (双向链表)
  11. 步长条件梯度下降算法步长和收敛条件的设置的一些看法
  12. telnet直接登录POP3
  13. 关于vb.net初学者,倒计时器的开发
  14. C++ Concurrency in Action, 2nd Edition 免积分下载
  15. A Neural Algorithm of Artistic Style : Neural Style Transfer with Eager Executon
  16. abs在c 语言中的作用,c语言中abs是什么意思
  17. html校验邮箱格式,正则验证邮箱格式
  18. 找到堡垒后的目标--逆向CDN的各种方式总结(干货,附解决方案
  19. 光学瞄准镜测距之数学原理
  20. ax200 兼容性问题 老路由器_我的华硕AX89X 160频宽和MU-MIMO问题,小米10测速-路由器交流...

热门文章

  1. dedecms 搬家流程
  2. fiilt1左耳连不上_「体验」FIIL T1 X ,让左耳和右耳开始约会
  3. python爬取英雄联盟所有皮肤价格表_用Python爬取英雄联盟(lol)全部皮肤
  4. java错误找不到或无法加载主类_java提示找不到或无法加载主类怎么办
  5. 后疫情时代,远程办公发展趋势如何?
  6. python计算总价_vue 实现购物车总价计算
  7. IList(T) 方法
  8. IP的含义、分类、子网划分、查。
  9. 双十一值得入手的运动耳机推荐,2022年好用排行榜运动耳机推荐
  10. 如何使用OpenVPN搭建局域安全网