【问题标题】:Unable to use Robot Framework custom library which has written using Python无法使用使用 Python 编写的 Robot Framework 自定义库
【发布时间】:2019-07-12 13:51:35
【问题描述】:

使用如下函数创建了一个示例 Python 脚本 (Elements.py):

from robot.api.deco import keyword

@keyword("join two strings")
def join_two_strings(arg1, arg2):
   return arg1 + " " + arg2

然后我已将 Robot Framework 脚本(.robot 文件)作为库导入:

*** Settings ***
Library                 AppiumLibrary
Library                 Selenium2Library
Library                 BuiltIn
#Here is the import of Custom Lib
Library                 Elements.py 

*** Variable ***

*** Test Cases ***
Example that calls a Python keyword
   ${result}=   join two strings   hello world
   Should be equal     ${result}    hello world

在脚本上面运行后,出现类似“没有找到名称为'连接两个字符串'的关键字”的错误。即使我在哪里导入了自定义库。

错误信息:

[ ERROR ] Error in file C:\Users\ramana.gouda\PycharmProjects\SafeMobile\Test_Suite\TestCase_346.robot: Test library 'Elements.py' does not exist.

TestCase 346 :: Creating internal cases using device                          

Example that calls a Python keyword                                   | FAIL |
No keyword with name 'join two strings' found.

【问题讨论】:

    标签: python pycharm robotframework


    【解决方案1】:

    我总是必须使用相对路径,除非文件与我的测试用例在同一目录中,并且根据错误看起来你的不是。

    所以在你的情况下,它看起来像下面这样(不完全是这样,因为我不知道 Elements.py 的位置):

    Library    ../../SafeMobile/Elements.py
    

    希望这会有所帮助!

    【讨论】:

    • 试过了,这很尴尬。自 3-4 小时以来,我已经以同样的方式完成了,它没有工作,现在工作了 :) 谢谢你的时间!
    【解决方案2】:

    如果您不想在所有机器人文件中包含相对路径,另一种选择是在开始测试时使用--pythonpath command line argument。

    -P, --python路径

    要添加到模块搜索路径的其他位置。

    这样你就可以拥有:

    Library    Elements.py
    

    在你的代码中,而你必须像这样启动它:

    robot  --pythonpath C:/Users/ramana.gouda/PycharmProjects/SafeMobile/ Test_Suite/TestCase_346.robot
    

    来自 SafeMobile 文件夹。


    您可以进一步创建一个argument file,您可以在其中收集所有路径设置。例如custom_libraries.txt:

    --pythonpath C:/Users/ramana.gouda/PycharmProjects/SafeMobile/
    --pythonpath C:/Users/ramana.gouda/PycharmProjects/SafeMobile/libs/
    

    你可以在启动测试时使用它:

    robot --argumentfile custom_libraries.txt Test_Suite/TestCase_346.robot
    

    这样,当您或其他人引入新库时,无需更改开始测试的方式。您只需确保将新库的路径添加到参数文件中。

    【讨论】:

    • 有没有办法可以将自定义库永久添加到 pythonpath 中?就像我们添加其他外部库一样。
    • @Sameem 外部库安装在Python\Python37\site-packages 文件夹下。如果您在此处包含您的库源代码,那么 python 应该会像其他人一样找到它。其他方法是使用库的路径更新 PYTHONPATH 环境变量。 How to add to the pythonpath in Windows?,Linux 上也应该有相同的答案。我个人更喜欢这里的命令行参数,因为它是可移植的,并且对于多个开发人员,无需在每台机器上设置 PYTHONPATH。
    猜你喜欢
    • 2020-03-22
    • 2021-08-31
    • 2012-11-01
    • 2013-04-07
    • 1970-01-01
    • 2015-11-15
    • 1970-01-01
    • 2021-03-27
    • 2020-02-05
    相关资源
    最近更新 更多