Selenium获取.har文件

我有一个两页的应用程序:
/登录
/简介
我想得到.har文件页面/个人资料
当我转到页面/登录时 ,使用key = connect.sid和value =“example value”创建cookie。 此cookie尚未激活。 我添加了活动connect.sid的cookie。

WebDriver webDriver = getDriver(); webDriver.get(LOGIN_PAGE); webDriver.manage().addCookie(connectsSId); 

它不起作用,因为在加载页面后,/ login创建了一个新的cookie。 我也试过这段代码:

 WebDriver webDriver = getDriver(); webDriver.get(PROFILE_PAGE); webDriver.manage().deleteAllCookies(); webDriver.manage().addCookie(connectsSId); 

这不起作用。 cookies被添加但似乎为时已晚。

  WebDriver webDriver = getDriver(); LoginPage loginPage = new LoginPage(getDriver()); LandingPage landingPage = loginPage.login(); landingPage.openProfilePage(); 

此代码为页面/登录创建了一个.har文件。
由于某种原因,只有在第一次调用页面后才会创建文件。 我无法解决这个问题。

您可以使用browsermob代理捕获所有请求和响应数据。 请参见此处

将PhantomJS与BrowserMobProxy一起使用。 PhantomJS帮助我们实现JavaScript页面。 以下代码也适用于HTTPS Web地址。

‘phantomjs.exe’放在C盘中,然后在C盘中获得‘HAR-Information.har’文件。

确保你不要在url的末尾加上‘/’ ,比如

 driver.get("https://www.google.co.in/") 

它应该是

 driver.get("https://www.google.co.in"); 

否则,它将无法正常工作。

 package makemyhar; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import net.lightbody.bmp.BrowserMobProxy; import net.lightbody.bmp.BrowserMobProxyServer; import net.lightbody.bmp.core.har.Har; import net.lightbody.bmp.proxy.CaptureType; import org.openqa.selenium.WebDriver; import org.openqa.selenium.phantomjs.PhantomJSDriver; import org.openqa.selenium.phantomjs.PhantomJSDriverService; import org.openqa.selenium.remote.CapabilityType; import org.openqa.selenium.remote.DesiredCapabilities; public class MakeMyHAR { public static void main(String[] args) throws IOException, InterruptedException { //BrowserMobProxy BrowserMobProxy server = new BrowserMobProxyServer(); server.start(0); server.setHarCaptureTypes(CaptureType.getAllContentCaptureTypes()); server.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT); server.newHar("Google"); //PHANTOMJS_CLI_ARGS ArrayList cliArgsCap = new ArrayList<>(); cliArgsCap.add("--proxy=localhost:"+server.getPort()); cliArgsCap.add("--ignore-ssl-errors=yes"); //DesiredCapabilities DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true); capabilities.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, true); capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cliArgsCap); capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,"C:\\phantomjs.exe"); //WebDriver WebDriver driver = new PhantomJSDriver(capabilities); driver.get("https://www.google.co.in"); //HAR Har har = server.getHar(); FileOutputStream fos = new FileOutputStream("C:\\HAR-Information.har"); har.writeTo(fos); server.stop(); driver.close(); } } 

在Selenium代码中设置首选项:

  profile.setPreference("devtools.netmonitor.har.enableAutoExportToFile", true); profile.setPreference("devtools.netmonitor.har.defaultLogDir", String.valueOf(dir)); profile.setPreference("devtools.netmonitor.har.defaultFileName", "network-log-file-%Y-%m-%d-%H-%M-%S"); 

并打开控制台:

 Actions keyAction = new Actions(driver); keyAction.keyDown(Keys.LEFT_CONTROL).keyDown(Keys.LEFT_SHIFT).sendKeys("q").keyUp(Keys.LEFT_CONTROL).keyUp(Keys.LEFT_SHIFT).perform();