Hello fellow testers!
I’ve seen several topics around how to add custom header values to all request when executing WebUI tests with Katalon, so I thought I’d share the results of my labor to get this working
This approach only works for Chrome at the moment, but as you’ll see, it would be easy to modify for other browser types.
1.) Download the zip file from the BrowserMob Proxy website, and extract the browsermob-core-{version}-sources.jar to the Drivers folder for your project.
2.) Add the following code to your script:
import org.openqa.selenium.Proxy
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.remote.CapabilityType
import org.openqa.selenium.remote.DesiredCapabilities
import com.kms.katalon.core.configuration.RunConfiguration
import com.kms.katalon.core.webui.driver.DriverFactory
import com.kms.katalon.core.webui.driver.WebUIDriverType
import com.kms.katalon.core.webui.util.WebDriverPropertyUtil
import net.lightbody.bmp.BrowserMobProxy
import net.lightbody.bmp.BrowserMobProxyServer
import net.lightbody.bmp.client.ClientUtil
// create a map to hold our custom header values...
Map<String, String> headers = new HashMap<String, String>();
headers.put("some_header", "some_value");
// instantiate a BrowserMob proxy, and add our custom headers...
BrowserMobProxy proxyServer = new BrowserMobProxyServer();
proxyServer.start(0);
proxyServer.addHeaders(headers);
Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxyServer);
// grab any existing desired capabilities, as configured in Project > Settings > Desired Capabilities > WebUI...
Map<String, Object> driverPreferenceProps = RunConfiguration.getDriverPreferencesProperties(DriverFactory.WEB_UI_DRIVER_PROPERTY);
DesiredCapabilities desiredCapabilities = WebDriverPropertyUtil.toDesireCapabilities(driverPreferenceProps, DriverFactory.getExecutedBrowser());
// add our proxy to the list...
desiredCapabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
// tell Katalon to use our custom driver...
System.setProperty("webdriver.chrome.driver", DriverFactory.getChromeDriverPath());
DriverFactory.changeWebDriver(new ChromeDriver(desiredCapabilities));
// DO NOT use WebUI.openBrowser(), as this will close our custom browser and open a new one...
WebUI.navigateToUrl("www.google.com");
Of course, it would be much cleaner to encapsulate this into a custom keyword, then pass your header map into it, but I’m just posting it this way for simplicity. You can also tweak this to work with other browser types by updating the lines under // tell Katalon to use our custom driver..
to instantiate a different one. I haven’t found a way to easily do this dynamically (based on your selection of the browser in the studio itself) using the existing DriverFactory methods, as most of the ones we would need are private (@ThanhTo @devalex88, maybe you can provide some suggestions).
Hope this is helpful! I’m also open to improvement suggestions, as this is just my first attempt.