简体   繁体   中英

Parse output.xml before Suite Teardown in Robot Framework

I want to read the statistics from output.xml in Robot Framework and send them as a notification in MS Teams. My plan was to do this by parsing output.xml in Suite Teardown but I have now noticed that that file is blank until after Suit Teardown so there is no information in it to parse. My question is if there is any way to reach this data before Suite Teardown is done? I have noticed the Listener API for Robot Framework, is it possible to access that from python?

This is my python code basically:

import xml.etree.ElementTree as ET

class TeamsNotifications(object):

    @staticmethod
    def get_stats():
        tree = ET.parse('output.xml')
        root = tree.getroot()

        stats_list = root.find('.//statistics//total')[1].attrib
        return stats_string

    def send_notification(self):
        stats = self.get_stats()
        # *send 'stats' as teams-notification*

And my robot code:

*** Settings ***
Library     x.TeamsNotifications

Suite Teardown  After suite teardown

*** Test Cases ***
----

*** Keywords ***
After suite teardown
    send notification

You can set up your library to be a listener in addition to providing keywords. You can get the statistics in the _end_suite method when the suite id is "s1". The passed-in result object has all of the data used to generate the report.

Here's a simple example:

class ExampleLibrary:
    ROBOT_LISTENER_API_VERSION = 3
    def __init__(self):
        self.ROBOT_LIBRARY_LISTENER = self

    def _end_suite(self, suite, result):
        if suite.id == "s1":
            # the root suite has ended
            print(result.stat_message)

Instead of printing result.stat_message you could pull data out of result.statistics for your notification.

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