这几天总结了的自动化测试基础代码,每行代码含注释。
上篇文章写了一个简单的入门示例,接着上篇文章总结。
下面这些代码全在一个py文件中
导入模块
import time, sys
sys.path.append('代码的路径')
from selenium import webdriver
# 导入的封装模块read_ini
from read_ini import read_ini
# 用于判断标签是否正确
from selenium.webdriver.support import expected_conditions as EC
封装的浏览器驱动,你用什么浏览器测试,传入什么名称
def open_browser(browser):
if browser == 'chrome':
driver = webdriver.Chrome()
elif browser == "firefox":
driver = webdriver.Firefox()
elif browser == 'ie':
driver = webdriver.Ie()
else:
driver = webdriver.Edge()
time.sleep(2)
return driver
先列数据都是这个对象中的参数
初始化浏览器参数
class SeleniumDriver:
def __init__(self, browser):
self.driver = open_browser(browser)
判断传入的url,是否是http协议
def get_url(self, url):
if self.driver != None:
self.driver.maximize_window()
if 'http://' in url:
self.driver.get(url)
else:
print("你的网站格式不对")
else:
print("case失败")
判断打开网址后,页面大小的调整
def handle_windows(self, *args):
value = len(args)
# 判断传入的参数是否有长度
if value == 1:
# 判断第一个参数
if args[0] == 'max':
self.driver.maximize_window()
elif args[0] == 'min':
# 最小化页面
self.driver.minimize_window()
elif args[0] == 'back':
# 网页向后
self.driver.back()
elif args[0] == 'go':
# 网页向前
self.driver.forward()
else:
# 网页刷新
self.driver.refresh()
elif value == 2:
# 如果值为2 获取数值网页放大到指定大小
self.driver.set_window_size(args[0], args[1])
else:
print('你传递的参数有问题')
time.sleep(5)
# 五秒后关闭
self.driver.quit()
判断标签title是否正确
def assert_title(self, title_name=None):
"""
判断title是否正确
"""
if title_name != None:
get_title = EC.title_contains(title_name)
get_title(self.driver)
return get_title(self.driver)
通过title判断页面是否正确
def open_url_is_true(self, url, title_name=None):
"""
通过title判断页面是否正确
"""
self.get_url(url)
return self.assert_title(title_name)
关闭网页
def close_driver(self):
self.driver.close()
切换窗口
def switch_windows(self, title_name=None):
"""
切换windows
"""
# 获取当前浏览器的所有窗口句柄
handl_list = self.driver.window_handles
# 当前窗口
current_handle = self.driver.current_window_handle
for i in handl_list:
if i != current_handle:
time.sleep(1)
# 切换到最新打开的窗口
self.driver.switch_to.window(i)
# 判断标签是否正确
if self.assert_title(title_name):
break
判断元素是否被展示
def element_isdisplay(self, element):
flag = element.is_selected()
if flag:
return element
else:
return False
获取元素标签,判断属于哪一类,执行操作
def get_element(self, info):
"""
获取元素element
@param by 定位方式
@param value 定位置
@return element 返回一个元素
"""
# get_local_element 函数获取数据库中的参数,此文件用ini
by, value = self.get_local_element(info)
element = None
try:
if by == 'id':
element = self.driver.find_element_by_id(value)
elif by == 'name':
element = self.driver.find_element_by_name(value)
elif by == 'css':
element = self.driver.find_element_by_css_selector(value)
elif by == 'class':
element = self.driver.find_element_by_class_name(value)
else:
element = self.driver.find_element_by_xpath(value)
except:
print("定位方式:", by, "定位值:", value, "定位出现错误,没有定位置")
# 返回element是否被展示
return self.element_isdisplay(element)
获取元素组,判断参数
def get_elements(self, info):
"""
获取元素elements
@param by 定位方式
@param value 定位置
@return elements 返回一个元素
"""
element_list = []
by, value = self.get_local_element(info)
if by == 'id':
elements = self.driver.find_elements_by_id(value)
elif by == 'name':
elements = self.driver.find_elements_by_name(value)
elif by == 'css':
elements = self.driver.find_elements_by_css_selector(value)
elif by == 'class':
elements = self.driver.find_elements_by_class_name(value)
else:
elements = self.driver.find_elements_by_xpath(value)
for element in elements:
if not self.element_isdisplay(element):
continue
else:
element_list.append(element)
return elements
标签层级定位
def get_level_element(self, info_level, node_info):
"""
层级定位
有一个父节点
父节点找子节点
"""
element = self.get_element(info_level)
node_by, node_value = node_info
if not element:
return False
if node_by == 'id':
node_element = element.find_element_by_id(node_value)
elif node_by == 'name':
node_element = element.find_element_by_name(node_value)
elif node_by == 'css':
node_element = element.find_element_by_css_selector(node_value)
elif node_by == 'class':
node_element = element.find_element_by_css_selector(node_value)
else:
node_element = element.find_element_by_xpath(node_value)
# return node_element
return self.element_isdisplay(node_element)
通过list定位
def get_list_element(self,info, index):
"""
通过list定位我们的元素
"""
elements = self.get_elements(info)
if index > len(elements):
return None
return elements[index]
input框输入.
def send_value(self, info, key):
"""
输入值
"""
element = self.get_element(info)
if not element:
element.send_keys(key)
print("输入失败,定位没有展现出来")
else:
if element is not None:
element.send_keys(key)
else:
print("输入失败,定位元素没找到。")
点击元素函数封装
def click_element(self, info):
"""
点击元素
"""
element = self.get_element(info)
if not element:
if element != None:
element.click()
else:
print('点击失败')
else:
print("点击失败,元素不可见")
判读是否选中元素
def check_box_isselected(self, info, check=None):
"""
判读选中checkbox
"""
element = self.get_element(info)
if not element:
flag = element.is_selected()
if flag:
if check != 'check':
self.click_element(info)
else:
if check == 'check':
self.click_element(info)
else:
print("元素不可见,没办法选中")
获取数据库中的数据
def get_local_element(self, info)
# 获取文件数据
data = read_ini.get_value(info)
# 分隔符>
data_info = data.split('>')
# [name, a
return data_info
简单的传值调用封装
selenium_driver = SeleniumDriver('chrome')
selenium_driver.open_url_is_true('http://www.imooc.com/article', '慕课网')
selenium_driver.send_value('username', '账户')
selenium_driver.send_value('password', '密码')
selenium_driver.click_element('定位参数')
time.sleep(5)
selenium_driver.close_driver()
调用模块read_ini内容
import configparser
class ReadIni:
def __init__(self):
self.data = self.load_ini()
def load_ini(self):
cf = configparser.ConfigParser()
cf.read('D:\Python自动化测试\python+selenium测试基础\测试代码\LocalElement.ini')
return cf
def get_value(self, key):
self.data.get('element', key)
# 返回值
read_ini = ReadIni()
ini文件数据