Comparing 2 texts using the java-diff-utils library in Katalon Studio

The original was posted Apr’21. I have revised this at Oct,23.


I have published a GitHub project:


Problem to solve

Web testers often want to compare 2 text files. The files could be in various format: CSV, JSON, XML, etc. They sometimes just want to check if 2 texts are identical or not. And they may want to see the detail differences. Katalon Studio does not offer any texts-diff feature; I want it.

Solution

Utilize java-diff-utils in Katalon Studio.

How to apply this to your own Katalon project

In the Drivers directory of your Katalon project, you NEED to install 2 external jar files.

Library Management

  1. java-diff-utils-4.12.jar

  2. katalon-studio-texts-diff-x.x.x.jar

The java-diff-utils-x.x.x is an OpenSource library in Java for performing the comparison operations between texts. This enables us to generate diff information.

I developed the katalon-studio-texts-diff-x.x.x library which wraps the java-diff-utils-x.x.x to utilize it in your Katalon Studio project. With it, you get a diff report in Markdown format with side-by-side view.

Creating and running your first test

You want to start Katalon Studio GUI and open your project.

You want to create a new Test Case ex01 (… any name you can choose in fact), of which code looks as follows. You can just copy and paste this:

import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths

/**
 * ex01 diff 2 strings and write the report into file
 */

String text1 = """<doc>
<body>
<section>
<p>Hello, John!</p>
</section>
<p></p>
</body>
</doc>
"""

String text2 = """<doc>
<body>
<section id="main">
<p>Hello, Paul!</p>
<p>Have a break!</p>
</section>
</body>
</doc>
"""

String outpath = "build/tmp/testoutput/ex01-output.md"

// take diff of 2 Strings, write the diff
CustomKeywords.'com.kazurayam.ks.TextsDiffer.diffStrings'(text1, text2, outpath)

Path out = Paths.get(outpath)
assert Files.exists(out)
assert out.toFile().length() > 0

Now you can run it as a usual Katalon Test Case by clicking the green button run.

Once done, the script will create a diff report and save it into a file build/tmp/testOutput/ex01-output.md. The output will be formatted in Markdown. The file will like this:

**DIFFERENT**

- inserted rows: 1
- deleted rows : 1
- changed rows : 2
- equal rows: : 5

|line#|S|original|revised|
|-----|-|--------|-------|
|1| |&lt;doc&gt;|&lt;doc&gt;|
|2| |&lt;body&gt;|&lt;body&gt;|
|3|C|<span style="color:red; font-weight:bold; background-color:#ffeef0">&lt;section&gt;</span>|<span style="color:green; font-weight:bold; background-color:#e6ffec">&lt;section id="main"&gt;</span>|
|4|C|&lt;p&gt;Hello, <span style="color:red; font-weight:bold; background-color:#ffeef0">John!&lt;</span>/p&gt;|&lt;p&gt;Hello, <span style="color:green; font-weight:bold; background-color:#e6ffec">Paul!&lt;</span>/p&gt;|
|5|I||<span style="color:green; font-weight:bold; background-color:#e6ffec">&lt;p&gt;Have a break!&lt;/p&gt;</span>|
|6| |&lt;/section&gt;|&lt;/section&gt;|
|7|D|<span style="color:red; font-weight:bold; background-color:#ffeef0">&lt;p&gt;&lt;/p&gt;</span>||
|8| |&lt;/body&gt;|&lt;/body&gt;|
|9| |&lt;/doc&gt;|&lt;/doc&gt;|

The raw Markdown text is hard to read in a plain text editor. So you want to view it using some Markdown viewer. For example, I personally use Visual Studio Code, Markdown preview. You can preview the report in VSCode as follows.

ex01

This looks nice, doesn’t it?

More types of input text

The sample Test Case ex01 uses com.kazurayam.ks.TextsDiffer.diffStrings() method that takes 2 Strings as input. The TextsDiffer class implements more methods that can take various types: Files, URLs etc.

See the Examples document for more use cases.

API

You can have a look at the API documentation of TextsDiffer class at

3 Likes

Hello, Paul!

Have a break!

|line#|original|revised| |-----|--------|-------| |1|<doc>|<doc>| |2|<body>|<body>| |3|*<section>*|**<section id="main">**| |4|<p>Hello, *John!<*/p>|<p>Hello, **Paul!<**/p>| |5||**<p>Have a break!</p>**| |6|</section>|</section>| |7|</body>|</body>| |8|</doc>|</doc>|

The original was posted Apr’21. I have revised this at Oct,23.

The new version provides a jar which contains a custom Keyword class com.kazurayam.ks.TextsDiffer. The keyword enables you to compare Strings, Files, URLs easily.