Join 34,000+ subscribers and receive articles from our blog about software quality, testing, QA and security.
 

Sanitizing Text For TestRail


#1

Greetings!

When I write test results from TestNG/Maven to a given test in TestRail via the api, I’ve had to go to some lengths to try and sanitize the text before sending it to TestRail. Special characters of various types seem to break the application in such a way that no test results show up at all in some cases. In my setup, this sanitization is taking place in the PHP script that runs the tests and sends back the results.

I’m wondering if the devs have encountered this and if there might be any advice you can offer. Also wondering if there might be any changes on the horizon that might make this easier to deal with in the future. I’d love to be able to format the output more nicely using html or whatever, but again it’s been very hit or miss, largely due to my lack of proper understanding of how text, special characters, UTF this or that and such is handled in PHP as well as TestRail.

Thanks in advance for any tips or advice anyone can offer!


#2

Hello Kent,

Thanks for your posting! There are no known issues in the current TestRail version we are aware of. We are happy to look into this in detail if you could provide a few examples via email (contact@gurock.com). Do you use the current TestRail version or an older version? I assume you use the rich-text format?

Cheers,
Tobias


#3

I am using the latest. I’ll try taking off some of the training wheels I put on a long while back and see if problems persist. It may also lead to the conclusion that the problem lies elsewhere than in TestRail’s handling of text. If not, I’ll try to put together a simple reproducer for troubleshooting.

I’ll get back to you. :slight_smile:


#4

Thanks, Kent, looking forward to your findings :slightly_smiling:

Cheers,
Tobias


#5

Okay, so here’s what appears to be happening and it’s not testrails fault. :slightly_smiling:

I have some tests that are just checking for correct text content in certain areas of the application. When they fail, I have them give verbose output comparing what was looked for vs. what was found. All well and good, but some of our html (5?) text contains non ascii characters, such as the fancy quotation marks that you will find in Microsoft Word and that sort of thing. Apparently HTML 5 can handle these, but they break the formatting of the json request when I make the add_result call to post results (including the verbose description which includes the non-ascii characters). So the json is dead on arrival and TestRail has every right to complain.

What I will be looking for now is a simple way for the PHP script to cleans the results of all non ascii characters before posting to testrail. Hopefully that will remedy the situation. I’m all ears if you have any thoughts or recommendations!


#6

I’ve found a bunch of fancy and not so fancy regex’s on stack overflow that should remedy this and reduce any chunk of text to plain ascii, but none seem to work in my case. I also do a json_encode (which I see already in the testrail portion of the PHP code) but that doesn’t seen to do it either.

For now, I just take the offending text out of the test. Look forward to hearing if anyone else has dealt with this successfully. I must be missing something very simple!


#7

I’m having better luck sanitizing the text on the Java end instead of PHP. Didn’t think of that before!

Question: How can I force things like a carriage return and such in the TestRail comment area? It seems to ignore things like \n and such. Is there a special markup language?


#8

I see now you accept Markdown.


#9

Final issue: My output in TestNG comes out with a bunch of lines separated by “\n”, which of course just gets printed out in one big glob when posted as text in TestRail. I’d like to search/replace those ("\n") with something else that will appear as actual line breaks in Markdown/TestRail.

I’ve tried many permutations of this:

text.replaceAll("\n,", “\s\s\n”)

…with no luck. My regex-fu is not so great, so alas I’m relying on luck a lot more than I should be! 2 spaces and a carriage return is all I ask… ; )


#10

Hello Kent,

Thanks for the additional details! I can recommend working with UTF-8 on the client side and TestRail fully supports Unicode and UTF-8 everywhere. If you are working with a non-ascii codepage or encoding in PHP, you can usually just utf8_encode your strings/texts before passing your data to TestRail’s API/the API client:

http://php.net/manual/en/function.utf8-encode.php

Are you already using our official PHP binding for the API?

http://docs.gurock.com/testrail-api2/bindings-php

Update:

Regarding json_encode: json_encode also expects UTF-8 data and passing a different encoding will likely result in invalid JSON.

Regarding newline characters: you can simply add "\n" or "\r\n" sequences to your texts to add newlines or empty lines. Some programming languages differentiate between '\n' (single-quote) and "\n" (double-quote) and only the latter would result in the actual newline characters you want.

I hope this helps!

Cheers,
Tobias


#11

Tobias,

\n, \r\n and so on appear as plain text and don’t affect the formatting at all in test results for me. For example, if I edit an existing test result and type the following:

Here is one line\nAnd here is another

I will get exactly that string as the final result, with no newline between them. Same if I use \r\n.

Is there a sample test case/result somewhere I could look at to see how this works?

Thanks,

-Kent


#12

For the first problem, utf8_encode solves it perfectly. Thanks!


#13

Hello Kent,

Great to hear that you are making progress. :slight_smile:

Regarding the newlines: if the \n etc. sequences are appearing as plain text, then this is likely caused by the formatting rule I mentioned earlier. Do you still use PHP? If so, you would need to make sure to specify the \n or \r\n characters with double-quotes:

http://php.net/manual/en/language.types.string.php#language.types.string.syntax.double

I hope this helps!

Cheers,
Tobias


#14

I’ve got it working now Tobias. I was complicating things way more than necessary!


#15

Hi Kent,

Great to hear that it works now :slightly_smiling:

Cheers,
Tobias