检查网络驱动程序是否在 Robot Framework 中启动

Check whether the web-driver is up in Robot Framework

我在Robot Framework中写了一些测试用例,我的项目结构是这样的:

TestProject
|__ TestScenarios
    |__ TestCategory1
    |__ TestCategory2
    |__ __init__.py
|__ RunTest.py

我有一个 suit setup 可以用给定的 URL 打开浏览器,还有一个 suit teardown 可以关闭 __init__.py 中的所有浏览器。我调用 RunTest.py 是为了 运行 我的测试用例;我在 RunTest.py 中设置了测试中应包含或排除的目录以及其他一些首选项。

如果我从顶级目录设置测试目录,即 TestScenarios,将调用 __init__.py。但是当我为 运行 设置子目录时,__init__.py 逻辑上不会被调用,因此不会有浏览器。如果我在子目录中添加另一个 __init__.py 文件,如果我从顶级目录 运行 测试就会有问题;会有多个浏览器。

我想要的是在每个子目录中添加一个__init__.py文件,但是在这些新的__init__.py文件中,我会检查浏览器是否打开,如果是,我将继续使用当前浏览器,否则我将打开一个新浏览器。所以。我可以轻松地在目录之间切换。

任何帮助我如何做我在上一段中谈到的事情??

您可以使用 subprocess 模块查看实时进程并检查您的浏览器是否打开:

import subprocess

def __init__(proc_name,flag=False):
   p = subprocess.Popen(['ps','-A'], stdout=subprocess.PIPE)
   out, err = p.communicate()
   for line in out.splitlines():
      if proc_name in line:
          print line
          flag=True
          # continue with current proc
          #pid = int(line.split(None, 1)[0]) if you need pid 
   if not flag:
          #start new proc 

如果你想终止一个进程,你可以像下面这样起诉 os,kill :

os.kill(pid, subprocess.signal.SIGKILL)

一个解决方案是 运行 如果没有打开浏览器,您知道将失败的关键字。如果关键字失败,请打开浏览器。如果成功,什么也不做。

它可能看起来像这样:

*** Keywords ***
| Maybe open browser
| | # Don't capture screenshot if this fails
| | ${fail keyword}= | Register keyword to run on failure | Nothing
| | 
| | # Run a keyword and capture the status
| | ${status}= | run keyword and return status | Get Window Identifiers
| | 
| | # Reset the run-on-failure keyword
| | Register keyword to run on failure | ${fail keyword}
| | 
| | # Return if the keyword succeeds, on the assumption
| | # that a browser window is already open
| | Run keyword unless | ${status} == False | Return from keyword 
| | 
| | # No browser was open, so open one
| | log | No browser was previously open
| | Open browser | http://www.google.com | chrome

在我看来,最好的解决方案是始终 运行 从根文件夹进行测试,并对 运行 特定套件使用命令行参数和标签。