mo-ta-to
mo-ta-to

Reputation: 246

robotframework - print the name of the variable itself and the file name where it is present

We are using robotframework as a testing framework and we are in a process of collecting the names of the variables that we used in the test case and actual location of where it is defined.

Any ideas?

Upvotes: 0

Views: 677

Answers (1)

Alexis Leloup
Alexis Leloup

Reputation: 250

You can use robot API to read your test file and find variables, keywords ...

Little example that list keywords and variables :

from robot.api.parsing import *
from robot.api import TestSuite


class TestModifier(ModelTransformer):
    """
    Test Modifier allows the generation of custom test files from the configuration given as argument
    """
    def __init__(self):
        self.suite = None
        self.model = None

    def visit_File(self, node):
        """
        Visit a robot file and edit it
        : param node : current node to explore
        """
        for section in node.sections:

            # Read keyword section and set configuration name
            if type(section) == KeywordSection:
                for keyword in section.body:
                    if hasattr(keyword, "body"):
                        print(keyword.body)

            elif type(section) == VariableSection:
                for variable in section.body:
                    if hasattr(variable, "body"):
                        print(variable.body)

        return self.generic_visit(node)

    def generate_model(self, model_path: str):
        """
        Generate test mode from model_path template
        :param model_path: robot file template
        """
        self.model = get_model(model_path)
        self.visit(self.model)
        self.suite = TestSuite.from_model(self.model)

    def save_model_as_test(self, path: str) -> bool:
        """
        Save generated test model as .robot test file
        :param path: path for save
        :return : True if save success else False
        """
        try:
            self.model.save(path)
            return True
        except:
            return False

# Debug only
if __name__ == "__main__":
    tm = TestModifier()
    tm.generate_model("Myfile.robot")

Upvotes: 1

Related Questions