Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

python - How to write Robotframework log and any keyword output in one line?

I want to print the output of any built-in or custom keyword using Log command in one line. for eg.

Log    Evaluate    1 + 2

This shows error that value of INFO for Log function is incorrect, i mean it assumes that Evaluate is parameter of Log function. I am aware that i can assign the value of Evaluate to a variable and then log that variable. But this adds multiple entries in log file. so just wondering if one liner is possible to do just as we do in python

def Evaluate(a, b):
   return a+b

print(Evaluate(1,2))
question from:https://stackoverflow.com/questions/65888578/how-to-write-robotframework-log-and-any-keyword-output-in-one-line

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can create a library with a keyword that will log the return value of Evaluate, but it will be the same as storing the return value of Evaluate in the Robot Framework script. So a one liner is possible ${retval}= Evaluate 1 + 2 but in general keywords are not substituted by their return value when passed as a parameter.

from robot.libraries.BuiltIn import BuiltIn

def evaluate_and_log_return_value(expression):
    retval= BuiltIn().evaluate(expression)
    BuiltIn().log(retval)

The test:

*** Settings ***
Library     var.py

*** Test Cases ***
Test 1
    Evaluate And Log Return Value    1 + 2
    
Test 2
    ${retval}=    Evaluate    1 + 2

You can check the output to see that basically the two are the same because variable assignments are logged by default. You do not have to call Log again if you are interested in the value of the assigned variable.

enter image description here

You can write a general keyword as well that will take any keyword by name with its arguments, it will execute it and log the return value.

from robot.libraries.BuiltIn import BuiltIn

def run_keyword_and_log_return_value(keyword, *args):
    BuiltIn().log(BuiltIn().run_keyword(keyword, *args))

Example:

*** Settings ***
Library     var.py

*** Test Cases ***
Test 3
    Run Keyword And Log Return Value    Evaluate    1 + 2
    Run Keyword And Log Return Value    Set Variable    data

Output:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...