自动化selenium开发
一、开发环境搭建
1、Firefox浏览器
1.1 下载firefix并安装。
1.2 Firefox中打开"开始菜单“ -> ”开发者“ -> ”获取更多工具“ -> 搜索并安装firebug.
安装完成之后,Firefox中鼠标右键选择”使用firebug查看元素“
1.3 Python中安装selenium库
cmd中输入pip install selenium
1.4 Python中调用selenium
from selenium import webdriver browser = webdriver.Firefox()
broser.quit()
2、chrome浏览器
2.1 下载chrome并安装。
2.2 下载chromedirver.exe
2.3 拷贝chromedriver.exe至chrome安装路径(安装路径可以右键桌面chrome,查看具体安装路径)
2.4 将如上chrome安装路径添加环境变量
二、webdriver的API与定位元素
1、打开网页
from selenium import webdriver browser = webdriver.Firefox()
browser.get('http://www.baidu.com')
2、判断网页是否打开正确方法:
- brower.title
- browser.current_url
3、网页后退:
- browser.back()
4、网页最大化
- browser.maximize_window()
2、元素的定位

- xpath定位元素

通过元素和属性进行定位,是W3C的一个标准,已如下html页面为例,说明xpath的使用

绝对路径方法:
定位到firstname对话框,语法如下
ele1 = browser.find_element_by_xpath('/html/body/form/input[1]')
定位到lastname对话框
ele2 = browser.find_element_by_xpath('/html/body/form/input[2]')
可通过get_attribute获取元素属性
ele1.get_attribute('name')
相对路径(扫描整个文档)方法:
ele1 = browser.find_element_by_xpath('//input[1]')
ele1.get_attribute('name')
打印结果:
u'firstname'
ele2 = browser.find_element_by_xpath('//input[2]')
ele2.get_attribute('name')
打印结果:
u'lastname‘
注:运行如下语句报错
ele3 = browser.find_element_by_xpath('//input[3]')
获取父节点:
ele = browser.find_element_by_xpath('//form//input')
ele.get_attribute('name')
ele2 = browser.find_element_by_xpath('//form//..')
ele2.tag_name
运行结果:
u'fistname'
u'body'
所有元素中具有xx属性:
ele = browser.find_element_by_xpath('//input[@name="lastname"]')
ele.get_attribute('name')
运行结果:
u'lastname'

- css selector
麦子学院的搜索框

使用firebug查看

复制其xpath,可按如下方式查找
ele = browser.find_element_by_css_selector('html body div.microoh-main header.navbar.navbar-default.navbar-fixed-top.affix-top div.container div#microoh-navbar-collapse.collapse.navbar-collapse form.navbar-form.navbar-left div.form-group input#search.form-control')
还可以使用input进行查找
ele = browser.find_element_by_css_selector('input[id="search"]')

三、鼠标和键盘事件

