Skip to content

Instantly share code, notes, and snippets.

@meza
Last active December 17, 2015 05:09
Show Gist options
  • Save meza/5555475 to your computer and use it in GitHub Desktop.
Save meza/5555475 to your computer and use it in GitHub Desktop.
Page load time testing
package hu.meza;
import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import net.lightbody.bmp.core.har.Har;
import net.lightbody.bmp.core.har.HarLog;
import net.lightbody.bmp.core.har.HarPage;
import net.lightbody.bmp.proxy.ProxyServer;
import org.junit.Assert;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.browserlaunchers.CapabilityType;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class PageLoadTestSteps {
public static final String PROXY = "localhost:4444";
private static ProxyServer server;
private static WebDriver driver;
private DesiredCapabilities capabilities;
public PageLoadTestSteps() {
if (server == null) {
server = new ProxyServer(4444);
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
driver.close();
}
});
}
}
@Before
public void setUpProxy() throws Exception {
server.start();
if (driver == null) {
Proxy proxy = server.seleniumProxy();
proxy.setHttpProxy(PROXY).setHttpsProxy(PROXY).setSocksProxy(PROXY);
capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, proxy);
driver = new FirefoxDriver(capabilities);
}
}
@After
public void cleanProxy() throws Exception {
server.cleanup();
server.stop();
}
@When("^I visit \"([^\"]*)\" without previously being there$")
public void visitWithoutPreviouslyBeingThere(String url) {
server.newHar("page");
driver.get(url);
server.endPage();
}
@When("^I visit \"([^\"]*)\" with previously being there$")
public void visitWithPreviouslyBeingThere(String url) {
driver.get(url);
server.newHar("page");
driver.get(url);
server.endPage();
}
@Then("^I should experience a load time below \"([^\"]*)\" seconds$")
public void shouldExperienceLoadTimeBelow(float seconds) {
Har har = server.getHar();
HarLog log = har.getLog();
for (HarPage page : log.getPages()) {
Long actual = page.getPageTimings().getOnLoad();
Assert.assertNotNull(actual);
float expected = seconds * 1000;
Assert.assertTrue(String.format("Page load time was more than %f. It was %d", expected, actual),
actual < expected);
}
}
}
Feature: page load times
Scenario: uncached page load time
When I visit "http://www.google.com" without previously being there
Then I should experience a load time below "2" seconds
Scenario: cached page load time
When I visit "http://www.google.com" with previously being there
Then I should experience a load time below "0.8" seconds
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>hu.meza</groupId>
<artifactId>page-load-test</artifactId>
<name>Page Load Test</name>
<version>1.0-SNAPSHOT</version>
<properties>
<selenium-api.version>LATEST</selenium-api.version>
<cucumber.version>LATEST</cucumber.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-api</artifactId>
<version>${selenium-api.version}</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-common</artifactId>
<version>${selenium-api.version}</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>${selenium-api.version}</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium-api.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-core</artifactId>
<version>${cucumber.version}</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>${cucumber.version}</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-jvm</artifactId>
<version>${cucumber.version}</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>${cucumber.version}</version>
</dependency>
<dependency>
<groupId>net.lightbody.bmp</groupId>
<artifactId>browsermob-proxy</artifactId>
<version>2.0-beta-8</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-api</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-api</artifactId>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-common</artifactId>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-core</artifactId>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-jvm</artifactId>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
</dependency>
<dependency>
<groupId>net.lightbody.bmp</groupId>
<artifactId>browsermob-proxy</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-api</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.10</version>
<dependencies>
<dependency>
<groupId>hu.meza.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
<configuration>
<configLocation>checkstyle.xml</configLocation>
<consoleOutput>true</consoleOutput>
</configuration>
<executions>
<execution>
<goals>
<goal>check</goal>
</goals>
<phase>verify</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.10</version>
<configuration>
<configLocation>checkstyle.xml</configLocation>
</configuration>
</plugin>
</plugins>
</reporting>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment