RobotFramework Selenium2Library 關鍵字詳解


*** Settings ***

Library           Selenium2Library

 

*** Keywords ***

Checkbox應該不被選擇

    [Arguments]    ${locator}

    Checkbox Should Not Be Selected    ${locator}

 

Frame應該包含文本

    [Arguments]    ${locator}    ${text}    ${loglevel}=INFO

    Frame Should Contain    ${locator}    ${text}    ${loglevel}

 

List應該無選項

    [Arguments]    ${locator}

    List Should Have No Selections    ${locator}

 

List選項應該是

    [Arguments]    ${locator}    @{items}

    List Selection Should Be    ${locator}    @{items}

 

Radio按鈕不應該被選擇

    [Arguments]    ${group_name}

    Radio Button Should Not Be Selected    ${group_name}

 

Radio按鈕應該被設置成

    [Arguments]    ${group_name}    ${value}

    Radio Button Should Be Set To    ${group_name}    ${value}

 

Url應該包含

    [Arguments]    ${expected}

    Location Should Contain    ${expected}

 

Xpath應該匹配X次

    [Arguments]    ${xpath}    ${expected_xpath_count}    ${msg}=    ${loglevel}=INFO

    Xpath Should Match X Times    ${xpath}    ${expected_xpath_count}    ${msg}    ${loglevel}

 

checkbox應該被選擇

    [Arguments]    ${locator}

    checkbox should be selected    ${locator}

 

url應該是

    [Arguments]    ${url}

    Location Should Be    ${url}

 

下次確認選擇取消

    Choose Cancel On Next Confirmation

 

下次確認選擇確定

    Choose Ok On Next Confirmation

 

從List中取消選項

    [Arguments]    ${locator}    @{items}

    Unselect From List    ${locator}    @{items}

 

從List選擇選項

    [Arguments]    ${locator}    @{items}

    Select From List    ${locator}    @{items}

 

偏移拖拽

    [Arguments]    ${source}    ${xoffset}    ${yoffset}

    Drag And Drop By Offset    ${source}    ${xoffset}    ${yoffset}

 

元素應該不包含文本

    [Arguments]    ${locator}    ${expected}    ${msg}=

    Element Should Not Contain    ${locator}    ${expected}    ${msg}

 

元素應該不可用

    [Arguments]    ${locator}

    Element Should Be Disabled    ${locator}

 

元素應該不可見

    [Arguments]    ${locator}    ${msg}=

    Element Should Not Be Visible    ${locator}    ${msg}

 

元素應該包含文本

    [Arguments]    ${locator}    ${text}

    Element Should Contain    ${locator}    ${text}

 

元素應該匹配x次

    [Arguments]    ${locator}    ${count}    ${msg}=    ${loglevel}=INFO

    Locator Should Match X Times    ${locator}    ${count}    ${msg}    ${loglevel}

 

元素應該可用

    [Arguments]    ${locator}

    Element Should Be Enabled    ${locator}

 

元素應該可見

    [Arguments]    ${locator}

    Element Should Be Visible    ${locator}

 

元素文本應該是

    [Arguments]    ${locator}    ${expected}    ${msg}=

    Element Text Should Be    ${locator}    ${expected}    ${msg}

 

全選List元素

    [Arguments]    ${locator}

    Select All From List    ${locator}

 

關閉所有瀏覽器

    Close All Browsers

 

關閉瀏覽器

    Close Browser

 

關閉窗口

    Close Window

 

切換瀏覽器

    [Arguments]    ${index}

    Switch Browser    ${index}

 

創建Webdriver

    [Arguments]    ${driver_name}    ${alias}=    ${kwargs}={}

    Create Webdriver    ${driver_name}    ${alias}    ${kwargs}

 

刪除Cookie

    [Arguments]    ${name}

    Delete Cookie    ${name}

 

刪除所有Cookie

    Delete All Cookies

 

刪除本地策略

    [Arguments]    ${strategy_name}

    Remove Location Strategy    ${strategy_name}

 

刷新頁面

    Reload Page

 

雙擊元素

    [Arguments]    ${locator}

    Double Click Element    ${locator}

 

取消選擇Checkbox

    [Arguments]    ${locator}

    Unselect Checkbox    ${locator}

 

取消選擇Frame

    Unselect Frame

 

回退

    Go Back

 

在圖片上按下鼠標左鍵

    [Arguments]    ${locator}

    Mouse Down On Image    ${locator}

 

在連接上按下鼠標左鍵

    [Arguments]    ${locator}

    Mouse Down On Link    ${locator}

 

應該出現警告彈窗

    [Arguments]    ${text}=

    Alert Should Be Present    ${text}

 

當前Frame不應該包含

    [Arguments]    ${text}    ${loglvl}=INFO

    Current Frame Should Not Contain    ${text}    ${loglvl}

 

當前Frame包含

    [Arguments]    ${text}    ${loglevel}=INFO

    Current Frame Contains    ${text}    ${loglevel}

 

