Command表示要執行的操作是什么,這些Selenium命令通常被稱為“selense”,他是一套用於執行測試的命令集。
在selenese中,一部分命令基於HTML標記,用於測試UI元素是否存在、驗證指定內容是否正確、檢查鏈接是否可用,並可用輸入字段、選擇列表的選項、提交表單並操作表格中的數據等;另一個部分命令輔助測試,例如驗證窗口大小、鼠標位置、警告信息、Ajax功能、彈出窗口、事件處理以及其他Web應用程序功能。
Selenium命令分為三種:Action(操作),Accessor(存儲),Assertion(斷言)。
一、Action:用於操作一般應用程序
1、瀏覽器的操作
(1)open(URL):打開指定的URL
(2)goBack():相當於瀏覽器上的后退鍵
(3)refresh():相當於瀏覽器上的刷新按鈕
(4)windowsFocus():用於激活當前選中的瀏覽器窗口
(5)windowsMaximize():用於將當前選中的瀏覽器窗口最大化
(6)close():相當於瀏覽器的關閉按鈕
2、界面元素的基本操作
(1)type(locatoe,value):用於在input類型的元素中輸入值,也可用於給下拉列表框、復選框賦值。
(2)typeKeys(locatoe,value):用於模擬鍵盤敲擊事件。
(3)click(locator):單擊鏈接、復選框或單選按鈕。
(4)clickAt(locator,coordString):與click命令相似,當需要填寫坐標。
(5)doubleClick(locator):雙擊。如果雙擊動作會導致頁面重新加載,最好在后面添加wautForPageToLoad命令。
(6)doubleClickAt(locator):與doubleClick想死你,但需要填寫坐標。
(7)select(selectLocator,optionLocator):該選項用於在下拉列表框中選擇指定選項。
關於選項的定位方式:a.label=文本值;b.value=真實值(即在html標記語言中的定義);c.id=id,基於選項的id;d.index=index,選項的索引,從0開始。
(8)check(locator):勾選復選框或單選框。
(9)uncheck(locator):取消勾選。
(10)focus(locator):將焦點轉移到指定的元素上。
3、鼠標鍵盤模擬操作
4、設置類操作
5、測試控制/調試類操作
(1)pause(waitTime):使測試在指定時間內暫停執行
(2)break():暫停當前正在執行的測試,直到用於手動單擊繼續按鈕。
(3)captureEntirePageScreeshot(filename,kwargs):將當前窗口進行截圖並保存。
(4)highlight(locator):暫時將指定元素的背景色改為黃色,並在稍后取消該效果,一般用於調試。
(5)echo(message):將指定信息打印出來。
二、Accessor:用於檢查應用程序的狀態,並將結果存儲在變量中
(1)store(expression,variableName):將指定的值存儲在變量中。
(2)storeTitle(variableName):用於存放網頁標題。
(3)storeLocation(variableName):用於存儲當前網頁的URL。
(4)storeValue(locator,variableName):用於存儲input元素所存放的值。
(5)storeEditable(locator,variableName):用於存儲input元素的可編輯狀態,可以則返回true,否則返回false。
(6)storeText(locator,variableName):用於某個元素的文本值。
(7)storeChecked(locator,variableName):用於存儲復選框或單選框的勾選情況,勾選則返回true,否則返回false。
(8)storeSelectedIndex(selectLocator,variableName):獲取所選項在列表中的索引。
(9)storeSelectedLable(selectLocator,variableName):獲取指定列表中所選項的文本值。
(10)storeSelectedValue(selectLocator,variableName):獲取指定列表中所選項的真實值。
(11)storeSelectOption(selectLocator,variableName):獲取指定列表中所有選項的文本值,以逗號隔開。
(12)storeTable(tableCellAddress,variableName):獲取表格中某個單元格的值。
(13)storAttribute(attributeLocator,variableName):獲取指定屬性的值。Target:格式為“元素定位表達式”+“@屬性名稱”
(14)storeTextPresent(pattern,variableName):驗證指定的文本是否在頁面中出現,出現則返回true,否則返回false。
(15)storeElementPresent(location,variableName):驗證元素是否存在與頁面中,出現則返回true,否則返回false。
(16)storeVisible(location,variableName):驗證指定元素是否在頁面中顯現,出現則返回true,否則返回false。
(17)storeSpeed(variableName):獲取執行速度。
三、Assertion:與Accessor相似,主要用於驗證某個命題是否為真,預期結果與實際結果進行比較的過程。
分為assert(斷言),verify(驗證),waitFor(等待)三類命令;
這三類命令有分為五種驗證手段:Title(獲取頁面標題),Value(獲取元素的值),Text(獲取元素的文本內容),Table(獲取元素的標簽),ElementPresent(獲取當前元素)。
區別:如果assert失敗,測試就會中斷;而verify失敗時,失敗將會記錄下來,但測試依然會繼續執行;而waitFor用於等待,直到等待的條件為真,條件為真,那么測試就會通過。
(title)
1、驗證網頁標題是否等於或不等於期望值:assertTitle(pattern)/assertNotTitle(pattern)/verutyTitle(pattern)/verifyNotTitle(pattern)/waitForTitle(pattern)/waitForNotTitle(pattern)。
2、驗證網頁的URL是否等於或不等於期望值:assertLocation(pattern)/assertNotLocation(pattern)/verutyLocation(pattern)/verifyNotLocation(pattern)/waitForLocation(pattern)/waitForNotLocation(pattern)。
(value)
3、驗證input元素的值是否等於或不等於期望值:assertValue(location,pattern)/assertNotValue(location,pattern)/verutyValue(location,pattern)/verifyNotValue(location,pattern)/waitForValue(location,pattern)/waitForNotValue(location,pattern)。
4、驗證input元素的可編輯狀態是否為預期狀態:assertEditable(location)/assertNotEditable(location)/verutyEditable(location)/verifyNotEditable(location)/waitForEditable(location)/waitForNotEditable(location)。
(text)
5、驗證某個元素的文本值是否等於期望值:assertText(location,pattern)/assertNotText(location,pattern)/verutyText(location,pattern)/verifyNotText(location,pattern)/waitForText(location,pattern)/waitForNotText(location,pattern)。
(Table)
6、驗證表格(table元素)中某個單元格(td元素)的值是否為期望值:assertTable(tableCellAddress,pattern)/assertNotTable(tableCellAddress,pattern)
/verutyTable(tableCellAddress,pattern)/verifyNotTable(tableCellAddress,pattern)/waitForTable(tableCellAddress,pattern)/waitForNotTable(tableCellAddress,pattern)。
(ElementPresent)
7、驗證指定元素是否存在於頁面上:assertElementPresent(location)/assertNotElementPresent(location)/verutyElementPresent(location)/verifyNotElementPresent(location)/waitForElementPresent(location)/waitForNotElementPresent(location)。
8、驗證頁面中是否顯示指定元素:assertVisible(location)/...
9、驗證指定的文本是否在頁面中出現:assertTextPresent(pattern)/...
10、驗證指定屬性的值是否為期望值:assertAttribute(attributeLocation,pattern)/...
11、驗證復選框或單選框的勾選情況是否符合期望:assertChecked(location)/...
12、驗證所選項在列表中的索引是否符合期望值:assertSelectedIndex(selectLocation,pattern)/...
13、驗證指定列表中所選項的文本值是否符合期望值:assertSelectedLable(selectlocation,pattern)/...
14、驗證指定列表中所選項的真實值(value屬性)是否為期望值:assertSelectedValue(selectlocation,pattern)/...
15、驗證指定列表中所有選項的文本是否符合期望值:assertSelectedOption(selectlocation,pattern)/...
更多的command可查看selenium幫助手冊,查閱更多的action等
本博文參考書《selenium自動化測試指南》,趙卓著;《selenium2自動化測試實戰 基於Python語言》,蟲師著;《零基礎實現web自動化測試》,溫素劍著