Search code examples
automated-testsrobotframeworkreturn-value

How to find a date and number from a file, with Get Current Date?


I have a file, dates.txt, which has dates in dd.mm.yyyy format, and a number next to it, and now I need to return just the number on correct date. I use Get Current Date and for example, on 15.08.2023, I need to the robot to grab the 44253 next to it. Any tips?

I tried to find answers, but couldn't find any thats suitable for my needs.

EDIT: fixed example, 8-> 08, added information, and cleaned up the question a little.

The data in the dates.txt are like this

08.05.2024     608243
09.05.2024     323409
10.05.2024     934510
11.05.2024     637343

So the date, and a couple of spaces before the number. The file itself is not too important, so changes can be made, as long as I can get the number on the correct date.

Only looking for solutions in Robot Framework.


Solution

  • Something like this should work

    *** Settings ***
    Library    DateTime
    Library    OperatingSystem
    Library    String
    
    *** Test Cases ***
    DemoCase
        ${number}    Get Todays Number
    
    *** Keywords ***
    Get Todays Number
        ${dates}    Get File    dates.txt
        ${now}    Get Current Date    result_format=%d.%m.%Y
        ${lines}    Split To Lines    ${dates}
        FOR    ${line}    IN    @{lines}
            ${date}    ${right}    Split String    ${line}    ${SPACE}    max_split=1
            IF    "${date}" == "${now}"
                ${number}    Set Variable    ${right.strip()}
                RETURN    ${number}
            END
        END
        Fail    Was not able to find number for "${now}"