截屏

    [Arguments]    @{filename}

    Capture Page Screenshot    @{filename}

 

打開瀏覽器

    [Arguments]    ${url}    ${browser}=chrome    @{args}

    Open Browser    ${url}    ${browser}    @{args}

 

打開菜單

    [Arguments]    ${locator}

    Open Context Menu    ${locator}

 

執行js

    [Arguments]    ${code}

    Execute Javascript    ${code}

 

執行異步js

    [Arguments]    ${code}

    Execute Async Javascript    ${code}

 

拖拽元素

    [Arguments]    ${source}    ${target}

    Drag And Drop    ${source}    ${target}

 

按下鼠標左鍵

    [Arguments]    ${locator}

    Mouse Down    ${locator}

 

按鍵

    [Arguments]    ${locator}    ${key}

    [Documentation]    按鍵 \ \ text_field \ \ q

    ...

    ...    按鍵 \ \ login_button \ \\13 \ \ #ASCII Code for enter Key

    Press Key    ${locator}    ${key}

 

提交表單

    [Arguments]    ${locator}=

    Submit Form    ${locator}

 

文本區值應該是

    [Arguments]    ${locator}    ${expected}    ${msg}=

    Textarea Value Should Be    ${locator}    ${expected}    ${msg}

 

文本區應該包含

    [Arguments]    ${locator}    ${expected}    ${msg}=

    Textarea Should Contain    ${locator}    ${expected}    ${msg}

 

文本框值應該是

    [Arguments]    ${locator}    ${expected}    ${msg}=

    Textfield Value Should Be    ${locator}    ${expected}    ${msg}

 

文本框應該包含

    [Arguments]    ${locator}    ${expected}    ${msg}=

    Textfield Should Contain    ${locator}    ${expected}    ${msg}

 

最大化

    Maximize Browser Window

 

標題應該是

    [Arguments]    ${title}

    Title Should Be    ${title}

 

根據Index從List中取消選項

    [Arguments]    ${locator}    @{indexes}

    Unselect From List By Index    ${locator}    @{indexes}

 

根據Index從List選擇

    [Arguments]    ${locator}    @{indexes}

    Select From List By Index    ${locator}    @{indexes}

 

根據Label從List中取消選項

    [Arguments]    ${locator}    @{Labels}

    Unselect From List By Label    ${locator}    @{Labels}

 

根據Label從List選擇

    [Arguments]    ${locator}    @{labels}

    Select From List By Label    ${locator}    @{labels}

 

根據Value從List中取消選項

    [Arguments]    ${locator}    @{values}

    Unselect From List By Value    ${locator}    @{values}

 

根據Value從List選擇

    [Arguments]    ${locator}    @{values}

    Select From List By Value    ${locator}    @{values}

 

根據值選擇列表元素

    [Arguments]    ${locator}    ${value}

    Select From List By Value    ${locator}    ${value}

 

模擬

    [Arguments]    ${locator}    ${event}

    Simulate    ${locator}    ${event}

 

注冊失敗后運行的關鍵字

    [Arguments]    ${kwd}

    Register Keyword To Run On Failure    ${kwd}

 

添加cookie

    [Arguments]    ${name}    ${value}    ${path}=    ${domain}=    ${secure}=    ${expiry}=

 

添加本地策略

    [Arguments]    ${strategy_name}    ${strategy_keyword}    ${persist}=False

    Add Location Strategy    ${strategy_name}    ${strategy_keyword}    ${persist}

 

清除元素文本

    [Arguments]    ${locator}

    Clear Element Text    ${locator}

 

點擊元素

    [Arguments]    ${path}

    Click Element    ${path}

 

點擊元素坐標

    [Arguments]    ${locator}    ${xoffset}    ${yoffset}

    Click Element At Coordinates    ${locator}    ${xoffset}    ${yoffset}

 

點擊圖片

    [Arguments]    ${locator}

    Click Image    ${locator}

 

點擊按鈕

    [Arguments]    ${path}

    Click Button    ${path}

 

點擊鏈接

    [Arguments]    ${locator}

    Click Link    ${locator}

 

睡眠

    [Arguments]    ${time}

    Sleep    ${time}

 

確認

    Confirm Action

 

窗口列表

    @{list}    List Windows

    [Return]    @{list}

 

等待元素不包含文本

    [Arguments]    ${locator}    ${text}    ${timeout}=    ${error}=

    Wait Until Element Does Not Contain    ${locator}    ${text}    ${timeout}    ${error}

 

等待元素不可見

    [Arguments]    ${locator}    ${timeout}=    ${error}=

    Wait Until Element Is Not Visible    ${locator}    ${timeout}    ${error}

 

等待元素包含文本

    [Arguments]    ${locator}    ${text}    ${timeout}=    ${error}=

    Wait Until Element Contains    ${locator}    ${text}    ${timeout}    ${error}

 

等待元素可用

    [Arguments]    ${locator}    ${timeout}=    ${error}=

    Wait Until Element Is Enabled    ${locator}    ${timeout}    ${error}

 

