i’ll explain my case: i would like to run RF from a python script and to send the test execution result as json output via http/s request in that same script.
As of now the script run the robot (with run_cli) then takes the output.json and sent it.
Is there any way to avoid writing a file on the file system and access the content of the output.json at runtime?
es.
from robot import run_cli
import requests
run_arguments=[
,'--output','output.json',
'TestPlan'
]
rc = run_cli(
runArguments,
exit=False
)
with open('output.json') as output_file:
output_content= json.loads(contentCredentialsFile)
requests.post('myurl', json=output_content)
I would like to avoid to save a file to file system and just access output_content.
Is it possibile? does RF expose an object via it’s API or its entrypoints that i can access at runtime? Maybe an ExecutionResult() or a TestSuite that I can transform with ResultWriter? Are RF Listeners required or is there a simpler way?
I’m not sure if you can access the result objects in the API from run_cli, I do know you can access them from the listener API, as you are using run_cli it should be easy enough to add the argument --listener with a listener file that does what you want.
Yes, using the listener is possible to access the datas i’m looking for although the format is not as the output.json where you can find this structure
I’m not familiar with the output.json, it’s a new feature that I’ve not had a need to use.
Maybe if you go digging through the api documentation you can find the function call that generates the json data? or maybe you’ll have to dig around the robot code to find it, if you can call that function it might make your life easier
However the code is the following, where the actual json format dumped in the output.json is created in the same method that dumps it in the file, giving no option for any user to access the output json format at runtime (without first creating the file on the os).
@pekkaklarck wouldn’t it better to have a get_json_format() function that creates the json format inside the Result class and then use this same function in to_json()? In this way the new output.json format would be accessible to any Result object and to any listener/user without having to first create the output.json file.
What do you think?
should I open an issue on the robotframework github?
Otherwise the only solution i have for my case is to copy your code into my python script, make some changes for my case and this only to access the same output.json robotframework creates, and this will happen again for all the users that have same need than me.
an open file object where to write the data to, or
You can use StringIO - it is in-memory string buffer that can be used instead of a file. Using that instead of actual file should work in this case and no actual file needs to be generated..
I succeeded in my aim accessing the robot.result.model.TestSuite with a listener end_suite method.
However the function to_json in robot.result.model.TestSuite is inherited from robot.model.modelobject.ModelObject which does not add statistics, errors, generator, generated, rpa, suites at json object level.
In order to get a json as the output.json, as the RF creates, it is required to pass as string the robot.result.model.TestSuite.to_json output to robot.result.resultbuilder.ExecutionResult and then call again the to_json method on the robot.result.executionResult.Result from ExecutionResult.
or manually format the json like the robot.result.executionResult.Result.to_json does.