Example usage for org.openqa.selenium.io TemporaryFilesystem getDefaultTmpFS

List of usage examples for org.openqa.selenium.io TemporaryFilesystem getDefaultTmpFS

Introduction

In this page you can find the example usage for org.openqa.selenium.io TemporaryFilesystem getDefaultTmpFS.

Prototype

public static TemporaryFilesystem getDefaultTmpFS() 

Source Link

Usage

From source file:com.opera.core.systems.OperaProfile.java

License:Apache License

/**
 * Creates a new, fresh random profile for Opera to use.  The actual profile directory will not be
 * generated until Opera is started.//from ww w.  j  a v a2 s.  c  o  m
 *
 * This is supported on all products that have the <code>-pd</code> command-line argument, such as
 * Opera Desktop.  core-gogi and Opera Mobile is not supported.
 */
public OperaProfile() {
    this(TemporaryFilesystem.getDefaultTmpFS().createTempDir("opera", "profile"));
    randomProfile = true;
}

From source file:com.opera.core.systems.OperaProfile.java

License:Apache License

/**
 * Cleans up the profile.  If the profile was random, it will be deleted.  This method should
 * typically be called when Opera is shut down.
 *///www.jav  a  2 s . c om
public void cleanUp() {
    if (randomProfile) {
        TemporaryFilesystem.getDefaultTmpFS().deleteTempDir(getDirectory());
    }
}

From source file:com.opera.core.systems.preferences.OperaFilePreferences.java

License:Apache License

/**
 * Constructs a new representation of Opera's preferences based on the given preference file.
 *
 * @param preferenceFile an INI style preference file
 *///from w w w  .j  a va2  s.co m
public OperaFilePreferences(File preferenceFile) {
    this.preferenceFile = preferenceFile;

    // Create new preference file if it doesn't exist
    if (!preferenceFile.exists()) {
        try {
            if (!preferenceFile.createNewFile()) {
                throw new IOException("File exists");
            }
        } catch (IOException e) {
            throw new WebDriverException("Unable to create new preference file: " + e.getMessage());
        }

        return;
    }

    // Due to the sucky nature of Opera's invalid preference files, we are forced to remove the
    // first line of the file.
    //
    // opera.ini looks much like this:
    //
    //   <BOM>
    //   Opera Preferences version 2.1
    //   ; Do not edit this file while Opera is running
    //   ; This file is stored in UTF-8 encoding
    //
    //   [User Prefs]
    //   Language Files Directory=
    //
    //     &c.

    TemporaryFilesystem fs = null;
    File temporaryPreferenceFile;
    Ini ini;

    try {
        fs = TemporaryFilesystem.getDefaultTmpFS();
        temporaryPreferenceFile = new File(fs.createTempDir("operadriver", "preferences").getAbsolutePath()
                + File.separator + "opera.ini");

        BufferedReader reader = new BufferedReader(new FileReader(preferenceFile));
        BufferedWriter writer = new BufferedWriter(new FileWriter(temporaryPreferenceFile));

        String newLine = System.getProperty("line.separator");
        String currentLine;
        while ((currentLine = reader.readLine()) != null) {
            if (!currentLine.contains("Opera Preferences version")) {
                writer.write(currentLine + newLine);
            }
        }

        // Make sure channels are closed so that last line is flushed
        reader.close();
        writer.close();

        // Read new preference file
        ini = new Ini(temporaryPreferenceFile);
    } catch (FileNotFoundException e) {
        throw new WebDriverException("Unknown file: " + preferenceFile.getAbsolutePath());
    } catch (IOException e) {
        throw new WebDriverException("Unable to read file: " + preferenceFile.getPath());
    } finally {
        if (fs != null) {
            fs.deleteTemporaryFiles();
        }
    }

    // Add each preference entry
    for (Map.Entry<String, Profile.Section> section : ini.entrySet()) {
        for (Map.Entry<String, String> entry : section.getValue().entrySet()) {
            set(section.getValue().getName(), entry.getKey(), entry.getValue());
        }
    }
}

From source file:com.screenslicer.core.scrape.Scrape.java

License:Open Source License

public static void forceQuit() {
    try {//from ww w.  ja v a  2s . c o m
        if (driver != null) {
            ((FirefoxDriver) driver).kill();
            Util.driverSleepStartup();
        }
    } catch (Throwable t) {
        Log.exception(t);
    }
    try {
        TemporaryFilesystem tempFS = TemporaryFilesystem.getDefaultTmpFS();
        tempFS.deleteTemporaryFiles();
    } catch (Throwable t) {
        Log.exception(t);
    }
}

From source file:com.thoughtworks.selenium.webdriven.commands.AttachFile.java

License:Apache License

private File downloadFile(String name) {
    URL url = getUrl(name);// w w w. j a  v  a2  s  .co m

    File dir = TemporaryFilesystem.getDefaultTmpFS().createTempDir("attachFile", "dir");
    File outputTo = new File(dir, new File(url.getFile()).getName());

    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(outputTo);
        Resources.copy(url, fos);
    } catch (IOException e) {
        throw new SeleniumException("Can't access file to upload: " + url, e);
    } finally {
        try {
            if (fos != null) {
                fos.close();
            }
        } catch (IOException e) {
            // Nothing sane to do. Log and continue.
            LOGGER.log(Level.WARNING, "Unable to close stream used for reading file: " + name, e);
        }
    }

    return outputTo;
}

From source file:crazy.seleiumTools.ProfilesIni.java

License:Apache License

public FirefoxProfile getDefaultProfile() {
    String profileName = this.nameOfpro;
    File profileDir = profiles.get(profileName);
    if (profileDir == null)
        return null;

    // Make a copy of the profile to use
    File tempDir = TemporaryFilesystem.getDefaultTmpFS().createTempDir("userprofile", "copy");
    try {/*from w w w . j  av  a  2  s .com*/
        FileHandler.copy(profileDir, tempDir);

        // Delete the old compreg.dat file so that our new extension is registered
        File compreg = new File(tempDir, "compreg.dat");
        if (compreg.exists()) {
            if (!compreg.delete()) {
                throw new WebDriverException("Cannot delete file from copy of profile " + profileName);
            }
        }
    } catch (IOException e) {
        throw new WebDriverException(e);
    }

    return new FirefoxProfile(tempDir);
}

From source file:crazy.seleiumTools.ProfilesIni.java

License:Apache License

public FirefoxProfile getProfile(String profileName) {
    File profileDir = profiles.get(profileName);
    if (profileDir == null)
        return null;

    // Make a copy of the profile to use
    File tempDir = TemporaryFilesystem.getDefaultTmpFS().createTempDir("userprofile", "copy");
    try {//  w w w  . j  av  a 2s .  c  o  m
        FileHandler.copy(profileDir, tempDir);

        // Delete the old compreg.dat file so that our new extension is registered
        File compreg = new File(tempDir, "compreg.dat");
        if (compreg.exists()) {
            if (!compreg.delete()) {
                throw new WebDriverException("Cannot delete file from copy of profile " + profileName);
            }
        }
    } catch (IOException e) {
        throw new WebDriverException(e);
    }

    return new FirefoxProfile(tempDir);
}

From source file:edu.usc.cs.ir.htmlunit.handler.HtmlUnitWebDriver.java

License:Apache License

public static String getHTMLContent(WebDriver driver) {
    try {//  w ww . j a  v a 2s. c o  m

        String innerHtml = "";
        if (enableJavascript) {
            WebElement body = driver.findElement(By.tagName("body"));
            innerHtml = (String) ((JavascriptExecutor) driver).executeScript("return arguments[0].innerHTML;",
                    body);
        } else
            innerHtml = driver.getPageSource().replaceAll("&amp;", "&");
        return innerHtml;
    } catch (Exception e) {
        TemporaryFilesystem.getDefaultTmpFS().deleteTemporaryFiles();
        cleanUpDriver(driver);
        throw new RuntimeException(e);
    }
}

From source file:edu.usc.cs.ir.htmlunit.handler.HtmlUnitWebDriver.java

License:Apache License

public static void cleanUpDriver(WebDriver driver) {
    if (driver != null) {
        try {/* w  ww.  j  a va  2s  . c  o m*/
            driver.close();
            driver.quit();
            TemporaryFilesystem.getDefaultTmpFS().deleteTemporaryFiles();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:gov.nasa.jpl.nutch.phantomjs.PhantomJSWebClient.java

License:Apache License

public static String getHTMLContent(WebDriver driver) {
    try {/*from  www  . j a v  a2  s. c  o m*/
        String innerHtml = driver.findElement(By.tagName("body")).getAttribute("innerHTML");
        return innerHtml;
    } catch (Exception e) {
        TemporaryFilesystem.getDefaultTmpFS().deleteTemporaryFiles();
        throw new RuntimeException(e);
    } finally {
        cleanUpDriver(driver);
    }
}