WebUI.authenticate with url with a port number fails

*Summary
It is not possible to authenticate a url which contains a port number (which happens often when running on localhost)

*Steps to reproduce
Try running this piece of code:

WebUI.authenticate("http://localhost:3000", "username", "password")

This fails, because the AuthenticateKeyword.getAuthenticatedUrl functions transforms this url into: http://username:password@localhost instead of http://username:password@localhost:3000 and therefore fails to navigate to the page and authenticate.

*Expected Results
Navigating to the page and authenticate it with the given credentials
This should fix the issue:

    private String getAuthenticatedUrl(URL url, String userName, String password) {
        StringBuilder getAuthenticatedUrl = new StringBuilder()

        getAuthenticatedUrl.append(url.getProtocol())
        getAuthenticatedUrl.append("://")
        getAuthenticatedUrl.append(userName)
        getAuthenticatedUrl.append(":")
        getAuthenticatedUrl.append(password)
        getAuthenticatedUrl.append("@")
        getAuthenticatedUrl.append(url.getHost())
        getAuthenticatedUrl.append(url.getPort())
        getAuthenticatedUrl.append(url.getPath())

        return getAuthenticatedUrl.toString()
    }

*Actual Results
Throwing an error.

*Screenshots / Videos
(please attach screenshots or videos if necessary to reproduce the issue)

*Blocker?
Yes

Number of affected users?
5


*Operating System
Windows 11

*Katalon Studio version
version 9.7.4

Environment (for Web Testing)
Chrome 133

Hi @peridos-team,

Welcome to our community. Thank you for letting us know this. I will check with my team and back to you soon the update on this.

You may try basic authentication URL string.

Instead of:
WebUI.authenticate("http://localhost:3000", "username", "password")

you could try:

String basicAuthUrl = "http://" +"<username>"+":"+"<password>"+"@localhost:3000"
 `WebUI.navigateToUrl(basicAuthUrl)`

Hope it helps!

@peridos-team

I tried to reproduce your problem. I made a Test Case:

/**
 *
 */
String getAuthenticatedUrl(URL url, String userName, String password) {
	StringBuilder getAuthenticatedUrl = new StringBuilder()

	getAuthenticatedUrl.append(url.getProtocol())
	getAuthenticatedUrl.append("://")
	getAuthenticatedUrl.append(userName)
	getAuthenticatedUrl.append(":")
	getAuthenticatedUrl.append(password)
	getAuthenticatedUrl.append("@")
	println "url.getHost()=\'${url.getHost()}\'"
	println "url.getPort()=\'${url.getPort()}\'"
	println "url.getPath()=\'${url.getPath()}\'"
	getAuthenticatedUrl.append(url.getHost())
	getAuthenticatedUrl.append(":")
	getAuthenticatedUrl.append(url.getPort())
	getAuthenticatedUrl.append(url.getPath()) 

	return getAuthenticatedUrl.toString()
}

URL url = new URL("http://localhost:3000")
String newUrl = getAuthenticatedUrl(url, "username", "password")
println newUrl

assert newUrl == "http://username:password@localhost:3000"

When I ran this, I got the output:

2月 18, 2025 7:00:51 午前 com.kms.katalon.core.logging.KeywordLogger startTest
情報: --------------------
2月 18, 2025 7:00:51 午前 com.kms.katalon.core.logging.KeywordLogger startTest
情報: START Test Cases/peridos-team
url.getHost()='localhost'
url.getPort()='3000'
url.getPath()=''
http://username:password@localhost3000
2月 18, 2025 7:00:52 午前 com.kms.katalon.core.logging.KeywordLogger log
重大: ❌ Test Cases/peridos-team FAILED.
Reason:
Assertion failed: 

