【文档说明】selenium2_python自动化测试实战课件.ppt,共(52)页,927.023 KB,由小橙橙上传
转载请保留链接:https://www.ichengzhen.cn/view-44430.html
以下为本文档部分文字说明:
LOGOselenium2python自动化测试实战---虫师http://fnng.cnblogs.comhttp://itest.infohttp://itest.info前言:谈谈变自动化http://itest.info前言:为什么要做自动化测试?A、节省手工测
试的人才和成本B、有助于提升测试团队的技术力量C、能够生成直观的图形化报表D、我不知道,领导要求做的http://itest.info前言:分层的自动化测试http://itest.info前言:什么样的产品适合做自动化测试?•功能成熟(需求变动较小)•产品更新维护周期长•项目进度不太大•
比较频繁的回归测试•软件开发比较规范,具有可测试性•可以脚本具有可复用性http://itest.info本课程学习重点:selenium技术:元素定位的几种方法WebDriverAPI,seleniumIDE,seleniumgridpython技术:函数、类、方法;读写文件,
unitest单元测试框架,HTMLTestRunner.py,发邮件模块,多线程技术等。http://itest.infoseleniumselenium的特点:•开源,免费•多浏览器支持:firefox、chrome、IE•多平台支持
:linux、windows、MAC•多语言支持:java、python、ruby、php、C#、•对web页面有良好的支持•简单(API简单)、灵活(用开发语言驱动)•支持分布式测试用例执行http://itest.in
foselenium家谱selenium1.0:selenium2.0=selenium1.0+WebDriverhttp://itest.infosleniumpython:环境搭建http://ite
st.info环境搭建:window安装:第一步、安装python第二步、安装setuptoolsC:\setuptools-1.3>pythonsetup.pyinstall第三步、安装pipC:\pip-1.4.1>pythonsetup.pyinstall第四步、安装se
leniumC:\Python27\Scripts>pipinstall-Uselenium环境变量:变量名:PATH变量值:;C:\Python27http://itest.info环境搭建:简易安装(ActivePython
):ActivePython包含了一个完整的Python内核,并附加了一些Python的Windows扩展第一步、下载安装ActivePython第二步、安装seleniumC:\Python27\Scripts>pipi
nstall-Useleniumhttp://itest.info环境搭建:linux安装(ubuntu):第一步、安装:setuptoolsroot@fnngj-H24X:~#apt-getinstallpython-setuptools第二步、安装piproot@fnngj-H24X:.
./pip-1.4.1#pythonsetup.pyinstall第三步、安装seleniumroot@fnngj-H24X:../pip-1.4.1#pipinstall-Useleniumhttp://itest.info第一个自动化脚本:百度搜索:#coding=utf-8from
seleniumimportwebdriverbrowser=webdriver.Firefox()browser.get("http://www.baidu.com")browser.find_elem
ent_by_id("kw1").send_keys("selenium")browser.find_element_by_id("su1").click()browser.quit()http://itest.infowebdriv
erpython:元素定位http://itest.info元素的定位:WebDriver提供的八种定位方法:find_element_by_id()find_element_by_name()find_element_by_class_name()find_element_by_tag_name
()find_element_by_link_text()find_element_by_partial_link_text()find_element_by_xpath()find_element_by_css_selector()http://itest.info元素的定位:id\n
ame\classname\tagname:百度搜索框前端代码(通过firebug查看)<inputid="kw1"class="s_ipt"type="text"maxlength="100"name=
"wd"autocomplete="off">find_element_by_id(‘kw1’)find_element_by_name(‘wd’)find_element_by_class_name(‘s_ipt’)find_e
lement_by_tag_name(‘input’)注:页面上的元素tagname相同的几率很高http://itest.info元素的定位:link\partiallink:百度首页文字链接:<ahref="http:/
/news.baidu.com"name="tj_news">新闻</a><ahref="http://tieba.baidu.com"name="tj_tieba">贴吧</a><ahref="http://zhidao.baidu.com"name="tj_zhidao">知
道</a>find_element_by_link_text(u‘新闻’)find_element_by_partial_link_text(‘新’)find_element_by_link_text(u‘贴吧’)...注:中文字符串加u是将中文转换成
unicode,防止编码问题。http://itest.info元素的定位:xpath:find_element_by_xpath(‘//*[@id='kw1']’)find_element_by_xpath(‘//inp
ut[@id='kw1']’)find_element_by_xpath(‘//input[@name='wd']’)find_element_by_xpath(‘//input[@class='s_ipt']’)find_element_by_xpat
h(‘//span[@class='bgs_iptwr']/input’)find_element_by_xpath(‘//form[@id='form1']/span/input’)....find_element_by_xpath(‘/html/bo
dy/div/div[4]/div[2]/div/form/span/input’)http://itest.info元素的定位:CSS常见语法:http://itest.info元素的定位:CSS:定位</from>find_element_by_css_
selector(‘from’)定位<divclass="subdiv">find_element_by_css_selector(‘.subdiv’)find_element_by_css_selector(‘from+div’)定位<ulid="reco
rdlist">find_element_by_css_selector(‘#recordlist’)find_element_by_css_selector(‘ul#recordlist’)find_element_by_css
_selector(‘div>ul’)定位<p>Heading</p>find_element_by_css_selector(‘div>ul’)find_element_by_css_selector
(‘div.subdiv>ul>p’)</form><divclass="subdiv"><ulid="recordlist"><p>Heading</p>http://itest.infowebdriverAPIhttp://itest.infoWebD
riverAPI:浏览器最大化:maximize_window()设置浏览器宽、高:set_window_size(480,800)控制浏览器后退,前进:back()forward()http://itest.infoWebDriv
erAPI:WebElement接口常用方法:clear清除元素的内容send_keys在元素上模拟按键输入click单击元素submit提交表单size返回元素的尺寸text获取元素的文本get_attribute(n
ame)获得属性值is_displayed()设置该元素是否用户可见http://itest.infoWebDriverAPI:ActionChains类鼠标操作的常用方法:context_click()右击double_cli
ck()双击drag_and_drop()拖动move_to_element()鼠标悬停在一个元素上click_and_hold()按下鼠标左键在一个元素上http://itest.infoWebDriverAPI:ActionChains类鼠标操作的常用方法:cont
ext_click()右击#引入ActionChains类fromselenium.webdriver.common.action_chainsimportActionChains....#定位到要右击的
元素right=driver.find_element_by_xpath("xx")#对定位到的元素执行鼠标右键操作ActionChains(driver).context_click(right).perform()....http://itest.infoWebDriverAPI:Acti
onChains类鼠标操作的常用方法:drag_and_drop()拖动#引入ActionChains类fromselenium.webdriver.common.action_chainsimportActionChains...#定位元素的原位置element=driver.find_el
ement_by_name("xxx")#定位元素要移动到的目标位置target=driver.find_element_by_name("xxx")#执行元素的移动操作ActionChains(driver).drag_and_drop(element,target).perf
orm()http://itest.infoWebDriverAPI:ActionChains类鼠标操作的常用方法:move_to_element()鼠标悬停#引入ActionChains类fromselen
ium.webdriver.common.action_chainsimportActionChains...#定位元素的原位置element=driver.find_element_by_name("xxx")#
定位元素要移动到的目标位置target=driver.find_element_by_name("xxx")#执行元素的移动操作ActionChains(driver).drag_and_drop(el
ement,target).perform()http://itest.infoWebDriverAPI:Keys类键盘操作的常用方法:send_keys(Keys.BACK_SPACE)删除键(BackSpace)send_keys(Keys.SPACE)空格键(Space)se
nd_keys(Keys.TAB)制表键(Tab)send_keys(Keys.ESCAPE)回退键(Esc)send_keys(Keys.ENTER)回车键(Enter)send_keys(Keys.CO
NTROL,'a')全选(Ctrl+A)send_keys(Keys.CONTROL,'c')复制(Ctrl+C)send_keys(Keys.CONTROL,'x')剪切(Ctrl+X)send_keys(Keys.CONT
ROL,'v')粘贴(Ctrl+V)http://itest.infoWebDriverAPI:ActionChains类鼠标操作的常用方法:move_to_element()鼠标悬停...#输入框输入内容driver.find_element_by_id("kw1").send_key
s("seleniumm")time.sleep(3)#删除多输入的一个mdriver.find_element_by_id("kw1").send_keys(Keys.BACK_SPACE)time.sl
eep(3)...http://itest.infoWebDriverAPI:打印信息(断言的信息):title返回当前页面的标题current_url获取当前加载页面的URLtext获取元素的文本信息http://ites
t.infoWebDriverAPI:打印信息(126邮箱):#获得前面title,打印title=driver.titleprinttitle#获得前面URL,打印now_url=driver.curre
nt_urlprintnow_url#获得登录成功的用户,打印now_user=driver.find_element_by_id("spnUid").textprintnow_userhttp://itest.infoWeb
DriverAPI:脚本中的等待时间:sleep():python提供设置固定休眠时间的方法。implicitly_wait():是webdirver提供的一个超时等待。WebDriverWait():同样也是webdirver提供的方法。http://it
est.infoWebDriverAPI:webdriver提供定位一组对象的方法:find_elements_by_id()find_elements_by_name()find_elements_by_class_name()find_eleme
nts_by_tag_name()find_elements_by_link_text()find_elements_by_partial_link_text()find_elements_by_xpath()find_elements_by_css_sel
ector()http://itest.infoWebDriverAPI:定位一组对象,例一:……#选择页面上所有的tagname为input的元素inputs=driver.find_elements_by_tag_name('i
nput')#然后从中过滤出tpye为checkbox的元素,单击勾选forinputininputs:ifinput.get_attribute('type')=='checkbox':input.click()……http://
itest.infoWebDriverAPI:定位一组对象,例二:……#选择所有的type为checkbox的元素并单击勾选checkboxes=driver.find_elements_by_css_selector('inpu
t[type=checkbox]')forcheckboxincheckboxes:checkbox.click()……http://itest.infoWebDriverAPI:层级定位:……#点击Link1链接(弹出下拉列表)driver.find_element_by_l
ink_text('Link1').click()#在父亲元件下找到link为Action的子元素menu=driver.find_element_by_id('dropdown1').find_element_by_link_text('Anotheraction')#
鼠标移动到子元素上ActionChains(driver).move_to_element(menu).perform()……http://itest.infoWebDriverAPI:frame表单嵌套的定位:switch_to_fr
ame方法……#先找到到ifrome1(id=f1)driver.switch_to_frame("f1")#再找到其下面的ifrome2(id=f2)driver.switch_to_frame("f2")#下面就可以正常的操作元素
了driver.find_element_by_id("kw1").send_keys("selenium")……http://itest.infoWebDriverAPI:div弹窗的处理:……#点击登录链接d
river.find_element_by_name("tj_login").click()#通过二次定位找到用户名输入框div=driver.find_element_by_class_name("tang-content").find_e
lement_by_name("userName")div.send_keys("username")……http://itest.infoWebDriverAPI:多窗口的处理:current_window_handle获得当前
窗口句柄window_handles返回的所有窗口的句柄到当前会话switch_to_window()用于处理多窗口之前切换http://itest.infoWebDriverAPI:多窗口的处理:#获得当前窗口nowhandle=driver.current_window
_handle#打开注册新窗口driver.find_element_by_name("tj_reg").click()#获得所有窗口allhandles=driver.window_handles#循环判断窗口是否为当前窗口forhandleinallhandles:ifhandle!
=nowhandle:driver.switch_to_window(handle)print'nowregisterwindow!'#切换到邮箱注册标签driver.find_element_by_id("mailRegTab").click()driver.close()drive
r.switch_to_window(nowhandle)#回到原先的窗口http://itest.infoWebDriverAPI:alert/confirm/prompt处理:switch_to_alert()用于获取网页上的警告信息。text返回alert/
confirm/prompt中的文字信息。accept点击确认按钮。dismiss点击取消按钮,如果有的话。send_keys输入值,这个alert\confirm没有对话框就不能用了,不然会报错。http://itest.infoWebDriverAPI:下拉框处理:二次定位:driver.
find_element_by_xx('xx').find_element_by_xx('xx').click()……#先定位到下拉框m=driver.find_element_by_id("ShippingMethod")#
再点击下拉框下的选项m.find_element_by_xpath("//option[@value='10.69']").click()……http://itest.infoWebDriverAPI:文件上传:driver.find_element_by_xx('xx
').send_keys('d:/abc.txt')……#定位上传按钮,添加本地文件driver.find_element_by_name("file").send_keys('D:\\selenium_use_case\upload_file.txt')……http://it
est.infoWebDriverAPI:文件下载:确定Content-Type:下载文件的类型方法一:curl-IURL|grep"Content-Type"方法二:importrequestsprint
requests.head(’http://www.python.org’).headers[’content-type’]http://itest.infoWebDriverAPI:文件下载:fp=webdriver.FirefoxProfile()fp.s
et_preference("browser.download.folderList",2)fp.set_preference("browser.download.manager.showWhenStarting",False)fp.set_preferen
ce("browser.download.dir",os.getcwd())fp.set_preference("browser.helperApps.neverAsk.saveToDisk","application/o
ctet-stream")browser=webdriver.Firefox(firefox_profile=fp)browser.get("http://pypi.python.org/pypi/s
elenium")browser.find_element_by_partial_link_text("selenium-2").click()http://itest.infoWebDriverAPI:调用javaScript:execute_script()调用js方法#隐藏文
字信息driver.execute_script('$("#tooltip").fadeOut();')#隐藏按钮:button=driver.find_element_by_class_name('btn')driver.execute_scrip
t('$(arguments[0]).fadeOut()',button)http://itest.infoWebDriverAPI:控制浏览器滚动条:#将页面滚动条拖到底部js="varq=document.documentElement.scrollTop=10000"driv
er.execute_script(js)#将滚动条移动到页面的顶部js_="varq=document.documentElement.scrollTop=0"driver.execute_script(js_)http://itest.infoWebDriver
API:cookie处理:get_cookies()获得所有cookie信息get_cookie(name)返回特定name有cookie信息add_cookie(cookie_dict)添加cookie,必须有name和value值delete_coo
kie(name)删除特定(部分)的cookie信息delete_all_cookies()删除所有cookie信息http://itest.infoWebDriverAPI:cookie处理:get_cookies
()获得所有cookie信息get_cookie(name)返回特定name有cookie信息add_cookie(cookie_dict)添加cookie,必须有name和value值delete_cookie(name)删除特定(部分)的
cookie信息delete_all_cookies()删除所有cookie信息http://itest.infoWebDriverAPI:验证码的解决方法:•去掉验证码•设置万能码•验证码识别技术•记录cookie
http://itest.infoWebDriverAPI:小结:如何使元素定位变得游刃有余?•规范前端开发(为页面属性加上必要的id\name)•深入理解和使用CSS、xpath•精通javascript、jquer
y•利用python语言帮忙谢谢重定向科技http://itest.info