Skip to content

Make test and suite messages available as plain text and HTML separately #4068

Description

@simonmeggle

Test results merged with rebot contain by default HTML in the status message field.
It could be helpful to have an option to change that to text-only.

Activity

  1. pekkaklarck commented on Aug 27, 2021

    @pekkaklarck
    Member

    Possible solutions:

    1. Make it possible to tell that the merge message should be plain text.
    2. Make it possible to convert any message from HTML to plain text.
    3. Decide not to do anything and expect tools using Robot APIs to handle HTML to plain text conversion.

    I'm not a big fan of option 1 because it's not too generic (also other messages can be in HTML), it would make messages in normal log less good, and I don't like this kind of configuration too much in general. Option 3 means doing nothing which isn't ideal either so let's concentrate on option 2. For that we have have several possibilities:

    1. Add separate message_as_html and message_as_text attributes (actual names subject to change) to test and suite objects. message would be as it's nowadays and these new ones would be read-only properties generated based on it.
    2. Make message always plain text and add new html_message (name, again, can be something else). Could be implemented so that there's separate _message that contains the actual message and these others are generated based on it. Both message and html_message would thus need to be settable properties (they'd set that _message).
    3. Add new utility functions to convert message into plain text and HTML.

    Option 2 above sounds best to me but has a problem that it's slightly backwards incompatible because message changes. That ought to be fine in a major release, though. Regardless the approach, we'd then need to have a way to convert HTML to plain text. I wouldn't like to have any external dependencies so we'd need to implement it ourselves. Probably just stripping HTML tags would be fine.

  2. pekkaklarck commented on Aug 27, 2021

    @pekkaklarck
    Member

    Converting HTML to text ought to be pretty easy with html.parser. We'd only need to implement handle_data go get the text but could also easily implement e.g. <b> to * conversion by adding handle_start/endtag methods. Should fine-tune the implementation so that it works well with our merge message.

  3. changed the title [-]Make HTML in test status message field of merged test cases configurable [/-] [+]Make test and suite messages available as plain text and HTML separately[/+] on Sep 1, 2021
  4. pekkaklarck commented on Sep 1, 2021

    @pekkaklarck
    Member

    After thinking this a bit more, I believe going with the option 2 discussed above is the best idea. I set the issue title and labels accordingly. Next we'd need someone to implement this. I know I have other more important tasks for the forthcoming RF 5.0 and won't have time for this. I'd obviously be happy to help anyone interested to get started, review pull requests, and so on. If @simonmeggle or someone else is interested, let me know!

  5. added
    good first issue
    help wanted
    on Sep 1, 2021
  6. pekkaklarck commented on Sep 1, 2021

    @pekkaklarck
    Member

    Added a label indicating help would be appreciated. Although this is far from trivial to implement, I also added a label telling that this is ok for new contributors.

  7. simonmeggle commented on Sep 1, 2021

    @simonmeggle
    Author
  8. pekkaklarck commented on Sep 13, 2021

    @pekkaklarck
    Member

    Design option 2 discussed in my earlier comment would in practice mean something like this:

    1. Create html_to_text utility. It can utilize html.parser as discussed above.

    2. Add code like this to handle setting and getting message:

      def __init__(...):
          ...
          self._message = ''
      
      @property
      def message(self):
          if self._message.startswith('*HTML*'):
              return html_to_text(self._message[6:])    # html_to_text needs to be implemented
          return self._message
      
      @message.setter
      def message(self, message):
          self._message = message
      
      @property
      def htm_message(self):
          if self._message.startswith('*HTML*'):
              return self._message[6:]
          return html_escape(self._message)    # We already have html_escape
      
      @html_message.setter
      def html_message(self, message):
          self._message = message
              self._message = '*HTML* ' + message

    Instead of storing message internally in _message attribute as a string with possible *HTML* prefix, we could store it as a tuple containing the message and a Boolean telling should it be interpreted as HTML or not. Yet another solution would be making it an object that contains most of the logic so that the properties above could be simplified.

    If we wanted to, we could make message itself and object that would have separate attributes for getting the message as plain text and HTML. The object itself should obviously have string representation as well so that it could be used as a string in formatting.

    The reason I want to discuss about the design is that we have #2549 about making it possible to make specify test and suite documentation as HTML. There we probably needed to have documentation available both as plain text and HTML as well. These needs are similar so we should use the same design in both places. That also means that the aforementioned html_to_text could be used in both cases and possible objects containing logic for returning either plain text or HTML version could also be reused.

  9. fixedforward commented on Dec 5, 2021

    @fixedforward
    Contributor

    @pekkaklarck Is someone already working on this? If not do you mind assigning me?

  10. pekkaklarck commented on Dec 7, 2021

    @pekkaklarck
    Member

    Apparently @simonmeggle hasn't worked on this lately so go ahead @makeevolution!

  11. fixedforward commented on Dec 13, 2021

    @fixedforward
    Contributor

    @simonmeggle @pekkaklarck So I'm investigating this, and was thinking about the HTML_to_text function. Would it be something like if the original message is "*HTML* Number is not my <b>MAGIC</b> number", then the output is "Number is not my MAGIC number"? If so, I played around with the html parser library and found the following code to work. What do you think? Maybe I can make a PR if you agree?

    I'm also unsure about how to create the atests for this. How should I test the status message of rebot? Any ideas just to help me get started?

    from html.parser import HTMLParser
    
    class HTMLToText(HTMLParser):
        def __init__(self):
            super().__init__()
            self._HTMLAsText = ''
    
        def handle_data(self, data):
            self._HTMLAsText = self._HTMLAsText + data
        
        @property
        def HTMLAsText(self):
            return self._HTMLAsText
    
    def html_to_text(HTML): 
        test = HTMLToText()
        test.feed(HTML)
        return test.HTMLAsText()
    
  12. simonmeggle commented on Dec 13, 2021

    @simonmeggle
    Author

    @makeevolution For the use within robotmk, I previously also used HTMLParser. But instead, i switched now to something simpler which also works. The HTML data are not arbitraty, but the RF log content; hence it is predictable what has to be replaced.

    Now, I save the dependency from HTMLParser and do a simple replacement of all HTML tags which can occurr:

        html = re.sub('<script.*?>|</script>|<style.*?>|<style>|<span.*?>|</span>|<a.*?>|</a>|<hr>', '', html)
        html = re.sub('<br>|<p>', '\\n', html)

    Perhaps this already does its job? Only a suggestion.

  13. fixedforward commented on Dec 14, 2021

    @fixedforward
    Contributor

    @simonmeggle what about if there is a bold or italics text? Your solution has the additional replacement of breaks or paragraphs with new line, is that for your own use?

  14. simonmeggle commented on Dec 14, 2021

    @simonmeggle
    Author

    @makeevolution Yes, this is completely for my own project which processes RF XML logs. This is not a perfect solution, don't take this as complete. :-)
    I just wanted to point out that there is probably a simpler way without dependencies if the incoming HTML is well known.

  15. fixedforward commented on Dec 14, 2021

    @fixedforward
    Contributor

    @simonmeggle Ah okay thanks for the idea! I think it then depends on @pekkaklarck on how he wants to proceed...

  16. hariharandakshnamurthy commented on Nov 19, 2024

    @hariharandakshnamurthy

    Is this issue Closed or open, Can i work on it?

  17. pekkaklarck commented on Nov 20, 2024

    @pekkaklarck
    Member

    This is still open. It hasn't had too high priority, but I see it would be useful. If you are interested to look at it, please explain briefly your planned design.

  18. hariharandakshnamurthy commented on Nov 20, 2024

    @hariharandakshnamurthy

    @pekkaklarck I saw your proposed idea in the previous comments, Which i would like to try.
    Maintain backward compatibility for current behavior (message returns plain text).
    Introduce a new html_message property to handle HTML representations.
    Add a private _message attribute to store the raw data and provide logic for handling text/HTML transformations.
    Implementation Steps

    Create Utility Functions:
    A function (html_to_text) to strip HTML tags and return plain text.
    Use Python's built-in html.parser for efficient tag stripping.
    Modify Test and Suite Objects:
    Add a _message attribute to store the actual content.
    Introduce new message and html_message properties:
    message will return plain text (default).
    html_message will return HTML, either by escaping the current plain text or as-is if the message is already HTML.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions