1. web页面性能-browsermob-proxy
Browsermob-proxy开源工具,用来截取页面加载性能相关的数据。数据格式为HTML Archive(HAR), 本质上是json,用来存储http请求/响应的信息
。这些数据可以被其他支出har的http分析工具使用,如firebug,httpwatch,fiddler等。通过这些数据,来分析网站的web前端性能瓶颈。
下载地址: https://github.com/lightbody/browsermob-proxy/releases
1) 把 jar包 加入到项目中
2) 代码:
package seleniumTest;
import java.io.File;
import java.io.IOException;
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;
import net.lightbody.bmp.core.har.Har;
import net.lightbody.bmp.proxy.ProxyServer;
public class browsermobproxyTest {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
ProxyServer server = new ProxyServer(9097);
server.start();
Proxy proxy = server.seleniumProxy();
DesiredCapabilities desiredCapabilities =new DesiredCapabilities();
desiredCapabilities.setCapability(CapabilityType.PROXY, proxy);
WebDriver driver=new FirefoxDriver(desiredCapabilities);
//driver.get("https://www.baidu.com/");
driver.manage().window().maximize();
server.newHar("baidu");
driver.get("https://www.baidu.com/");
Har har=server.getHar();<span style="font-family: Arial, Helvetica, sans-serif;">//获取</span><span class="s1" style="font-family: Arial, Helvetica, sans-serif;">har</span><span style="font-family: Arial, Helvetica, sans-serif;">数据</span>
File harFile = new File("/Users/apple/Documents/harbaidu.com.json");
har.writeTo(harFile);
server.stop();
driver.close();
}
}
3) 生成的HAR文件harbaidu.com.json拖入下面的页面中:
http://ericduran.github.io/chromeHAR
效果如下图:看数据加载的时间消耗
2. jmeter
如果想在jmeter中使用webdriver, 需要安装webdriver 插件,下载地址:
http://www.jmeter-plugins.org/
1) 下载解压后的lib中的jar放入jmeter的lib中
注意:JMeterPlugins-WebDriver.jar 要放入jmeter/lib/ext文件中
2) 使用: http://jmeter-plugins.org/wiki/WebDriverSampler/?utm_source=jmeter&utm_medium=helplink&utm_campaign=WebDriverSampler
var pkg = JavaImporter(org.openqa.selenium)
var support_UI =JavaImporter(org.openqa.selenium.support.ui.WebDriverWait)
var wait = new support_UI.WebDriverWait(WDS.browser,5000)
WDS.sampleResult.sampleStart()
WDS.browser.get('http://www.baidu.com/')
var searchField = WDS.browser.findElement(pkg.By.id('kw'))
searchField.click()
searchField.sendKeys(['jemter webdriver demo'])
var button = WDS.browser.findElement(pkg.By.id('su'))
button.click()
WDS.sampleResult.sampleEnd()
注: 自己试验的报如下错误:
2015/11/14 22:35:31 ERROR - jmeter.threads.JMeterThread: Test failed! java.lang.NoClassDefFoundError: Could not initialize class org.apache.http.conn.ssl.SSLConnectionSocketFactory
junit4:webdriver+jmeter:
http://blog.wedoqa.com/2013/03/how-to-integrate-a-junit4-webdriver-test-into-jmeter/