您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 国内外标准规范 > Selenium中文手册
五、Selenium中文手册Commands(命令)Action对当前状态进行操作失败时,停止测试Assertion校验是否有产生正确的值ElementLocators指定HTML中的某元素Patterns用于模式匹配1.ElementLocators(元素定位器)id=ididlocator指定HTML中的唯一id的元素name=namenamelocator指定HTML中相同name的元素中的第一个元素identifier=ididentifierlocator首先查找HTML是否存在该id的元素,若不存在,查找第一个该name的元素dom=javascriptExpressiondomlocator用JavaScript表达式来定位HTML中的元素,注意必须要以document开头例如:dom=document.forms['myForm'].myDropdowndom=document.images[56]xpath=xpathExpressionxpathlocator用XPath表达式来定位HTML中的元素,必须注意要以//开头例如:xpath=//img[@alt='Theimagealttext']xpath=//table[@id='table1']//tr[4]/td[2]link=textPatternlinklocator用link来选择HTML中的连接或锚元素例如:link=Thelinktext在没有locator前序的情况下Withoutalocatorprefix,Seleniumuses:如果以document.开头,则默认是使用domlocator,如果是以//开头,则默认使用xpathlocator,其余情况均认作identifierlocator2.StringMatchingPatterns(字符串匹配模式)glob:patthernglob模式,用通配符*代表任意长度字符,?代表一个字符regexp:regexp正则表达式模式,用JavaScript正则表达式的形式匹配字符串exact:string精确匹配模式,精确匹配整个字符串,不能用通配符在没有指定字符串匹配前序的时候,selenium默认使用golb匹配模式3.SelectOptionSpecifiers(Select选项指定器)label=labelPattern通过匹配选项中的文本指定选项例如:label=regexp:^[Oo]thervalue=valuePattern通过匹配选项中的值指定选项例如:value=otherid=id通过匹配选项的id指定选项例如:id=option1index=index通过匹配选项的序号指定选项,序号从0开始例如:index=2在没有选项选择前序的情况下,默认是匹配选项的文本Actions描述了用户所会作出的操作。Action有两种形式:action和actionAndWait,action会立即执行,而actionAndWait会假设需要较长时间才能得到该action的相响,而作出等待,open则是会自动处理等待时间。clickclick(elementLocator)-点击连接,按钮,复选和单选框-如果点击后需要等待响应,则用clickAndWait-如果是需要经过JavaScript的alert或confirm对话框后才能继续操作,则需要调用verify或assert来告诉Selenium你期望对对话框进行什么操作。clickaCheckboxclickAndWaitsubmitButtonclickAndWaitanyLinkopenopen(url)-在浏览器中打开URL,可以接受相对和绝对路径两种形式-注意:该URL必须在与浏览器相同的安全限定范围之内open/mypageopen(inputLocator,value)-模拟人手的输入过程,往指定的input中输入值-也适合给复选和单选框赋值-在这个例子中,则只是给钩选了的复选框赋值,注意,而不是改写其文本typenameFieldJohnSmithtypeAndWaittextBoxThatSubmitsOnChangenewValueselectselect(dropDownLocator,optionSpecifier)-根据optionSpecifier选项选择器来选择一个下拉菜单选项-如果有多于一个选择器的时候,如在用通配符模式,如f*b*,或者超过一个选项有相同的文本或值,则会选择第一个匹配到的值selectdropDownAustralianDollarsselectdropDownindex=0selectAndWaitcurrencySelectorvalue=AUDselectAndWaitcurrencySelectorlabel=AuslianD*rsgoBack,closegoBack()模拟点击浏览器的后退按钮close()模拟点击浏览器关闭按钮selectWindowselect(windowId)-选择一个弹出窗口-当选中那个窗口的时候,所有的命令将会转移到那窗口中执行selectWindowmyPopupWindowselectWindownullpausepause(millisenconds)-根据指定时间暂停Selenium脚本执行-常用在调试脚本或等待服务器段响应时pause5000pause2000fireEventfireEvent(elementLocatore,evenName)模拟页面元素事件被激活的处理动作fireEventtextFieldfocusfireEventdropDownblurwaitForConditionwaitForCondition(JavaScriptSnippet,time)-在限定时间内,等待一段JavaScript代码返回true值,超时则停止等待waitForConditionvarvalue=selenium.getText(foo);value.match(/bar/);3000waitForValuewaitForValue(inputLocator,value)-等待某input(如hiddeninput)被赋予某值,-会轮流检测该值,所以要注意如果该值长时间一直不赋予该input该值的话,可能会导致阻塞waitForValuefinishIndicationisfinishedstore,stroreValuestore(valueToStore,variablename)保存一个值到变量里。该值可以由自其他变量组合而成或通过JavaScript表达式赋值给变量storeMrJohnSmithfullnamestore$.{title}$.{firstname}$.{suname}fullnamestorejavascript.{Math.round(Math.PI*100)/100}PIstoreValueinputLocatorvariableName把指定的input中的值保存到变量中storeValueuserNameuserIDtypeuserName$.{userID}storeText,storeAttributestoreText(elementLocator,variablename)把指定元素的文本值赋予给变量storeTextcurrentDateexpectedStartDateverifyValuestartDate$.{expectedStartDate}storeAttribute(.{}elementLocator@attributeName,variableName.{})把指定元素的属性的值赋予给变量storeAttributeinput1@classclassOfInput1verifyAttributeinput2@class$.{classOfInput1}chooseCancel..,answer..chooseCancelOnNextConfirmation()-当下次JavaScript弹出confirm对话框的时候,让selenium选择Cancel-如果没有该命令时,遇到confirm对话框Selenium默认返回true,如手动选择OK按钮一样chooseCancelOnNextConfirmation-如果已经运行过该命令,当下一次又有confirm对话框出现时,也会同样地再次选择CancelanswerOnNextPrompt(answerString)-在下次JavaScript弹出prompt提示框时,赋予其anweerString的值,并选择确定answerOnNextPromptKangarooAssertions允许用户去检查当前状态。两种模式:Assert和Verify,当Assert失败,则退出测试;当Verify失败,测试会继续运行。assertLocation,assertTitleassertLocation(relativeLocation)判断当前是在正确的页面verifyLocation/mypageassertLocation/mypageassertTitle(titlePattern)检查当前页面的title是否正确verifyTitleMyPageassertTitleMyPageassertValueassertValue(inputLocator,valuePattern)-检查input的值-对于checkbox或radio,如果已选择,则值为on,反之为offverifyValuenameFieldJohnSmithassertValuedocument.forms[2].nameFieldJohnSmithassertSelected,assertSelectedOptionsassertSelected(selectLocator,optionSpecifier)检查select的下拉菜单中选中的选型是否和optionSpecifer(Select选择选项器)的选项相同verifySelecteddropdown2JohnSmithverifySelecteddorpdown2value=js*123assertSelecteddocument.forms[2].dropDownlabel=J*SmithassertSelecteddocument.forms[2].dropDownindex=0assertSelectOptions(selectLocator,optionLabelList)-检查下拉菜单中的选项的文本是否和optionLabelList相同-optionLabelList是以逗号分割的一个字符串verifySelectOptionsdropdown2JohnSmith,DaveBirdassertSelectOptionsdocument.forms[2].dropdownSmith,J,Bird,DassertTextassertText(elementLocator,textPattern)-检查指定元素的文本-只对有包含文本的元素生效-对于Mozilla类型的浏览器,用textContent取元素的文本,对于IE类型的浏览器,用innerText取元素文本verifyTextstatusMessageSuccessfulassertText//div[@id='foo']//h1SuccessfulassertTextPresent,assertAttributeassertTextPresent(text)检查在当前给用户显示的页面上是否有出现指定的文本verifyTextPresentYouarenowloggedinassertTextPresentYouarenowloggedinassertAttribute(.{}e
本文标题:Selenium中文手册
链接地址:https://www.777doc.com/doc-2858053 .html