检查webdriver是否在Robot Fram中启动

2024-03-28 09:15:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Robot Framework中编写了一些测试用例,我的项目结构如下:

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

{{{cda}中所有打开的RunTest.py来运行我的测试用例;我在RunTest.py中设置测试中应该包括或排除哪个目录以及其他一些首选项。在

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

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

有什么帮助我怎么做我在最后一段所说的??在


Tags: 文件项目py目录initrobot浏览器测试用例
2条回答

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

它可能看起来像这样:

*** 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

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

您可以使用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

^{pr2}$

相关问题 更多 >