Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print available tags while using Robot Framework

If I have a large test suite in Robot Framework and a lot of tags is it possible to to get to know a list of tag names available within the suite ?

some thing like pybot --listtags ??

It will be useful for the person who is actually going to run the test.

For example in a scenario related to publishing of news articles , the test cases may be tagged as "publish", "published", or "publishing" .

The tester is not going to have RIDE at his/her disposal . And hence he/she may not know the exact tag name .

Under these circumstances I thought it will be useful to extract the available tags to display - without running any tests . And then he/she can choose to run the test with the desired tag

I searched the robot framework user guide and didn't see any command line options that do this.

like image 594
binithb Avatar asked Dec 15 '22 22:12

binithb


2 Answers

There is nothing provided by robot to give you this information. However, it's pretty easy to write a python script that uses the robot parser to get all of the tag information. Here's a quick hack that I think is correct (though I only tested it very briefly):

from robot.parsing import TestData
import sys

def main(path):
    suite = TestData(parent=None, source=path)
    tags = get_tags(suite)
    print ", ".join(sorted(set(tags)))

def get_tags(suite):
    tags = []

    if suite.setting_table.force_tags:
        tags.extend(suite.setting_table.force_tags.value)

    if suite.setting_table.default_tags:
        tags.extend(suite.setting_table.default_tags.value)

    for testcase in suite.testcase_table.tests:
        if testcase.tags:
            tags.extend(testcase.tags.value)

    for child_suite in suite.children:
        tags.extend(get_tags(child_suite))

    return tags

if __name__ == "__main__":
    main(sys.argv[1])

Note that this will not get any tags created by the Set Tags keyword, nor does it take into account tags removed by Remove Tags.

Save the code to a file, eg get_tags.py, and run it like this:

$ python /tmp/get_tags.py /tmp/tests/
a tag, another force tag, another tag, default tag, force tag, tag-1, tag-2
like image 75
Bryan Oakley Avatar answered Dec 17 '22 12:12

Bryan Oakley


I have used a Robot Framework output file listener to list all tags of the current suite.

from lxml import etree as XML

"""Listener that prints the tags of the executed suite."""

ROBOT_LISTENER_API_VERSION = 3

tags_xpath = ".//tags/tag"

def output_file(path):
    root = XML.parse(path).getroot()
    tag_elements = root.xpath(tags_xpath)

    tags = set()

    for element in tag_elements:
        tags.add(element.text)

    print("\nExisting tags: " + str(tags) + "\n")

You can use such listener along with dry run mode to quickly get the tag data of a suite.

robot --listener get_tags.py --dryrun ./tests

The tags will be listed at the output file section of the console log.

==============================================================================

Existing tags: {'Tag1', 'a', 'Tag3.5', 'Feature1', 'b', 'Tag3', 'Feature2'}

Output:  D:\robot_framework\output.xml
Log:     D:\robot_framework\log.html
Report:  D:\robot_framework\report.html
like image 24
Bence Kaulics Avatar answered Dec 17 '22 12:12

Bence Kaulics