assert newUrl == "http://username:password@localhost:3000"
       |      |
       |      false
       'http://username:password@localhost3000'

	at peridos-team.run(peridos-team:24)
	at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
	at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
	at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:448)
	at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:439)
	at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:418)
	at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:410)
	at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:285)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:137)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:125)
	at TempTestCase1739829645836.run(TempTestCase1739829645836.groovy:25)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

2月 18, 2025 7:00:52 午前 com.kms.katalon.core.logging.KeywordLogger log
重大: ❌ Test Cases/peridos-team FAILED.
Reason:
Assertion failed: 

assert newUrl == "http://username:password@localhost:3000"
       |      |
       |      false
       'http://username:password@localhost3000'

	at peridos-team.run(peridos-team:24)
	at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
	at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
	at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:448)
	at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:439)
	at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:418)
	at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:410)
	at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:285)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:137)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:125)
	at TempTestCase1739829645836.run(TempTestCase1739829645836.groovy:25)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

2月 18, 2025 7:00:52 午前 com.kms.katalon.core.logging.KeywordLogger endTest
情報: END Test Cases/peridos-team

The code has an obvious mistake.

You should rather write:

        getAuthenticatedUrl.append(url.getHost())
        getAuthenticatedUrl.append(":")          // INSERT THIS!
        getAuthenticatedUrl.append(url.getPort())

Hi @peridos-team,

WebUI.authenticate() really does not work with localhost:port, our team will review this for improvement in the future.

For now, I would like to suggest the sample here:

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

WebUI.openBrowser('')

// Instead of using WebUI.authenticate("http://localhost:5000", 'admin', 'password123', 12)
WebUI.navigateToUrl('http://admin:password123@localhost:5000')

WebUI.closeBrowser()

Hope this can help

I examined this.

I wrote a Test Case.

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

WebUI.authenticate("http://localhost:5000", 'admin', 'password123', 12)

When I ran this, the test case failed. I got the following output in the Console tab.

2月 22, 2025 6:36:24 午前 com.kms.katalon.core.logging.KeywordLogger logFailed
重大: ❌ Unable to navigate to authenticated page (Root cause: com.kms.katalon.core.exception.StepFailedException: Unable to navigate to authenticated page
	at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.stepFailed(WebUIKeywordMain.groovy:117)
	at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.runKeyword(WebUIKeywordMain.groovy:43)
	at com.kms.katalon.core.webui.keyword.builtin.AuthenticateKeyword.authenticate(AuthenticateKeyword.groovy:51)
	at com.kms.katalon.core.webui.keyword.builtin.AuthenticateKeyword.execute(AuthenticateKeyword.groovy:44)
	at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:74)
	at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.authenticate(WebUiBuiltInKeywords.groovy:2793)
	at TC1.run(TC1:20)
...

In the Console tab, I pointed the portion of AuthenticateKeyword.groovy:51 in the stacktrace message, and I clicked it with the mouse pointer; then Katalon Studio opened the source code of com.kms.katalon.core.webui.keyword.builtin.AuthenticateKeyword.

In there I found the the implementation of the getAuthenticatedUrl method:

private String getAuthenticatedUrl(URL url, String userName, String password) {
        StringBuilder getAuthenticatedUrl = new StringBuilder()

        getAuthenticatedUrl.append(url.getProtocol())
        getAuthenticatedUrl.append("://")
        getAuthenticatedUrl.append(userName)
        getAuthenticatedUrl.append(":")
        getAuthenticatedUrl.append(password)
        getAuthenticatedUrl.append("@")
        getAuthenticatedUrl.append(url.getHost())
            // OBVIOUS MISTAKE HERE
        getAuthenticatedUrl.append(url.getPath())

        return getAuthenticatedUrl.toString()
    }

The getAuthenticatedUrl has an obvious mistake: the port number is missing. Therefore the WebUI.authenticate() keyword wont work.

Hi all,

This has been enhanced in our v10.2.3, WebUI.authenticate keyword is now supported on TestCloud, remote, and localhost test runs. Thank you