Skip to content

Instantly share code, notes, and snippets.

@barancev
Created January 20, 2014 10:59
Show Gist options
  • Save barancev/8518256 to your computer and use it in GitHub Desktop.
Save barancev/8518256 to your computer and use it in GitHub Desktop.
INFO 01/20 10:53:06 o.b.p.j.h.HttpServer - Version Jetty/5.1.x
INFO 01/20 10:53:07 o.b.p.j.u.Container - Started HttpContext[/,/]
INFO 01/20 10:53:07 o.b.p.j.h.SocketLis~ - Started SocketListener on 0.0.0.0:8091
INFO 01/20 10:53:07 o.b.p.j.u.Container - Started org.browsermob.proxy.jetty.jetty.Server@48b915d
Using anonymous profile
Set Driver to FF
driver for thread = FirefoxDriver: firefox on XP (14edeb49-c0ca-4026-9d16-1bde8d79b7b8)
import java.io.File;
import java.io.FileOutputStream;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.*;
import org.browsermob.core.har.Har;
import org.browsermob.proxy.ProxyServer;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
/**
* The wrapper for the WebDriver
*/
public class WebDriverWrapper {
private static WebDriver driver = null;
private static ThreadLocal<WebDriver> driverForThread = new ThreadLocal<WebDriver>() {
@Override
protected WebDriver initialValue() {
WebDriver driver = null;
try {
driver = getWebDriver("firefox");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return driver;
}
};
private static Har harSF = new Har();
static Random rand = new Random();
private static int proxyPort = rand.nextInt((8120 - 8090) + 1) + 8090;
private static ProxyServer server = new ProxyServer(proxyPort);
/**
* Method get the browser instance and try to create WebDriver object for test
* @return WebDriver after initialization
*/
public static WebDriver getWebDriver() throws Exception {
System.out.println("driver for thread = " + driverForThread.get());
if(driverForThread.get() != null){
return driverForThread.get();
}
driverForThread.set(driver);
return getWebDriver("firefox");
}
/**
* Method helps create new WebDriver object for test accordingly to browser
* @param browser Browser
* @return webDriver WebDriver
* @throws UnknownHostException
*/
public static WebDriver getWebDriver(String browser) throws UnknownHostException {
ProxyServer server = new ProxyServer(proxyPort);
try{
server.start();}
catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}
server.setCaptureHeaders(true);
server.setCaptureContent(true);
server.newHar("test");
DesiredCapabilities capabilities = new DesiredCapabilities();
Proxy proxy = server.seleniumProxy();
FirefoxProfile profile = new FirefoxProfile();
profile.setAcceptUntrustedCertificates(true);
profile.setAssumeUntrustedCertificateIssuer(true);
profile.setPreference("network.proxy.http", "localhost");
profile.setPreference("network.proxy.http_port", proxyPort);
profile.setPreference("network.proxy.ssl", "localhost");
profile.setPreference("network.proxy.ssl_port", proxyPort);
profile.setPreference("network.proxy.type", 1);
profile.setPreference("network.proxy.no_proxies_on", "");
profile.setProxyPreferences(proxy);
capabilities.setCapability(FirefoxDriver.PROFILE,profile);
capabilities.setCapability(CapabilityType.PROXY, proxy);
String profileName = System.getProperty("firefox_profile");
if (profileName != null) {
System.setProperty("webdriver.firefox.profile", profileName);
System.out.println("Using profile " + profileName);
} else {
System.out.println("Using anonymous profile");
}
System.setProperty("webdriver.assume.untrusted.issuer", "false");
driver = new FirefoxDriver(capabilities);
harSF = server.getHar();
maximize();
System.out.println("Set Driver to FF");
return driver;
}
public static void maximize(){
driver.manage().window().maximize();
}
public static void doMaximize() {
String script = "if (window.screen){var win = window.open(window.location); win.moveTo(0,0);win.resizeTo(window.screen.availWidth,window.screen.availHeight);};";
((JavascriptExecutor) driver).executeScript(script);
}
/**
* Gets the resource class path.
* @param s the resource
* @return the absolute resource class path String
*/
private static String getClassPathResourcePath(String s) {
File file = null;
try {
file = new File(WebDriverWrapper.class.getClassLoader().getResource(s).toURI());
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
final String absolutePath = file.getAbsolutePath();
System.out.println("Driver path: " + absolutePath);
return absolutePath;
}
/**
* Close browser.
* @param driver the WebDriver
*/
public static void closeBrowser(WebDriver driver){
try {
String workspace = System.getenv("WORKSPACE");
String nodeName = System.getenv("NODE_NAME");
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy__HH_mm_ss");
String snapshotName = "har" + nodeName + "_" + workspace;
String harPath = workspace + "/har_files/" + snapshotName + "---" + format.format(new Date()) + ".txt";
System.out.println("Writing to " + harPath);
FileOutputStream fos = new FileOutputStream(harPath);
harSF.writeTo(fos);
server.stop();
//we must call quit every time, despite of the browser. Otherwise chromedriver remains in memory
driver.quit();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
WebDriver driver = WebDriverWrapper.getWebDriver();
driver.manage().window().maximize();
driver.quit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment