【问题标题】:Send a keyword to another keyword as a parameter将一个关键字作为参数发送给另一个关键字
【发布时间】:2015-07-24 17:03:01
【问题描述】:

我创建了一个类似于Wait Until Keyword Succeeds 的关键字,所以我想向它发送另一个关键字:

def keyword_expecting_keyword(self, func, *args):
    return func(*args)

def normal_keyword(self, arg):
    return arg

现在我希望调用keyword_expecting_keyword 并将normal_keyword 作为参数传递:

Keyword Expecting Keyword    Normal Keyword    123

但是当我这样做时,我得到了一个TypeError: 'unicode' object is not callable,所以不是普通关键字引用机器人发送一个函数名称。

注意:如果重要的话,我使用的是旧的 RobotFramework 1.2.3(机器人是 v2.8,所以没关系,骑行 GUI 是 v1.2.3)

用真实代码更新:

关键字代码

class ElementKeywords(object):
    def has_text(self, element):
        return bool(self.get_element_property(element, 'text'))

    def wait_until_result(self, timeout, poll_period, func, *args):
        time_spent = 0
        timeout = convert_time(timeout)
        poll_period = convert_time(poll_period)
        result = False
        thrown_exception = None
        while True:
            try:
                result = func(*args)
                thrown_exception = None
            except Exception as exc:
                result = False
                thrown_exception = exc
            if result or poll_period > timeout or time_spent > timeout:
                break
            time_spent += poll_period
            time.sleep(poll_period)
        if result:
            return True
        if thrown_exception:
            raise thrown_exception

        msg = 'Failed to receive positive result from {func} in {timeout} ' \
              'seconds'.format(func=func.__name__, timeout=str(timeout))
        raise TimeoutError(msg)

测试用例

*** Settings ***
Test Setup        Web Setup
Test Teardown     Web Teardown
Resource          web_resources.txt

*** Test Cases ***
Check Index
    [Tags]    US123456
    Web Login
    Clean Redis
    ${job_id}=    Create Pool
    Web Refresh
    ${data_pool_element}=    Get Element By Xpath    //div[@id="progress-pool"]/div[1]
    Wait Until Result    20    1    Has Text    ${data_pool_element}
    Validate Pool    ${job_id}

堆栈跟踪

20150723 14:42:02.293 :  INFO : 
//div[@id="progress-pki-pool"]/div[1]
(None, u'//div[@id="progress-pki-pool"]/div[1]')
20150723 14:42:02.293 : TRACE : Return: <selenium.webdriver.remote.webelement.WebElement object at 0x7f74173f2450>
20150723 14:42:02.294 :  INFO : ${pki_pool_element} = <selenium.webdriver.remote.webelement.WebElement object at 0x7f74173f2450>
20150723 14:42:02.295 : TRACE : Arguments: [ u'20' | u'1' | u'Has Text' | <selenium.webdriver.remote.webelement.WebElement object at 0x7f74173f2450> ]
20150723 14:42:23.315 :  FAIL : TypeError: 'unicode' object is not callable
20150723 14:42:23.316 : DEBUG : 
Traceback (most recent call last):
  File "/home/andrii/projects/automated-tests/library/keywords/element_keywords.py", line 88, in wait_until_result
    raise thrown_exception
20150723 14:42:23.317 : TRACE : Arguments: [  ]
20150723 14:42:23.317 : DEBUG : DELETE http://127.0.0.1:50355/hub/session/ee7b5402-a2fc-47fc-ade3-38c2c28cc208 {"sessionId": "ee7b5402-a2fc-47fc-ade3-38c2c28cc208"}
20150723 14:42:23.323 : DEBUG : Finished Request

【问题讨论】:

  • 你没有给我们所有的信息,我怀疑你有一个名为normal_keyword的变量已经设置为unicode
  • 我试图简化代码,所以它只包含可调用部分。但我绝对没有变量 Normal 关键字(在我的情况下 - 没有与可调用变量同名的变量)。
  • @Minras 我认为如果您提供完整的回溯和您的代码会很好
  • @VigneshKalai,我在问题末尾添加了真实代码。
  • @Minras 你的完整追溯怎么样

标签: python robotframework


【解决方案1】:

func 作为字符串传入,并且您正在尝试执行该字符串,这就是您收到错误的原因。简单地说,你不能指望func(),它永远不会起作用,因为你没有被传递给函数的引用。

您需要做的是获取内置库的句柄,并使用它为您执行关键字。我不知道这种旧版本的机器人是否可以做到这一点。

解决方案看起来像这样:

from robot.libraries.BuiltIn import BuiltIn
...
result = BuiltIn().run_keyword(func, *args)

机器人框架用户指南在名为 Using BuiltIn libraryUsing other libraries directly 的部分中提到了这一点

【讨论】:

  • 成功了,谢谢!好吧,在 RF 1.2.3 中,导入看起来像 from robot.libraries.BuiltIn import BuiltIn,但按名称调用的调用者有效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-09
  • 2010-09-24
  • 2017-07-06
  • 2018-11-16
  • 2021-03-04
相关资源
最近更新 更多