python之自动化测试模型知识总结

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_44846959/article/details/100895200

自动化测试模型
#自动化测试模型介绍
#1.线性测试
#2.模块驱动化测试
#3.数据驱动测试
#4.关键字驱动测试

#2.模块化驱动测试实例
from selenium import webdriver
driver=webdriver.Firefox()
driver.implicitly_wait(10)
driver.get(“http://www.126.com”)
#登录
driver.find_element_by_id(‘idInput’).clear()
driver.find_element_by_id(‘idInput’).send_keys(‘username’)
driver.find_element_by_id(‘pwdInout’).clear()
driver.find_element_by_id(‘pwdInput’).send_keys(‘password’)
driver.find_element_by_id(‘loginBtn’).click()
#退出
driver.find_element_by_link_text(“退出”).click()
driver.quit()
driver.logout()#调用退出模块
#登录
def user_login(self,driver):
driver.find_element_by_id(“idInput”).clear()
driver.find_element_by_id(“idInput”).send_keys(“username”)
driver.find_element_by_id(“pwdInput”).clear()
driver.find_element_by_id(“pwdInput”).send_keys(“123456”)
driver.find_element_by_id(“loginBtn”).click()
#退出
def user_logout(self,driver):
driver.find_element_by_link_text(“退出”).click()
driver.quit()
#数据驱动测试实例
#5.3.1参数化邮箱登录
#修改接口需要驱动,用户名,密码等参数
def user_login(self,driver,username,password):
driver.find_element_by_id(“idInput”).clear()
driver.find_element_by_id(“idInput”).send_keys(username)
driver.find_element_by_id(“pwdInput”).clear()
driver.find_element_by_id(“pwdInput”).send_keys(password)
driver.find_element_by_id(“loginBtn”).click()
#修改user_login()方法的入参,为其增加username,password的入参
#将得到具体的参数作为登录时的数据。
#5.3.2参数化搜索关键字
from seelnium import webdriver
search_text=[‘python’,‘中文’,‘text’]
for text in search_text:
driver=webdriver.Firefox()
driver.implicitly_wait(10)
driver.get(“http://www.baidu.com”)
driver.find_element_by_id(“kw”).send_keys(text)
driver.find_element_by_id(“su”).click()
driver.quit()

#5.3.3读取txt文件
read()#读取整个文件
readline()#读取一行数据
readlines()#读取所有行的数据
#现在使用txt文件来存放用户名和密码数据,并通过读取该文件中的数据作为
#用例的数据
#首先将用户名和密码按行写入txt文件中,这里把用户名和密码用,隔开
user_file=open(‘user_info.txt’,‘r’)
lines=user_file.readlines()
user_file.close()

for line in lines:
username=line.split(",")[0]
password=line.split(",")[1]
print(username,password)
#首先通过open()方法读r的形式打开user_info.txt文件
#使用readlines()方法按行读取txt文件
#读取csv文件
import csv #导入csv包
#读取本地csv文件
date=csv.reader(open(“info.csv”,‘r’))
#循环输出每一行的信息
for user in date:
print(user)
#首先导入cvs模块,通过reader()方法读取CSV文件,通过for循环遍历文件
#中的每一行数据
import csv
date=csv.reaser(open(“info.csv”,‘r’))
#取用户的邮箱地址
for user in date:
print(user[1])
#读取xml文件
#1.获得标签信息
from xml.dom import minidom
#打开xml文档
dom=mindom.parse(“info.xml”)
#得到文档元素
root=dom.documentElement
print(root.nodeName)
print(root.nodeValue)
print(root.nodeType)
print(root.ELEMENT_NODE)
#首先导入xml的minidom模块,用来处理xml文件,parse()用于打开一个xml文件
#documentElement用于得到xml文件的唯一根元素
#每一个节点都有它的nodeName,nodeValue,nideType等属性
#node为节点名称;nodeValue为节点的值,只对文本节点有效
#2.获得任意标签名
from xml.dom import minidom
#打开xml文档
dom=minidom.parse(‘info.xml’)
#得到文档元素对象
root=dom.documentElement
tagname=root.getElementsByTagName(‘login’)
print(tagname[1].tagName)
tagname=root.getElementsByTagName(‘province’)
print(tagname[2].tagName)
#getElementByName()可以通过标签名获取标签,他所获得
#的对象是以数组形式存放
getElementByTagName()#可以通过标签名获取标签,他所获得的对象是以数组的形式存放
getElementByTagName(‘province’)#获得的是标签名为province的一组标签
#3获得标签的属性值
from xml.dom import minidom
#打开xml文档
dom=minidom.parse(‘info.xml’)
#得到文档元素对象
root=dom.documentElement
logins=root.getElementsByTagName(‘login’)
#获得login标签的username属性值
username=logins[0].getAttribute(‘username’)
print(username)
#获得login标签的password属性值
password =logins[0].getAttribute(‘password’)
print(password)
#获得第二个login标签的username属性值
username=login[1].getAttribute(“password”)
print(password)
#getAttribute()方法用于获取元素的属性值
#4.获得标签对之间的数据
from xml.dom import minidom
#打开xml文档
root=dom.documentElement
provinces=dom.getElementsByTagName(‘province’)
citys=dom.getElementsByTagName(‘city’)
#获得第二个province标签对的值
p2=provinces[1].firstChild.date
print(p2)
#获得第一个city标签对的值
c1=citys[0].firstChild.date
print(c1)
#获得第二个city标签对的值
c2=city[1].firstChild.date
print(c2)

猜你喜欢

转载自blog.csdn.net/weixin_44846959/article/details/100895200
今日推荐