将一个关键字作为参数发送给另一个关键字

Send a keyword to another keyword as a parameter

我创建了一个与 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

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

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

解决方案看起来像这样:

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

机器人框架用户指南在名为 Using BuiltIn library and Using other libraries directly

的部分中提到了这一点