分類 |
方法 |
方法描述 |
客戶端操作 |
__init__(self, host, port, browserStartCommand, browserURL) |
構造函數。host:selenium server的ip;port:elenium server的port,默認為4444;browserStartCommand:瀏覽器類型,iexplore,firefox。browserURL:打開的url。 |
start(self) |
啟動客戶端 |
|
stop(self) |
關閉客戶端 |
|
鼠標點擊 |
click(self, locator) |
點擊鏈接、按鈕、單選或者復選框 |
double_click(self, locator) |
雙擊鏈接、按鈕、單選或者復選框 |
|
click_at(self, locator, coordString) |
點擊鏈接、按鈕、單選或者復選框后調用waitForPageToLoad,coordString是鼠標事件相對於locator返回元素的位置 |
|
double_click_at(self, locator, coordString) |
雙擊鏈接、按鈕、單選或者復選框后調用waitForPageToLoad,coordString是鼠標事件相對於locator返回元素的位置 |
|
鼠標操作 |
mouse_over(self, locator) |
模擬用戶將鼠標懸停在指定元素上 |
mouse_out(self, locator) |
模擬用戶將鼠標從指定元素移走 |
|
mouse_down(self, locator) |
模擬用戶按下鼠標鍵,未釋放 |
|
mouse_down_at(self, locator, coordString) |
模擬用戶按下鼠標鍵,未釋放,coordString是鼠標事件相對於locator返回元素的位置 |
|
mouse_up(self, locator) |
模擬用戶釋放鼠標鍵 |
|
mouse_up_at(self, locator, coordString) |
模擬用戶釋放鼠標鍵,coordString是鼠標事件相對於locator返回元素的位置 |
|
mouse_move(self, locator) |
鼠標拖動指定元素 |
|
mouse_move_at(self, locator, coordString) |
鼠標拖動指定元素,coordString是鼠標事件相對於locator返回元素的位置 |
|
按鍵操作 |
key_press(self, locator, keySequence) |
模擬用戶按下和釋放鍵,keySequence可以是一個字符串(被按下鍵的ASCII碼值);也可以是單獨的字符,例如”e”。 |
shift_key_down(self) |
按下shift鍵直到調用doShiftUp()或者載入新頁面 |
|
shift_key_up(self) |
釋放shift鍵 |
|
|
|
|
|
|
|
alt_key_down(self) |
按下alt鍵直到調用doAltUp()或者載入新頁面 |
|
alt _key_up(self) |
釋放alt鍵 |
|
control_key_down(self) |
按下control鍵直到調用doControlUp()或者載入新頁面 |
|
control _key_up(self) |
釋放control鍵 |
|
key_down(self, locator, keySequence) |
模擬用戶按鍵不釋放,keySequence可以是一個字符串(被按下鍵的ASCII碼值);也可以是單獨的字符,例如”e”。 |
|
key_up(self, locator, keySequence) |
模擬用戶釋放鍵,keySequence可以是一個字符串(被按下鍵的ASCII碼值);也可以是單獨的字符,例如”e”。 |
|
設置輸入值 |
type(self, locator, value) |
設置輸入框的值;也可用於設置組合框和復選框的值,所設置的值是勾選的選項,是不可見的文本。 |
單選和復選 |
check(self, locator) |
選中單選或者復選框 |
uncheck(self, locator) |
取消選中單選或者復選框 |
|
is_checked(self, locator) |
檢查單選或者復選按鈕是否選中,如果指定元素不存在或者不是切換按鈕則失敗 |
|
下拉框 |
select(self, selectLocator, optionLocator) |
使用option locator選擇下拉列表的選項;其中option locator有以下幾種形式: 1、 label=labelPattern:基於label匹配選項,例如可視化文本,* label=regexp:^[Oo]ther。(注意label前面的星號) 2、 value=valuePattern:基於值匹配選項,* value=other。 3、 id=id:基於id匹配選項,* id=option1。 4、 index=index:基於索引(從零開始)匹配選項,* index=2。 如果未指定任何前綴,默認情況下是label。 selectLocator是下拉列表定位。 |
add_selection(self, locator, optionLocator) |
對於多選元素,在已選選項集合中增加一個選項,locator是多選框定位。 |
|
remove_selection(self, locator, optionLocator) |
對於多選元素,從已選選項集合中刪除一個選項,locator是多選框定位。 |
|
get_selected_labels(self, selectLocator) |
獲取下拉列表或者多級下拉列表的多個已選選項的所有label(可視化文本) |
|
get_selected_label(self, selectLocator) |
獲取下拉列表或者多級下拉列表的某個選項的label(可視化文本) |
|
get_selected_values(self, selectLocator) |
獲取下拉列表或者多級下拉列表的多個已選選項的所有value(value屬性) |
|
get_selected_value(self, selectLocator) |
獲取下拉列表或者多級下拉列表的某個選項的value(value屬性) |
|
get_selected_indexes(self, selectLocator) |
獲取下拉列表或者多級下拉列表的多個已選選項的所有index(index從零開始) |
|
get_selected_index(self, selectLocator) |
獲取下拉列表或者多級下拉列表的某個選項的index(index從零開始) |
|
get_selected_ids(self, selectLocator) |
獲取下拉列表或者多級下拉列表的多個已選選項的所有element ID |
|
get_selected_id(self, selectLocator) |
獲取下拉列表或者多級下拉列表的某個選項的element ID |
|
is_something_selected(self, selectLocator) |
判斷下拉列表的某個選項是否選中 |
|
get_select_options(self, selectLocator) |
獲取下拉列表的所有label |
|
設置超時或者等待事件 |
set_speed(self, value) |
設置操作后的等待時間,以毫秒為單位,設置后作用於每一個selenium操作,默認情況下為0毫秒。 |
get_speed(self) |
獲取操作后的等待時間 |
|
set_timeout(self, timeout) |
為action設置超時,默認為30秒。一般用於open以及waitFor*。運行錯誤返回error。 |
|
wait_for_page_to_load(self, timeout) |
等待裝載新頁面。可以使用該命令代替”AndWait”命令,例如"clickAndWait", "selectAndWait", "typeAndWait"等(這些命令只在js API中提供)。Selenium裝載新頁面時通常會將”newPageLoaded” flag置為true,直到flag恢復為false才繼續執行命令。超時后返回error。 |
|
提交 |
submit(self, formLocator) |
表單提交,在沒有提交按鈕是非常有用的表單提交接口函數 |
窗口操作 |
open(self, url) |
打開url,url可以是相對或者絕對url,open按鈕一直等待頁面載入后再處理,其隱式調用“AndWait”。注意:由於安全限制,url的域需要與runner HTML(Selenium Server)的域保持一致,如果需要使用不同域的url,需要啟動新的瀏覽器會話。 |
open_window(self, url, windowID) |
如果標識為ID的窗口還未打開,則打開彈出窗口。打開窗口后,需要使用selectWindow命令選擇。注意:當window.open調用發生在“onLoad”事件之前或者事件過程時,Selenium無法成功調用window.open,在這種情況下,可以使用Selenium的openWindow命令強制打開窗口,注意使用空白頁面,例如openWindow(””,”muFunnyWindow”)。 注意:url可以為空,’windowID’是js window ID。 |
|
select_window(self, windowID) |
選擇彈出的窗口,選中后所有的命令都在選中窗口中執行。如果想再次選擇主窗口,windowID使用null。Selenium根據windowID查找窗口對象的幾種策略:1、如果windowID是null,選中瀏覽器初始化時打開的最原始的窗口。2、如果windowID是當前應用窗口的js變量名,則該變量包含js window.open()方法的返回值。3、最后,Selenium查找其哈希表,在該表中維護着字符串與窗口對象的映射表,這里的字符串與js方法window.open(url, windowName, windowFeatures, replaceFlag)中的第二個參數匹配。如果不知道窗口的名字,可以通過selenium日志查看window.open創建窗口時的名字標識。同open_window,無法成功調用window.open時,可強制性打開空白頁面。 |
|
select_frame(self, locator) |
在當前窗口選擇frame或者iframe,可以多次調用該命令選擇嵌套的frame。如果想選擇parent frame,定位符使用’relative=parent’;如果想選擇top frame,使用’relative=top’。也可以使用DOM表達式直接查找frame,例如dom=frames["main"].frames["subframe"]。 |
|
go_back(self) |
模擬用戶點擊瀏覽器的后退按鈕 |
|
refresh(self) |
模擬用戶點擊瀏覽器的刷新按鈕 |
|
close(self) |
模擬用戶點擊瀏覽器的關閉按鈕 |
|
window_focus(self, windowName) |
將焦點設置到窗口上,windowName為窗口名 |
|
window_maximize(self, windowName) |
最大化窗口,windowName為窗口名 |
|
get_all_window_ids(self) |
返回瀏覽器打開的所有窗口ID |
|
get_all_window_names(self) |
返回瀏覽器打開的所有窗口名 |
|
get_all_window_titles(self) |
返回瀏覽器打開的所有窗口標題 |
|
彈出窗口 |
wait_for_pop_up(self, windowID, timeout) |
為等待窗口彈出設置超時,windowID為js的窗口ID,超時的單位為毫秒。超過超時時間后返回錯誤。 |
默認情況下,返回true,就像用戶手工點擊ok;運行完此命令后,下次再調用confirm()則返回false,就像用戶點擊Cancle。 |
||
answer_on_next_prompt(self, answer) |
在下次js窗口彈出時,selenium返回answer中的字符串 |
|
is_alert_present(self) |
判斷是否有alert窗口出現,此API不會拋出異常 |
|
is_prompt_present(self) |
判斷是否有prmp窗口出現,此API不會拋出異常 |
|
is_confirmation_present(self) |
判斷是否有confirm窗口出現,此API不會拋出異常 |
|
get_alert(self) |
獲取先前彈出的js alert窗口的內容,如果沒有alert窗口則失敗。類似於手動點擊ok。注意:1、如果alert出現但是沒有獲取或者驗證它,則selenium的下次操作會失敗。2、selenium中的alert不會彈出可視化的alert。3、不支持頁面onload()時間處理過程中的js alert窗口,在這種情況下,將會彈出可視化對話窗口,直到手動點擊ok后selenium才會繼續執行。 |
|
get_confirmation(self) |
獲取先前彈出的js confirm窗口的內容,如果沒有confirm窗口則失敗。類似於手動點擊ok,但是如果在之前執行了chooseCancelOnNextConfirmation命令,則類似於手動點擊cancel。注意:1、如果confirm出現但是沒有獲取或者驗證它,則selenium的下次操作會失敗。2、selenium中的confirm不會彈出可視化的confirm。3、不支持頁面onload()時間處理過程中的js confirm窗口,在這種情況下,將會彈出可視化對話窗口,直到手動點擊ok后selenium才會繼續執行。 |
|
get_prompt(self) |
獲取先前彈出的js prompt窗口的內容,如果沒有prompt窗口則失敗。注意:1、如果alert出現但是沒有獲取或者驗證它,則selenium的下次操作會失敗。2、selenium中的prompt不會彈出可視化的prompt。3、不支持頁面onload()時間處理過程中的js prompt窗口,在這種情況下,將會彈出可視化對話窗口,直到手動點擊ok后selenium才會繼續執行。4、調用此API之前必須調用answerOnNextPromp命令,prompt才能成功處理。 |
|
獲取頁面元素,元素索引 |
get_all_buttons(self) |
返回頁面所有button的ID,如果某個button沒有ID,返回“” |
get_all_links(self) |
返回頁面所有link的ID,如果某個link沒有ID,返回“” |
|
get_all_fields(self) |
返回頁面所有輸入域的ID,如果某個輸入域沒有ID,返回“” |
|
get_attribute_from_all_windows(self, attributeName) |
根據屬性名獲取某個屬性在所有已知窗口中的實例 |
|
get_element_index(self, locator) |
獲取元素對於0索引的相對索引,忽略comment命令和空文本節點。 |
|
獲取元素屬性 |
get_location(self) |
獲取當前頁面的絕對url |
get_title(self) |
獲取當前頁面的絕對title |
|
get_body_text(self) |
獲取頁面的內容 |
|
get_value(self, locator) |
獲取輸入量的值(以空格截斷),對於單選和復選框,返回值為on或者off |
|
get_text(self, locator) |
獲取元素的文本,適用於任何包含文本的元素。此API調用textContent(firefox)或者innerText(IE)。(不同js) |
|
get_table(self, tableCellAddress) |
獲取表格的單元格,tableCellAddress的格式為tableLocator.row.column,其中行和列的索引從零開始,例如foo.1.4。 |
|
get_attribute(self, attributeLocator) |
獲取元素屬性的value |
|
get_element_position_left(self, locator) |
獲取元素的水平位置 |
|
get_element_position_top(self, locator) |
獲取元素的垂直位置 |
|
get_element_width(self, locator) |
獲取元素的寬度 |
|
get_element_height(self, locator) |
獲取元素的高度 |
|
驗證元素 |
is_text_present(self, pattern) |
驗證頁面上是否有滿足pattern的文本 |
is_element_present(self, locator) |
驗證頁面上是否有滿足pattern的文本 |
|
is_visible(self, locator) |
驗證元素是否可見,元素不存在時失敗。元素不可見通過將css的”visibility”設置為“hidden”或者將“display”設置為“none”。 |
|
is_editable(self, locator) |
驗證輸入元素是否被disable,元素不存在時失敗。 |
|
is_ordered(self, locator1, locator2) |
驗證兩個元素是否屬於同一個父節點,並且排序。兩個元素相同是不排序。 |
|
移動元素 |
dragdrop(self, locator, movementsString) |
將定位到的元素拖曳,movementString是目的地址相對於當前位置的偏移,以像素衡量,例如”+70,-300”。不推薦使用dragAndDrop代替。 |
drag_and_drop(self, locator, movementsString) |
將定位到的元素拖曳,movementString是目的地址相對於當前位置的偏移,以像素衡量,例如”+70,-300”。不推薦使用dragAndDrop代替。 |
|
drag_and_drop_to_object(self, locatorOfObjectToBeDragged, locatorOfDragDestinationObject) |
將元素拖曳到目標對象上,已兩個元素的左上角位置重合 |
|
光標操作 |
set_cursor_position(self, locator, position) |
在輸入框或者多行文本中移動光標,如果元素不是輸入框或者多行文本,則失敗。position是輸入域中光標的數字位置,其中0是開頭,-1是結尾。 |
get_cursor_position(self, locator) |
返回輸入元素(輸入框或者多選框)或者多行文本框的文本光標位置。如果指定元素不是輸入元素或者多行文本框,或者指定元素沒有光標,該方法調用失敗。注意:該函數在某些瀏覽器上可能會失效(瀏覽器對js解析的差異性)。特別是當光標或者選擇項被js清除時,該命令趨向於返回光標最后定位的位置,即使光標已經離開頁面。 |
|
js代碼 |
get_eval(self, script) |
執行script中的js腳本代碼片段,只返回最后一行的結果。注意:1、默認情況下,腳本代碼片段在selenium對象的上下文中執行,因此this是指向selenium對象,window指向最上層的運行測試窗口,而不是應用窗口。2、如果想指向應用窗口,使用this.browserbot.getCurrentWindow()。3、如果想定位到應用頁面中的某個元素,使用this.page().findElement(“foo”)定位foo元素。 |
get_expression(self, expression) |
用於js的預處理,用於產生類似assertExpression和waitForExpression等。expression是返回值。 |
|
wait_for_condition(self, script, timeout) |
不斷地運行js代碼片段知道返回為true,代碼片段可以有多行,但只返回最后一行的結果,timeout的單位為毫秒。注意:默認情況下js代碼片段在selenium runner的測試窗口下運行而不是應用窗口,如果需要在應用窗口下運行,先調用selenium.browserbot.getCurrentWindow()。運行錯誤返回error。 |
|
獲取html源碼 |
get_html_source(self) |
返回整個html源碼 |
Cookie操作 |
get_cookie(self) |
返回當前測試頁面的所有cookies。 |
create_cookie(self, nameValuePair, optionsString) |
以當前測試頁面path和domain創建新的cookie,也可以顯示指定path。nameValuePair是鍵值對。optionsString目前支持兩種可選項,path=/path/和max_age=60(cookie的有效時間,以秒為單位)。 |
|
delete_cookie(self, name, path) |
刪除指定路徑下的name的value |
|
事件觸發 |
fire_event(self, locator, eventName) |
顯示地模擬事件,以觸發相應的”onevent”處理函數,eventName例如是focus或者blur等。 |
多frame和多窗口 |
get_whether_this_frame_match_frame_expression(self, currentFrameString, target) |
用於代理注入模式,在該模式下,get_whether_this_frame_match_frame_expression在每個frame和window中運行,幫助selenium識別出當前的frame。在這種情況下,當測試腳本調用selectFrame時,每個frame都調用此API計算哪個frame被選中,選中的frame選擇true,其它frame返回false。CurrentFrameString是當前frame,target是新frame(可能是相對於當前frame) |
get_whether_this_window_match_window_expression(self, currentWindowString, target) |
原理同上 |
|
輔助功能:日志,狀態欄, |
get_log_messages(self) |
很少使用,主要用於當frame來自不同域時,獲取日志信息,因為在這種情況下不符合js日志集中管理機制。 |
set_context(self, context, logLevelThreshold) |
寫消息到狀態欄或者增加瀏覽器端的日志,logLevelThreshold用於指定日志級別(debug,info,warn,error)。注意瀏覽器端的日志不會返回給服務器,並且對於客戶驅動器不可見。 |