等待元素可見

    [Arguments]    ${locator}    ${timeout}=30

    Wait Until Element Is Visible    ${locator}    ${timeout}

 

等待執行成功

    [Arguments]    ${timeout}    ${retry}    ${keyword}    @{args}

    Wait Until Keyword Succeeds    ${timeout} minutes    ${retry} sec    ${keyword}    @{args}

 

等待條件成立

    [Arguments]    ${condition}    ${timeout}=    ${error}=

    Wait For Condition    ${condition}    ${timeout}    ${error}

 

等待頁面不包含元素

    [Arguments]    ${locator}    ${timeout}=    ${error}=

    Wait Until Page Does Not Contain Element    ${locator}    ${timeout}    ${error}

 

等待頁面包含元素

    [Arguments]    ${locator}    ${timeout}=    ${error}=

    Wait Until Page Contains Element    ${locator}    ${timeout}    ${error}

 

等待頁面包含文本

    [Arguments]    ${text}    ${timeout}=    ${error}=

    Wait Until Page Contains    ${text}    ${timeout}    ${error}

 

給元素分配ID

    [Arguments]    ${locator}    ${id}

    Assign Id To Element    ${locator}    ${id}

 

獲取Cookie值

    [Arguments]    ${name}

    ${value}    Get Cookie Value    ${name}

    [Return]    ${value}

 

獲取List元素

    [Arguments]    ${locator}

    @{items}    Get List Items    ${locator}

 

獲取List多個選定值

    [Arguments]    ${locator}

    @{values}    Get Selected List Values    ${locator}

    [Return]    @{values}

 

獲取List多個選定標簽

    [Arguments]    ${locator}

    @{labels}    Get Selected List Labels    ${locator}

    [Return]    @{labels}

 

獲取List選定值

    [Arguments]    ${locator}

    ${value}    Get Selected List Value    ${locator}

    [Return]    ${value}

 

獲取List選定標簽

    [Arguments]    ${locator}

    ${label}    Get Selected List Label    ${locator}

    [Return]    ${label}

 

獲取Selenium等待時間

    ${sec}    Get Selenium Implicit Wait

    [Return]    ${sec}

 

獲取Selenium超時

    ${timeout}    Get Selenium Timeout

    [Return]    ${timeout}

 

獲取Selenium速率

    ${res}    Get Selenium Speed

    [Return]    ${res}

 

獲取xpath匹配次數

    [Arguments]    ${xpath}

    ${count}    Get Matching Xpath Count    ${xpath}

    [Return]    ${count}

 

獲取元素值

    [Arguments]    ${locator}

    ${value}    Get Value    ${locator}

    [Return]    ${value}

 

獲取元素屬性

    [Arguments]    ${attribute_locator}

    Get Element Attribute    ${attribute_locator}

 

獲取元素文本

    [Arguments]    ${locator}

    ${text}    Get Text    ${locator}

    [Return]    ${text}

 

獲取坐標

    ${location}    Get Location

    [Return]    ${location}

 

獲取垂直坐標

    [Arguments]    ${locator}

    ${vertical}    Get Vertical Position    ${locator}

    [Return]    ${vertical}

 

獲取當前Url

    ${url}    Log Location

    [Return]    ${url}

 

獲取當前頁面標題

    ${title}    Log Title

    [Return]    ${title}

 

獲取所有Cookie

    @{cookies}    Get Cookies

    [Return]    @{cookies}

 

獲取所有連接

    @{links}    Get All Links

    [Return]    @{links}

 

獲取文本

    [Arguments]    ${path}

    ${text}    get text    ${path}

    [Return]    ${text}

 

獲取標題

    ${title}    get title

    [Return]    ${title}

 

獲取水平坐標

    [Arguments]    ${locator}

    @{position}    Get Horizontal Position    ${locator}

    [Return]    @{position}

 

獲取焦點

    [Arguments]    ${locator}

    Focus    ${locator}

 

獲取窗口名字

    @{names}    Get Window Names

    [Return]    @{names}

 

獲取窗口坐標

    ${x}    ${y}=    Get Window Position

    [Return]    ${x}    ${y}

 

獲取窗口大小

    ${width}    ${higth}=    GET Window Size

    [Return]    ${width}    ${higth}

 

獲取窗口標識

    @{windows}    Get Window Identifiers

    [Return]    @{windows}

 

獲取窗口標題

    @{titles}    Get Window Titles

    [Return]    @{titles}

 

獲取表格內容

    [Arguments]    ${table_locator}    ${row}    ${column}    ${loglevel}=INFO

    ${cell}    Get Table Cell    ${table_locator}    ${row}    ${column}    ${loglevel}

    [Return]    ${cell}

 

獲取警告信息

    ${msg}    Get Alert Message

    [Return]    ${msg}

 

獲取資源

    ${source}    Get Source

    [Return]    ${source}

 

