測試環境為 CentOS 8 x86_64 (虛擬機)
透過 Robot Framework 做自動化跟一般透過 Python 套件來做的差別在於不懂 Python 程式的人也可以使用,只要會編寫 Robot Framework 設定檔就可以跑自動化測試.
但 Robot Framework 有支援哪種類的自動化測試 LIBRARIES 可以查看 – https://robotframework.org/#resources 其中的.
- SeleniumLibrary 用於測試網頁
- SSHLibrary 用於可以透過 SSH 連線進行測試,下面來說明.
所需套件
[root@localhost ~]# pip install robotframework [root@localhost ~]# pip install robotframework-sshlibrary
其中 robotframework-sshlibrary – https://github.com/robotframework/SSHLibrary 會安裝 scp, paramiko – https://benjr.tw/105529 與 Cryptography modules (如 paramiko 版本小於 2.0 時則安裝 PyCrypto ).
編寫 Robot Framework 設定檔
[root@localhost ~]# vi ssh-sample.robot *** Settings *** Documentation This example demonstrates executing a command on a remote machine ... and getting its output. ... ... Notice how connections are handled as part of the suite setup and ... teardown. This saves some time when executing several test cases. Library SSHLibrary Suite Setup Open Connection And Log In Suite Teardown Close All Connections *** Variables *** ${HOST} localhost ${USERNAME} root ${PASSWORD} 111111 *** Test Cases *** Execute Command And Verify Output [Documentation] Execute Command can be used to run commands on the remote machine. ... The keyword returns the standard output by default. ${output}= Execute Command echo Hello SSHLibrary! Should Be Equal ${output} Hello SSHLibrary! *** Keywords *** Open Connection And Log In Open Connection ${HOST} Login ${USERNAME} ${PASSWORD}
程式說明:
Robot Framework 使用預先定義好的 Keywords 關鍵字來執行各種行為 除了 build-in 外還有 LIBRARIES 的 Keywords.
- Build-in keyword , 詳細請參考 https://robotframework.org/robotframework/latest/libraries/BuiltIn.html 說明.
- SSHLibrary Keywords , 詳細請參考 http://robotframework.org/SSHLibrary/SSHLibrary.html 說明.
Settings
*** Settings *** Documentation This example demonstrates executing a command on a remote machine ... and getting its output. ... ... Notice how connections are handled as part of the suite setup and ... teardown. This saves some time when executing several test cases. Library SSHLibrary Suite Setup Open Connection And Log In Suite Teardown Close All Connections
- Documentation
文件說明 - Library
使用那些 Librarys - Suite Setup
在 Test Cases 開始前要做的動作. 動作為 Open Connection And Log In 定義在自訂的 Keywords 裡面,其中的 Open Connection 與 Login 是 SSHLibrary 預先定義好的 Keywords.*** Keywords *** Open Connection And Log In Open Connection ${HOST} Login ${USERNAME} ${PASSWORD}
- Suite Teardown
在 Test Cases 結束後要做的動作. Close All Connections 一樣是 SSHLibrary 預先定義好的 Keywords
Variables
*** Variables *** ${HOST} localhost ${USERNAME} root ${PASSWORD} 111111
變數,請自行填入相對應要登錄的 SSH 資料.
Test Cases
*** Test Cases *** Execute Command And Verify Output [Documentation] Execute Command can be used to run commands on the remote machine. ... The keyword returns the standard output by default. ${output}= Execute Command echo Hello SSHLibrary! Should Be Equal ${output} Hello SSHLibrary!
透過 SSHLibrary 預設關鍵字 Execute Command 執行(並等待回傳值) echo Hello SSHLibrary! 指令.
${output}= Execute Command echo Hello SSHLibrary!
檢查執行的預期結果.
Should Be Equal ${output} Hello SSHLibrary!
Should Be Equal 是 Build-In keyword 關鍵字,除了 Should Be Equal 外還有以以下 Should Contain , Should Be Empty , Should Be Equal As Integers 等..
其中的字串比對 Pattern matching 使用 fnmatch 來定義,請參考 – https://docs.python.org/3/library/fnmatch.html
- *
matches everything - ?
matches any single character - [seq]
matches any character in seq - [!seq]
matches any character not in seq
Keywords
這邊就是可自行定義關鍵字可供呼叫,其中的 Open Connection And Log In 在 Settings 的 Suite Setup 呼叫過.
*** Keywords *** Open Connection And Log In Open Connection ${HOST} Login ${USERNAME} ${PASSWORD}
執行結果.
[root@localhost ~]# robot ssh-sample.robot /usr/local/lib/python3.6/site-packages/paramiko/transport.py:32: CryptographyDeprecationWarning: Python 3.6 is no longer supported by the Python core team. Therefore, support for it is deprecated in cryptography. The next release of cryptography (40.0) will be the last to support Python 3.6. from cryptography.hazmat.backends import default_backend ============================================================================== Ssh-Sample :: This example demonstrates executing a command on a remote mac... ============================================================================== Execute Command And Verify Output :: Execute Command can be used t... | PASS | ------------------------------------------------------------------------------ Ssh-Sample :: This example demonstrates executing a command on a r... | PASS | 1 test, 1 passed, 0 failed ============================================================================== Output: /root/output.xml Log: /root/log.html Report: /root/report.html
Python paramiko
相較於透過 Python 的 paramiko , 使用 Robot Framework 做自動化連不懂 Python 程式的人也可以使用.
[root@localhost ~]# python3 Python 3.6.8 (default, Mar 25 2022, 11:15:52) [GCC 8.5.0 20210514 (Red Hat 8.5.0-10)] on linux Type "help", "copyright", "credits" or "license" for more information. import paramiko username = "root" password = "111111" hostname = "192.168.31.132" port = 22 client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect(hostname, port, username, password) stdin, stdout, stderr = client.exec_command('echo Hello SSHLibrary!') result = stdout.readlines() print (result)