# Enabling Data URL in Katalon Studio

**URL:** <https://forum.katalon.com/t/enabling-data-url-in-katalon-studio/54146>\
**Category:** Tips and Tricks\
**Tags:** tips-tricks, katalon-studio\
**Created:** [April 24, 2021, 5:28am UTC](https://forum.katalon.com/t/enabling-data-url-in-katalon-studio/54146 "2021-04-24T05:28:32Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [April 24, 2021, 5:28am UTC](https://forum.katalon.com/t/enabling-data-url-in-katalon-studio/54146/1 "2021-04-24T05:28:32Z")

</div>

I have published a GitHub repository.

> **[GitHub - kazurayam/katalon-studio-data-url: Katalon Studio Plugin, which enables...](https://github.com/kazurayam/katalon-studio-data-url)**
>
> Katalon Studio Plugin, which enables WebUI.openBrowser(String url) to open Data URLs. For example, "data:text/html,Hello, world!"

* * *

# Problem to solve

In the Katalon Forum, there are a few posts that ask Katalon Studio to support Data URL.

- [URLs supplied to Katalon APIs MUST support data URIs](http://forum.katalon.com/t/urls-supplied-to-katalon-apis-must-support-data-uris/12203/)
- [Katalon does not support Data URLs (rfc2397)](http://forum.katalon.com/t/katalon-does-not-support-data-urls-rfc2397/23672/)

I want to write a Test Case script like this:

```auto
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

WebUI.openBrowser("data:text/html,<h1>Hello, world!</h1>")

```

Here the code fragment " `data:text/html,<h1>Hello, world!</h1>` " is called [Data URLs](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs) as described in [RFC2397](https://tools.ietf.org/html/rfc2397).

I expect to see a view in browser like this:

[![1_want_to_see_this](https://europe1.discourse-cdn.com/katalon/original/3X/3/b/3b8a74e2238d146cb47c8ce7486ccaeab459f5e9.jpeg)](https://github.com/kazurayam/katalon-studio-data-url/blob/master/docs/images/1_want_to_see_this.png)

But when I executed this script in Katalon Studio v7.9.1, I saw the following result which is disappointing:

[![2_actually_seen](https://europe1.discourse-cdn.com/katalon/original/3X/5/6/56b2fe0541c0b0c321830837e5b42b1e3a6aea0c.jpeg)](https://github.com/kazurayam/katalon-studio-data-url/blob/master/docs/images/2_actually_seen.png)

Katalon Studio’s keywords `WebUI.openBrowser(String url)` and `WebUI.navigateToUrl(String url)` do not support Data URLs.

# Solution

I studied the source code and found a way to enable Katalon Studio to support Data URL. I have developed a plugin module.

# Description

## How to install the plugin

- visit [Releases](https://github.com/kazurayam/katalon-studio-data-url/releases/) page, download the latest version of `katalon-studio-data-url-x.x.x.jar` .
- create your Katalon Studio project, which has a `Plugins` directory under the project directory.
- locate the downloaded jar file in the `Plugins` directory.
- close/reopen your project to let Katalon Studio acknowledge the jar.

You are done.

## How to write your Test Case

In a Test Case script in the Script mode, add one line of `import` statement and one line of `dataurl.enable()` before calling `WebUI.openBrowser(String)` and `WebUI.navigateToUrl(String)` . That’s all. The WebUI keywords will be enabled for `data:` URLs.

Let me show you some examples.

### WebUI.openBrowser() ito open ‘data:text/html’

[`Test Cases/research/3_webui_enabledTo_navigateTo_data_html`](https://github.com/kazurayam/katalon-studio-data-url/blob/master/Scripts/research/3_webui_enabledTo_navigateTo_data_html/Script1619174550245.groovy)

```auto
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

// enable WebUI keywords to accept 'data:' URL
import com.kazurayam.ks.util.DataURLEnabler as dataurl
dataurl.enable()

WebUI.openBrowser("data:text/html,<h1>Hello, world!</h1>")

WebUI.delay(5)
WebUI.closeBrowser()

```

### WebUI.navigateToUrl() to open ‘data:image/png’

[`Test Cases/research/4_webui_enabledTo_navigateTo_data_png`](https://github.com/kazurayam/katalon-studio-data-url/blob/master/Scripts/research/4_webui_enabledTo_navigateTo_data_png/Script1619174581290.groovy)

```auto
import com.kazurayam.ks.util.DataURLEnabler as dataurl
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

// create a "data:image/png;base64,xxxxxxxxxxxxxxx" for demo
String pngdataurl = dataurl.demoImage()
//println pngdataurl

// enable WebUI keywords to accept 'data:' URL
dataurl.enable()

WebUI.openBrowser('')
WebUI.navigateToUrl(pngdataurl)

WebUI.delay(5)
WebUI.closeBrowser()

```

When I ran this script, I got the follwing display result:

[![4_demo_png.png](https://europe1.discourse-cdn.com/katalon/original/3X/3/0/3091da42bfede4e4b6a66a867f9be83c16f73420.jpeg)](https://github.com/kazurayam/katalon-studio-data-url/blob/master/docs/images/4_demo_png.png)

## Research: how Katalon Studio misinterprets a `data:` URL to be a `file:` URL

You can read the source of Katalon Studio Keyword:

- [`WebUI.navigateToUrl`](https://github.com/katalon-studio/katalon-studio-testing-framework/blob/master/Include/scripts/groovy/com/kms/katalon/core/webui/keyword/builtin/NavigateToUrlKeyword.groovy)

I found an interesting portion in it:

```auto
    public void navigateToUrl(String rawUrl, FailureHandling flowControl) throws StepFailedException {
        WebUIKeywordMain.runKeyword({
            ...
            URL url = PathUtil.getUrl(rawUrl, "http")
            ...
            WebDriver webDriver = DriverFactory.getWebDriver()
            webDriver.navigate().to(url.toString())
            ...

```

My experiment revealed that `PathUtil.getUrl("data:text/html,<h1>Hello, world!</h1>", ...)` returned an instance of `java.net.URL` with String value of `file:///Users/username/projectdir/data:text/html,%3Ch1%3EHello,%20world!%3C/h1%3E` .

I am sure that `com.kms.katalon.core.util.internal.PathUtil` is misinterpreting it. So how the source of `PathUtil.getUrl(String url, ...)` looks like?

- [`PathUtil`](https://github.com/katalon-studio/katalon-studio-testing-framework/blob/master/Include/scripts/groovy/com/kms/katalon/core/util/internal/PathUtil.java)

It is just constructing an instance of `java.net.URL` class:

```auto
    public static URL getUrl(String rawUrl, String defaultProtocol) throws MalformedURLException, URISyntaxException {
        URL url = null;
        try {
            url = new URL(rawUrl);
        } catch (MalformedURLException e) {
            ...

```

I consulted with Guru Google.

_Me_ : Does the default implementation of [`java.net.URL`](https://docs.oracle.com/javase/7/docs/api/java/net/URL.html#URL) support Data URL? Can I instanciate it with `new URL("data:text/html,Hello world!")` ?

_Guru_ : No, it doesn’t. As the javadoc clearly states, the default implementation supports only http, https, ftp, file, and jar.

_Me_ : Is it possible to let `java.net.URL` instanciated with with `data:` URL somehow?

_Guru_ : Yes, it is possible. The [javadoc of the constructor of `java.net.URL`](https://docs.oracle.com/javase/7/docs/api/java/net/URL.html#URL(java.lang.String,%20java.lang.String,%20int,%20java.lang.String)) clearly describes how to.

## How this plugin is implemented

The plugin is implemented with just 2 `.groovy` files.

- [`com.kazurayam.ks.util.DataURLEnabler`](https://github.com/kazurayam/katalon-studio-data-url/blob/master/Keywords/com/kazurayam/ks/util/DataURLEnabler.groovy)
- [`com.kazurayam.net.data.Handler`](https://github.com/kazurayam/katalon-studio-data-url/blob/master/Keywords/com/kazurayam/net/data/Handler.groovy)

Please read the source to find out how it is implemented.
