Example usage for org.openqa.selenium WebDriverException WebDriverException

List of usage examples for org.openqa.selenium WebDriverException WebDriverException

Introduction

In this page you can find the example usage for org.openqa.selenium WebDriverException WebDriverException.

Prototype

public WebDriverException(Throwable cause) 

Source Link

Usage

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

License:Apache License

/**
 * Install the extension by writing to widgets.dat
 *
 * @param wuid    Unique identifier of the widget.
 * @param oexpath Location of .oex file within profile
 *//* w ww.  j  a  va2  s .  c  o  m*/
private void writeOEXtoWidgetsDat(String wuid, File oexpath) throws IOException {
    // Define a path relative to the profile directory. Don't use an absolute path,
    // because that will break when used with the Remote web driver.
    String oexpathRelative = "{LargePreferences}widgets/" + oexpath.getName();

    String widgetString = Files.toString(widgetsDat, Charsets.UTF_8);
    int beforePreferencesEndTag = widgetString.lastIndexOf("</preferences>");
    if (beforePreferencesEndTag == -1) {
        throw new WebDriverException("</preferences> not found in " + widgetsDat.getPath());
    }
    String widgetDatSection = String.format(WIDGET_DAT_ITEM, wuid, oexpathRelative);
    // Insert the new <section ..> ... </section> before </preferences>
    widgetString = widgetString.substring(0, beforePreferencesEndTag) + widgetDatSection
            + widgetString.substring(beforePreferencesEndTag);
    Files.write(widgetString, widgetsDat, Charsets.UTF_8);
}

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

License:Apache License

/**
 * Create the minimal directory structure and prefs.dat for an Opera extension.
 *
 * @param wuid ID of extension as known in widgets.dat
 */// w ww . j  a  va  2 s  .  c o  m
private void createOEXDirectory(String wuid) throws IOException {
    File oexDirectory = new File(directory, wuid);
    if (!oexDirectory.exists() && !oexDirectory.mkdirs()) {
        throw new WebDriverException("Unable to create directory path: " + directory.getPath());
    }
    File prefsDatPath = new File(oexDirectory, "prefs.dat");
    String prefContent = String.format(WIDGET_PREFS_DAT_CONTENT, wuid);
    Files.write(prefContent, prefsDatPath, Charsets.UTF_8);
}

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

License:Apache License

/**
 * This method will try and find Opera on any platform. It performs the following steps:
 * //from  ww w .  j  a  v  a2 s . c o  m
 * 1. Check the environment variable OPERA_PATH. If it exists, and the file it points to exists,
 * then return
 * 
 * 2. Check if Opera exists at the default location on the respective OS
 * 
 * 3. (Unix) Call `which opera` to find the location 4. Give up and return null
 * 
 * @return the path to Opera, or null
 */
public static String operaPath() {
    String envPath = System.getenv("OPERA_PATH");

    if (isPathValid(envPath)) {
        return envPath;
    } else if (envPath != null && envPath.length() > 0) {
        throw new WebDriverException("Path \"" + envPath + "\" in OPERA_PATH does not exist");
    }

    List<String> paths = new ArrayList<String>();

    switch (currentPlatform) {
    case LINUX:
    case UNIX:
        paths.add(which("opera"));
        paths.add(which("opera-next"));
        paths.add("/usr/bin/opera");
        paths.add("/usr/bin/opera-next");
        break;

    case MAC:
        paths.add("/Applications/Opera.app/Contents/MacOS/Opera");
        paths.add("/Applications/Opera Next.app/Contents/MacOS/Opera");
        break;

    case WINDOWS:
    case XP:
    case VISTA:
        String programFiles = getWindowsProgramFilesDirectory();
        paths.add(programFiles + "\\Opera\\opera.exe");
        paths.add(programFiles + "\\Opera Next\\opera.exe");
        break;

    default: // Android?
        throw new WebDriverException("Unable to resolve the path to Opera on this platform");
    }

    for (String path : paths) {
        if (isPathValid(path)) {
            return path;
        }
    }

    return null;
}

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

License:Apache License

/**
 * Locates a specified program using the `which` program on UNIX or LINUX platforms. If no program
 * is found, it will return null./*w  w  w. j  a va 2 s. co  m*/
 * 
 * @param program the program binary to find
 * @return the absolute path to the binary, or null if program is not found
 */
