将不可序列化的参数发送到关键字
Send a non-serializable parameter to a keyword
我试图将 Web 驱动程序元素作为参数传递给关键字并在执行日志中得到异常:
[ ERROR ] Calling listener method 'start_keyword' of listener '/usr/local/lib/python2.7/dist-packages/robotide/contrib/testrunner/TestRunnerAgent.py' failed: TypeError: <selenium.webdriver.remote.webelement.WebElement object at 0x7f686eb02390> is not JSON serializable
[ ERROR ] Calling listener method 'end_keyword' of listener '/usr/local/lib/python2.7/dist-packages/robotide/contrib/testrunner/TestRunnerAgent.py' failed: TypeError: <selenium.webdriver.remote.webelement.WebElement object at 0x7f686eb02390> is not JSON serializable
测试仍然通过,因此这不会影响结果,但在日志中看到这一点并不好。此外,我不明白,如果在将所有关键字参数传递给实际 Python 代码之前将所有关键字参数都序列化,为什么它会起作用,而这个是不可序列化的。
问题:我可以在 RobotFramework 中传递不可序列化的参数吗?现在我尝试发送的不是元素,而是元素标识符的命名元组。
我使用的代码:
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}
问题不在您的代码中。您可以传递不可序列化的对象。 RIDE 测试运行程序似乎有一个错误。从命令提示符尝试 运行 您的测试用例,问题应该会消失。
我试图将 Web 驱动程序元素作为参数传递给关键字并在执行日志中得到异常:
[ ERROR ] Calling listener method 'start_keyword' of listener '/usr/local/lib/python2.7/dist-packages/robotide/contrib/testrunner/TestRunnerAgent.py' failed: TypeError: <selenium.webdriver.remote.webelement.WebElement object at 0x7f686eb02390> is not JSON serializable
[ ERROR ] Calling listener method 'end_keyword' of listener '/usr/local/lib/python2.7/dist-packages/robotide/contrib/testrunner/TestRunnerAgent.py' failed: TypeError: <selenium.webdriver.remote.webelement.WebElement object at 0x7f686eb02390> is not JSON serializable
测试仍然通过,因此这不会影响结果,但在日志中看到这一点并不好。此外,我不明白,如果在将所有关键字参数传递给实际 Python 代码之前将所有关键字参数都序列化,为什么它会起作用,而这个是不可序列化的。
问题:我可以在 RobotFramework 中传递不可序列化的参数吗?现在我尝试发送的不是元素,而是元素标识符的命名元组。
我使用的代码:
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}
问题不在您的代码中。您可以传递不可序列化的对象。 RIDE 测试运行程序似乎有一个错误。从命令提示符尝试 运行 您的测试用例,问题应该会消失。