Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The parameter "contents" from addResponseFilter((response, contents, messageInfo) -> sometimes comes null #885

Open
josemariopedrosa opened this issue Nov 19, 2021 · 0 comments

Comments

@josemariopedrosa
Copy link

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();
}`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant