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