收藏 分销(赏)

selenium2 python自动化测试实战.pdf

上传人:曲**** 文档编号:229869 上传时间:2023-03-20 格式:PDF 页数:51 大小:842.96KB
下载 相关 举报
selenium2 python自动化测试实战.pdf_第1页
第1页 / 共51页
selenium2 python自动化测试实战.pdf_第2页
第2页 / 共51页
点击查看更多>>
资源描述
/Redirectselenium2 python自动化测试实战.http:/itest.ivfo前言:谈谈变自动化http:/itest.ivfo前言:为什么要做自动化测试?A、节省手工测试的人和成本B、有助于提升测试团队的技术力量C、能够生成直观的图形化报表D、我不知道,领导要求做的http:/itest.ivfo刖B:分层的自动化测试UI测试集成接口测试单元测试http:/itest.ivfo前言:什么样的产品适合做自动化测试?功能成熟(需求变动较小)产品更新维护周期长 项目进度不太大 比较频繁的回归测试 软件开发比较规范,具有可测试性 可以脚本具有可复用性http:/itest.ivfo本课程学习重点:selenium 技术:元素定位的几种方法WebDriver API,selenium IDE,selenium gridpython 技术:函数、类、方法;读写文件,unitest单元测试框架,HTMLTest.Runner.py,发邮件模块,多线程技术等。http:/itest.ivfoseleniumselenium的特点:开源,免费 多浏览器支持:firefox、chrome IE 多平台支持:linux、windows MAC 多语言支持:java、python ruby php C#、对web页面有良好的支持 简单(API简单)、灵活(用开发语言驱动)支持分布式测试用例执行http:/itest.ivfoselenium 家谱selenium 1.0:selenium IDEselenium RCClientLauncherHttp Proxyselenium 2.0=selenium 1.0+WebDriverhttp:/itest.ivfoslenium python:环境搭建http:/itest.ivfo环境搭建:window 安装:第一步、安装python第二步、安装setuptoolsC:setuptools-l.3python setup,py install第三步、安装pipC:pip-l.4.1 python setup,py install第四步、安装seleniumC:Python27Scripts pip install-U selenium环境变量:|变量名:PATH变量值:;C:Python27http:/itest.ivfo环境搭建:简易安装(ActivePython):ActivePython包含了一个完整的Python内核,并附加了一些Python 的Windows扩展第一步、下载安装ActivePython第二步、安装seleniumC:Python27Scripts pip install-U seleniumhttp:/itest.ivfo环境搭建:linux安装(ubuntu):第一步、安装: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.ivfo第一个自动化脚本:百度搜索:#coding=utf-8from selenium import webdriverbrowser=webdriver.Firefox()browser.get(http:/)browser.find_element_by_id(kwl).send_keys(selenium)browser.find_element_by_id(sul).click()browser.quit()http:/itest.ivfowebdriver python:元素定位http:/itest.ivfo元素的定位: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.ivfo元素的定位:idnameclass nametag name:百度搜索框前端代码(通过firebug查看)find_element_by_id(kwl)find_element_by_name(wd)find_element_by_class_name(s_ipt)find_element_by_tag_name(input)注:页面上的元素tag name相同的几率很高http:/itest.ivfo元素的定位:linkpartial link:百度首页文字链接:f 闻v/a贴 吧v/a知道find_element_by_link_text(u,新闻,)find_element_by_partial_link_text(新)find_element_by_link_text(u。占吧)注:中文字符串加u是将中文转换成unicode,防止编码问题。http:/itest.ivfo元素的定位:xpath:二 d二:二 fll 三匚1 三:7*.三二 1 P.*U一 二二.十:-:二+二二二二日 三三厂二己三二二_一1”input iffcrlnrl*cla55=*E_ipt*type=*text*naxlenrthlOD*name=*ird*Butocfind_element_by_xpath(find_element_by_xpath(find_element_by_xpath(find_element_by_xpath(find_element_by_xpath(find_element_by_xpath(7/*id=kwl)7/inputid=kwl,)7/inputname=wcT)7/inputclass=s_ipt,)7/spanclass=bg s_iptwr/inpuf)7/formid=forml/span/input,)find_element_by_xpath(7html/body/div/div4/div2/div/form/span/input,)http:/itest.ivfo元素的定位:CSS常见语法:*通用元素选择器,匹配任何元素E标签选择器,匹配所有使用E标签的元素.infoclass选择器,匹配所有class属性中包含info的元素footerid选择器,匹配所有id属性等于footer的元素ErF多元素选择器,同时匹配所有E元素或F元素,E和F之间用逗号分 隔EF后代元素选择器,匹配所有属于E元素后代的F元素,E和F之间用 空格分隔EF子元素选择器,匹配所有E元素的子元素FE-F毗邻元素选择器,匹配紧随E元素之后的同级元素F(只匹配第一 个)EF同级元素选择器,匹配所有在E元素之后的同级F元素EtaVal属性att的值为va1的E元素(区分大小写)Eatt=Vai属性an的值以val开头的E元素(区分大小写)Eatt$=val属性art的值以val结尾的E元素(区分大小写)Eatt*=Vall属性att的值包含val的E元素(区分大小写)Eartl=vl,att2*=v2,属性artl的值为丫1,att2的值包含v2(区分大小写)E:containsCxxxxr)内容中包含XXXX的E元素Emot(s)匹配不符合当前选择器的任何元素http:/itest.ivfo元素的定位:css:Heading定位/from find_element_by_css_seIector(from)定位vdiv class=subdivfind_element_by_css_seIector(.subdiv)find_element_by_css_seIector(from+div)定位vul id=recordlistfind_element_by_css_seIector(#recordlist)find_element_b y _cs s_s e I e cto r(ul#record I isf)find_element_by_css_seIector(ul,)定位 vp Heading v/p find_element_by_css_selector(divul)find_element_by_css_selector(div.subdiv ul p)http:/itest.ivfowebdriver APIhttp:/itest.ivfoWebDriver API:浏览器最大化:maximize_window()设置浏览器宽、高:set_window_size(480,800)控制浏览器后退,前进:back()forward()http:/itest.ivfoWebDriver API:WebElement接口常用方法:clear 清除元素的内容send_keys 在元素上模拟按键输入click 单击元素submit 提交表单size 返回元素的尺寸text 获取元素的文本get_attribute(name)获得属性值 is_displayed()设置该元素是否用户可见http:/itest.ivfoWebDriver API:Actionchains类鼠标操作的常用方法:context_click()右击double_click()双击drag_and_drop()拖动move_to_element()鼠标悬停在一个元素上click_and_hold()按下鼠标左键在一个元素上http:/itest.ivfoWebDriver API:Actionchains类鼠标操作的常用方法:context_click()右击#弓 I入Actionchains类from mon.action_chains import Actionchains#定位到要右击的元素right=driver.find_element_by_xpath(xxn)#对定位到的元素执行鼠标右键操作Actionchains(driver).context_click(right).perform()http:/itest.ivfoWebDriver API:Actionchains类鼠标操作的常用方法:drag_and_drop()拖动#弓 I 入Actionchains 类from mon.action_chains import Actionchains#定位元素的原位置element=driver.find_element_by_name(xxx)#定位元素要移动到的目标位叠target=driver.find_element_by_name(xxx)#执行元素的移动操作Actionchains(driver).drag and drop(elementAtarget).perfor m()http:/itest.ivfoWebDriver API:Actionchains类鼠标操作的常用方法:move_to_element()鼠标悬停#弓|入Actionchains类from mon.action_chains import ActionChains#定位元素的原位置1mnt=drivr.find_1mnt_by_nam(xxx)#定位元素要移动到的目标位宣targt=drivr.find_1mnt_by_nam(xxx)#执行元素的移动操作ActionChains(drivr).drag_and_drop(1mnt,target).prform()http:/itest.ivfoWebDriver API:Keys类键盘操作的常用方法:send_keys(Keys.BACK_SPACE)册1除键(Backspace)send_keys(Keys.SPACE)空格键(Space)send_keys(Keys.TAB)制表键(兀b)send_keys(Keys.E SCAPE)回退键(E sc)send_keys(Keys.E NTE R)回车键(E nter)send_keys(Keys.CONTROL,a)全选 send_keys(Keys.CONTROL,c)复制 send_keys(Keys.CONTROL,x)剪切 send_keys(Keys.CONTROL,v)粘贝占(Ctrl+A)(Ctrl+C)(Ctrl+X)(Ctrl+V)http:/itest.ivfoWebDriver API:Actionchains类鼠标操作的常用方法:move_to_element()鼠标悬停#输入框输入内容drivr.find_1mnt_by_id(kwl).snd_kys(s1ni umm)tim.sip(3)#删除多输入的一个mdrivr.find_1mnt_by_id(kwl).snd_kys(Kys.BA CK_SPACE)tim,sleep(3)http:/itest.ivfoWebDriver API:打印信息(断言的信息):title返回当前页面的标题current_url获取当前加载页面的URLtext获取元素的文本信息http:/itest.ivfoWebDriver API:打印信息(126邮箱):#获得前面title,打印title=driver.titleprint title#获得前面URL,打印now_url=driver.current_urlprint now_url#获得登录成功的用户,打印now_user=driver.find_element_by_id(spnUid).text print now_userhttp:/itest.ivfoWebDriver API:脚本中的等待时间:sleep():python提供设置固定休眠时间的方法。implicitly_wait():是webdirver提供的一个超时等待。WebDriverWait():同样也是webdirver提供的方法。http:/itest.ivfoWebDriver API:webdriver提供定位一组对象的方法:find_elements_by_id()find_elements_by_name()find_elements_by_class_name()find_elements_by_tag_name()fi nd_el e m ents_by_lin k_text()find_elements_by_partial_link_text()find_elements_by_xpath()fi nd_el e m ents_by_css_selecto r()http:/itest.ivfoWebDriver API:定位一组对象,例一:#选择页面上所有的tag name为input的元素 inputs=driver.find_element _by_tag_name(input)#然后从中过滤出tpye为checkbox的元素,单击勾选for input in inputs:if input.get_attribute(type)=checkbox:input.click()http:/itest.ivfoWebDriver API:定位一组对象,例二:#选择所有的type为checkbox的元素并单击勾选checkboxes=driver.find_elements_by_css_selector(,inputtype=checkbox,)for checkbox in checkboxes:checkbox.click()http:/itest.ivfoWebDriver API:层级定位:#点击Linkl链接(弹出下拉列表)driver.find_element_by_link_text(,Linkl).click()#在父亲元件下找到link为Action的子元素menu=driver.find_element_by_id(,dropdownll).find_element_by_link_text(,Anot her action)#鼠标移动到子元素上ActionChains(driver).move_to_element(menu).performOhttp:/itest.ivfoWebDriver API:frame表单嵌套的定位:switch_to_frame 方法#先找到到ifromel(id=fl)driver.switch_to_frame(fl)#再找到其下面的ifrome2(id=f2)driver.switch_to_frame(f2)#下面就可以正常的操作元素了driver.find_element_by_id(kwl).send_keys(selenium)http:/itest.ivfoWebDriver API: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(username)http:/itest.ivfoWebDriver API:多窗口的处理:current_window_handle获得当前窗口句柄window_handles返回的所有窗口的句柄到当前会话 switch_to_window()用于处理多窗口之前切换http:/itest.ivfoWebDriver API:多窗口的处理:#获得当前窗口nowhandle=driver.current_window_handle#打开注册新窗口driver.find_element_by_name(tj_reg).click()#获得所有窗口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)#回至!J 原先的窗口http:/itest.ivfoWebDriver API:alert/confirm/promptb:switch_to_alert()用于获取网页上的警告信息。text返回alert/confirm/prompt中的文字信息。accept点击确认按钮。dismiss点击取消按钮,如果有的话。send_keys输入值,这个alertconfirm没有对话框就不能用了,不然会报错。http:/itest.ivfoWebDriver API:下拉框处理:二次定位:d river.find_element_by_xx(,xx).find_element_by_xx(,xx,).click()#先定位到下拉框m=driver.find_element_by_id(ShippingMethod)#再点击下拉框下的选项m.find_element_by_xpath(,7/optionvalue=10.69,)-click()http:/itest.ivfoWebDriver API:文件上传:d river.find_element_by_xx(xx,).send_keys(,d:/abc.txt)#定位上传按钮,添加本地文件driver.find_element_by_name(file).send_keys(,D:selenium_use_caseupload_file.txt,)http:/itest.ivfoWebDriver API:文件下载:确定Content-Type:下载文件的类型方法一:curl-I URL|grep Content-Type方法二:import requestsprint requests.head(?http:/www.python.org?).headers content-type?http:/itest.ivfoWebDriver API:文件下载:fp=webdriver.F irefoxProfile()fp.set_preference(browser.download.folderList,2)fp.set_preference(browser.download.manager.showWhenStarting,F alse)fp.set_preference(browser.download.dir,os.getcwd()fp.set_preference(browser.helperApps.neverAsk.saveToDisk,application/octet-stream)browser=we b d r i ve r.F i refox(f i refox_p rof i I e=f p)browser.get(http:/pypi.python.org/pypi/selenium)browser.find_element_by_partialjink_text(,selenium-2),click()http:/itest.ivfoWebDriver API:调用 JavaScript:execute_script()调用js方法#隐藏文字信息driver.execute_script($(#tooltip).fadeOut();)#隐藏按钮:button=driver.find_element_by_class_name(btn,)driver.execute_script($(arguments0).fadeOutO,,button)http:/itest.ivfoWebDriver API:控制浏览器滚动条:#将页面滚动条拖到底部js=var q=document.documentElement.scrollTop=10000 driver.execute_script(js)#将滚动条移动到页面的顶部js_=var q=document.documentElement.scrollTop=0 driver.execute_script(js_)http:/itest.ivfoWebDriver API: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.ivfoWebDriver API: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.ivfoWebDriver API:验证码的解决方法:去掉验证码 设置万能码 验证码识别技术 记录 cookiehttp:/itest.ivfoWebDriver API:小结:如何使元素定位变得游刃有余?规范前端开发(为页面属性加上必要的idname)深入理解和使用CSS、xpath 精通javascript、jquery 利用python语言帮忙http:/itest.ivfo
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传
相似文档                                   自信AI助手自信AI助手

当前位置:首页 > 考试专区 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服