獲取頁面元素

    [Arguments]    ${locator}

    @{elements}    Get Webelements    ${locator}

    [Return]    @{elements}

 

獲取頁面標題

    ${title}    Get Title

    [Return]    ${title}

 

獲取頁面資源

    [Arguments]    ${loglevel}=INFO

    ${source}    Log Source    ${loglevel}

    [Return]    ${source}

 

表頭應該包含

    [Arguments]    ${table_locator}    ${expected}    ${loglevel}=INFO

    table header should contain    ${table_locator}    ${expected}    ${loglevel}

 

表頭應該包含文本

    [Arguments]    ${table_locator}    ${expected}    ${loglevel}=INFO

    Table Header Should Contain    ${table_locator}    ${expected}    ${loglevel}

 

表應該包含文本

    [Arguments]    ${table_locator}    ${expected}    ${loglevel}=INFO

    Table Should Contain    ${table_locator}    ${expected}    ${loglevel}

 

表格Footer應該包含文本

    [Arguments]    ${table_locator}    ${expected}    ${loglevel}

    Table Footer Should Contain    ${table_locator}    ${expected}    ${loglevel}

 

表格列應該包含文本

    [Arguments]    ${table_locator}    ${col}    ${expected}    ${loglevel}=INFO

    Table Column Should Contain    ${table_locator}    ${col}    ${expected}    ${loglevel}

 

表格應該包含

    [Arguments]    ${table_locator}    ${row}    ${column}    ${expected}

    table cell should contain    ${table_locator}    ${row}    ${column}    ${expected}

 

表格應該包含文本

    [Arguments]    ${table_locator}    ${row}    ${column}    ${expected}    ${loglevel}=INFO

    Table Cell Should Contain    ${table_locator}    ${row}    ${column}    ${expected}    ${loglevel}

 

表格行應該包含文本

    [Arguments]    ${table_locator}    ${row}    ${expected}    ${loglvl}=INFO

    Table Row Should Contain    ${table_locator}    ${row}    ${expected}    ${loglvl}

 

設置Selenium等待

    [Arguments]    ${seconds}

    ${return}    Set Selenium Implicit Wait    ${seconds}

    [Return]    ${return}

 

設置Selenium超時

    [Arguments]    ${seconds}

    Set Selenium Timeout    ${seconds}

 

設置Selenium速率

    [Arguments]    ${speed}

    Set Selenium Speed    ${speed} seconds

 

設置瀏覽器等待時間

    [Arguments]    ${seconds}

    Set Browser Implicit Wait    ${seconds}

 

設置窗口坐標

    [Arguments]    ${x}    ${y}

    Set Window Position    ${x}    ${y}

 

設置窗口大小

    [Arguments]    ${width}    ${higth}

    Set Window Size    ${width}    ${higth}

 

跳轉

    [Arguments]    ${url}

    Go To    ${url}

 

跳轉到

    [Arguments]    ${url}

    go to    ${url}

 

輸入密碼

    [Arguments]    ${path}    ${passwd}

    Input password    ${path}    ${passwd}

 

輸入文本

    [Arguments]    ${path}    ${str}

    Input Text    ${path}    ${str}

 

選擇Checkbox

    [Arguments]    ${locator}

    Select Checkbox    ${locator}

 

選擇Frame

    [Arguments]    ${locator}

    Select Frame    ${locator}

 

選擇Radio按鈕

    [Arguments]    ${group_name}    ${value}

    Select Radio Button    ${group_name}    ${value}

 

選擇文件

    [Arguments]    ${locator}    ${file_path}

    Choose File    ${locator}    ${file_path}

 

選擇窗口

    [Arguments]    ${locator}=

    Select Window    ${locator}

 

頁面應該不包含Checkbox

    [Arguments]    ${locator}    ${msg}=    ${loglevel}=INFO

    Page Should Not Contain Checkbox    ${locator}    ${msg}    ${loglevel}

 

頁面應該不包含Link

    [Arguments]    ${locator}    ${msg}=    ${loglevel}=INFO

    Page Should Not Contain Link    ${locator}    ${msg}    ${loglevel}

 

頁面應該不包含List

    [Arguments]    ${locator}    ${msg}=    ${loglevel}=INFO

    Page Should Not Contain List    ${locator}    ${msg}    ${loglevel}

 

頁面應該不包含Radio按鈕

    [Arguments]    ${locator}    ${msg}=    ${loglevel}=INFO

    Page Should Not Contain Radio Button    ${locator}    ${msg}    ${loglevel}

 

頁面應該不包含元素

    [Arguments]    ${locator}    ${msg}=    ${loglevel}=INFO

    Page Should Not Contain Element    ${locator}    ${msg}    ${loglevel}

 

頁面應該不包含圖片

    [Arguments]    ${locator}    ${msg}=    ${loglevel}=INFO

    Page Should Not Contain Image    ${locator}    ${msg}    ${loglevel}

 