使用前需要导入的类以及方法:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains browser = webdriver.Firefox()
browser.get('http://maiziedu.com/')
#定位到“企业直通班”
ele = b.find_element_by_xpath(r'/html/body/div[5]/header/div/div[2]/ul[1]/li[1]/a')
#鼠标移动至“企业直通班”
ActionChains(browser).move_to_element(ele).perform()
#定位子元素“软件测试"
ele_sub = b.find_element_by_xpath(r'/html/body/div[5]/header/div/div[2]/ul[1]/li[1]/div/a[17]')
ele_sub.click()
之所以使用xpath进行定位,因为个人python版本为2.7.9,python3.0版本,可以直接使用find_element_by_link_text('企业直通班’) and find_element_by_link_text('软件测试')

from selenium import webdriver
from selenium.webdriver.common.keys import Keys b = webdriver.Firefox()
b.get('http://maiziedu.com/') ele = b.find_element_by_id('search')
ele.send_keys('python')
ele.clear()
ele.send_keys('python1')
ele.send_keys(Keys.BACKSPACE)
ele.send_keys(Keys.CONTROL,'a')
ele.send_keys(Keys.CONTROL,'x')
ele.send_keys(Keys.CONTROL,'v')
四、对话框处理与登录测试
from selenium import webdriver
import time xpath = r'/html/body/div[5]/header/div/div[2]/div[1]/div/a[1]'
account = r'gjp_1988@126.com'
pwd = r'*******' def account_login():
b = webdriver.Firefox()
b.get('http://maiziedu.com/')
time.sleep(3)
b.maximize_window() ele = b.find_element_by_xpath(xpath)
ele.click()
time.sleep(1) ac_ele = b.find_element_by_id('id_account_l')
ac_ele.clear()
ac_ele.send_keys(account)
pwd_ele = b.find_element_by_id('id_password_l')
pwd_ele.clear()
pwd_ele.send_keys(pwd)
b.find_element_by_id('login_btn').click()
time.sleep(4) try:
b.find_element_by_id('login-form-tips')
print("account login error!")
except:
print("account login pass!") time.sleep(4)
b.quit() if __name__ == '__main__':
account_login()
五、多窗口切换
from selenium import webdriver
tmp = webdriver.Firefox()
tmp.get('http://www.baidu.com') #input python and click search
ele_kw = tmp.find_element_by_id('kw')
ele_kw.send_keys('python')
ele_su = tmp.find_element_by_id('su')
ele_su.click() #click one of search text
tmp.find_element_by_link_text('Welcome to Python.org').click() #list all window_handle
tmp.window_handles #display current window_handle
tmp.current_window_handle #switch window_handle
tmp.switch_to_window(tmp.window_handle[0])
tmp.swithc_to_window(tmp.window_handle[1])
需要识记如下方法:
current_window_handle:当前句柄
window_handles:列出所有句柄
switch_to_window():切换句柄
六、测试脚本中等待方法
等待的目的:使脚本更稳定的运行

说明:第一个函数设置所有的等待时间
第二个设置一个等待时间
help(WebDriverWait) class WebDriverWait(__builtin__.object)
| Methods defined here:
|
| __init__(self, driver, timeout, poll_frequency=0.5, ignored_exceptions=None)
| Constructor, takes a WebDriver instance and timeout in seconds.
|
| :Args:
| - driver - Instance of WebDriver (Ie, Firefox, Chrome or Remote)
| - timeout - Number of seconds before timing out
| - poll_frequency - sleep interval between calls
| By default, it is 0.5 second.
| - ignored_exceptions - iterable structure of exception classes ignored during calls.
| By default, it contains NoSuchElementException only. 还需要调用两个方法:
- until(self, method, message='') | Calls the method provided with the driver as an argument until the return value is not False. 说明:调用方法,直到这个方法返回真;否则timeout后报错
- until_not(self, method, message='') | Calls the method provided with the driver as an argument until the return value is False.
说明:调用方法,直到这个方法返回假;否则timeout后报错
测试代码如下:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait b = webdriver.Firefox()
b.get('http://maiziedu.com/')
b.maximize_window() ele = WebDriverWait(b,10).until(lambda b:b.find_element_by_xpath(xpath))
# ele = b.find_element_by_xpath(xpath)
ele.click()
七、alter对话框处理

八、测试用例设计



九、测试用例模块化

改写代码如下:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import time URL = 'http://maiziedu.com'
xpath = r'/html/body/div[5]/header/div/div[2]/div[1]/div/a[1]'
class_name = r'visible-md visible-lg'
account = 'gjp_1988@126.com'
pwd = '********' def openBrowser():
browser_handle = webdriver.Firefox()
return browser_handle def loadUrl(browser,URL):
browser.get(URL)
browser.maximize_window()
time.sleep(3) def findElement(browser,**args):
if 'login_name' in args:
ele_login = WebDriverWait(browser,10).until(lambda browser:browser.find_element_by_xpath((args['login_name'])))
ele_login.click()
ele_account = browser.find_element_by_id(args['account_name'])
ele_pwd = browser.find_element_by_id(args['pwd_name'])
ele_login_btn = browser.find_element_by_id(args['login_id'])
return ele_account,ele_pwd,ele_login_btn def sendVal(ele_tuple,args):
listkey = ['username','password']
i = 0
for key in listkey:
ele_tuple[i].send_keys('')
ele_tuple[i].clear()
ele_tuple[i].send_keys(args[key])
i += 1
ele_tuple[2].click() def account_login():
b = openBrowser()
loadUrl(b,URL) ele_dict = {'login_name':xpath,'account_name':'id_account_l','pwd_name':'id_password_l','login_id':'login_btn'}
ele_tuple = findElement(b,**ele_dict) user_dict = {'username':account,'password':pwd}
sendVal(ele_tuple,user_dict) if __name__ == '__main__':
account_login()
十、测试从文件导入
将如上代码中webinfo和userinfo分离
webinfo.txt如下
url=http://www.maiziedu.com/
text_xpath=r'/html/body/div[5]/header/div/div[2]/div[1]/div/a[1]'
userid=id_account_l
pwdid=id_password_l
loginid=login_btn
使用userdata.py处理webinfo.txt,代码如下:
def get_webinfo(path):
web_info = {}
config = open(path)
for line in config:
result = [ele.strip() for ele in line.split('=')]
web_info.update(dict([result])) return web_info if __name__ == '__main__':
info = get_webinfo('webinfo.txt')
print(info)
# for key in info:
# print(key,info[key])
对其中strip函数的说明:
s为字符串,rm为要删除的字符序列
s.strip(rm) 删除s字符串中开头、结尾处,位于 rm删除序列的字符
s.lstrip(rm) 删除s字符串中开头处,位于 rm删除序列的字符
s.rstrip(rm) 删除s字符串中结尾处,位于 rm删除序列的字符
eg:
a = ' 123'
a.strip() a = '\t\tabcd'
a.strip()
打印结果:
‘123’
‘abcd'
userinfo.txt如下:
username=gjp_1988@126.com password=****
在如上userdata.py中,添加对userinfo.txt处理函数:
def get_webinfo(path):
web_info = {}
config = open(path)
for line in config:
result = [ele.strip() for ele in line.split('=')]
web_info.update(dict([result])) return web_info def get_userinfo(path):
user_info = []
config = open(path)
for line in config:
user_dict = {}
result = [ele.strip() for ele in line.split(' ')]
for r in result:
account = [ele.strip() for ele in r.split('=')]
user_dict.update(dict([account]))
# print user_dict
user_info.append(user_dict)
# print user_info return user_info
if __name__ == '__main__':
# info = get_webinfo('webinfo.txt')
# print(info)
# for key in info:
# print(key,info[key])
info = get_userinfo('userinfo.txt')
# for key in info:
# print(key,info[key])
print type(info)
print info
主程序作如下修改:
#encoding=utf-8
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import time
from userdata import get_webinfo,get_userinfo def openBrowser():
browser_handle = webdriver.Firefox()
return browser_handle def loadUrl(browser,URL):
browser.get(URL)
browser.maximize_window()
time.sleep(3) def findElement(browser,args):
if 'text_xpath' in args:
ele_login = WebDriverWait(browser,10).until(lambda browser:browser.find_element_by_xpath(args['text_xpath']))
ele_login.click()
ele_account = browser.find_element_by_id(args['userid'])
ele_pwd = browser.find_element_by_id(args['pwdid'])
ele_login_btn = browser.find_element_by_id(args['loginid'])
return ele_account,ele_pwd,ele_login_btn def sendVal(ele_tuple,args):
listkey = ['username','password']
i = 0
for key in listkey:
ele_tuple[i].send_keys('')
ele_tuple[i].clear()
ele_tuple[i].send_keys(args[key])
i += 1
ele_tuple[2].click() def check_result(handle):
time.sleep(3)
try:
ele = handle.find_element_by_id('login-form-tips')
print(id(ele))
print('login error!')
print(ele.text)
except:
print('login passs!') if __name__ == '__main__':
b = openBrowser()
webinfo_dict = get_webinfo('webinfo.txt')
print webinfo_dict
loadUrl(b,webinfo_dict['url']) # ele_dict = {'login_name':xpath,'account_name':'id_account_l','pwd_name':'id_password_l','login_id':'login_btn'}
ele_tuple = findElement(b,webinfo_dict) userinfo_dict = get_userinfo('userinfo.txt')
print userinfo_dict
# user_dict = {'username':account,'password':pwd}
for each in userinfo_dict:
sendVal(ele_tuple,each) check_result(b)
十一、增加测试报告接口
module.py代码如下:
#coding=utf-8
import time
import os class Loginfo(object):
def __init__(self,path = '',mode = 'w'):
fname = time.strftime('%Y-%m-%d',time.gmtime())
path_current = os.path.join(path,(fname + '.txt'))
self.log = open(path_current,mode) def write_log(self,msg):
self.log.write(msg) def close_log(self):
self.log.close() if __name__ == '__main__':
log = Loginfo(r'D:\360Downloads')
log.write_log('www.baidu.com')
log.close_log()
主程序作如下修改(其余部分同上):
def check_result(handle,log,args):
time.sleep(3)
try:
ele = handle.find_element_by_id('login-form-tips')
print('login error!')
msg = '%s %s:error %s' %(args['username'],args['password'],ele.text())
log.write_log(msg)
except:
print('login passs!')
msg = '%s %s:pass' %(args['username'],args['password'])
log.write_log(msg) if __name__ == '__main__':
b = openBrowser()
log = Loginfo()
webinfo_dict = get_webinfo('webinfo.txt')
# print webinfo_dict
loadUrl(b,webinfo_dict['url']) # ele_dict = {'login_name':xpath,'account_name':'id_account_l','pwd_name':'id_password_l','login_id':'login_btn'}
ele_tuple = findElement(b,webinfo_dict) userinfo_dict = get_userinfo('userinfo.txt')
# print userinfo_dict
# user_dict = {'username':account,'password':pwd}
for each in userinfo_dict:
sendVal(ele_tuple,each)
check_result(b,log,each)
log.close_log() time.sleep(3)
b.quit()
自动化selenium开发的更多相关文章
- Selenium2学习-001-Selenium2 WebUI自动化Java开发 Windows 环境配置
此文主要介绍 Selenium2 WebUI自动化Java开发 Windows 环境配置,供各位亲们参考,若有不足之处,敬请各位大神指正,非常感谢! 所需软件列表如下所示: 所属分类 具体名称 备注 ...
- Selenium Web 自动化 - Selenium(Java)环境搭建
Selenium Web 自动化 - Selenium(Java)环境搭建 2016-07-29 1 下载JDK JDK下载地址:http://www.oracle.com/technetwork/j ...
- Selenium Web 自动化 - Selenium常用API
Selenium Web 自动化 - Selenium常用API 2016-08-01 目录 1 对浏览器操作 1.1 用webdriver打开一个浏览器 1.2 最大化浏览器&关闭浏览器 ...
- Selenium自动化脚本开发总结
Selenium Selenium 是ThoughtWorks专门为Web应用程序编写的一个验收测试工具. Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE.Mo ...
- Selenium+C#自动化脚本开发学习
1:Selenium中对浏览器的操作 首先生成一个Web对象 IWebDriver driver = new FirefoxDriver(); //打开指定的URL地址 driver.Navigate ...
- 「UI 测试自动化selenium」汇总
<selenium 基础之java实现> selenium RC 环境配置 菜鸟学自动化测试(一)----selenium IDE 菜鸟学自动化测试(二)----selenium IDE ...
- Web自动化selenium技术快速实现爬虫
selenium是大家众所周知的web自动化测试框架,主要用来完成web网站项目的自动化测试,但其实如果要实现一个web爬虫,去某些网站爬取数据,其实用selenium来实现也很方便. 比如,我们现在 ...
- 2020年第二期《python接口自动化+测试开发》课程,已开学!
2020年第二期<python接口自动化+python测试开发>课程,12月15号开学! 主讲老师:上海-悠悠 上课方式:QQ群视频在线教学,方便交流 本期上课时间:12月15号-3月29 ...
- web自动化-selenium 入门篇
selenium安装介绍 selenium是web浏览器的自动化工具 官网:https://www.selenium.dev 构成: WebDriver: 浏览器提供的浏览器api来控制浏览器(模拟用 ...
随机推荐
- JavaScript事件循环(Event Loop)机制
JavaScript 是单线程单并发语言 什么是单线程 主程序只有一个线程,即同一时间片断内其只能执行单个任务. 为什么选择单线程? JavaScript的主要用途是与用户互动,以及操作DOM.这决定 ...
- poj 2455 二分+最大流
这个因为点少用邻接矩阵做的. 题意:求由1到n的t条不重复路径中最大边权值的最小值. 思路:先对边权进行排序,然后二分边权值,建图求从1到n的最大流,当最大流为t时便求出答案. 代码: #includ ...
- 所谓编码--泛谈ASCII、Unicode、UTF8、UTF16、UCS-2等编码格式
最近在看nodejs的源码,看到stream的实现里面满地都是encoding,不由想起以前看过的一篇文章--在前面的随笔里面有提到过--阮一峰老师的<字符编码笔记:ASCII,Unicode和 ...
- dreamweaver代码提示失效
原文地址:dreamweaver代码提示失效作者:云中雁 2007-03-23 12:19:22| 分类: 编程手记 | 标签:web2.0 javascript |字号大中小 订阅 吴庆民 ...
- MongoDB学习之路(五)
MongoDB $type 操作符 类型 数字 备注 Double 1 String 2 Object 3 Array 4 Binary data 5 Undefined 6 已废弃 Object i ...
- Alpha个人总结
一.我的问题: 1.第一章1.2.1 在软件的特殊性中说到,"大型软件有超过数百万行的源代码,上万个不同的文件,而软件工程师通常一次只能看到30-80行源代码,他们的智力.记忆力和常人差不多 ...
- 201521123107 《Java程序设计》第6周学习总结
第6周作业-接口.内部类与Swing 1.本周学习总结 2.书面作业 1.clone方法 1.1 Object对象中的clone方法是被protected修饰,在自定义的类中覆盖clone方法时需要注 ...
- 个人作业3——个人总结(Alpha阶段)。
一:个人总结: 陆续几周以及加上上上一周的Alpha冲刺阶段,完成了实验室故障报修系统的基础框架以及内容.这个过程苦中有乐,或许苦中寻乐更加恰当,以一个小组团队的形式来完成这个项目,我们大家就变成了一 ...
- 201521123108 《Java程序设计》第4周学习总结
1. 本章学习总结 2. 书面作业 Q1. 注释的应用 使用类的注释与方法的注释为前面编写的类与方法进行注释,并在Eclipse中查看.(截图) 答: Q2. 面向对象设计(大作业1-非常重要) 2. ...
- 201521123093 java 第十三周学习总结
1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 2. 书面作业 1. 网络基础 1.1 比较ping www.baidu.com与ping cec.jmu ...