一、开发环境搭建

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开发的更多相关文章

  1. Selenium2学习-001-Selenium2 WebUI自动化Java开发 Windows 环境配置

    此文主要介绍 Selenium2 WebUI自动化Java开发 Windows 环境配置,供各位亲们参考,若有不足之处,敬请各位大神指正,非常感谢! 所需软件列表如下所示: 所属分类 具体名称 备注 ...

  2. Selenium Web 自动化 - Selenium(Java)环境搭建

    Selenium Web 自动化 - Selenium(Java)环境搭建 2016-07-29 1 下载JDK JDK下载地址:http://www.oracle.com/technetwork/j ...

  3. Selenium Web 自动化 - Selenium常用API

    Selenium Web 自动化 - Selenium常用API 2016-08-01 目录 1 对浏览器操作  1.1 用webdriver打开一个浏览器  1.2 最大化浏览器&关闭浏览器 ...

  4. Selenium自动化脚本开发总结

    Selenium Selenium 是ThoughtWorks专门为Web应用程序编写的一个验收测试工具. Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE.Mo ...

  5. Selenium+C#自动化脚本开发学习

    1:Selenium中对浏览器的操作 首先生成一个Web对象 IWebDriver driver = new FirefoxDriver(); //打开指定的URL地址 driver.Navigate ...

  6. 「UI 测试自动化selenium」汇总

    <selenium 基础之java实现> selenium RC 环境配置 菜鸟学自动化测试(一)----selenium IDE 菜鸟学自动化测试(二)----selenium IDE ...

  7. Web自动化selenium技术快速实现爬虫

    selenium是大家众所周知的web自动化测试框架,主要用来完成web网站项目的自动化测试,但其实如果要实现一个web爬虫,去某些网站爬取数据,其实用selenium来实现也很方便. 比如,我们现在 ...

  8. 2020年第二期《python接口自动化+测试开发》课程,已开学!

    2020年第二期<python接口自动化+python测试开发>课程,12月15号开学! 主讲老师:上海-悠悠 上课方式:QQ群视频在线教学,方便交流 本期上课时间:12月15号-3月29 ...

  9. web自动化-selenium 入门篇

    selenium安装介绍 selenium是web浏览器的自动化工具 官网:https://www.selenium.dev 构成: WebDriver: 浏览器提供的浏览器api来控制浏览器(模拟用 ...

随机推荐

  1. 大型网站的 HTTPS 实践(一)—— HTTPS 协议和原理

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt387 1 前言 百度已经于近日上线了全站 HTTPS 的安全搜索,默认会将 ...

  2. jvm系列 (五) ---类加载机制

    类的加载机制 目录 jvm系列(一):jvm内存区域与溢出 jvm系列(二):垃圾收集器与内存分配策略 jvm系列(三):锁的优化 jvm系列 (四) ---强.软.弱.虚引用 我的博客目录 什么是类 ...

  3. ini文件必须要全路径名啊

    弄半天必须要全路径名啊.实在不行用./set.ini也可以 用UNICODE保存ini文件

  4. Swing-布局管理器之GridLayout(网格布局)-入门

    注:本文内容源自于三十一.Java图形化界面设计——布局管理器之GridLayout(网格布局),笔者在学习过程中根据自身理解修改了部分代码. 网格布局特点: l  使容器中的各组件呈M行×N列的网格 ...

  5. 201521123001《Java程序设计》第6周学习总结

    1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图,对面向对象思想进行一个总结. 注1:关键词与内容不求多,但概念之间的联系要清晰,内容覆盖 ...

  6. 201521123025<<java程序设计>>第4周学习总结

    Q1. 本周学习总结 Q2.书面作业 1.注释的应用 使用类的注释与方法的注释为前面编写的类与方法进行注释,并在Eclipse中查看.(截图) 2.面向对象设计(大作业1,非常重要) 2.1 将在网上 ...

  7. 201521123065《Java程序设计》第2周学习总结

    1.本周学习总结 字符串创建后是不可变的,可以使用加号进行字符串之间的拼接. 使用for循环对字符串进行修正会不断产生新的字符串,应使用StringBuilder. 字符串内容的比较要用equal. ...

  8. 201521123004 《Java程序设计》第1周学习总结

    1. 本章学习总结 (1)安装各种软件(jdk,eclipse,git(安装不了)) 注册账号(博客,网易邮箱(QQ邮箱不能用)码云) 创建项目(码云,Java) (2)了解JAVA语言的发展史(su ...

  9. 201521123080《Java程序设计》第12周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 流(Stream): Stream是从起源(source)到接收(sink)的有序数据 按照流向分可以分为输入 ...

  10. 201521123019 《java程序设计》 第13周学习总结

    1. 本章学习总结 2. 书面作业 Q1. 网络基础 1.1 比较ping www.baidu.com与ping cec.jmu.edu.cn,分析返回结果有何不同?为什么会有这样的不同? 访问cec ...