How to generate .HAR file for web test suite?

Hello Team,
I want to generate .HAR file for my web test suite. Please suggest. Able to generate for API.

1 Like

I have the same requirement, please do let us know if you were able to find a solution.

Hi Team,

I am getting this .HAR file running through SauceLab. But same I need to generate through WebUI. Could you please post the Solution for these

Hi Team Katalon,

I have the same requirement to generate .har file for WebUI testing. Please suggest steps for the same.

Hi guys,

I found a solution.

As first step you should download the BrowserMob Proxy jars file (https://bmp.lightbody.net/) and add this files to KS (Project -> Settings -> Library Management).
After that you should create a Keywords groovy class (or something like that) in order to use the browsermob features. Below an example :

 @Keyword
	def initProxy() {
		BrowserMobProxyServer initPorxy = new BrowserMobProxyServer()
		Proxy initSeleniumProxy = new Proxy()
		DesiredCapabilities InitDesCapa = new DesiredCapabilities()
		
		return [
			initPorxy,
			InitDesCapa,
			initSeleniumProxy
			
		]
		
	}

	@Keyword
	def setUpProxy(BrowserMobProxyServer proxy,DesiredCapabilities desiredCapabilities, Proxy seleniumProxy) {
		proxy.start(0)
		String proxyStr = "localhost:" + proxy.getPort();
		seleniumProxy.setHttpProxy(proxyStr);
		seleniumProxy.setSslProxy(proxyStr);
		desiredCapabilities.setCapability (CapabilityType.ACCEPT_SSL_CERTS, true);
		desiredCapabilities.setCapability (CapabilityType.ACCEPT_INSECURE_CERTS, true);
		desiredCapabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
		return [
			proxy,
			desiredCapabilities
		]
	}

	@Keyword
	def setProxy(BrowserMobProxyServer proxy) {
		proxy.setHarCaptureTypes(CaptureType.getAllContentCaptureTypes());
	proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT, CaptureType.REQUEST_HEADERS, CaptureType.RESPONSE_HEADERS);
	proxy.newHar("request")
	return proxy
	}

now you can recall this methods in test cases/test suites KS components.
For example, I have a test case dedicated to start of my test execution :

......
    BrowserMobProxyServer proxyForHar
    DesiredCapabilities desiredCapabilities
    Proxy seleniumProxy

    (proxyForHar,desiredCapabilities,seleniumProxy) = CustomKeywords.'utility.initProxy'()


    (proxyForHar,desiredCapabilities) = 
    CustomKeywords.'utility.setUpProxy'(proxyForHar,desiredCapabilities,seleniumProxy)


    System.setProperty("webdriver.chrome.driver", DriverFactory.getChromeDriverPath())
    WebDriver driver = new ChromeDriver(desiredCapabilities)

    proxyForHar = CustomKeywords.'utility.setProxy'(proxyForHar)
    GlobalVariable.proxy = proxyForHar

    DriverFactory.changeWebDriver(driver)

    WebUI.navigateToUrl(GlobalVariable.url)
.......

and another test case dedicated to end of my test execution :

.....    
    Har har = GlobalVariable.proxy.getHar()

    FileOutputStream fos = new FileOutputStream("Include/HarName")

    har.writeTo(fos)

    GlobalVariable.proxy.stop()

    WebUI.closeBrowser(FailureHandling.STOP_ON_FAILURE)

When your test execution has been completed, you should find a new har file under the “Include” directory.

I hope I’ve helped :slight_smile:

1 Like