頁面應該不包含按鈕

    [Arguments]    ${locator}    ${msg}=    ${loglevel}=INFO

    Page Should Not Contain Button    ${locator}    ${msg}    ${loglevel}

 

頁面應該不包含文本

    [Arguments]    ${text}    ${lvl}=INFO

    Page Should Not Contain    ${text}    ${lvl}

 

頁面應該不包含文本框

    [Arguments]    ${locator}    ${msg}=    ${loglevel}=INFO

    Page Should Not Contain Textfield    ${locator}    ${msg}    ${loglevel}

 

頁面應該包含Checkbox

    [Arguments]    ${locator}    ${msg}=    ${loglevel}=INFO

    Page Should Contain Checkbox    ${locator}    ${msg}    ${loglevel}

 

頁面應該包含Link

    [Arguments]    ${locator}    ${msg}=    ${loglevel}=INFO

    Page Should Contain Link    ${locator}    ${msg}    ${loglevel}

 

頁面應該包含List

    [Arguments]    ${locator}    ${msg}=    ${loglevel}=INFO

    Page Should Contain List    ${locator}    ${msg}    ${loglevel}

 

頁面應該包含Radio按鈕

    [Arguments]    ${locator}    ${msg}=    ${loglevel}=INFO

    Page Should Contain Radio Button    ${locator}    ${msg}    ${loglevel}

 

頁面應該包含元素

    [Arguments]    ${locator}    ${msg}=    ${loglevel}=INFO

    Page Should Contain Element    ${locator}    ${msg}    ${loglevel}

 

頁面應該包含圖片

    [Arguments]    ${locator}    ${msg}=    ${loglevel}=INFO

    Page Should Contain Image    ${locator}    ${msg}    ${loglevel}

 

頁面應該包含按鈕

    [Arguments]    ${locator}    ${msg}=    ${loglevel}=INFO

    Page Should Contain Button    ${locator}    ${msg}    ${loglevel}

 

頁面應該包含文本

    [Arguments]    ${text}    ${loglevel}=INFO

    Page Should Contain    ${text}    ${loglevel}

 

頁面應該包含文本框

    [Arguments]    ${locator}    ${msg}=    ${loglevel}=INFO

    Page Should Contain Textfield    ${locator}    ${msg}    ${loglevel}

 

鼠標懸停

    [Arguments]    ${locator}

    Mouse Over    ${locator}

 

鼠標移出

    [Arguments]    ${locator}

    Mouse Out    ${locator}

 

鼠標釋放

    [Arguments]    ${locator}

Mouse Up    ${locator}

 

 

RFS自動化測試工具安裝與使用總結

一,調試
1,在調試時,總時提示“無法打開瀏覽器”
解決辦法:
1,把瀏覽器的代理關閉
2,把瀏覽器的顯示比例調到100%
3,在IE設置的安全選項中,把啟用保護模式開啟 
4,去除進入系統彈出的登錄提示
   在瀏覽器中---工具---internet選項---安全---把只在intranet自動登錄,改為匿名登錄

二,安裝加載庫
1,RequestsLibrary
先安裝Requests工具,再安裝RequestsLibrary
再在robot中進行加載lib,如果加載的庫還是紅色的,重啟就可以了

三,常用關鍵字
open browser    打開瀏覽器
input text    在文本框中錄入內容
input password    在文本框中錄入密碼
click button    點擊按鈕
page should  (NOT)contain (link,button,element,checkbox,list)   頁面是否出現指定的內容


close browser    關閉瀏覽器

多個打開瀏覽器切換
open browser    http://127.0.0.1:8000/    ie    local    
switch browser    local        

針對頁面彈出窗口的確認鍵
confirm action----類似於點擊彈出窗的確認或OK鍵

alert should be present  當前彈窗的內容   -----判斷當前指定內容的彈窗是否存在

get alert message---獲取彈出窗的信息

${msg}=     get alert message

should be equal as string  ${msg}   *****

在彈窗中選擇取消操作

a,choose cancle on next confirmaction

b,confirm action


設置日志級別
Set Log Level    TRACE

下拉列表
select from list xpath value
select from list by value    xpath value
select from list by index    xpath value

框架
select frame    id=fra
unselect frame

復選框
select checkbox    id=CheckYes
unselect checkbox    id=CheckNo

checkbox should not be selected
select checkbox
checkbox should be selected

單選框
select radio button    radio1---單選框名稱    A--value)

click element    id=registerGenderMale

Oracle數據庫
connect to database using custom params(關鍵字)    cx_Oracle    用戶名,密碼,SID
${row}    row count    select * from 表1;

等待頁面加載
wait until page contains element
sleep
reload page---刷新

注釋
Comment

幾個關鍵字的區別
Wait Until Keyword Succeeds
wait until page contains element
SLEEP
element should contain
page should contain element
page should contain
Element Should Not Be Visible

判斷的使用

判斷條件---數字對比
Run Keyword If    ${res} == 0    執行的關鍵字
... ELSE 執行的關鍵字2

判斷條件---字符對比
Run Keyword If    ‘${res}’ == ‘ttttt'    執行的關鍵字
判斷條件---布爾類型對比
Run Keyword If    '${res}' == '${true}'    執行的關鍵字

HTTP接口
create session    httpserver    http://localhost:8000
${addr}    Get request    httpserver    users/s
should be equal as strings    ${addr.status_code}    200
log    ${addr.content}    
${resp}    tojson    ${addr.content}

四,常見問題解答
1,定位不到元素
Element locator '//input[@id="registerName"]' did not match any elements.
前端代碼
<td>
<div class="c">
<span id="disp_registerName" class="cp edit_hide" style="display: none;"></span>
<input id="registerName" class="pa_ui_text edit_show pa_ui_element_normal" type="text" otitle="車主姓名" value="" name="register.name" style="display: inline;">
</div>
</td>
解決方法:
先從最里面的元素開始一級一級往上找,找到iframe,然后先select frame這個ID就可以定位到里面的元素了。

2,InvalidElementStateException: Message: u'Element must not be hidden, disabled or read-only'
針對根據復選框的狀態,解決文本框中默認帶出內容時。如果這樣寫就會報錯
select checkbox    id=noLicenseFlag
input text    id=registerName    湘A*
像這樣的組合,后面那句input text    id=registerName    湘A*直接刪除就可以了。

3,頁面操作按鈕提示找不到elements
click button id=carInfoSubmitBtn
改為click element id=carInfoSubmitBtn

4,根據錄入框內容彈出框框,定位不到元素
采用相對路徑的方式定位,用firefox瀏覽器打開地址,按F12獲取元素位置,右擊---選擇復制XPATH
放在腳本中形式為:xpath=/html/body/div[9]/div[2]/table/tbody/tr[4]/td[4]

6,InvalidElementStateException: Message: u'Element must not be hidden, disabled or read-only'
出現這個現象是因為腳本執行快,而頁面的元素還沒有加載完成造成的。
等待頁面加載
wait until page contains element或sleep

7,頁面執行時沒有報錯,但一直停在那里了
利用Mouse out 關鍵字,移開鼠標動作。

8,使用close broswer關閉瀏覽器不起作用
首先安裝pywin32-217.win32-py2.7.exe,再安裝AutoItLibrary庫,在close broswer關鍵字
下一行,增加刪除進線的關鍵字: process close iexplore.exe

10,查看所有有關robot相關庫的版本
pip list

11,通過pip升級庫或程序版本
1,python -m pip install --upgrade pip
報錯:
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
2,根據提示,執行命令
python -m pip install --upgrade pip
報錯:Requirement already up-to-date: pip in c:\python27\lib\site-packages
3,找到c:\python27\lib\site-packages下這個目錄pip-8.1.1.dist-info並刪除,重新執行
python -m pip install --upgrade pip
4,查看是否更新成功
pip list后顯示最新版本

12,利用pip更新庫或RIDE或robot
pip install robotframework-ride --upgrade
再用pip list可以看到升級成功的版本號了

13,Run Keyword And return status關鍵字識別不了
利用pip更新RIDE,robot,selenium2Library

14,ImportError:No module named cx_Oracle
根據連接oracle客戶端的版本如:11G,操作系統是32位還是64位
安裝對應的cx_Oracle安裝包,如:cx_Oracle-5.1.2-11g.win32-py2.7

15,Unable to acquire Oracle environment handle
在oracle客戶端的目錄中把oci.dll,oraocci11.dll,oraociei11.dll文件復制到C:\Python27\Lib\site-packages目錄中

16,日志目錄
Log:     d:\users\liujun~1\appdata\local\temp\RIDEwzyfth.d\log.html
Report:  d:\users\liujun~1\appdata\local\temp\RIDEwzyfth.d\report.html

17,在IE中如果click不起作用,用如下關鍵字
press key id=kw    \\13

18,單選框實例
<p class="checkBox">
<input id="registerGenderMale" class="pa_ui_element_normal" type="radio" value="M" name="register.gender" otitle="車主-性別男">
<label class="radiotxt" hidefocus="hidefoces" for="registerGenderMale">男</label>
<input id="registerGenderFemale" class="pa_ui_element_normal" type="radio" value="F" name="register.gender" otitle="車主-性別女">
<label class="radiotxt" hidefocus="hidefoces" for="registerGenderFemale">女</label>
</p>
select radio button    register.gender    M

19,下拉列表實例
<div class="pa_ui_dropselect_inner paui-clearfix">
<span class="pa_ui_dropselect_input_Container" style="width: 71px;">
<input class="pa_ui_dropselect_input" type="text" readonly="readonly" otitle="V3版-送單地址-縣級市" style="width: 71px; opacity: 1;">
</span>
<span class="pa_ui_dropselect_button" otitle="V3版-送單地址-縣級市">
</div>
select from list class="pa_ui_dropselect_inner paui-clearfix" 江北區

 

