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

时间:2015-07-23 11:56:02

标签: python robotframework

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

更新了真实代码:

关键字代码

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

1 个答案:

答案 0 :(得分:1)

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

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

解决方案看起来像这样:

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

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

的部分中提到了这一点