Search code examples
apitestingautomated-testsrobotframework

How to extract the [Documentation] text from Robot framework test case


I am trying to extract the content of the [Documentation] section as a string for comparision with other part in a Python script. I was told to use Robot framework API https://robot-framework.readthedocs.io/en/stable/ to extract but I have no idea how.

However, I am required to work with version 3.1.2

Example:

*** Test Cases ***
ATC Verify that Sensor Battery can enable and disable manufacturing mode
    [Documentation]    E1: This is the description of the test 1
    ...                E2: This is the description of the test 2
    [Tags]    E1    TRACE{Trace_of_E1}
    ...       E2    TRACE{Trace_of_E2}

Extract the string as

E1: This is the description of the test 1

E2: This is the description of the test 2


Solution

  • Have a look at these examples. I did something similar to generate testplans descritio. I tried to adapt my code to your requirements and this could maybe work for you.

    import os
    import re
    from robot.api.parsing import (
        get_model, get_tokens, Documentation, EmptyLine, KeywordCall,
        ModelVisitor, Token
    )
    
    class RobotParser(ModelVisitor):
        def __init__(self):
            # Create object with remarkup_text to store formated documentation
            self.text = ''
    
    
        def get_text(self):
            return self.text
    
        def visit_TestCase(self, node):
            # The matched `TestCase` node is a block with `header` and
            # `body` attributes. `header` is a statement with familiar
            # `get_token` and `get_value` methods for getting certain
            # tokens or their value.
            
    
    
            for keyword in node.body:
                # skip empty lines
                if keyword.get_value(Token.DOCUMENTATION) == None:
                    continue
                self.text += keyword.get_value(Token.ARGUMENT)
    
        def visit_Documentation(self,node):
            # The matched "Documentation" node with value
            self.remarkup_text += node.value + self.new_line 
    
        def visit_File(self, node):
            # Call `generic_visit` to visit also child nodes.       
            return self.generic_visit(node)
    
    if __name__ == "__main__":
        path = "../tests"
        for filename in os.listdir(path):
            if re.match(".*\.robot", filename):
                model = get_model(os.path.join(path, filename))
                robot_parser = RobotParser()
                robot_parser.visit(model)
                text=robot_parser._text()