20,通過PIP安裝Selenium2Library報錯
C:\Users\Administrator>pip install Selenium2Library
Collecting Selenium2Library
  Could not find a version that satisfies the requir
 versions: )
No matching distribution found for Selenium2Library

解決辦法:
pip install robotframework-Selenium2Library
就可以了

21,安裝IEDriver報錯
WebDriverException: Message: 'IEDriverServer.exe' executable needs to be in PATH. Please download fromhttp://selenium-release.storage.googleapis.com/index.html and read up at https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver
解決辦法:
下載對應系統版本的IEDriverServer.exe放在IE的安裝目錄下和Python的根目錄下,並設置PATH。

22,滾動鼠標拖動滾動條到最頂部或最底部
execute javascript id.scrollTop其中id為滾動條區域對應的id,用法:
如:execute javascript J_introCon.scrollTop=0是拖到最頂部,execute javascript J_introCon.scrollTop=100000是拖到最底部

 

23,在CASE中的setup中可以設置運行CASE前,運行SETUP里面的關鍵字。teardown中可以設置運行CASE后,指定要運行的關鍵字

 

24,滿足條件結束腳本
exit for loop
如:如果count值為0,則結束CASE
run keyword if ${count}==0 exit for loop

 

25,執行SQL時報錯:DatabaseError:ORA-00911:Invalid character
去掉SQL后面的 ; 

 

26,執行SQL查詢
Execute sql String  select * from 表名

 

假設環境已經搭建好了。這里用RIDE( Robot Framework Test Data Editor)工具來編寫用例。下面我們對Robot Framework簡稱rf。

我們先考慮下一個最基本的登錄功能的測試用例。

一、自動化測試人工測試

在開始編寫用例之前,我們先來思考下自動化測試和人工測試的區別。對於web頁面的人工測試,我們想下,如果去測試,怎么操作呢?不外乎如下的基本動作:

1)打開瀏覽器

2)輸入url (前提web服務器要正常啟動運行着)

3)等待頁面顯示出來

4)用眼睛看頁面顯示的內容是否與自己想象的一致,如果一致,認為功能正常,否則,會認為程序有問題。

5)通過鼠標、鍵盤執行相關的操作,通過頁面的變化和內容顯示繼續進行檢查功能是否正常。

那么什么是自動化測試呢?其本質就是將人的操作過程(打開瀏覽器、輸入url、鼠標點擊、鍵盤輸入等)以及驗收標准(在人腦中驗收)轉換為測試代碼。

有了測試代碼,就可以讓其自動運行了。

二、登錄用例設計

一個登錄功能,想象下如果是人工測試,那基本的測試過程一般如下:

1)打開瀏覽器、輸入登錄url

2)輸入用戶名、密碼(也許還有別的輸入項,如驗證碼,則取決於程序本身),點擊登錄按鈕

3)如果是正確的用戶名密碼,應該出來相應的頁面;如果是錯誤的,應該出來錯誤頁面或錯誤提示信息。

那我們看看利用Robot Framework怎么寫用例。

三、用例編寫

1、成功登陸的用例1

successLogin
    open browser    http://localhost/nau/login    ff
    input text    id=userid    xxx
    input password    id=password    yyy
    click button    id=loginBtn
    Wait Until Element Is Visible    id=userid
    close browser

下面我們看下上面代碼的含義

1)successLogin是用例名,是自己取的

2)后面的語句,每句都是 “關鍵字+參數(0個或多個)”的格式,其中藍色的詞組就是關鍵字。這個例子的關鍵字是rf框架中的內置關鍵字。

用戶可以定義自己的關鍵字。

open browser    http://localhost/nau/login    ff

這句代碼的含義,其實看上去就能理解。open browser是關鍵字,表示打開瀏覽器;http://localhost/nau/login 是第一個參數,是要打開的頁面url;ff是第二個參數,代表要用的瀏覽器類型,其中ff表示是firefox瀏覽器,gc表示是chrome瀏覽器,ie表示是Internet Explorer瀏覽器。這里我們用的是firefox瀏覽器。

input text    id=userid    xxx

這句代碼,input text  是關鍵字,表示要在html組件(如文本框)中輸入信息, id=userid 是 第一個參數,用於定位用來輸入的html組件,這里的id表示通過元素的id來定位,userid就是元素的id值。如果頁面中存在一個id為userid的輸入框,則就能找到。 xxx是第二個參數,表示要輸入的值。想象下,如果是人工操作,就是在界面中找到這個輸入框,敲擊鍵盤輸入xxxx這幾個字符。

看到這里,我們可以把關鍵字當作一個函數(其實它本質上就是一個函數),后面跟的是函數參數,有的關鍵字有參數,有的沒有。

input password    id=password    yyy

這句代碼,也非常好理解了,就是在密碼框中輸入密碼了。其中input password為關鍵字。

click button    id=loginBtn

這句代碼,看上去也明白了,click button 是關鍵字,表示點擊按鈕;id=loginBtn是第一個參數,用於定位要點擊的按鈕,這里也是用id來定位的。

