简体   繁体   中英

Selenium java+browsermob proxy i wanted to extract separate har file from the pages that my selenium script hits

I have 3 pages to navigate page1, page2 and page3. I wanted to capture 3 separate HAR file. i tried the below code when it visit page1 its extracted page one performance data in har file and when i visited page2 its extracted page1 and page2 data but I wanted a separate one please help me to achieve this thanks.

Proxy and driver initialization class file

public class InitializeBrowserDriver {

    public WebDriver driver;
    public Properties importprop;
    public BrowserMobProxy myproxy;
    public Har har;

    public WebDriver initializer() throws IOException {
        importprop=new Properties();
        FileInputStream filepath=new 
        FileInputStream(System.getProperty("user.dir")+"\\src\\main\\java\\datafile.properties");
        importprop.load(filepath);
        myproxy=new BrowserMobProxyServer();
        myproxy.start(0);
        
        Proxy seleniumproxy=new Proxy();
        seleniumproxy.setHttpProxy("localhost:" +myproxy.getPort());
        seleniumproxy.setSslProxy("localhost:" +myproxy.getPort());
        
        DesiredCapabilities capability=new DesiredCapabilities();
        
        capability.setCapability(CapabilityType.PROXY, seleniumproxy);
        capability.acceptInsecureCerts();
        capability.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);
        
        EnumSet <CaptureType> capturenetworklog=CaptureType.getAllContentCaptureTypes();
        
        capturenetworklog.addAll(CaptureType.getHeaderCaptureTypes());
        capturenetworklog.addAll(CaptureType.getRequestCaptureTypes());
        capturenetworklog.addAll(CaptureType.getResponseCaptureTypes());
        capturenetworklog.addAll(CaptureType.getCookieCaptureTypes());
        
        
        myproxy.setHarCaptureTypes(capturenetworklog);
        myproxy.newHar("newhar");
        
        WebDriverManager.chromedriver().setup();
        ChromeOptions options = new ChromeOptions();
        options.merge(capability);
        driver=new ChromeDriver(options);
        har=myproxy.getHar();
        driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
        return driver;       
    }

    /*public String urlAccess()
    {
        String url=importprop.getProperty("baseurl");
        return url;
    }*/
}

Navigating to different pages using by extending above class:

public class NavigationToPage extends InitializeBrowserDriver {
    
    private void fileWriter() throws IOException {
        String title=driver.getTitle();
        har=myproxy.getHar();
        File myharfile=new File(System.getProperty("user.dir")+"/harfiles/"+title+".har");
        har.writeTo(myharfile);
        System.out.println(title+ "HAR file has been created successfully");
    }
    
    @BeforeClass
    public void beforeMethods() throws IOException {
        driver=initializer();
        driver.get(importprop.getProperty("baseurl"));
        driver.manage().window().maximize();
        fileWriter();
    }
    
    @Test
    public void toContactUs() throws IOException {
                driver.findElement(By.xpath(importprop.getProperty("toContactUs"))).click();
        fileWriter();
    }

    @Test
    public void toSolutions() throws IOException {
        WebDriverWait wait=new WebDriverWait(driver, 3);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(importprop.getProperty("toSolutions"))));
        this.driver.findElement(By.xpath(importprop.getProperty("toSolutions"))).click();
        fileWriter();
    }
    
    @AfterClass
    public void quitBrowser() throws InterruptedException {
        Thread.sleep(1000);
        driver.quit();
        myproxy.stop();
    }
}

Try this solution: https://stackoverflow.com/a/57045946/5966983

  • For each Test / Navigation (In your case - after performing click ) create new HAR. myproxy.newHar("page2"); myproxy.newHar("page3");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM