javaselenium-webdriverquerystringparameter

Can we use Selenium or any other jars to get network response and validate


I have a requirement here that I need to verify the network response values using selenium & java.

It would be great if there is any code that could help me out.

The action that I want to perform (which is needed to be automated) is

  1. load the application https://www.ancestry.com

  2. press F12 and go to network tab when application is loading

  3. select a respective network containing b/ss

  4. click on it and get the values in the query parameters

I have highlighted them in the below image

Note: I need to perform this action for every click event that happen after the application is loaded.

Is this possible in selenium

Network Request

Network Request

This is the code that I tried to get the values recorded am I doing it right please help

public static void main(String[] args) throws IOException {
    System.setProperty("webdriver.chrome.driver",
            new File(".").getCanonicalPath() + "\\WebDriver\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.get("https://www.ancestry.com");
    //capture(driver);
    driver.quit();
}

public static void capture(WebDriver driver) throws IOException {
    OutputStream logfile = new FileOutputStream(new File(new 
    File(".").getCanonicalPath() + "\\log.txt"), true);
    PrintStream printlog = new PrintStream(logfile);
    LogEntries logs = driver.manage().logs().get(LogType.PERFORMANCE);
    for (LogEntry entry : logs) {
        if (entry.toString().contains("\"type\":\"Img\""))
            System.out.println(entry.toString());

        printlog.append(new Date(entry.getTimestamp()) + " " + entry.toString() + " "
                + System.getProperty("line.separator"));
        printlog.append(System.getProperty("line.separator"));
        printlog.append(System.getProperty("line.separator"));
    }
    printlog.close();
}

Solution

  • We can get all the network logs using browser mob proxy

    private static BrowserMobProxy proxy = new BrowserMobProxyServer();
    public webLib(String browser) {
            logReport.logMethodStart("Launch Application");
            Proxy seleniumProxy = null;
            proxy.start();
            seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
    
            if (browser.equalsIgnoreCase("chrome")) {
                try {
                    System.setProperty("webdriver.chrome.driver",
                            new File(".").getCanonicalFile() + seperator + "WebDriver" + seperator + "chromeDriver.exe");
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
                ChromeOptions options = new ChromeOptions();
    
                options.setCapability(CapabilityType.PROXY, seleniumProxy);
                options.setAcceptInsecureCerts(true);
                options.addArguments("--ignore-certificate-errors");
                options.addArguments("--disable-popup-blocking");
                options.addArguments("--no-sandbox");
                options.addArguments("--no-sandbox");
    
                driver = new ChromeDriver(options);
            } else if (browser.equalsIgnoreCase("firefox")) {
    
            }
    
            driver.manage().deleteAllCookies();
            driver.manage().window().maximize();
            driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    
            if (driver != null) {
                logReport.logStep(Status.PASS, "Browser launched successfully", driver);
            }
    
        }
    

    You can reffer the following site https://www.swtestacademy.com/webdriver-browsermob-proxy/