目錄
Example3 Mail126.robot:測試文件,存放測試用例
本篇介紹3個使用RF進行web測試的示例,滿滿的乾貨。執行的過程不方便截圖了,只貼出代碼供大家參考吧,希望對大家有幫助~
代碼示例1:百度搜索
我們先以簡單的百度搜索爲例來說明Selenium2Library一些常用關鍵字的使用方法。和selenium進行自動化的流程類似,過程如下:
- 打開一個指定的瀏覽器,例如chrome、IE、firefox等等;
- 定位到輸入框,可以是id,name,xpath等方式;
- 輸入要搜索的內容
- 定位到搜索按鈕並單擊
- 斷言
接下來我們用robot framework按以上流程編寫腳本
代碼如下:
*** Settings ***
Library Selenium2Library
*** Test Cases ***
baidu
Comment 用Chrome瀏覽器訪問百度
Open Browser http://www.baidu.com Chrome
Comment 使用d定位到輸入框,輸入gloryroad
Input Text //*[@id='kw'] gloryroad
Comment 使用id定位到搜索框並單擊
Click Element //*[@id='su']
Comment 等待2描述
sleep 2
Comment 斷言網頁的標題變爲gloryroad_百度搜索
Title Should Be gloryroad_百度搜索
Comment 關閉瀏覽器
Close Browser
代碼示例2:126郵箱登錄、新建聯繫人、發郵件
*** Settings ***
Suite Teardown Close Browser
Library Selenium2Library
*** Test Cases ***
Loign
Comment 打開Chrome瀏覽器,訪問http://mail.126.com
Open Browser http://mail.126.com chrome
Comment 等待5秒
sleep 5
Comment 瀏覽器窗口最大化
Maximize Browser Window
Comment 切換到登錄rame
Select Frame //*[contains(@id,"x-URS-iframe")]
Comment 定位輸入框並輸入用戶名
Input Text //input[@name="email" and @tabindex="1"] testman1980
Comment 定位密碼輸入框並輸入密碼
Input password //*[@name='password'] wulaoshi1978
Comment 單擊登錄按鈕
click element //*[@id='dologin']
Comment 等待2秒
sleep 2
Comment 斷言頁面源碼中包含關鍵字"退出"
page should contain 退出
Add Contact Person
Comment 單擊通訊錄標籤
click element //div[text()="通訊錄"]
Sleep 1
Comment 單擊新建聯繫人標籤
click element //span[.='新建聯繫人']
Comment 輸入聯繫人姓名
Input Text //a[@title="編輯詳細姓名"]/preceding-sibling::div/input xiaoxiao
Comment 輸入聯繫人郵箱
Input Text //*[@id="iaddress_MAIL_wrap"]//input [email protected]
Comment 設爲星標聯繫人
click element //span[text()="設爲星標聯繫人"]/preceding-sibling::span/b
Comment 輸入手機號碼
Input Text //div[.="手機號碼"]/following::input[1] 12345678909
Comment 輸入備註信息
Input Text //textarea 學生
Comment 單擊確定按鈕
click element //span[.="確 定"]
Sleep 1
Comment 斷言頁面源碼中包含關鍵字"退出"
page should contain 退出
Send Email
Comment 等待1秒
Sleep 1
Comment 單擊首頁標籤
click element //div[text()="首頁"]
Comment 單擊寫信按鈕
click element //span[.="寫 信"]
Comment 輸入聯繫人郵箱
Input Text //div[@title="發給多人時地址請以分號隔開"]//input [email protected]
Comment 輸入主題
Input Text //div[@aria-label="郵件主題輸入框,請輸入郵件主題"]/input 測試結果
Comment 上傳文件
choose file //div[contains(@title, "600首MP3")]/input F:\\PBKDF.txt
Comment 顯示等待直到出現"上傳完成"
Wait Until Element Is Visible //span[text()="上傳完成"]
Comment 切換到郵件正文rame
Select Frame //iframe[@class="APP-editor-iframe"]
Comment 輸入郵件正文內容
Input Text //html/body 每天進步一點點
Comment 切換到默認的rame
Unselect Frame
Comment 單擊發送按鈕
click element //div[@class="nui-toolbar-item"]//span[.="發送"]
Comment 等待3秒
Sleep 3
Comment 斷言頁面源碼中包含"發送成功"
${html} Get Source
Should Contain ${html} 發送成功
代碼示例3:封裝爲關鍵字並使用數據驅動
示例2中的代碼都是面向過程的,如果操作的數據變了之後那麼所有的代碼要複製粘貼一般,把數據修改再運行,這肯定不是我們想要的。造成這種現象的原因就是:
- 代碼中操作的數據都是寫死的,不能根據實際的需要靈活的變化。解決方法就是所有就需要把動作抽象爲一個函數,這樣可以反覆的調用;把變化的值抽象爲函數的參數,每次調用都操作不同的數據;
- 定位表達式也是寫死的,如果定位表達式有變化的話就必須修改代碼。解決方法:將定位表達式抽象出來放到一個資源文件中,這樣每次只維護這個資源文件即可。
- 測試case,變量還有操作值都放到一起,耦合性太高。解決方法:分而治之,也就是封裝的思想,測試case,公共函數,資源文件分開存放。
最終的代碼結構如下:
ObjectDeposit.txt:定位表達式的配置文件
將每個頁面抽象爲一個字典,頁面中的元素作爲字典中的key,元素的定位方式作爲value
*** Variables ***
#登錄頁面
&{loginPage} frame=//iframe[contains(@id,"x-URS-iframe")] userName=//input[@name="email"] passWord=//input[@name="password"] loginBtn=dologin asserContent=//div[text()="通訊錄"]
#首頁
&{homePage} addressBook=//div[text()="通訊錄"] homePage=//div[text()="首頁"]
#聯繫人頁面
&{addcontactsPage} createContactsBtn=//span[.="新建聯繫人"] contactPersonName=//a[@title="編輯詳細姓名"]/preceding-sibling::div/input contactPersonEmail=//*[@id="iaddress_MAIL_wrap"]//input starContacts=//span[text()="設爲星標聯繫人"]/preceding-sibling::span/b contactPersonMobile=//div[.="手機號碼"]/following::input[1] contactPersonComment=//textarea saveContacePerson=//span[.="確 定"]
#寫信頁面
&{sendmailsPage} writeEmailBtn=//span[.="寫 信"] recivePerson=//div[@title="發給多人時地址請以分號隔開"]//input subject=//div[@aria-label="郵件主題輸入框,請輸入郵件主題"]/input addAttachmentBtn=//div[contains(@title, "600首MP3")]/input asserContent=//span[text()="上傳完成"] mailBodyFrame=//iframe[@class="APP-editor-iframe"] mailBody=//html/body
... sendBtn=//div[@class="nui-toolbar-item"]//span[.="發送"]
public.robot:存放自定義的公共關鍵字
*** Settings ***
Library Selenium2Library
Resource ObjectDeposit.txt # 定位表達式的配置文件
*** Keywords ***
Login
[Arguments] ${user name} ${password}
Log //************Login
Comment 打開Chrome瀏覽器,訪問http://mail.126.com
Open Browser http://mail.126.com chrome
Comment 等待5秒
sleep 5
Comment 瀏覽器窗口最大化
Maximize Browser Window
Comment 切換到登錄rame
Select Frame ${loginPage.frame}
Comment 定位輸入框並輸入用戶名
Input Text ${loginPage.userName} ${user name}
Comment 定位密碼輸入框並輸入密碼
Input password ${loginPage.passWord} ${password}
Comment 單擊登錄按鈕
click element ${loginPage.loginBtn}
Comment 等待2秒
sleep 2
Comment 斷言頁面源碼中包含關鍵字"退出"
page should contain 退出
Add Contact Person
[Arguments] ${contacter} ${email} ${phone} ${comment}
Log //************Add Contact Person
Comment 單擊通訊錄標籤
click element ${homePage.addressBook}
Sleep 1
Comment 單擊新建聯繫人標籤
click element ${addcontactsPage.createContactsBtn}
Comment 輸入聯繫人姓名
Input Text ${addcontactsPage.contactPersonName} ${contacter}
Comment 輸入聯繫人郵箱
Input Text ${addcontactsPage.contactPersonEmail} ${email}
Comment 設爲星標聯繫人
click element ${addcontactsPage.starContacts}
Comment 輸入手機號碼
Input Text ${addcontactsPage.contactPersonMobile} ${phone}
Comment 輸入備註信息
Input Text ${addcontactsPage.contactPersonComment} ${comment}
Comment 單擊確定按鈕
click element ${addcontactsPage.saveContacePerson}
Sleep 1
Comment 斷言頁面源碼中包含關鍵字"退出"
page should contain 退出
Send Email
[Arguments] ${reciver} ${subject} ${filepath} ${content}
Log //************Send Email
Comment 等待1秒
Sleep 1
Comment 回到首頁
click element ${homePage.homePage}
Comment 單擊寫信按鈕
Wait Until Element Is Visible ${sendmailsPage.writeEmailBtn}
click element ${sendmailsPage.writeEmailBtn}
Comment 輸入聯繫人郵箱
Input Text ${sendmailsPage.recivePerson} ${reciver}
Comment 輸入主題
Input Text ${sendmailsPage.subject} ${subject}
Sleep 5
Comment 上傳文件
choose file ${sendmailsPage.addAttachmentBtn} ${filepath}
Comment 顯示等待直到出現"上傳完成"
Wait Until Element Is Visible ${sendmailsPage.asserContent}
Comment 切換到郵件正文rame
Select Frame ${sendmailsPage.mailBodyFrame}
Comment 輸入郵件正文內容
Input Text ${sendmailsPage.mailBody} ${content}
Comment 切換到默認的rame
Unselect Frame
Comment 單擊發送按鈕
click element ${sendmailsPage.sendBtn}
Comment 等待3秒
Sleep 3
Comment 斷言頁面源碼中包含"發送成功"
${html} Get Source
Should Contain ${html} 發送成功
Example3 Mail126.robot:測試文件,存放測試用例
*** Settings ***
Suite Teardown Close Browser
Resource ObjectDeposit.txt # 定位表達式的配置文件
Resource public.robot # 封裝好的公共關鍵字
*** Test Cases ***
test
Login testman1980 wulaoshi1978
Add Contact Person xiaoxiao [email protected] 12345678909 學生
Send Email [email protected] 測試結果 F:\\PBKDF.txt 每天進步一點點