Wait Until Element Is Visible    id=userid

前面的幾句代碼,進行的相關的操作,這句代碼就是檢查操作結果。如果登錄成功,會出現新的頁面,並且頁面上應該有個元素會顯示用戶的登錄名。

這里的Wait Until Element Is Visible  是關鍵字,顧名思義,就是等待元素可見; id=userid就是要顯示的元素,這里同樣是通過id定位。

實際上這個檢查是不完善的,這里只是檢查了是否有id為userid的元素,但元素的內容呢(正常內容應該是xxx),沒有檢查。這點我么在下個用例介紹。

close browser

上面是這個用例的最后一個語句。這很好理解了,就是關閉瀏覽器。close browser是關鍵字,該關鍵字沒有參數。

執行該用例。如果系統存在用戶名為xxx和密碼為yyy的用戶,則該用例就會成功。

 

2、成功登陸用例2

上面是一個最基本的用例。上面用例存在一個問題,就是檢查用例成功的標准是看希望的html元素是否存在。但很多時候不僅需要判斷希望的元素是否存在,還需要判斷元素的內容是否符合預期。就上面這個用例,希望 id為userid的元素的內容是xxx。那用例應該如何寫呢?

successLogin2
    open browser    http://localhost/nau/login    ff
    input text    id=userid    xxx
    input password    id=password    yyy
    click button    id=loginBtn
    ${value}    get text    id=userid
    Should Be Equal    ${value}   xxx
    close browser

和上個用例相比,區別就是檢查語句(上面紅色的2行)。

其中${value}    get text    id=userid的含義是 利用關鍵字 get text   獲取id為userid元素的內容放到變量${value}中。

而 Should Be Equal    ${value}   xxx 語句,Should Be Equal  是關鍵字,用來斷言兩個參數的值是否相等,如果不等,則表示失敗。

3、失敗登錄用例

上面是驗證成功登錄的用例。那應該也有驗證登錄失敗的用例。其思路差不多,就是當登錄失敗后的頁面是啥。通過代碼來進行檢查。

我們假設登錄失敗后會出現一個新的頁面,頁面中有文本 “用戶名或密碼錯誤”字樣。那么用例就可以這么寫。

errorLogin
    open browser    http://localhost/nau/login    ff
    input text    id=userid    xxx
    input password    id=password    yyy123
    click button    id=loginBtn
    Wait Until Page Contains    用戶名或密碼錯誤
    close browser

相比前面的用例,區別還是驗收語句。上面的驗收語句一看就明白,Wait Until Page Contains    是關鍵字,用於檢查頁面中是否包含預期的信息。

4、自定義關鍵字

可以看出,上面3個用例,前面的4個語句,區別只是輸入的參數 用戶名和密碼的值區別,我們自然會想到,可以把這4個語句封裝成一個關鍵字,包含兩個參數用戶名和密碼。

關鍵字定義如下:

login
    [Arguments]    ${username}    ${password}
    open browser    http://localhost/boot/login    ff
    input text    id=userid    ${username}
    input password    id=password    ${password}
    click button    id=loginBtn

使用該關鍵字的用例如下

loginSuccessWithKeywords
    login   xxx  yyy
    ${value}    get text    id=userid
    Should Be Equal    ${value}    xxx
    close browser

loginErrorWithKeywords
    login   xxx  yyy123
    Wait Until Page Contains    用戶名或密碼錯誤
    close browser

5、用例的setupteardown

每個用例,從整個實現講。一般包括如下三大部分:

1)執行條件設置部分(setup部分):因為用例的執行,通常需要一些條件。如前面的登錄成功用例,需要用戶已經存在。

2)用例執行部分:包括該用例進行的操作,對結果的驗證

3)清理部分(teardown部分):在自動化測試中,我們希望用例執行后,對當前環境不會有任何破壞。因此一般需要做些清理工作,包括第一部分條件設置對系統造成的影響和用例執行過程中對系統造成的影響。

rf對這也提供了支持,可以把一個用例分成三部分。雖然我們寫成一部分,從最終效果上講沒區別。但分開了,會更加符合邏輯,用例也非常清楚。

對於我們這個登錄用例,可以看出,每個用例完成的最后一步都要關閉瀏覽器,這里用的是close browser。

因為各個用例都是公共的,我們可以把這放到用例包的teardown中。

*** Settings ***
Test Teardown     close browser

這樣每個用例的最后語句close browser都不需要了。

把一些語句放到teardown中,還有一個好處是,無論用例執行部分出現什么問題,最后的teardown都會保證被執行。

 

三、小結

本篇文章,我們通過一個登錄例子來介紹了rf在web界面自動化測試上的基本應用。雖然比較簡單,但已經把用例編寫最核心的概念和步驟做了介紹。其它功能的用例與之區別在於用例的復雜程度區別(比如准備測試條件、用例執行步驟、結果驗證的復雜性、用例執行影響的清理)。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2024 CODEPRJ.COM