Python 3 robot framework - failed to set use case retry

Posted by Divine Winds on Sat, 15 Jan 2022 06:12:53 +0100

During UI automation pin testing, test cases often fail occasionally due to environmental, network and other reasons, which not only makes the UI test script unstable, but also takes time to check the real reason of execution failure every time an error is reported, which is a waste of time and experience. However, in fact, RF can also set the number of retries, If it still fails after 3 retries, the use case is identified as failed.

Specific method: rewrite the RobotFramework source code and add the – retry option to realize the automatic re execution of test level failed cases. The failed instance will run again N times until it succeeds or runs out of retries. Only the last execution result will be reflected in the generated report file, but all execution steps will be recorded in the log file.

If the number of retries is set to 3, ride will still print the log of 3 failures.

The specific steps are as follows:

1, Modify the file D: \ Python 36 \ lib \ site packages \ robot \ run py

1. Add import module

import imp
imp.reload(sys)
from xml.dom import minidom

2. Add make method to robotframework class

    def make(self, outxml):
        xmldoc = minidom.parse(outxml)
        suiteElementList = xmldoc.getElementsByTagName('suite')
        mySuite = []
        for suiteElement in suiteElementList:
            if suiteElement.childNodes is not None:
                for element in suiteElement.childNodes:
                    if element.nodeName == 'test':
                        mySuite.append(suiteElement)
                        break
        for suite in mySuite:
            testElements = {}
            for element in suite.childNodes:
                if element.nodeName == 'test':
                    name = element.getAttribute('name')
                    if testElements.get(name) == None:
                        testElements.update({name: [element]})
                    else:
                        testElements.get(name).append(element)
            for n, el in testElements.items():
                for i in el[0:-1]:
                    textElement = i.nextSibling
                    suite.removeChild(i)
                    suite.removeChild(textElement)
        savefile = open(outxml, 'w', encoding='utf-8')
        root = xmldoc.documentElement
        root.writexml(savefile)
        savefile.close()

3. main method of robotframework class, add red content self make(settings.output)

2, Modify D: \ Python 36 \ lib \ site packages \ robot \ conf \ settings py

Modify the of the RobotSettings class_ extra_cli_opts dictionary, add "'retry ': ('retry', 2)," (the number of times here can be set according to needs, indicating that Retry-1 times will be executed after the use case fails)

III. modify D: \ Python 36 \ lib \ site packages \ robot \ model \ itemlist py

Modify the visit method as follows:

    def visit(self, visitor):
            for item in self:
                if self.__module__ == 'robot.model.testcase' and hasattr(visitor,"_context"):
                    testStatus = ''
                    for i in range(0,int(visitor._settings._opts['Retry'])):
                        if testStatus != 'PASS':
                            if item.name in visitor._executed_tests:
                                visitor._executed_tests.pop(item.name)
                            item.visit(visitor)
                            testStatus = visitor._context.variables['${PREV_TEST_STATUS}']
                        else:
                            break
                else:
                    item.visit(visitor)

4, Modify D: \ Python 36 \ lib \ site packages \ robot \ contrib \ testunner \ uses py

Modify the USAGE string and add - Z -- retry retry Set the retry times if test failed

Many materials on the Internet say that the - X parameter is used. In fact, the - X parameter has been used. When it is set to - X to run the use case, an error will be reported. Therefore, before setting the parameters here, search in the file to ensure that the parameters to be set are not used at present.

After the above settings are completed, you can realize the automatic retry of case operation failure.

Topics: Robot