一、edit標簽使用
1、導入庫
點擊 Edit 標簽頁右側的“Library”按鈕,來添加庫。在添加庫之前,首先庫已經在 Python 下進行了安裝。如,添加“Selenium2Library”庫
如果添加的庫不存在或庫名錯誤,將會顯示為紅色,很色表示正常;
如果你是在“測試套件”中添加的庫,那么這個庫中所提供的關鍵字可以被當前測試套件下的用例使用;
如果你是在“測試項目”中添加的庫,當前項目下的測試用例不能使用庫中的關鍵字,需要在用例相應的“測試套件”中再次添加庫。
按 F5 就可以查看庫中所提供的關鍵字:
2、導入資源
點擊 Edit 標簽頁右側的“Resource”按鈕來添加資源。這個資源一般為項目相關的文件。比如,項目
的自定義關鍵字文件。
(1)創建Resource
(2)定義和使用關鍵字
(3)導入ReSource
關於添加資源的作用域與庫一樣。我這里是添加到的測試套件中,那么它的作用域就是當前測試套件下的所有用例。
(4)使用關鍵字
3、定義變量
點擊 Edit 標簽頁右側的“Add Scalar”按鈕來創建變量。這里創建的變量可以被整個測試套件中的用例所使用。也可以認為是一個“公共變量”。
(1)創建一個變量
Name 用於定義變量名:${hi},Value 用於給變量賦值。
(2)使用變量
4、定義列表變量
列表變量可以用來定義一維或二維數組。下面我們就來創建一個列表變量。點擊 Edit 標簽頁右側的“Add List”按鈕來創建變量
(1)定義列表變量
Name 定義變量名為:${student},Value 填寫列表變量的值
(2)使用列表變量
二、Text Edit 標簽
text edit中顯示的是測試套件的txt,也就是所有的測試用例,切換到E:\robot\測試項目\測試套件.txt也能看到這個文件。平時可以直接切換到text edit編寫測試用例
三、run標簽
(1)運行與停止
在 Run 標簽頁提供了運行與停止的按鈕,使用很簡單。可是你知道到點擊“運行”按鈕的時候,RobotFramework 是怎么執行“測試套件.txt”文件的么?點擊“停止”按鈕的時候,Robot Framework 又做了什么操作來終止用例的執行的?
首先打開 C:\Python27\Lib\site-packages\robotide\run 目錄下的 process.py 文件。
import os import time import tempfile import subprocess class Process(object): def __init__(self, command): self._command = self._parse_command(command) self._process = None self._error = None self._out_file = None self._out_path = None self._out_fd = None def _parse_command(self, command): if isinstance(command, basestring): return [val.replace('<SPACE>', ' ') for val in command.split()] return command def start(self): self._out_fd, self._out_path = \ tempfile.mkstemp(prefix='rfproc_', suffix='.txt', text=True) self._out_file = open(self._out_path) if not self._command: self._error = 'The command is missing from this run configuration.' return try: self._process = subprocess.Popen(self._command, stdout=self._out_fd, stderr=subprocess.STDOUT) except OSError, err: self._error = str(err) def is_finished(self): return self._error is not None or self._process.poll() is not None def stop(self): self._process.kill() def wait(self): if self._process is not None: self._process.wait() def get_output(self, wait_until_finished=False): """Returns the output produced by the process. If ``wait_until_finished`` is True, blocks until the process is finished and returns all output. Otherwise the currently available output is returned immediately. Currently available output depends on buffering and might not include everything that has been written by the process. """ if self._error: self._close_outputs() return self._error if wait_until_finished: self._process.wait() output = self._out_file.read() if self.is_finished(): self._close_outputs() return output def _close_outputs(self): self._out_file.close() os.close(self._out_fd) self._remove_tempfile()
查看標紅的部分即可:
首先看 start()方法,通過 tempfile 模塊的 mkstemp()方法找到“txt”文件,也就是“測試套件.txt”文這類件。接着通過 open()方法打開。
在 get_output()方法中通過 read()方法來讀取“txt”文件。最后把讀取的文件的賦值給變量 output 並返回。
在_close_outputs()方法中通過 close()關閉打開的“txt”文件。
停止測試用例的執行非常單間,由 stop()方法實現,通過調用 kill()將用例的執行進程殺死。
(2)報告與日志
當用例運行結束,Robot Framework 生成三個文件:output.xml、log.html 和 report.html。output.xml 記錄的測試結果是 xml 文件,這個文件不夠直觀。相比較而言 log.html 和report.html 報告要直觀得多,因為是 html 格式的嘛。
查看 log.html 文件,點擊 Run 標簽而上的“Log”按鈕,通過默認瀏覽器打開。在 log.html 文件中可以查看用例執行的每一步,適合跟蹤定義問題。