1、使用selenium中的webdriver模块对浏览器进行操作
1)b = webdriver.Firefox() #打开浏览器
2)b.get('http://www.baidu.com') #打开一个网页
判断url,title找到元素方式
3)b.title,current_url 判断访问是否有效
4) ele = find_element_by_id/name() #定位元素
5)ele.clear() ele.send_keys() 清空和输入
6)b.back() #back
2、webdriver模块对浏览器进行操作:元素的定位
7)ele = find_element_by_link_text(‘企业直通班’)
8)ele = find_element_by_partial_link_text(‘直通班’)
9)ele = find_element_by_css_selector('input[id=\'search\']')
ele = find_element_by_css_selector(‘img[alt="麦子学院三周年"]’)
10)xpath定位文件:/ // . .. @id count local-name()定位元素
ele = find_element_by_xpath('//input')
ele = find_element_by_xpath('//input[0]')
ele = find_element_by_xpath('//form//input[0]')
ele = find_element_by_xpath('//form//input/..')
find_element_by_xpath('//input[@id]')
find_element_by_xpath('//input[@name="firstname"]')
find_element_by_xpath('//*[count(input)=2]') 统计xxx元素=2的节点
find_element_by_xpath('//*[local-name()='xxx']') 找到tag为xxx的元素
find_element_by_xpath('//*[starts-with(local-name(),'x')]') 找到所有tag以x开头的元素
find_element_by_xpath('//*[contains(local-name(),'x')]') 找到所有tag包含x的元素
find_element_by_xpath('//*[string-length(local-name())=3]') 找到所有tag长度为3的元素
find_element_by_xpath('//xxx | //yyy') 多个路径查找
3、ActionChains类与输入事件
1)from selenium.webdriver.common.action_chains import ActionChains
2)ActionChains(driver):用于生成模拟用户行为
3)perform():执行存储行为
4)鼠标事件:
context_click 右击事件
double_click 双击事件
drag_and_drop 拖动
move_to_element() 鼠标停在一个元素上
click_and_hold 按下鼠标左键在一个元素上
例子:
from selenium.webdriver.common.action_chains import ActionChains
ele = b.find_element_by_link_text('企业直通班')
ActionChains(b).move_to_element(ele).perform()
sub_ele = b.find_element_by_link_text('软件测试')
5)键盘事件:send_keys()
from selenium.webdriver.common.keys import Keys
send_keys(Keys.BACKSPACE) 退格键
send_keys(Keys.CONTRL,'a') 全选
send_keys(Keys.CONTRL,'v') 粘贴
send_keys(Keys.CONTRL,'c') 复制
send_keys(Keys.CONTRL,'x') 剪切
send_keys(Keys.ENTER) 回车
4、多窗口切换
从百度搜索麦子学院并转到麦子学院首页
current_window_handle 显示当前句柄 d.current_window_handle
window_handles 列出所有的句柄 d.window_handles
switch_to_window 切换句柄 d.switch_to_window(d.window_handles[2])
d.current_url 列出当前页面url
5、selenium模块中的等待方法:
1)implicitly_wait() 设置webdriver等待时间,超时
2)WebDriverWait 等待条件满足或者超时后退出 from selenium.webdriver.support.ui import WebDriverWait
WebDriverWait:poll_frequency->check->until->method return not NOT False
(until_not)->method return False
6、alter对话框处理
alter对象
switch_to_alert() 切到alter,返回一个alter对象
accept 确认
dismiss 取消
send_keys 有输入框才能使用,否则报错
alter = d.switch_to_alert()
7、测试用例设计
优点:独立,可单独执行
缺点: 1、灵活性差 2、不具备大规模测试条件,维护成本大
脚本功能分析与模块化:
OpenBrowser
OpenUrl http://www.baidu.com
FindElement 元素ID、class、xpath或其他
SendKeys 发送值
CheckResult
测试脚本模块化和数据隔离
8、使用Excel读取用户数据
python处理Excel的模块
xlrd:读取Excel文件数据
XlsxWrite:写Excel文件
安装xlrd与XlsxWriter模块:
1、安装xlrd:pip install xlrd;
2、安装XlsxWrite:pip install XlsxWriter;
xlrd模块读取Excel内容:
1、xl = xlrd.open_workbook('test.xls') 打开Excel
2、table = xl.sheets()[0] 通过索引获取工作表
3、row = table.row_values(0) 获取第一行内容
4、col = table.col_values(0) 获取第一列整列内容
5、table.nrows 行数 table.ncols 列数
6、table.cell(0,0).value 单元格值
9、XlsxWriter生成测试报告
1、写入,合并单元格,设置单元格格式
2、支持公式,超链接
3、支持插入图片
4、支持生成chart
XlsxWrite用例:
1、创建Excel:xl = xlsxwrite.Workbook('test.xls')
2、添加sheet:table = xl.add_worksheet('hello')
3、写单元格:table.write_string(0,0,'first')/('A1','first')
4、设置单元格大小:table.set_column('C:E',15)
5、Excel关闭:xl.close()
xlsxwriter模块写入格式:
write_boolean 写boolean值
wirte_datatime 写日期
write_number 写数字
write_string 写字符串
write_blank 空
wirte_url 写链接
write_formula 写公式
write_image 插入图片
write_range 合并单元格写入
xlsxwriter单元格格式:add_format
color:red 颜色
num_format:yy-mm-dd 日期格式
url:www.baidu.com 超链接
bold:True 加粗
font_size:12 字体设置
underline:True 下划线设置
bg_color:red 单元格颜色