什么是在机器人框架中以毫秒为单位获取时间的关键字?

时间:2014-06-02 08:41:13

标签: testing selenium automated-tests robotframework

目前我正在使用关键字Get time epoch,这是以秒为单位的返回时间。但我需要时间,以毫秒为单位,这样我就可以获得特定事件的时间跨度。

还是有其他方法可以获得特定事件或者测试时间的时间跨度吗?

5 个答案:

答案 0 :(得分:3)

检查新的测试库DateTime,其中包含关键字Get Current Date,它也返回毫秒。它还有关键字Subtract Dates来计算两个时间戳之间的差异。

答案 1 :(得分:3)

机器人的一个更强大的功能是你可以使用Evaluate关键字从测试脚本直接调用python代码。例如,您可以调用time.time()函数,并进行一些数学运算:

*** Test cases
| Example getting the time in milliseconds
| | ${ms}= | Evaluate | int(round(time.time() * 1000)) | time
| | log | time in ms: ${ms}

请注意,即使time.time返回浮点值,并非所有系统都会返回比一秒更精确的值。

答案 2 :(得分:0)

在报告中,对于每个套件,测试和关键字,您可以获得有关开始,结束和长度的信息以及毫秒详细信息。类似的东西:

Start / End / Elapsed:  20140602 10:57:15.948 / 20140602 10:57:16.985 / 00:00:01.037

答案 3 :(得分:0)

我没有看到使用Builtin的方法,看看:

def get_time(format='timestamp', time_=None):
    """Return the given or current time in requested format.

    If time is not given, current time is used. How time is returned is
    is deternined based on the given 'format' string as follows. Note that all
    checks are case insensitive.

    - If 'format' contains word 'epoch' the time is returned in seconds after
      the unix epoch.
    - If 'format' contains any of the words 'year', 'month', 'day', 'hour',
      'min' or 'sec' only selected parts are returned. The order of the returned
      parts is always the one in previous sentence and order of words in
      'format' is not significant. Parts are returned as zero padded strings
      (e.g. May -> '05').
    - Otherwise (and by default) the time is returned as a timestamp string in
      format '2006-02-24 15:08:31'
    """
    time_ = int(time_ or time.time())
    format = format.lower()
    # 1) Return time in seconds since epoc
    if 'epoch' in format:
        return time_
    timetuple = time.localtime(time_)
    parts = []
    for i, match in enumerate('year month day hour min sec'.split()):
        if match in format:
            parts.append('%.2d' % timetuple[i])
    # 2) Return time as timestamp
    if not parts:
        return format_time(timetuple, daysep='-')
    # Return requested parts of the time
    elif len(parts) == 1:
        return parts[0]
    else:
        return parts

你必须编写自己的模块,你需要这样的东西:

import time

def get_time_in_millies():
    time_millies = lambda: int(round(time.time() * 1000))

    return time_millies

然后在Ride中为套件导入此库,您可以使用方法名称,如关键字,在我的情况下,它将是Get Time In Millies。更多信息here

答案 4 :(得分:0)

按照janne的建议使用DateTime library

*** Settings ***
Library    DateTime

*** Test Cases ***
Performance Test
    ${timeAvgMs} =    Test wall clock time    100    MyKeywordToPerformanceTest    and    optional    arguments
    Should be true    ${timeAvgMs} < 50

*** Keywords ***
MyKeywordToPerformanceTest
    # Do something here

Test wall clock time
    [Arguments]    ${iterations}    @{commandAndArgs}
    ${timeBefore} =    Get Current Date
    :FOR   ${it}    IN RANGE    ${iterations}
    \    @{commandAndArgs}
    ${timeAfter} =    Get Current Date
    ${timeTotalMs} =    Subtract Date From Date    ${timeAfter}    ${timeBefore}    result_format=number
    ${timeAvgMs} =    Evaluate    int(${timeTotalMs} / ${iterations} * 1000)
    Return from keyword    ${timeAvgMs}