自动化selenium开发

时间:2023-03-10 06:53:04
自动化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、元素的定位

自动化selenium开发

  • xpath定位元素

自动化selenium开发

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

自动化selenium开发

绝对路径方法:

定位到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'  

自动化selenium开发

  • css selector

麦子学院的搜索框

自动化selenium开发

使用firebug查看

自动化selenium开发

复制其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"]')

  

自动化selenium开发

三、鼠标和键盘事件

自动化selenium开发

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

自动化selenium开发

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('软件测试')

自动化selenium开发

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():切换句柄  

六、测试脚本中等待方法  

等待的目的:使脚本更稳定的运行

自动化selenium开发

说明:第一个函数设置所有的等待时间

第二个设置一个等待时间

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对话框处理  

 自动化selenium开发

八、测试用例设计

自动化selenium开发

 

自动化selenium开发

自动化selenium开发

九、测试用例模块化

自动化selenium开发

改写代码如下:

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)

十一、增加测试报告接口  

自动化selenium开发  

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()