简体   繁体   中英

In Robot Framework, can you create one resource object as with libraries?

In robot framework you can create an object from a library. Directly from the documentation, it looks like this.

*** Settings ***
Library    com.company.TestLib    WITH NAME    TestLib

My question is can you do the same thing with a resource file? I have been trying to create a keyword python file with instance variables that store the test start time and test name. I use the test name and time as a unique ID in a JavaScript data file for the web. I saw both values reverting to the default 'blank' and decided to insert some print statements about an instance ID variable. This was the result.

var testRuns = {};
testRuns['Test Site 2 2021-12-09 02:09:19.849706']=[];***ID79
testRuns['blank'].push({Case: 'Just Proving the Point.txt', Step: 'Verify Menu HTML', Time: '18970 days, 7:09:19.889702', Status: 'PASS***ID0', Details: 'Actual: Content, Expected: Content'});***ID0
testRuns['blank'].push({Case: 'Find Content Menu.txt', Step: 'Verify Menu HTML', Time: '18970 days, 7:09:19.919697', Status: 'PASS***ID34', Details: 'Actual: Content, Expected: Content'});***ID34

The randomly generated ID is created at construction of the python class (below). Each line in the output has the ID tacked on and you can see that they are different. Each one appears to be an entirely new instance of python class created in the resource file.

class kw_DataExtraction:

   def __init__(self, primaryFile=defaultFile, secondaryFile=defaultSecondaryFile):
      self.dataFile = primaryFile
      self.otherFile = secondaryFile
      self.currentTestName = "blank"
      self.testCaseStart = datetime.fromtimestamp(0)
      self.myID = random.randint(0,100)

So, I tried something like the below and it is apparently not legal syntax. Is there a way to instantiate the resources just once and consistently use it?

Resource   ./../OQE/DataExtraction.resource    WITH NAME    dEx

Okay. Thanks to @Dev for helping to clarify the question. The issue here is an aspect of Robot Framework that I wouldn't have considered. This led me to a very good answer related to state How to Preserve Object state in Robot Framework . After some investigation, I was able to successfully solve my problem and the full explanation is below.


From the RF documentation http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#test-library-scope

"Because the state can affect how keywords actually behave, it is important to make sure that changes in one test case do not accidentally affect other test cases. These kind of dependencies may create hard-to-debug problems, for example, when new test cases are added and they use the library inconsistently. Robot Framework attempts to keep test cases independent from each other: by default, it creates new instances of test libraries for every test case."

In other words, it is a design decision that RF should have fresh state for all test cases. The behavior I was seeing was a direct result of that design decision.

In my case, I am using "test case" as steps within a whole task, what the documentation calls Robot Process Automation (RPA) http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#rpa . So in my work I want to designate the Python class with a more permanent state. To do that I add the ROBOT_LIBRARY_SCOPE variable to the class.

class kw_DataExtraction:
   ROBOT_LIBRARY_SCOPE = 'SUITE'

With that in place I reran my test and the output shows the same ID number at the end and the right test case name is preserved. Mystery solved.

var testRuns = {};
testRuns['Test Site 2 2021-12-09 12:13:25.780586']=[];***ID56
testRuns['Test Site 2 2021-12-09 12:13:25.780586'].push({Case: 'Just Proving the Point.txt', Step: 'Verify Menu HTML', Time: '0:00:00.039995', Status: 'PASS***ID56', Details: 'Actual: Content, Expected: Content'});***ID56
testRuns['Test Site 2 2021-12-09 12:13:25.780586'].push({Case: 'Find Content Menu.txt', Step: 'Verify Menu HTML', Time: '0:00:00.070994', Status: 'PASS***ID56', Details: 'Actual: Content, Expected: Content'});***ID56

TL;DR; You cannot create just one library object by default. Default behavior in RF is to create a new instance for each test case. Add ROBOT_LIBRARY_SCOPE to any Python library to adjust the longevity of that class with respect to test cases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM