browsermob-proxy
browsermob-proxy copied to clipboard
The parameter "contents" from addResponseFilter((response, contents, messageInfo) -> sometimes comes null
Sometimes when I execute the code below the parameter "contents" from the function "addResponseFilter" comes null and I can't set the content to mock the request (seems that when I try to call "setTextContents" with a null contents object the excutuion get out of the loop of addResponseFilter.
Generaly it works at first run of the day or right after open the ide with the code, what makes me think that is a problem with some sychronism, but I don't know how to handle this.
Someone can help me?
` @Test public void mockTest(){ WebDriver driver; BrowserMobProxyServer proxyServer;
proxyServer = new BrowserMobProxyServer();
proxyServer.start(18882);
Proxy proxyConfig = new Proxy()
.setHttpProxy("127.0.0.1:18882")
.setSslProxy("127.0.0.1:18882")
.setFtpProxy("127.0.0.1:18882");
String localPath = System.getProperty("user.dir");
System.setProperty("webdriver.chrome.driver", localPath+"/src/test/resources/drivers/chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.setProxy(proxyConfig);
options.setAcceptInsecureCerts(true);
options.addArguments("--ignore-certificate-errors");
options.addArguments("--acceptSslCerts");
options.addArguments("--disable-web-security");
options.addArguments("--allow-insecure-localhost");
options.addArguments("--ignore-urlfetcher-cert-requests");
proxyServer.newHar("https://url.com.br");
proxyServer.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);
String url = "https://url/auth/v1/userdata";
String responseBody = Utils.getFileContent("src/test/java/com/pv/mockJsons/mock.json");
driver = new ChromeDriver(options);
driver.navigate().to("https://url.com.br/");
proxyServer.addResponseFilter((response, contents, messageInfo) -> {
System.out.println(messageInfo.getOriginalUrl());
if (messageInfo.getOriginalUrl().equals(url)) {
if(messageInfo.getOriginalRequest().getMethod().toString().toLowerCase().equals("get")){
contents.setTextContents(responseBody);
}
}
});
driver.findElement(By.name("usuario")).sendKeys("user");
driver.findElement(By.name("senha")).sendKeys("password");
driver.findElement(By.id("login")).click();
}`