如何通过调用Python函数在Robotframework中运行.vbs文件?

时间:2019-04-08 08:06:51

标签: python-3.x vbscript robotframework

我正在尝试在Robotframework中创建一个自定义关键字,该关键字应该运行一个.vbs文件,然后将该关键字添加到测试摘要中,以便该.vbs文件应在每个测试用例完成后运行。但是,当我运行代码时,出现错误“关键字'Common.CallVBS'预期为0参数,得到1”。我是Python和编码的新手,无法弄清楚。

下面是我在“ CustomLibraries”目录下的.py文件中的代码-

Import subprocess

def call_vbs(scriptpath):
    try:
        subprocess.call('cscript.exe ' + scriptpath)
        return "PASS"
    except:
        return "FAIL"

下面是“ CommonCommands.robot”文件,其中在“ CallVBS”末尾有自定义关键字-

*** Settings ***
Documentation  It holds common functionalities related to all the Test Cases
Library  SeleniumLibrary
Library  ../CustomLibraries/Calling_VBS.py

*** Variables ***
${scriptpath}  ${EXECDIR}\\CustomLibraries\\saving.vbs

*** Keywords ***
CallVBS
    ${success}  call_vbs  ${scriptpath}
    Run Keyword If  "${success}"=="FAIL"  FAIL  Not able to launch VBS file
    log  ${scriptpath}

1 个答案:

答案 0 :(得分:2)

这不是与库中的call_vbs关键字链接,而是与Robot文件中的CallVBS关键字链接。

调用CallVBS关键字时,您不应添加任何参数。或者,像这样在CallVBS关键字中添加一个arugments行:

CallVBS
    [Arguments]    ${argument1}
    ${success}  call_vbs  ${scriptpath}
    Run Keyword If  "${success}"=="FAIL"  FAIL  Not able to launch VBS file
    log  ${scriptpath}

此外,我建议您将python关键字包装在一个类中,如下所示:

Import subprocess

class Custom(object):
    def call_vbs(self, scriptpath):
        try:
           subprocess.call('cscript.exe ' + scriptpath)
           return "PASS"
        except:
           return "FAIL"