selenium2 python自动化测试实战PPT.ppt

上传人:豆**** 文档编号:66112881 上传时间:2022-12-14 格式:PPT 页数:52 大小:925.50KB
返回 下载 相关 举报
selenium2 python自动化测试实战PPT.ppt_第1页
第1页 / 共52页
selenium2 python自动化测试实战PPT.ppt_第2页
第2页 / 共52页
点击查看更多>>
资源描述

《selenium2 python自动化测试实战PPT.ppt》由会员分享,可在线阅读,更多相关《selenium2 python自动化测试实战PPT.ppt(52页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、LOGOselenium2python自自动化化测试实战-虫虫师http:/http:/itest.infohttp:/itest.info前言:前言:谈谈变自动化谈谈变自动化http:/itest.info前言:前言:为什么要做自动化测试?A、节省手工测试的人才和成本B、有助于提升测试团队的技术力量C、能够生成直观的图形化报表D、我不知道,领导要求做的http:/itest.info前言:前言:分层的自动化测试http:/itest.info前言:前言:什么样的产品适合做自动化测试?功能成熟(需求变动较小)产品更新维护周期长项目进度不太大比较频繁的回归测试软件开发比较规范,具有可测试性可以脚

2、本具有可复用性http:/itest.info本本课程学程学习重点:重点:selenium 技术:元素定位的几种方法 WebDriver API,selenium IDE,selenium gridpython 技术:函数、类、方法;读写文件,unitest单元测试框架,HTMLTestRunner.py,发邮件模块,多线程技术等。http:/itest.infoseleniumseleniumselenium selenium 的特点:的特点:开源,免费多浏览器支持:firefox、chrome、IE多平台支持:linux、windows、MAC多语言支持:java、python、ruby、

3、php、C#、对web页面有良好的支持简单(API 简单)、灵活(用开发语言驱动)支持分布式测试用例执行http:/itest.infoseleniumselenium家家谱selenium 1.0selenium 1.0:selenium 2.0 selenium 2.0=selenium 1.0+WebDriver http:/itest.infoslenium python:环境搭建环境搭建http:/itest.info环境搭建:境搭建:window window 安装:安装:第一步、安装python第二步、安装setuptoolsC:setuptools-1.3python setu

4、p.py install第三步、安装pipC:pip-1.4.1 python setup.py install 第四步、安装seleniumC:Python27Scripts pip install-U selenium环境变量:变量名:PATH变量值:;C:Python27 http:/itest.info环境搭建:境搭建:简易安装(简易安装(ActivePythonActivePython):):ActivePython 包含了一个完整的 Python 内核,并附加了一些 Python 的 Windows扩展第一步、下载安装ActivePython第二步、安装seleniumC:Pyth

5、on27Scripts pip install-U selenium http:/itest.info环境搭建:境搭建:linuxlinux安装(安装(ubuntuubuntu):):第一步、安装:setuptoolsrootfnngj-H24X:#apt-get install python-setuptools第二步、安装piprootfnngj-H24X:./pip-1.4.1#python setup.py install第三步、安装seleniumrootfnngj-H24X:./pip-1.4.1#pip install-U seleniumhttp:/itest.info第一个自

6、第一个自动化脚本:化脚本:百度搜索:#coding=utf-8from selenium import webdriverbrowser=webdriver.Firefox()browser.get(http:/)browser.find_element_by_id(kw1).send_keys(selenium)browser.find_element_by_id(su1).click()browser.quit()http:/itest.infowebdriver python:元素定位元素定位http:/itest.info元素的定位:元素的定位:WebDriver 提供的八种定位方法:

7、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元素的定位:元素的定位:idnameclass nametag name:百度搜索框前端代码(通过firebug查看)find_element_b

8、y_id(kw1)find_element_by_name(wd)find_element_by_class_name(s_ipt)find_element_by_tag_name(input)注:页面上的元素tag name 相同的几率很高http:/itest.info元素的定位:元素的定位:linkpartial link:百度首页文字链接:新 闻贴 吧知 道find_element_by_link_text(u新 闻)find_element_by_partial_link_text(新)find_element_by_link_text(u贴 吧).注:中文字符串加u 是将中文转换成

9、unicode,防止编码问题。http:/itest.info元素的定位:元素的定位:xpath:find_element_by_xpath(/*id=kw1)find_element_by_xpath(/inputid=kw1)find_element_by_xpath(/inputname=wd)find_element_by_xpath(/inputclass=s_ipt)find_element_by_xpath(/spanclass=bg s_iptwr/input)find_element_by_xpath(/formid=form1/span/input).find_elemen

10、t_by_xpath(/html/body/div/div4/div2/div/form/span/input)http:/itest.info元素的定位:元素的定位:CSS 常见语法常见语法:http:/itest.info元素的定位:元素的定位:CSS:定位find_element_by_css_selector(from)定位find_element_by_css_selector(.subdiv)find_element_by_css_selector(from+div)定位find_element_by_css_selector(#recordlist)find_element_by

11、_css_selector(ul#recordlist)find_element_by_css_selector(divul)定位Headingfind_element_by_css_selector(divul)find_element_by_css_selector(div.subdiv ul p)Headinghttp:/itest.infowebdriver APIhttp:/itest.infoWebDriverAPIWebDriverAPI:浏览器最大化:maximize_window()设置浏览器宽、高:set_window_size(480,800)控制浏览器后退,前进:bac

12、k()forward()http:/itest.infoWebDriverAPIWebDriverAPI:WebElement接口常用方法接口常用方法:clear 清除元素的内容send_keys 在元素上模拟按键输入click 单击元素submit 提交表单size 返回元素的尺寸text 获取元素的文本get_attribute(name)获得属性值is_displayed()设置该元素是否用户可见http:/itest.infoWebDriverAPIWebDriverAPI:ActionChains 类鼠标操作的常用方法:类鼠标操作的常用方法:context_click()右击 dou

13、ble_click()双击 drag_and_drop()拖动 move_to_element()鼠标悬停在一个元素上 click_and_hold()按下鼠标左键在一个元素上http:/itest.infoWebDriverAPIWebDriverAPI:ActionChains 类鼠标操作的常用方法:类鼠标操作的常用方法:context_click()右击#引入ActionChains类from mon.action_chains import ActionChains.#定位到要右击的元素right=driver.find_element_by_xpath(xx)#对定位到的元素执行鼠标

14、右键操作ActionChains(driver).context_click(right).perform().http:/itest.infoWebDriverAPIWebDriverAPI:ActionChains 类鼠标操作的常用方法:类鼠标操作的常用方法:drag_and_drop()拖动#引入ActionChains类from mon.action_chains import ActionChains.#定位元素的原位置element=driver.find_element_by_name(xxx)#定位元素要移动到的目标位置target=driver.find_element_by

15、_name(xxx)#执行元素的移动操作ActionChains(driver).drag_and_drop(element,target).perform()http:/itest.infoWebDriverAPIWebDriverAPI:ActionChains 类鼠标操作的常用方法:类鼠标操作的常用方法:move_to_element()鼠标悬停#引入ActionChains类from mon.action_chains import ActionChains.#定位元素的原位置element=driver.find_element_by_name(xxx)#定位元素要移动到的目标位置t

16、arget=driver.find_element_by_name(xxx)#执行元素的移动操作ActionChains(driver).drag_and_drop(element,target).perform()http:/itest.infoWebDriverAPIWebDriverAPI:Keys 类键盘操作的常用方法:类键盘操作的常用方法:send_keys(Keys.BACK_SPACE)删除键(BackSpace)send_keys(Keys.SPACE)空格键(Space)send_keys(Keys.TAB)制表键(Tab)send_keys(Keys.ESCAPE)回退键(

17、Esc)send_keys(Keys.ENTER)回车键(Enter)send_keys(Keys.CONTROL,a)全选(Ctrl+A)send_keys(Keys.CONTROL,c)复制(Ctrl+C)send_keys(Keys.CONTROL,x)剪切(Ctrl+X)send_keys(Keys.CONTROL,v)粘贴(Ctrl+V)http:/itest.infoWebDriverAPIWebDriverAPI:ActionChains 类鼠标操作的常用方法:类鼠标操作的常用方法:move_to_element()鼠标悬停.#输入框输入内容driver.find_element

18、_by_id(kw1).send_keys(seleniumm)time.sleep(3)#删除多输入的一个mdriver.find_element_by_id(kw1).send_keys(Keys.BACK_SPACE)time.sleep(3).http:/itest.infoWebDriverAPIWebDriverAPI:打印信息(断言的信息):打印信息(断言的信息):title 返回当前页面的标题current_url获取当前加载页面的URLtext 获取元素的文本信息http:/itest.infoWebDriverAPIWebDriverAPI:打印信息(打印信息(126邮箱)

19、:邮箱):#获得前面title,打印title=driver.titleprint title#获得前面URL,打印now_url=driver.current_urlprint now_url#获得登录成功的用户,打印now_user=driver.find_element_by_id(spnUid).textprint now_userhttp:/itest.infoWebDriverAPIWebDriverAPI:脚本中的等待时间:脚本中的等待时间:sleep():python提供设置固定休眠时间的方法。implicitly_wait():是webdirver 提供的一个超时等待。Web

20、DriverWait():同样也是webdirver 提供的方法。http:/itest.infoWebDriverAPIWebDriverAPI:webdriver提供定位一组对象的方法:提供定位一组对象的方法:find_elements_by_id()find_elements_by_name()find_elements_by_class_name()find_elements_by_tag_name()find_elements_by_link_text()find_elements_by_partial_link_text()find_elements_by_xpath()find_

21、elements_by_css_selector()http:/itest.infoWebDriverAPIWebDriverAPI:定位一组对象,例一:定位一组对象,例一:#选择页面上所有的tag name 为input的元素inputs=driver.find_elements_by_tag_name(input)#然后从中过滤出tpye为checkbox的元素,单击勾选for input in inputs:if input.get_attribute(type)=checkbox:input.click()http:/itest.infoWebDriverAPIWebDriverAPI

22、:定位一组对象,例二:定位一组对象,例二:#选择所有的type为checkbox的元素并单击勾选checkboxes=driver.find_elements_by_css_selector(inputtype=checkbox)for checkbox in checkboxes:checkbox.click()http:/itest.infoWebDriverAPIWebDriverAPI:层级定位:层级定位:#点击Link1链接(弹出下拉列表)driver.find_element_by_link_text(Link1).click()#在父亲元件下找到link为Action的子元素me

23、nu=driver.find_element_by_id(dropdown1).find_element_by_link_text(Another action)#鼠标移动到子元素上ActionChains(driver).move_to_element(menu).perform()http:/itest.infoWebDriverAPIWebDriverAPI:frame表单嵌套的定位:表单嵌套的定位:switch_to_frame 方法#先找到到ifrome1(id=f1)driver.switch_to_frame(f1)#再找到其下面的ifrome2(id=f2)driver.swi

24、tch_to_frame(f2)#下面就可以正常的操作元素了driver.find_element_by_id(kw1).send_keys(selenium)http:/itest.infoWebDriverAPIWebDriverAPI:div弹窗的处理:弹窗的处理:#点击登录链接driver.find_element_by_name(tj_login).click()#通过二次定位找到用户名输入框div=driver.find_element_by_class_name(tang-content).find_element_by_name(userName)div.send_keys(u

25、sername)http:/itest.infoWebDriverAPIWebDriverAPI:多窗口的处理:多窗口的处理:current_window_handle 获得当前窗口句柄window_handles返回的所有窗口的句柄到当前会话switch_to_window()用于处理多窗口之前切换http:/itest.infoWebDriverAPIWebDriverAPI:多窗口的处理:多窗口的处理:#获得当前窗口nowhandle=driver.current_window_handle#打开注册新窗口driver.find_element_by_name(tj_reg).click

26、()#获得所有窗口allhandles=driver.window_handles#循环判断窗口是否为当前窗口for handle in allhandles:if handle!=nowhandle:driver.switch_to_window(handle)print now register window!#切换到邮箱注册标签 driver.find_element_by_id(mailRegTab).click()driver.close()driver.switch_to_window(nowhandle)#回到原先的窗口http:/itest.infoWebDriverAPIWe

27、bDriverAPI:alert/confirm/prompt处理:处理:switch_to_alert()用于获取网页上的警告信息。text 返回 alert/confirm/prompt 中的文字信息。accept 点击确认按钮。dismiss 点击取消按钮,如果有的话。send_keys 输入值,这个alertconfirm没有对话框就不能用了,不然会报错。http:/itest.infoWebDriverAPIWebDriverAPI:下拉框处理:下拉框处理:二次定位:driver.find_element_by_xx(xx).find_element_by_xx(xx).click(

28、)#先定位到下拉框m=driver.find_element_by_id(ShippingMethod)#再点击下拉框下的选项m.find_element_by_xpath(/optionvalue=10.69).click()http:/itest.infoWebDriverAPIWebDriverAPI:文件上传:文件上传:driver.find_element_by_xx(xx).send_keys(d:/abc.txt)#定位上传按钮,添加本地文件driver.find_element_by_name(file).send_keys(D:selenium_use_caseupload_

29、file.txt)http:/itest.infoWebDriverAPIWebDriverAPI:文件下载:文件下载:确定Content-Type:下载文件的类型方法一:curl-I URL|grep Content-Type方法二:import requestsprint requests.head(http:/www.python.org).headerscontent-typehttp:/itest.infoWebDriverAPIWebDriverAPI:文件下载:文件下载:fp=webdriver.FirefoxProfile()fp.set_preference(browser.

30、download.folderList,2)fp.set_preference(browser.download.manager.showWhenStarting,False)fp.set_preference(browser.download.dir,os.getcwd()fp.set_preference(browser.helperApps.neverAsk.saveToDisk,application/octet-stream)browser=webdriver.Firefox(firefox_profile=fp)browser.get(http:/pypi.python.org/p

31、ypi/selenium)browser.find_element_by_partial_link_text(selenium-2).click()http:/itest.infoWebDriverAPIWebDriverAPI:调用调用javaScript:execute_script()调用js方法#隐藏文字信息driver.execute_script($(#tooltip).fadeOut();)#隐藏按钮:button=driver.find_element_by_class_name(btn)driver.execute_script($(arguments0).fadeOut()

32、,button)http:/itest.infoWebDriverAPIWebDriverAPI:控制浏览器滚动条:控制浏览器滚动条:#将页面滚动条拖到底部js=var q=document.documentElement.scrollTop=10000driver.execute_script(js)#将滚动条移动到页面的顶部js_=var q=document.documentElement.scrollTop=0driver.execute_script(js_)http:/itest.infoWebDriverAPIWebDriverAPI:cookie处理:处理:get_cookie

33、s()获得所有cookie信息get_cookie(name)返回特定name 有cookie信息 add_cookie(cookie_dict)添加cookie,必须有name 和value 值delete_cookie(name)删除特定(部分)的cookie信息delete_all_cookies()删除所有cookie信息http:/itest.infoWebDriverAPIWebDriverAPI:cookie处理:处理:get_cookies()获得所有cookie信息get_cookie(name)返回特定name 有cookie信息 add_cookie(cookie_dic

34、t)添加cookie,必须有name 和value 值delete_cookie(name)删除特定(部分)的cookie信息delete_all_cookies()删除所有cookie信息http:/itest.infoWebDriverAPIWebDriverAPI:验证码的解决方法:验证码的解决方法:去掉验证码设置万能码验证码识别技术记录cookiehttp:/itest.infoWebDriverAPIWebDriverAPI:小结:小结:如何使元素定位变得游刃有余?如何使元素定位变得游刃有余?规范前端开发(为页面属性加上必要的idname)深入理解和使用CSS、xpath精通javascript、jquery 利用python 语言帮忙谢谢重定向科技http:/itest.info

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 高考资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com