[英]What is a keyword in Robot Framework?
我在任何地方都找不到解釋。 即使在介紹關鍵字的教程中,也沒有解釋我將如何從該概念中受益。
因此,為了明確我的問題:假設我有一個客戶端和一個我編程的服務器。 我想使用 Robot Framework 來測試它們是否協同工作。 “關鍵字”在哪里適合這里? 這可以用簡單抽象的測試術語來解釋嗎?
PS:非常感謝解釋其他概念,例如庫和變量(盡管它們看起來很明顯)。
將關鍵字視為單個測試步驟。 正如測試在概念上由許多步驟組成一樣,機器人測試由許多關鍵字組成。 關鍵字是構建所有機器人測試的基礎。
有機器人提供的通用關鍵字,也有您可以自己創建的專用關鍵字。 機器人框架的真正威力在於當您創建自己的關鍵字時,測試可以專注於測試邏輯而不是底層實現。
例如,讓我們考慮登錄到您的服務器的驗收測試可能是什么。 從敏捷產品所有者或首席設計師的角度來看,它可能如下所示:
這可能是產品所有者在故事卡或票證跟蹤系統中添加的驗收標准。 如果這是一個有人可以運行的實際測試,那不是很好嗎?
這些步驟中的每一個都可以被視為一個關鍵字。 機器人的一大優點是您可以編寫一個看起來與原始規范幾乎相同的測試:
*** Test Cases ***
Login of an existing customer
[Setup] Open a browser to Super Website 2000!
[Teardown] close all browser windows
Enter a valid username
Enter a valid password
Click the GO button
You should be on the dashboard page
為了運行這個測試用例,你需要定義這些關鍵字,因為機器人不知道“打開瀏覽器到超級網站 2000!”是什么意思。 方法。 您可以使用 python 或其他幾種語言編寫它們,也可以通過組合現有關鍵字來編寫它們。
例如,前幾個關鍵字可以使用Selenium2Library關鍵字實現,如下所示:
*** Settings ***
Library Selenium2Library
*** Variables ***
${ROOT} http://super.website2000.com
${BROWSER} chrome
*** Keywords ***
Open a browser to Super Website 2000!
# this is a pre-defined Selenium2Library keyword
Open browser ${ROOT} ${BROWSER}
Enter a valid username
# these are pre-defined Selenium2Library keywords
wait until element is visible id=username_input
input text id=username_input Test User #1
Enter a valid password
# these are pre-defined Selenium2Library keywords
wait until element is visible id=password_input
input text id=password_input LetMeIn!
如您所見,您可以使用關鍵字來制作非常易讀的測試用例。 可以使用其他關鍵字設計關鍵字,也可以使用編程語言編寫關鍵字。
當然,您不必像這樣編寫關鍵字。 您可以在測試中直接使用 Selenium2Library 關鍵字,這將使您的測試看起來像這樣:
*** Test Cases ***
Login of an existing customer
[Setup] Open browser ${ROOT} ${BROWSER}
[Teardown] close all browsers
wait until element is visible id=username_input
input text id=username_input Test User #1
wait until element is visible id=password_input
input text id=password_input LetMeIn!
wait until element is enabled id=submit_button
click button id=submit_button
wait until element is visible id=//div[@class='dashboard']
location should be ${ROOT}/dashboard
我個人認為測試的第一個版本更具可讀性,代價是必須維護一些自定義關鍵字。
通過使用關鍵字,您可以隱藏網頁實現的細節,以便您可以專注於測試的邏輯。 另外,多個測試可以重用相同的關鍵字。
實現的實際細節(元素 ID、URL 等)都可以嵌入到關鍵字中。 如果這些細節發生變化,您不必更改任何測試用例。 相反,您更改關鍵字,您的測試將繼續運行。 想象一下,如果開發人員將輸入的 id 更改為username_form_field
和password_form_field
-- 您是要編輯每個必須登錄的測試用例,還是要編輯所有測試共享的一兩個關鍵字?
機器人框架中的變量非常強大。 例如,您可以在一個位置定義站點的根 URL,而不是在每個測試中對其進行硬編碼。 對於大多數生產站點,您必須使用兩個、三個甚至更多 URL 運行測試。 例如,您可能有一個本地開發箱、一個 qa 箱、一個暫存箱和一個生產箱。
Robot 允許您覆蓋命令行或參數文件中的變量。 這意味着您可以創建一組適用於多個系統的測試。 例如,要在登台時使用 firefox 運行測試,您可以這樣做(為了清楚起見,分成多行):
$ pybot \
--variable ROOT:http://staging.example.com \
--variable BROWSER:firefox \
/path/to/tests
要使用 chrome 在 QA1 上運行完全相同的測試,您可以這樣做:
$ pybot \
--variable ROOT:http://qa1.example.com \
--variable BROWSER:chrome \
/path/to/tests
簡而言之,關鍵字被組織到庫中。 Robot 帶有許多庫,互聯網上還有更多庫。
庫可以像這些示例中那樣用機器人語法編寫,但庫也可以用 Python 和 java 等編程語言編寫。 使用編程語言可以完成復雜的邏輯,使用機器人語言可以讓您更輕松地將現有關鍵字組合成新的關鍵字。
如果您在 Scrum 團隊中工作,關鍵字驅動的方法可以幫助團隊變得非常高效。 例如,如果您的測試人員技能不高,開發人員可以創建一個關鍵字庫與測試交互,這樣測試人員就不必擔心頁面的細節。
另一方面,如果您有技術含量高的測試人員,他們可以自己承擔編寫關鍵字的任務,以便開發人員可以將更多時間花在實際產品上。
在這兩種情況下,關鍵字驅動的方法使 qa 和開發團隊能夠協同工作以創建高質量的產品。
另一個答案非常好,就問題的主體而言——對於用戶驗收測試、行為驅動測試,絕對如此。
我想給出一個稍微不同的角度 - 關鍵字類似於軟件編程中的函數/方法。
在與開發人員或其他技術人員交談時,它總是幫助我說“關鍵字只是功能”,這將為深入交談設置正確的上下文。 為了搭載上述示例之一,這個 Robotframework 關鍵字:
Enter a valid username
# these are pre-defined Selenium2Library keywords
wait until element is visible id=username_input
input text id=username_input Test User #1
看起來與 POM 中的 python 方法(或 java、c# 或 - 你命名)幾乎相同:
def enter_a_valid_username(self):
self.driver_instance.wait_until_element_is_visible('id=username')
self.driver_instance.input_text('id=username_input', 'Test User #1')
將值傳遞給關鍵字是調用帶參數的函數,因此返回值:
Attempt to login with user and password
[Documentation] Enters the provided user and password, clicks the "Login" button, and returns boolean True/False is the user logged in.
[Arguments] ${user} ${pass}
wait until element is visible id=username_input
input text id=username_input ${user}
input text id=password_input ${pass}
click button id=submit_button
${success}= Run Keyword And Return Status Dashboard Page Should Be Opened
[Return] ${success}
模擬作為一種方法:
def attempt_login(self, user, pass):
self.driver_instance.wait_until_element_is_visible('id=username')
self.driver_instance.input_text('id=username_input', user)
self.driver_instance.input_text('id=password_input', pass)
self.driver_instance.click_button('id=submit_button')
try:
self.dashboard_is_opened()
return True
except IncorrectPageException:
return False
需要強調的是,Robotframework 語法中創建的關鍵字是函數,而不是方法——它們不是存儲狀態的對象的一部分,因此它們的交叉通信是通過當前范圍內的共享變量; 這推動了程序化,而不是面向對象的編程。
當關鍵字進入更高級別的抽象時,Robotframework 的美麗真正閃耀 - 例如頂級關鍵字The account is terminated
在其實現中調用了關鍵字The account is not present in the Users page
並且No DB record for the account
,它們中的每個都有較低級別和較低級別的關鍵字調用。
The account is terminated
|
\--> | The account is not present in the Users page
| \--> | Go to the Users page
| \--> | ${location}= Get Location
| | | Run Keyword If '${location}' != '${url users page}' Go To '${url users page}'
| | | The Users Page Is Opened
| | \--> | # checks the currently opened page is Users
| | | ...
| | ${users}= Get All Users
| | ...
|
\--> | No DB record for the account
| ${users}= Get All DB Accounts
| \--> | Connect To The DB
| | ${DB data}= Execute Query SELECT * FROM users; # etc
| | ...
| Should Not Contain ${users} ${the user} msg=The user is still present!
這極大地有助於可維護性、實現更改和調試,同時頂級用法保持不變; 這種方法也是良好軟件設計的一個屬性,有助於將類比關鍵字是功能傳遞給軟件開發人員。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.