Selenium備忘手冊(cè)
??? 最近的項(xiàng)目準(zhǔn)備用Selenium作一部分的Regression Test。在SpringSide里參考了一下,又下了個(gè)Selenium IDE玩玩,覺(jué)得還蠻容易上手,基本上不需要手動(dòng)寫測(cè)試代碼。
??? 但實(shí)操起來(lái)時(shí)面對(duì)各種復(fù)雜的頁(yè)面情況遇到不少麻煩。感覺(jué)Selenium 的offical documentation寫的比較high level, 最后找了個(gè)though works的ppt,算得上比較全面易懂。匆匆翻譯了一下,供后來(lái)者參考。
一、?格式
1.?Test Case 格式
? | ?Title | ? |
?命令(Command)? | ?目標(biāo)(Target)? | ?值(Value) |
?命令(Command) | ?目標(biāo)(Target)? | ??( ) |
?判斷(Assertion)? | ?期望值(Expected)? | ?實(shí)際值(Actual) |
2.?Test Suites 格式
?Title |
?TestCase1.html |
?TestCase2.html |
?TestCase3.html |
二、?Commands (命令)
-
Action
對(duì)當(dāng)前狀態(tài)進(jìn)行操作
失敗時(shí),停止測(cè)試 -
Assertion
校驗(yàn)是否有產(chǎn)生正確的值 -
Element Locators
指定HTML中的某元素 -
Patterns
用于模式匹配
1.?Element Locators (元素定位器)
-
id=id
id locator 指定HTML中的唯一id的元素? -
?name=name
name locator指定 HTML中相同name的元素中的第一個(gè)元素 -
?identifier=id
identifier locator 首先查找HTML是否存在該id的元素, 若不存在,查找第一個(gè)該name的元素? -
dom=javascriptExpression
dom locator用JavaScript表達(dá)式來(lái)定位HTML中的元素,注意必須要以”document”開(kāi)頭
例如:
dom=document.forms[‘myForm’].myDropdown
dom=document.images[56] -
?xpath=xpathExpression
xpath locator用 XPath 表達(dá)式來(lái)定位HTML中的元素,必須注意要以”//”開(kāi)頭
例如:
xpath=//img[@alt=’The image alt text’]
xpath=//table[@id=’table1’]//tr[4]/td[2] -
?link=textPattern
link locator 用link來(lái)選擇HTML中的連接或錨元素
例如:
link=The link text - 在沒(méi)有l(wèi)ocator前序的情況下 Without a locator prefix, Selenium uses:
如果以”document.”開(kāi)頭,則默認(rèn)是使用 dom locator,如果是以“//”開(kāi)頭,則默認(rèn)使用xpath locator,其余情況均認(rèn)作identifier locator
2.?String Matching Patterns (字符串匹配模式)
-
glob:patthern
glob模式,用通配符”*”代表任意長(zhǎng)度字符,“?”代表一個(gè)字符 -
regexp:regexp
正則表達(dá)式模式,用JavaScript正則表達(dá)式的形式匹配字符串 -
exact:string
精確匹配模式,精確匹配整個(gè)字符串,不能用通配符 - 在沒(méi)有指定字符串匹配前序的時(shí)候,selenium 默認(rèn)使用golb 匹配模式
3.?Select Option Specifiers (Select選項(xiàng)指定器)
-
label=labelPattern
通過(guò)匹配選項(xiàng)中的文本指定選項(xiàng)
例如:label=regexp:^[Oo]ther -
value=valuePattern
通過(guò)匹配選項(xiàng)中的值指定選項(xiàng)
例如:value=other -
id=id
通過(guò)匹配選項(xiàng)的id指定選項(xiàng)
例如: id=option1 -
index=index
通過(guò)匹配選項(xiàng)的序號(hào)指定選項(xiàng),序號(hào)從0開(kāi)始
例如:index=2 - 在沒(méi)有選項(xiàng)選擇前序的情況下,默認(rèn)是匹配選項(xiàng)的文本
三、?Actions
描述了用戶所會(huì)作出的操作。
Action 有兩種形式: action和actionAndWait, action會(huì)立即執(zhí)行,而actionAndWait會(huì)假設(shè)需要較長(zhǎng)時(shí)間才能得到該action的相響,而作出等待,open則是會(huì)自動(dòng)處理等待時(shí)間。
-
click
click(elementLocator)
-?點(diǎn)擊連接,按鈕,復(fù)選和單選框
-?如果點(diǎn)擊后需要等待響應(yīng),則用”clickAndWait”
-?如果是需要經(jīng)過(guò)JavaScript的alert或confirm對(duì)話框后才能繼續(xù)操作,則需要調(diào)用verify或assert來(lái)告訴Selenium你期望對(duì)對(duì)話框進(jìn)行什么操作。?click ?aCheckbox ? ?clickAndWait ?submitButton ? ?clickAndWait? ?anyLink ? -
open
open(url)
-?在瀏覽器中打開(kāi)URL,可以接受相對(duì)和絕對(duì)路徑兩種形式
-?注意:該URL必須在與瀏覽器相同的安全限定范圍之內(nèi)?open? ?/mypage ? ?open ?http://localhost/? ? -
type
?type(inputLocator, value)
-?模擬人手的輸入過(guò)程,往指定的input中輸入值
-?也適合給復(fù)選和單選框賦值
-?在這個(gè)例子中,則只是給鉤選了的復(fù)選框賦值,注意,而不是改寫其文本?type ?nameField? ?John Smith ?typeAndWait? ?textBoxThatSubmitsOnChange ?newValue -
select
select(dropDownLocator, optionSpecifier)
-?根據(jù)optionSpecifier選項(xiàng)選擇器來(lái)選擇一個(gè)下拉菜單選項(xiàng)
-?如果有多于一個(gè)選擇器的時(shí)候,如在用通配符模式,如”f*b*”,或者超過(guò)一個(gè)選項(xiàng)有相同的文本或值,則會(huì)選擇第一個(gè)匹配到的值?select?? ?dropDown? ?Australian Dollars ?select?? ?dropDown ??index=0 ?selectAndWait? ?currencySelector? ?value=AUD ?selectAndWait? ?currencySelector? ?label=Auslian D*rs - ?goBack,close
goBack()
模擬點(diǎn)擊瀏覽器的后退按鈕
close()
模擬點(diǎn)擊瀏覽器關(guān)閉按鈕 -
selectWindow
select(windowId)
-?選擇一個(gè)彈出窗口
-?當(dāng)選中那個(gè)窗口的時(shí)候,所有的命令將會(huì)轉(zhuǎn)移到那窗口中執(zhí)行?selectWindow? ?myPopupWindow ? ?selectWindow ?null ? -
pause
pause(millisenconds)
-?根據(jù)指定時(shí)間暫停Selenium腳本執(zhí)行
-?常用在調(diào)試腳本或等待服務(wù)器段響應(yīng)時(shí)?pause? ?5000 ? ?pause? ?2000 ? -
fireEvent
?fireEvent(elementLocatore,evenName)
模擬頁(yè)面元素事件被激活的處理動(dòng)作?fireEvent? ?textField focus? ?fireEvent ?dropDown ?blur -
waitForCondition
waitForCondition(JavaScriptSnippet,time)
- 在限定時(shí)間內(nèi),等待一段JavaScript代碼返回true值,超時(shí)則停止等待?waitForCondition? ?var value=selenium.getText("foo"); value.match(/bar/); ?3000 -
waitForValue
waitForValue(inputLocator, value)
-?等待某input(如hidden input)被賦予某值,
-?會(huì)輪流檢測(cè)該值,所以要注意如果該值長(zhǎng)時(shí)間一直不賦予該input該值的話,可能會(huì)導(dǎo)致阻塞?waitForValue ?finishIndication isfinished? ? ? ? -
store,stroreValue
store(valueToStore, variablename)
保存一個(gè)值到變量里。
該值可以由自其他變量組合而成或通過(guò)JavaScript表達(dá)式賦值給變量?store? ?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, storeAttribute
storeText(elementLocator, variablename)
把指定元素的文本值賦予給變量?storeText? ?currentDate? ?expectedStartDate ?verifyValue? ?startDate? ?${expectedStartDate}
storeAttribute(elementLocator@attributeName,variableName)
把指定元素的屬性的值賦予給變量?storeAttribute? input1@class?? ?classOfInput1 ?verifyAttribute? ?input2@class? ?${classOfInput1} -
chooseCancel.., answer..
chooseCancelOnNextConfirmation()
-?當(dāng)下次JavaScript彈出confirm對(duì)話框的時(shí)候,讓selenium選擇Cancel
-?如果沒(méi)有該命令時(shí),遇到confirm對(duì)話框Selenium默認(rèn)返回true,如手動(dòng)選擇OK按鈕一樣?chooseCancelOnNextConfirmation?? ? ?
-?如果已經(jīng)運(yùn)行過(guò)該命令,當(dāng)下一次又有confirm對(duì)話框出現(xiàn)時(shí),也會(huì)同樣地再次選擇Cancel
answerOnNextPrompt(answerString)
- 在下次JavaScript彈出prompt提示框時(shí),賦予其anweerString的值,并選擇確定?answerOnNextPrompt? ?Kangaroo? ?
四、?Assertions
允許用戶去檢查當(dāng)前狀態(tài)。兩種模式: Assert 和 Verify,?當(dāng)Assert失敗,則退出測(cè)試;當(dāng)Verify失敗,測(cè)試會(huì)繼續(xù)運(yùn)行。
-
assertLocation, assertTitle
assertLocation(relativeLocation)
判斷當(dāng)前是在正確的頁(yè)面?verifyLocation? ?/mypage ? ?assertLocation? ?/mypage ? -
assertTitle(titlePattern)
檢查當(dāng)前頁(yè)面的title是否正確?verifyTitle? ?My Page? ? ?assertTitle? ?My Page? ? -
assertValue
assertValue(inputLocator, valuePattern)
-?檢查input的值
-?對(duì)于 checkbox或radio,如果已選擇,則值為”on”,反之為”off”?verifyValue? ?nameField? ?John Smith ?assertValue? ?document.forms[2].nameField ?John Smith -
assertSelected, assertSelectedOptions
assertSelected(selectLocator, optionSpecifier)
檢查select的下拉菜單中選中的選型是否和optionSpecifer(Select選擇選項(xiàng)器)的選項(xiàng)相同?verifySelected? ?dropdown2? ?John Smith ?verifySelected? ?dorpdown2? ?value=js*123 ?assertSelected? ?document.forms[2].dropDown ?label=J*Smith ?assertSelected? ?document.forms[2].dropDown? ?index=0 -
assertSelectOptions(selectLocator, optionLabelList)
-?檢查下拉菜單中的選項(xiàng)的文本是否和optionLabelList相同
-?optionLabelList是以逗號(hào)分割的一個(gè)字符串?verifySelectOptions? ?dropdown2? ?John Smith,Dave Bird ?assertSelectOptions? ?document.forms[2].dropdown ?Smith,J,Bird,D -
assertText
assertText(elementLocator,textPattern)
-?檢查指定元素的文本
-?只對(duì)有包含文本的元素生效
-?對(duì)于Mozilla類型的瀏覽器,用textContent取元素的文本,對(duì)于IE類型的瀏覽器,用innerText取元素文本?verifyText? ?statusMessage? ?Successful ?assertText? ?//div[@id='foo']//h1 ?Successful
? -
assertTextPresent, assertAttribute
assertTextPresent(text)
檢查在當(dāng)前給用戶顯示的頁(yè)面上是否有出現(xiàn)指定的文本?verifyTextPresent? ?You are now logged in? ? ?assertTextPresent? ?You are now logged in? ? -
assertAttribute(
elementLocator@attributeName
, ValuePattern)
檢查當(dāng)前指定元素的屬性的值?verifyAttribute? txt1@class ?bigAndBlod ?assertAttribute? ?document.images[0]@alt ?alt-text ?verifyAttribute? ?//img[@id='foo']/alt? ?alt-text -
assertTextPresent, etc.
assertTextPresent(text)
assertTextNotPresent(text)
assertElementPresent(elementLocator)
assertElementNotPresent(elementLocator)?verifyElementPresent? ?submitButton? ? ?assertElementPresent? ?//img[@alt='foo']? ?
-
assertTable
assertTable(cellAddress, valuePattern)
-?檢查table里的某個(gè)cell中的值
-?cellAddress的語(yǔ)法是tableName.row.column, 注意行列序號(hào)都是從0開(kāi)始?verifyTable? ?myTable.1.6 ?Submitted ?assertTable? ?results0.2? ?13
? -
assertVisible, nonVisible
assertVisible(elementLocator)
-?檢查指定的元素是否可視的
-?隱藏一個(gè)元素可以用設(shè)置css的‘visibility’屬性為’hidden’,也可以設(shè)置‘display’屬性為‘none’?verfyVisible? ?postcode? ? ?assertVisible? ?postcode? ? -
assertNotVisible(elementLocator)
?verfyNotVisible? ?postcode? ? ?assertNotVisible? ?postcode? ?
-
Editable, non-editable
assertEditable(inputLocator)
檢查指定的input是否可以編輯?verifyEditable? ?shape? ? ?assertEditable? ?colour? ? -
assertNotEditable(inputLocator)
檢查指定的input是否不可以編輯 -
assertAlert
assertAlert(messagePattern)
-?檢查JavaScript是否有產(chǎn)生帶指定message的alert對(duì)話框
-?alert產(chǎn)生的順序必須與檢查的順序一致
-?檢查alert時(shí)會(huì)產(chǎn)生與手動(dòng)點(diǎn)擊’OK’按鈕一樣的效果。如果一個(gè)alert產(chǎn)生了,而你卻沒(méi)有去檢查它,selenium會(huì)在下個(gè)action中報(bào)錯(cuò)。
-?注意:Selenium 不支持 JavaScript 在onload()事件時(shí) 調(diào)用alert();在這種情況下,Selenium需要你自己手動(dòng)來(lái)點(diǎn)擊OK. -
assertConfirmation
assertConfirmation(messagePattern)
-?檢查JavaScript是否有產(chǎn)生帶指定message的confirmation對(duì)話框和alert情況一樣,confirmation對(duì)話框也必須在它們產(chǎn)生的時(shí)候進(jìn)行檢查
-?默認(rèn)情況下,Selenium會(huì)讓confirm() 返回true, 相當(dāng)于手動(dòng)點(diǎn)擊Ok按鈕的效果。你能夠通過(guò)chooseCancelOnNextConfirmation命令讓confirm()返回false.同樣地,如果一個(gè)cofirmation對(duì)話框出現(xiàn)了,但你卻沒(méi)有檢查的話,Selenium將會(huì)在下個(gè)action中報(bào)錯(cuò)
-?注意:在Selenium的環(huán)境下,confirmation對(duì)話框框?qū)⒉粫?huì)再出現(xiàn)彈出顯式對(duì)話框
-?注意:Selenium不支持在onload()事件時(shí)調(diào)用confirmation對(duì)話框,在這種情況下,會(huì)出現(xiàn)顯示confirmatioin對(duì)話框,并需要你自己手動(dòng)點(diǎn)擊。 -
?assertPrompt
assertPrompt(messagePattern)
-?檢查JavaScript是否有產(chǎn)生帶指定message的Prompt對(duì)話框
-?你檢查的prompt的順序Prompt對(duì)話框產(chǎn)生的順序必須相同
-?必須在verifyPrompt之前調(diào)用answerOnNextPrompt命令
-?如果prompt對(duì)話框出現(xiàn)了但你卻沒(méi)有檢查,則Selenium會(huì)在下個(gè)action中報(bào)錯(cuò)?answerOnNextPrompt? ?Joe? ? ?click? ?id=delegate? ? ?verifyPrompt? ?Delegate to who?? ?
五、?Parameters and Variables
參數(shù)和變量的聲明范圍由簡(jiǎn)單的賦值到JavaScript表達(dá)式賦值。
Store,storeValue 和storeText 為下次訪問(wèn)保存值。
在Selenium內(nèi)部是用一個(gè)叫storeVars的map來(lái)保存變量名。
-
Variable Substitution 變量替換
提供了一個(gè)簡(jiǎn)單的方法去訪問(wèn)變量,語(yǔ)法 ${xxx}?store? ??Mr? ?title ?storeValue? ?nameField? ?surname ?store? ??${title} ${suname} ?fullname ?type? ?textElement? ?Full name is: ${fullname} -
?JavaScript Evaluation JavaScript賦值
你能用JavaScript來(lái)構(gòu)建任何你所需要的值。
這個(gè)參數(shù)是以javascript開(kāi)頭,語(yǔ)法是 javascript{‘with a trailing’}。
可以通過(guò)JavaScript表達(dá)式給某元素賦值。store? ?javascript{'merchant'+(new Date()).getTime()}? ?merchantId ?type? textElement? ?javascript{storedVars['merchantId'].toUpperCase()} -
Generating Unique values 產(chǎn)生唯一值. ?
問(wèn)題:你需要唯一的用戶名
解決辦法: 基于時(shí)間來(lái)產(chǎn)生用戶名,如’fred’+(new Date().getTime())