private static String which(String program) {
    if (!currentPlatform.is(UNIX) && !currentPlatform.is(LINUX)) {
        throw new WebDriverException("Executing program `which` not possible on this platform");
    }

    CommandLine which = new CommandLine("which", program);
    which.execute();
    return which.getStdOut().trim();
}

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

License:Apache License

public static OperaProduct get(String product) {
    if (product == null) {
        return ALL;
    }//from ww  w  .  jav a2 s  .c o  m

    if (!lookup.containsKey(product)) {
        throw new WebDriverException("Unknown product: " + product);
    }

    return lookup.get(product);
}

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

License:Apache License

/**
 * Creates a representation of the profile in the given directory.  If you specify a profile
 * directory that does not exist Opera will generate a new, fresh profile in the same location
 * when it is started.//from ww  w  .  j  a  v a  2s  .c  om
 *
 * @param profileDirectory the profile to use
 * @throws IllegalArgumentException if <code>profileDirectory</code> is not set
 */
public OperaProfile(File profileDirectory) {
    // Opera generates profile if it doesn't exist, so we don't have to worry about whether
    // profileDirectory exists or not.
    checkArgument(!profileDirectory.getPath().isEmpty(), "Profile directory path is empty");

    directory = profileDirectory;
    preferenceFile = getPreferenceFile(directory);

    // Log whether directory exists or not for convenience
    if (!directory.exists()) {
        if (!directory.mkdirs()) {
            throw new WebDriverException("Unable to create directory path: " + directory.getPath());
        }
    }

    // Load preferences from profile if preference file exists, or create a new preference file
    setPreferences(new OperaFilePreferences(preferenceFile));
}

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

License:Apache License

private void assertIsConnected() {
    if (!driver.getScopeServices().isConnected()) {
        throw new WebDriverException("Unable to update proxy configuration; not connected!");
    }/*  ww w .  ja  va2 s  .co  m*/
}

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

License:Apache License

/**
 * Get the runner to use for starting and managing the Opera instance.
 *
 * @return the current runner/*w w  w. j  a v a2  s  . co  m*/
 */
public OperaRunner getRunner() {
    String klassName = (String) options.get(RUNNER).getValue();

    // If no runner is set, use the default one
    if (klassName == null) {
        setRunner(OperaLauncherRunner.class);
        return getRunner();
    }

    Class<?> klass;
    try {
        klass = Class.forName(klassName);
    } catch (ClassNotFoundException e) {
        throw new WebDriverException("Unable to find runner class on classpath: " + klassName);
    }

    Constructor constructor;
    try {
        constructor = klass.getDeclaredConstructor(OperaSettings.class);
    } catch (NoSuchMethodException e) {
        throw new WebDriverException("Invalid constructor in runner: " + klass.getName());
    }

    OperaRunner runner;
    try {
        runner = (OperaRunner) constructor.newInstance(this);
    } catch (InstantiationException e) {
        throw new WebDriverException("Unable to create new instance of runner", e);
    } catch (IllegalAccessException e) {
        throw new WebDriverException("Denied access to runner: " + klass.getName());
    } catch (InvocationTargetException e) {
        throw new WebDriverException("Runner threw exception on construction", e);
    }

    return runner;
}

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

License:Apache License

/**
 * Take a screenshot of the area this element covers. If the hash of the image matches any of the
 * given hashes then no image is saved, otherwise it saves a copy of the image to the given
 * filename./*www  . j  a v a 2  s .com*/
 *
 * @param filename     The location to save the screenshot.
 * @param timeout      The number of milliseconds to wait before taking the screenshot.
 * @param includeImage Whether to get the image data. Disable if you just need the MD5 hash.
 * @param hashes       Known image hashes.
 * @return The MD5 hash of the screenshot.
 */
public String saveScreenshot(String filename, long timeout, boolean includeImage, String... hashes) {
    Canvas canvas = buildCanvas();
    ScreenShotReply reply = execService.screenWatcher(canvas, timeout, includeImage, hashes);
    if (includeImage && reply.getPng() != null) {
        FileChannel stream;
        try {
            stream = new FileOutputStream(filename).getChannel();
            stream.write(ByteBuffer.wrap(reply.getPng()));
            stream.close();
        } catch (Exception e) {
            throw new WebDriverException("Failed to write file: " + e.getMessage());
        }
    }
    return reply.getMd5();
}

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 ww  .j av  a  2s  . c  o 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());
        }
    }
}