【发布时间】:2016-04-04 07:41:18
【问题描述】:
你能帮我跟踪使用 chrome 驱动程序的页面加载时间吗?我知道 selinium 不适合增加性能时间
我想将捕获时间显示在:
开发者工具->网络->加载时间
【问题讨论】:
标签: java google-chrome selenium automation selenium-chromedriver
你能帮我跟踪使用 chrome 驱动程序的页面加载时间吗?我知道 selinium 不适合增加性能时间
我想将捕获时间显示在:
开发者工具->网络->加载时间
【问题讨论】:
标签: java google-chrome selenium automation selenium-chromedriver
提供的图片链接中的网页加载时间线只不过是您可以通过 BrowserMob 代理获取的 HTTP 存档 (HAR) 数据。
blog post 中给出了 BrowserMob 使用的 java 实现。
import java.io.FileOutputStream;
import org.browsermob.core.har.Har;
import org.browsermob.proxy.ProxyServer;
import org.openqa.selenium.By;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
public class PerfTest {
public static void main(String[] args) throws Exception {
String strFilePath = "";
// start the proxy
ProxyServer server = new ProxyServer(4444);
server.start();
//captures the moouse movements and navigations
server.setCaptureHeaders(true);
server.setCaptureContent(true);
// get the Selenium proxy object
Proxy proxy = server.seleniumProxy();
// configure it as a desired capability
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, proxy);
// start the browser up
WebDriver driver = new FirefoxDriver(capabilities);
// create a new HAR with the label "apple.com"
server.newHar("assertselenium.com");
driver.get("http://assertselenium.com");
// get the HAR data
Har har = server.getHar();
FileOutputStream fos = new FileOutputStream(strFilePath);
har.writeTo(fos);
server.stop();
driver.quit();
}
}
并从上一行生成的 HAR 文件中获取页面加载时间,可以在此堆栈溢出 question 中看到解释。
public class ParseHarFile {
public static void main(String[] args) {
String fileName = "www.google.com.har";
ReadHarFile(fileName);
}
public static void ReadHarFile(String fileName){
File f = new File("C:\\Users\\Administrator\\Desktop\\test_files\\" + fileName);
HarFileReader r = new HarFileReader();
try
{
System.out.println("Reading " + fileName);
HarLog log = r.readHarFile(f);
// Access all pages elements as an object
HarPages pages = log.getPages();
long startTime = pages.getPages().get(0).getStartedDateTime().getTime();
System.out.println("page start time: " + startTime);
// Access all entries elements as an object
HarEntries entries = log.getEntries();
List<HarEntry> hentry = entries.getEntries();
long loadTime = 0;
int entryIndex = 0;
//Output "response" code of entries.
for (HarEntry entry : hentry)
{
System.out.println("entry: " + entryIndex);
//Output request type
System.out.println("request code: "+entry.getRequest().getMethod());
// Output start time
System.out.println("start time: "+entry.getStartedDateTime().getTime());
// Output start time
System.out.println("time: " + entry.getTime());
long entryLoadTime = entry.getStartedDateTime().getTime()
+ entry.getTime();
if(entryLoadTime > loadTime){
loadTime = entryLoadTime;
}
System.out.println();
entryIndex++;
}
long loadTimeSpan = loadTime - startTime;
System.out.println("loadTimeSpan: " + loadTimeSpan);
Double webLoadTime = ((double)loadTimeSpan) / 1000;
double webLoadTimeInSeconds = Math.round(webLoadTime * 100.0) / 100.0;
System.out.println("Web Load Time: " + webLoadTimeInSeconds) ;
}
catch (JsonParseException e)
{
e.printStackTrace();
System.out.println("Parsing error during test");
}
catch (IOException e)
{
e.printStackTrace();
System.out.println("IO exception during test");
}
}
}
【讨论】:
ProxyServer server = new ProxyServer(9090);。
页面加载时间插件可用于chrome,它在位置栏旁边显示加载时间,并提供详细信息。
【讨论】:
参考这个:-
how to access Network panel on google chrome developer toools with selenium?
虽然您只是想要时间,但请使用以下代码:-
long start = System.currentTimeMillis();
driver.get("Some url");
long finish = System.currentTimeMillis();
long totalTime = finish - start;
System.out.println("Total Time for page load - "+totalTime);
【讨论】: