selenium 中文手册

上传人:第*** 文档编号:31321158 上传时间:2018-02-06 格式:DOCX 页数:7 大小:35.28KB
返回 下载 相关 举报
selenium 中文手册_第1页
第1页 / 共7页
selenium 中文手册_第2页
第2页 / 共7页
selenium 中文手册_第3页
第3页 / 共7页
selenium 中文手册_第4页
第4页 / 共7页
selenium 中文手册_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《selenium 中文手册》由会员分享,可在线阅读,更多相关《selenium 中文手册(7页珍藏版)》请在金锄头文库上搜索。

1、五、Selenium 中文手册Commands (命令)Action对当前状态进行操作失败时,停止测试Assertion校验是否有产生正确的值Element Locators指定 HTML 中的某元素Patterns用于模式匹配1. Element Locators (元素定位器)id=idid locator 指定 HTML 中的唯一 id 的元素 name=namename locator 指定 HTML 中相同 name 的元素中的第一个元素identifier=ididentifier locator 首先查找 HTML 是否存在该 id 的元素, 若不存在,查找第一个该 name 的

2、元素 dom=javascriptExpressiondom locator 用 JavaScript 表达式来定位 HTML 中的元素,注意必须要以document开头例如:dom=document.formsmyForm.myDropdowndom=document.images56xpath=xpathExpressionxpath locator 用 XPath 表达式来定位 HTML 中的元素, 必须注意要以/ 开头例如:xpath=/imgalt=The image alt textxpath=/tableid=table1/tr4/td2link=textPatternlink

3、locator 用 link 来选择 HTML 中的连接或锚元素例如:link=The link text在没有 locator 前序的情况下 Without a locator prefix, Selenium uses:如果以document.开头,则默认是使用 dom locator,如果是以/ 开头,则默认使用xpath locator,其余情况均认作 identifier locator2. String Matching Patterns (字符串匹配模式)glob:patthernglob 模式,用通配符*代表任意长度字符,?代表一个字符regexp:regexp正则表达式模式,

4、用 JavaScript 正则表达式的形式匹配字符串exact:string精确匹配模式,精确匹配整个字符串,不能用通配符在没有指定字符串匹配前序的时候,selenium 默认使用 golb 匹配模式3. Select Option Specifiers (Select 选项指定器)label=labelPattern通过匹配选项中的文本指定选项例如:label=regexp:Oothervalue=valuePattern通过匹配选项中的值指定选项例如:value=otherid=id通过匹配选项的 id 指定选项例如: id=option1index=index通过匹配选项的序号指定选项,

5、序号从 0 开始例如:index=2在没有选项选择前序的情况下,默认是匹配选项的文本Actions描述了用户所会作出的操作。Action 有两种形式: action 和 actionAndWait, action 会立即执行,而 actionAndWait 会假设需要较长时间才能得到该 action 的相响,而作出等待,open 则是会自动处理等待时间。clickclick(elementLocator)- 点击连接,按钮,复选和单选框- 如果点击后需要等待响应,则用clickAndWait- 如果是需要经过 JavaScript 的 alert 或 confirm 对话框后才能继续操作,则需

6、要调用 verify或 assert 来告诉 Selenium 你期望对对话框进行什么操作。 click aCheckbox clickAndWait submitButton clickAndWait anyLink openopen(url)- 在浏览器中打开 URL,可以接受相对和绝对路径两种形式- 注意:该 URL 必须在与浏览器相同的安全限定范围之内 open /mypage open http:/localhost/typetype(inputLocator, value)- 模拟人手的输入过程,往指定的 input 中输入值- 也适合给复选和单选框赋值- 在这个例子中,则只是给钩

7、选了的复选框赋值,注意,而不是改写其文本 type nameField John Smith typeAndWait textBoxThatSubmitsOnChange newValue selectselect(dropDownLocator, optionSpecifier)- 根据 optionSpecifier 选项选择器来选择一个下拉菜单选项- 如果有多于一个选择器的时候,如在用通配符模式,如f*b*,或者超过一个选项有相同的文本或值,则会选择第一个匹配到的值 select dropDown Australian Dollars select dropDown index=0 se

8、lectAndWait currencySelector value=AUD selectAndWait currencySelector label=Auslian D*rs goBack,closegoBack()模拟点击浏览器的后退按钮close()模拟点击浏览器关闭按钮selectWindowselect(windowId)- 选择一个弹出窗口- 当选中那个窗口的时候,所有的命令将会转移到那窗口中执行 selectWindow myPopupWindow selectWindow null pausepause(millisenconds)- 根据指定时间暂停 Selenium 脚本执

9、行- 常用在调试脚本或等待服务器段响应时 pause 5000 pause 2000 fireEventfireEvent(elementLocatore,evenName)模拟页面元素事件被激活的处理动作 fireEvent textField focus fireEvent dropDown blur waitForConditionwaitForCondition(JavaScriptSnippet,time)- 在限定时间内,等待一段 JavaScript 代码返回 true 值,超时则停止等待 waitForCondition var value=selenium.getText(f

10、oo); value.match(/bar/); 3000 waitForValuewaitForValue(inputLocator, value)- 等待某 input(如 hidden input)被赋予某值,- 会轮流检测该值,所以要注意如果该值长时间一直不赋予该 input 该值的话,可能会导致阻塞 waitForValue finishIndication isfinished store,stroreValuestore(valueToStore, variablename)保存一个值到变量里。该值可以由自其他变量组合而成或通过 JavaScript 表达式赋值给变量 store

11、 Mr John Smith fullname store $.title $.firstname $.suname fullname store javascript.Math.round(Math.PI*100)/100 PI storeValue inputLocator variableName 把指定的 input 中的值保存到变量中storeValue userName userID type userName $.userID storeText, storeAttributestoreText(elementLocator, variablename)把指定元素的文本值赋予给变

12、量 storeText currentDate expectedStartDate verifyValue startDate $.expectedStartDate storeAttribute(.elementLocatorattributeName,variableName. )把指定元素的属性的值赋予给变量storeAttribute input1class classOfInput1 verifyAttribute input2class $.classOfInput1 chooseCancel., answer.chooseCancelOnNextConfirmation()- 当

13、下次 JavaScript 弹出 confirm 对话框的时候,让 selenium 选择 Cancel- 如果没有该命令时,遇到 confirm 对话框 Selenium 默认返回 true,如手动选择 OK 按钮一样 chooseCancelOnNextConfirmation - 如果已经运行过该命令,当下一次又有 confirm 对话框出现时,也会同样地再次选择CancelanswerOnNextPrompt(answerString)- 在下次 JavaScript 弹出 prompt 提示框时,赋予其 anweerString 的值,并选择确定answerOnNextPrompt

14、Kangaroo Assertions允许用户去检查当前状态。两种模式: Assert 和 Verify, 当 Assert 失败,则退出测试;当Verify 失败,测试会继续运行。assertLocation, assertTitleassertLocation(relativeLocation)判断当前是在正确的页面 verifyLocation /mypage assertLocation /mypage assertTitle(titlePattern)检查当前页面的 title 是否正确 verifyTitle My Page assertTitle My Page assertVa

15、lueassertValue(inputLocator, valuePattern)- 检查 input 的值- 对于 checkbox 或 radio,如果已选择,则值为on,反之为off verifyValue nameField John Smith assertValue document.forms2.nameField John Smith assertSelected, assertSelectedOptionsassertSelected(selectLocator, optionSpecifier)检查 select 的下拉菜单中选中的选型是否和 optionSpecifer

16、(Select 选择选项器)的选项相同 verifySelected dropdown2 John Smith verifySelected dorpdown2 value=js*123 assertSelected document.forms2.dropDown label=J*Smith assertSelected document.forms2.dropDown index=0 assertSelectOptions(selectLocator, optionLabelList)- 检查下拉菜单中的选项的文本是否和 optionLabelList 相同- optionLabelList 是以逗号分割的一个字符串 verifySelectOptions dropdown2 John Smith,Dave Bird assertSelectOptions doc

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

当前位置:首页 > 建筑/环境 > 工程造价

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号