:::

3. Selenium API參考手冊【ZZ】

 大家在編寫測試案例想查閱Selenium API說明時,可以查閱本文,否則請跳過!

 (註:這裡API版本比較老,新版本的Selenium的API在這裡不一定能找到。)

Selenium API說明文檔

(轉載自:http://wiki.javascud.org/display/springs/SeleniumRefrence)

Commands (命令)

  • Action
    對當前狀態進行操作
    失敗時,停止測試
  • Assertion
    校驗是否有產生正確的值
  • Element Locators
    指定HTML中的某元素
  • Patterns
    用於模式匹配

1. Element Locators (元素定位器)

  • id=id
    id locator 指定HTML中的唯一id的元素
  • name=name
    name locator指定 HTML中相同name的元素中的第一個元素
  • identifier=id
    identifier locator 首先查找HTML是否存在該id的元素, 若不存在,查找第一個該name的元素
  • dom=javascriptExpression
    dom locator用JavaScript表達式來定位HTML中的元素,注意必須要以"document"開頭
    例如:
    dom=document.forms['myForm'].myDropdown
    dom=document.images[56]
  • xpath=xpathExpression
    xpath locator用 XPath 表達式來定位HTML中的元素,必須注意要以"//"開頭
    例如:
    xpath=//img[@alt='The image alt text']
    xpath=//table[@id='table1']//tr[4]/td[2]
  • link=textPattern
    link locator 用link來選擇HTML中的連接或錨元素
    例如:
    link=The link text
  • 在沒有locator前序的情況下 Without a locator prefix, Selenium uses:
    如果以"document."開頭,則默認是使用 dom locator,如果是以"//"開頭,則默認使用xpath locator,其餘情況均認作identifier locator

2. String Matching Patterns (字符串匹配模式)

  • glob:patthern
    glob模式,用通配符"*"代表任意長度字符,"?"代表一個字符
  • regexp:regexp
    正則表達式模式,用JavaScript正則表達式的形式匹配字符串
  • exact:string
    精確匹配模式,精確匹配整個字符串,不能用通配符
  • 在沒有指定字符串匹配前序的時候,selenium 默認使用golb 匹配模式

3. Select Option Specifiers (Select選項指定器)

  • label=labelPattern
    通過匹配選項中的文本指定選項
    例如:label=regexp:^[Oo]ther
  • value=valuePattern
    通過匹配選項中的值指定選項
    例如:value=other
  • id=id
    通過匹配選項的id指定選項
    例如: id=option1
  • index=index
    通過匹配選項的序號指定選項,序號從0開始
    例如:index=2
  • 在沒有選項選擇前序的情況下,默認是匹配選項的文本

Actions

描述了用戶所會作出的操作。
Action 有兩種形式: action和actionAndWait, action會立即執行,而actionAndWait會假設需要較長時間才能得到該action的相響,而作出等待,open則是會自動處理等待時間。

  • click
    click(elementLocator)
    - 點擊連接,按鈕,複選和單選框
    - 如果點擊後需要等待響應,則用"clickAndWait"
    - 如果是需要經過JavaScript的alert或confirm對話框後才能繼續操作,則需要調用verify或assert來告訴Selenium你期望對對話框進行什麼操作。
    click aCheckbox  
    clickAndWait submitButton  
    clickAndWait anyLink  
  • open
    open(url)
    - 在瀏覽器中打開URL,可以接受相對和絕對路徑兩種形式
    - 注意:該URL必須在與瀏覽器相同的安全限定範圍之內
    open /mypage  
    open http://localhost/  
  • type
    type(inputLocator, value)
    - 模擬人手的輸入過程,往指定的input中輸入值
    - 也適合給複選和單選框賦值
    - 在這個例子中,則只是給鉤選了的複選框賦值,注意,而不是改寫其文本
    type nameField John Smith
    typeAndWait textBoxThatSubmitsOnChange newValue
  • select
    select(dropDownLocator, optionSpecifier)
    - 根據optionSpecifier選項選擇器來選擇一個下拉菜單選項
    - 如果有多於一個選擇器的時候,如在用通配符模式,如"f*b*",或者超過一個選項有相同的文本或值,則會選擇第一個匹配到的值
    select  dropDown Australian Dollars
    select  dropDown index=0
    selectAndWait currencySelector value=AUD
    selectAndWait currencySelector label=Auslian D*rs
  •  goBack,close
    goBack()
    模擬點擊瀏覽器的後退按鈕
    close()
    模擬點擊瀏覽器關閉按鈕
  • selectWindow
    select(windowId)
    - 選擇一個彈出窗口
    - 當選中那個窗口的時候,所有的命令將會轉移到那窗口中執行
    selectWindow myPopupWindow  
    selectWindow null  
  • pause
    pause(millisenconds)
    - 根據指定時間暫停Selenium腳本執行
    - 常用在調試腳本或等待服務器段響應時
    pause 5000  
    pause 2000  
  • fireEvent
     fireEvent(elementLocatore,evenName)
    模擬頁面元素事件被激活的處理動作
    fireEvent textField focus
    fireEvent dropDown blur
  • waitForCondition
    waitForCondition(JavaScriptSnippet,time)
    - 在限定時間內,等待一段JavaScript代碼返回true值,超時則停止等待
    waitForCondition var value=selenium.getText("foo"); value.match(/bar/); 3000
  • waitForValue
    waitForValue(inputLocator, value)
    - 等待某input(如hidden input)被賦予某值,
    - 會輪流檢測該值,所以要注意如果該值長時間一直不賦予該input該值的話,可能會導致阻塞
    waitForValue finishIndication isfinished
         
  • store,stroreValue
    store(valueToStore, variablename)
    保存一個值到變量裡。
    該值可以由自其他變量組合而成或通過JavaScript表達式賦值給變量
    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()
    - 當下次JavaScript彈出confirm對話框的時候,讓selenium選擇Cancel
    - 如果沒有該命令時,遇到confirm對話框Selenium默認返回true,如手動選擇OK按鈕一樣
    chooseCancelOnNextConfirmation     

    - 如果已經運行過該命令,當下一次又有confirm對話框出現時,也會同樣地再次選擇Cancel
    answerOnNextPrompt(answerString)
    - 在下次JavaScript彈出prompt提示框時,賦予其anweerString的值,並選擇確定

    answerOnNextPrompt Kangaroo  

Assertions

允許用戶去檢查當前狀態。兩種模式: Assert 和 Verify, 當Assert失敗,則退出測試;當Verify失敗,測試會繼續運行。

  • assertLocation, assertTitle
    assertLocation(relativeLocation)
    判斷當前是在正確的頁面
    verifyLocation /mypage  
    assertLocation /mypage  
  • assertTitle(titlePattern)
    檢查當前頁面的title是否正確
    verifyTitle My Page  
    assertTitle My Page  
  • assertValue
    assertValue(inputLocator, valuePattern)
    - 檢查input的值
    - 對於 checkbox或radio,如果已選擇,則值為"on",反之為"off"
    verifyValue nameField John Smith
    assertValue document.forms[2].nameField John Smith
  • assertSelected, assertSelectedOptions
    assertSelected(selectLocator, optionSpecifier)
    檢查select的下拉菜單中選中的選型是否和optionSpecifer(Select選擇選項器)的選項相同
    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)
    - 檢查下拉菜單中的選項的文本是否和optionLabelList相同
    - optionLabelList是以逗號分割的一個字符串
    verifySelectOptions dropdown2 John Smith,Dave Bird
    assertSelectOptions document.forms[2].dropdown Smith,J,Bird,D
  • assertText
    assertText(elementLocator,textPattern)
    - 檢查指定元素的文本
    - 只對有包含文本的元素生效
    - 對於Mozilla類型的瀏覽器,用textContent取元素的文本,對於IE類型的瀏覽器,用innerText取元素文本
    verifyText statusMessage Successful
    assertText //div[@id='foo']//h1 Successful
  • assertTextPresent, assertAttribute
    assertTextPresent(text)
    檢查在當前給用戶顯示的頁面上是否有出現指定的文本
    verifyTextPresent You are now logged in  
    assertTextPresent You are now logged in  
  • assertAttribute(.{}elementLocator@attributeName.{}, ValuePattern)
    檢查當前指定元素的屬性的值
    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)
    verifyElementPresent submitButton  
    assertElementPresent //img[@alt='foo']   assertElementNotPresent(elementLocator)
  • assertTable
    assertTable(cellAddress, valuePattern)
    - 檢查table裡的某個cell中的值
    - cellAddress的語法是tableName.row.column, 注意行列序號都是從0開始
    verifyTable myTable.1.6 Submitted
    assertTable results0.2 13
  • assertVisible, nonVisible
    assertVisible(elementLocator)
    - 檢查指定的元素是否可視的
    - 隱藏一個元素可以用設置css的'visibility'屬性為'hidden',也可以設置'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是否有產生帶指定message的alert對話框
    - alert產生的順序必須與檢查的順序一致
    - 檢查alert時會產生與手動點擊'OK'按鈕一樣的效果。如果一個alert產生了,而你卻沒有去檢查它,selenium會在下個action中報錯。
    - 注意:Selenium 不支持 JavaScript 在onload()事件時 調用alert();在這種情況下,Selenium需要你自己手動來點擊OK.
  • assertConfirmation
    assertConfirmation(messagePattern)
    - 檢查JavaScript是否有產生帶指定message的confirmation對話框和alert情況一樣,confirmation對話框也必須在它們產生的時候進行檢查
    - 默認情況下,Selenium會讓confirm() 返回true, 相當於手動點擊Ok按鈕的效果。你能夠通過chooseCancelOnNextConfirmation命令讓confirm()返回false.同樣 地,如果一個cofirmation對話框出現了,但你卻沒有檢查的話,Selenium將會在下個action中報錯
    - 注意:在Selenium的環境下,confirmation對話框框將不會再出現彈出顯式對話框
    - 注意:Selenium不支持在onload()事件時調用confirmation對話框,在這種情況下,會出現顯示confirmatioin對話框,並需要你自己手動點擊。
  • assertPrompt
    assertPrompt(messagePattern)
    - 檢查JavaScript是否有產生帶指定message的Prompt對話框
    - 你檢查的prompt的順序Prompt對話框產生的順序必須相同
    - 必須在verifyPrompt之前調用answerOnNextPrompt命令
    - 如果prompt對話框出現了但你卻沒有檢查,則Selenium會在下個action中報錯
    answerOnNextPrompt Joe  
    click id=delegate  
    verifyPrompt Delegate to who?  

Parameters and Variables

參數和變量的聲明範圍由簡單的賦值到JavaScript表達式賦值。
Store,storeValue 和storeText 為下次訪問保存值。
在Selenium內部是用一個叫storeVars的map來保存變量名。

  • Variable Substitution 變量替換
    提供了一個簡單的方法去訪問變量,語法 $.{xxx}
    store Mr title
    storeValue nameField surname
    store $.{title} $.{suname} fullname
    type textElement Full name is: $.{fullname}
  • JavaScript Evaluation JavaScript賦值
    你能用JavaScript來構建任何你所需要的值。
    這個參數是以javascript開頭,語法是 javascript.{'with a trailing'}。
    可以通過JavaScript表達式給某元素賦值。
    store javascript.{'merchant'+(new Date()).getTime()} merchantId
    type textElement javascript.{storedVars['merchantId'].toUpperCase()}

 


:::

搜尋

QR Code 區塊

https%3A%2F%2Ftad0616.net%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbsn%3D28%26tbdsn%3D826

書籍目錄

展開 | 闔起

線上使用者

58人線上 (17人在瀏覽線上書籍)

會員: 0

訪客: 58

更多…