解決Robot Framework運行時沒有Log的方案


Robot Framework自動化測試過程中,運行多次后會出現RIDE沒有log的情況。

造成這種現象的原因是:

執行失敗的測試用例,chrome.exe和chromedriver.exe進程沒有關閉。

解決方法:手動關閉chromedriver進程,ride就可以正常運行。

但是每次手動去關閉chromedriver進程比較麻煩,

---------------------------------------------------------------------------------------------------------

下面給大家介紹一種自動化關閉進程的方法:

在執行測試用例時,先調用關閉進程的批處理文件,關閉進程后再接着去執行下一步測試用例。

 

1.       創建關閉進程的批處理文件

將下面的代碼保存為批處理文件killChrome.bat(存放路徑:D:\RobotTest\測試項目)。

 

創建批處理比較簡單,難的是如何封裝系統關鍵字

關鍵字需求:接收一個目錄路徑,自動遍歷目錄下以及子目錄下的所有批處理(.bat)文件並執行。

2.       封裝系統關鍵字

在..\Python2.7\Lib\site-packages目錄下創建CustomLibrary目錄,用於放自定義的library庫。在其下面創建runbat.py文件(其中的path路徑為killChrome.bat的存放路徑):

 1 # -*- coding: UTF-8 -*-
 2 #   作用:執行批處理文件
 3 # 
 4 #   創建者:大道OA團隊 大東哥
 5 #
 6 #   創建時間:2017-09-21 
 7 #
 8 
 9 __version__ = "1.0"
10 
11 from robot.api import logger
12 import os
13 
14 class Runbat(object):
15 
16     def run_all_bat(self,path):
17         """
18         接收一個目錄的路徑,並執行目錄下的所有bat文件.
19         
20         Usage is:
21         | run all bat | filepath |
22         """
23         for root,dirs,files in os.walk(path):
24             for f in files:
25                 if os.path.splitext(f)[1] == '.bat':
26                     os.chdir(root)
27                     os.system(f)
28                
29     def __execute_sql(self, path):
30         logger.debug("Executing : %s" % path)
31         print path
32         
33     def decode(self,customerstr):
34         return customerstr.decode('utf-8')
35         
36 if __name__ == '__main__':
37     path = u'D:\\RobotTest\\測試項目'
38     run = Runbat()
39     run.run_all_bat(path)

 

注意在run_all_bat()方法下面加上清晰的注釋,最好給個實例。這樣在robot framework 的幫助中能看到這些信息,便於使用者理解這個關鍵字的使用。

對於創建普通的模塊來說這樣已經ok了。但要想在robot framework啟動后加載這個關鍵字,還需要在CustomLibrary目錄下創建__init__.py文件,並且它不是空的。

 1 # Copyright 2009-2015 MongoDB, Inc.
 2 #
 3 # Licensed under the Apache License, Version 2.0 (the "License");
 4 # you may not use this file except in compliance with the License.
 5 # You may obtain a copy of the License at
 6 #
 7 # http://www.apache.org/licenses/LICENSE-2.0
 8 #
 9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 
15 from CustomLibrary.runbat import Runbat
16 
17 __version__ = "1.0"
18 
19 
20 class CustomLibrary(Runbat):
21 
22     ROBOT_LIBRARY_SCOPE = 'GLOBAL'

 

這個文件中其實有用的信息就四行,但必不可少。robot framwork 在啟動時會加載這個文件,因為在這個文件里指明了有個runbat文件下面有個Runbat類。從而加載類里的方法(run_all_bat())。注意.py文件的編碼(utf-8編碼)和縮進格式,不然無法導入庫。

 

下面,啟動robot framework RIDE,按F5:

 

找到了我們創建的關鍵字,下面就是在具體的項目或測試套件中引用CustomLibrary

 

然后,在具體的測試用例中使用“run all bat” 關鍵字。(關鍵字后面需填入killChrome.bat的存放路徑,注意斜杠)

 

這樣在每次執行用例時,ride會先去關閉Chrome和ChromeDriver進程,然后再去執行測試用例。


免責聲明!

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



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