解决Robot Framework运行时没有Log的方案

Robot Framework自动化测试过程中,运行多次后会出现RIDE没有log的情况。

造成这种现象的原因是:

执行失败的测试用例,chrome.exe和chromedriver.exe进程没有关闭。

解决方法:手动关闭chromedriver进程,ride就可以正常运行。

但是每次手动去关闭chromedriver进程比较麻烦,

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

下面给大家介绍一种自动化关闭进程的方法:

在执行测试用例时,先调用关闭进程的批处理文件,关闭进程后再接着去执行下一步测试用例。

1.       创建关闭进程的批处理文件

将下面的代码保存为批处理文件killChrome.bat(存放路径:D:RobotTest测试项目)。

创建批处理比较简单,难的是如何封装系统关键字

关键字需求:接收一个目录路径,自动遍历目录下以及子目录下的所有批处理(.bat)文件并执行。

2.       封装系统关键字

在..Python2.7Libsite-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进程,然后再去执行测试用例。

原文地址:https://www.cnblogs.com/greattao/p/7569840.html