/** * Function for obtaining the HTML BODY using the selected * {@link org.openqa.selenium.WebDriver}. * There are a number of configuration properties within * <code>nutch-site.xml</code> which determine whether to * take screenshots of the rendered pages and persist them * as timestamped .png's into HDFS. * @param url the URL to fetch and render * @param conf the {@link org.apache.hadoop.conf.Configuration} * @return the rendered inner HTML page */ public static String getHtmlPage(String url, Configuration conf) { WebDriver driver = getDriverForPage(url, conf); try { if (conf.getBoolean("selenium.take.screenshot", false)) { takeScreenshot(driver, conf); } String innerHtml = driver.findElement(By.tagName("body")).getAttribute("innerHTML"); return innerHtml; // I'm sure this catch statement is a code smell ; borrowing it from lib-htmlunit } catch (Exception e) { TemporaryFilesystem.getDefaultTmpFS().deleteTemporaryFiles(); throw new RuntimeException(e); } finally { cleanUpDriver(driver); } }
private DefaultSession(final DriverFactory factory, TemporaryFilesystem tempFs, SessionId sessionId, final Capabilities capabilities) throws Exception { this.knownElements = new KnownElements(); this.sessionId = sessionId; this.tempFs = tempFs; final BrowserCreator browserCreator = new BrowserCreator(factory, capabilities); final FutureTask<EventFiringWebDriver> webDriverFutureTask = new FutureTask<EventFiringWebDriver>(browserCreator); executor = new ThreadPoolExecutor(1, 1, 600L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>()); // Ensure that the browser is created on the single thread. EventFiringWebDriver initialDriver = execute(webDriverFutureTask); if (!isQuietModeEnabled(browserCreator, capabilities)) { // Memo to self; this is not a constructor escape of "this" - probably ;) initialDriver.register(new SnapshotScreenListener(this)); } this.driver = initialDriver; this.capabilities = browserCreator.getCapabilityDescription(); updateLastAccessTime(); }
public static void cleanUpDriver(WebDriver driver) { if (driver != null) { try { driver.quit(); TemporaryFilesystem.getDefaultTmpFS().deleteTemporaryFiles(); } catch (Exception e) { throw new RuntimeException(e); } } }
@Override public String call() throws Exception { TemporaryFilesystem tempfs = getSession().getTemporaryFileSystem(); File tempDir = tempfs.createTempDir("upload", "file"); new Zip().unzip(file, tempDir); // Select the first file File[] allFiles = tempDir.listFiles(); if (allFiles == null || allFiles.length != 1) { throw new WebDriverException("Expected there to be only 1 file. There were: " + allFiles.length); } return allFiles[0].getAbsolutePath(); }
public static Session createSession(DriverFactory factory, SessionId sessionId, Capabilities capabilities) throws Exception { File tmpDir = new File(System.getProperty("java.io.tmpdir"), sessionId.toString()); if (!tmpDir.mkdir()) { throw new WebDriverException("Cannot create temp directory: " + tmpDir); } TemporaryFilesystem tempFs = TemporaryFilesystem.getTmpFsBasedOn(tmpDir); return new DefaultSession(factory, tempFs, sessionId, capabilities); }
@VisibleForTesting public static Session createSession(DriverFactory factory, TemporaryFilesystem tempFs, SessionId sessionId, Capabilities capabilities) throws Exception { return new DefaultSession(factory, tempFs, sessionId, capabilities); }
public TemporaryFilesystem getTemporaryFileSystem() { return tempFs; }
TemporaryFilesystem getTemporaryFileSystem();