...13import org.openqa.selenium.chrome.ChromeOptions;14import org.openqa.selenium.logging.LogEntries;15import org.openqa.selenium.logging.LogEntry;16import org.openqa.selenium.logging.LogType;17import org.openqa.selenium.logging.LoggingPreferences;18import org.openqa.selenium.remote.CapabilityType;19import org.openqa.selenium.remote.DesiredCapabilities;20import java.io.File;21import java.io.FileWriter;22import java.io.IOException;23import java.io.UnsupportedEncodingException;24import java.net.ServerSocket;25import java.net.URLEncoder;26import java.util.Arrays;27import java.util.logging.Level;28/**29 * Created by Erik Krogh Kristensen on 10-11-2015.30 */31public class SeleniumDriver {32 private static String getEmptyPageUrl(String scriptPath, int port) {33 try {34 scriptPath = URLEncoder.encode(scriptPath, "UTF-8");35 } catch (UnsupportedEncodingException e) {36 throw new RuntimeException(e);37 }38 boolean isWindows = System.getProperty("os.name").toLowerCase().contains("windows");39 String workingDir = System.getProperty("user.dir");40 if (isWindows) {41 return "file:///" + workingDir + "\\lib\\selenium\\driver.html?script=" + scriptPath + "&port=" + port;42 } else {43 return "file:///" + workingDir + "/lib/selenium/driver.html?script=" + scriptPath + "&port=" + port;44 }45 }46 public static String executeScript(String script) throws IOException, HttpException {47 setDriverPath();48 ChromeDriver driver = new ChromeDriver(buldCapabilities());49 File scriptFile;50 String tmpFileSuffix = "tmpFileSeleniumDriverThing.js";51 try {52 scriptFile = File.createTempFile("script-", tmpFileSuffix);53 FileWriter out = new FileWriter(scriptFile);54 IOUtils.write(script.getBytes(), out);55 out.close();56 } catch (IOException e) {throw new RuntimeException();}57 ServerSocket socket = new ServerSocket(0);58 int port = socket.getLocalPort();59 System.out.println("Listening for JSNAP at port: " + port);60 driver.get(getEmptyPageUrl(scriptFile.getAbsolutePath(), port));61 String message = getResponse(socket);62 driver.close();63 driver.quit();64 System.out.println("Message recieved, length: " + message.length());65 return message;66 }67 private static String getResponse(ServerSocket serverSocket) throws IOException, HttpException {68 DefaultHttpServerConnection conn = new DefaultHttpServerConnection();69 conn.bind(serverSocket.accept(), new BasicHttpParams());70 HttpRequest request = conn.receiveRequestHeader();71 conn.receiveRequestEntity((HttpEntityEnclosingRequest)request);72 HttpEntity entity = ((HttpEntityEnclosingRequest)request).getEntity();73 return EntityUtils.toString(entity);74 }75 private static LogEntry findJsnapEntry(LogEntries entries) {76 for (LogEntry entry : Lists.reverse(Arrays.asList(Iterators.toArray(entries.iterator(), LogEntry.class)))) {77 if (entry.getLevel() == Level.INFO && entry.getMessage().contains("{\"global\":1")) {78 return entry;79 }80 }81 throw new RuntimeException("Could not find a fitting logEntry");82 }83 private static void setDriverPath() {84 String operatingSystem = System.getProperty("os.name");85 if (operatingSystem.contains("Windows")) {86 System.setProperty("webdriver.chrome.driver", "./lib/selenium/chromedriver.exe");87 } else if (operatingSystem.contains("Linux")) {88 System.setProperty("webdriver.chrome.driver", "./lib/selenium/chromedriverLinux64");89 } else if (operatingSystem.contains("Mac")) {90 System.setProperty("webdriver.chrome.driver", "./lib/selenium/chromedriverMac");91 } else {92 throw new RuntimeException("Unknown operating system: " + operatingSystem);93 }94 }95 private static DesiredCapabilities buldCapabilities() {96 ChromeOptions options = new ChromeOptions();97 options.addArguments("window-size=400,400");98 DesiredCapabilities capabilities = DesiredCapabilities.chrome();99 LoggingPreferences loggingPreferences = new LoggingPreferences();100 loggingPreferences.enable(LogType.BROWSER, Level.ALL);101 capabilities.setCapability(CapabilityType.LOGGING_PREFS, loggingPreferences);102 capabilities.setCapability(ChromeOptions.CAPABILITY, options);103 return capabilities;104 }105}...