aboutsummaryrefslogtreecommitdiff
path: root/src/batchtools/webdrivers/src/main/java/com/galois/fiveui/drivers/Drivers.java
blob: 6450e25599335200b6a4c3fad1f5f30019a16210 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
 * Module : Drivers.java Copyright : (c) 2011-2012, Galois, Inc.
 *
 * Maintainer : Stability : Provisional Portability: Portable
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.galois.fiveui.drivers;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;

import com.google.common.io.Files;

/**
 * @author creswick
 *
 */
public class Drivers {
    private static final String FIREFOX_BIN_PATH = "FIREFOX_BIN_PATH";
    private static final String CHROME_BIN_PATH = "CHROME_BIN_PATH";

    private static final String CD_BINARY_NAME = "chromedriver";
    private static final String CD_BASE_PATH = mkPath("..", "tools",
            "seleniumChromeDrivers");

    private static final String FIVEUI_ROOT_PATH = "FIVEUI_ROOT_PATH";
    private static final String defaultFiveuiRootPath = "../../../";
    private static final String firefoxProfilePath = "profiles/firefox";
    private static final String chromeProfilePath = "profiles/chrome";
    
    private static List<File> tmpDirs = new ArrayList<File>();

    /**
     * Query the OS environment for the FiveUI root path, or return a default.
     */
    public static String getRootPath() {
        String rootPath = System.getProperty(FIVEUI_ROOT_PATH);
        return (null != rootPath) ? rootPath + File.separator : defaultFiveuiRootPath;
    }

    /**
     * Return a new Firefox webdriver.
     * @param ffProfile Directory path for the desired Firefox profile to use. If
     *                  null a temporary blank profile is used.
     * @return
     */
    public static FirefoxDriver buildFFDriver(String ffProfile) {
        File profileDir;
        if (null == ffProfile) {
        	profileDir = Files.createTempDir();
        	profileDir.deleteOnExit();
        } else {
        	profileDir = new File(ffProfile);
        }
        System.out.println("com.galois.fiveui.drivers: using directory for Firefox profile: " + profileDir);
        FirefoxProfile profile = new FirefoxProfile(profileDir);


        String ffBinaryPath = System.getProperty(FIREFOX_BIN_PATH);

        FirefoxDriver driver;
        if (null == ffBinaryPath) {
            System.err
                    .println("WARNING: Running essentially random version of Firefox!");
            System.err.println("         set a path to firefox with -D"
                    + FIREFOX_BIN_PATH + "=<path to firefox>");
            driver = new FirefoxDriver(profile);
        } else {
            FirefoxBinary binary = new FirefoxBinary(new File(ffBinaryPath));
            driver = new FirefoxDriver(binary, profile);
        }

        return driver;
    }

    public static ChromeDriver buildChromeDriver() {

        String rootPath = getRootPath();

        // set the chrome driver path:
        String chromeDriverPth =
                mkPath(CD_BASE_PATH, osNameArch(), CD_BINARY_NAME);
        System.setProperty("webdriver.chrome.driver", chromeDriverPth);

        // setting the path to chrome also seems to cause issues:
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--user-data-dir=" + rootPath + chromeProfilePath); // ,
                                                                    // "--enable-logging",
                                                                    // "--v=1");

        String chromeBinaryPath = System.getProperty(CHROME_BIN_PATH);
        if (null == chromeBinaryPath) {
            System.err
                    .println("WARNING: Running essentially random version of Chrome!");
            System.err.println("         set a path to Chrome with -D"
                    + CHROME_BIN_PATH + "=<path to chrome>");
        } else {
            options.setBinary(new File(chromeBinaryPath));
        }
        // For use with ChromeDriver:
        return new ChromeDriver(options);
    }

    private static String mkPath(String... components) {
        StringBuilder path = new StringBuilder();
        int remaining = components.length;
        for (String c : components) {
            path.append(c);
            remaining--;
            if (remaining != 0) {
                path.append(File.separator);
            }
        }

        return path.toString();
    }

    /**
     * Determine the name of the directory that the chromedriver is in, based on
     * os.name and os.arch.
     *
     * @return The name of the directory containing 'chromedriver'
     */
    private static String osNameArch() {
        String rawOsName = System.getProperty("os.name").toLowerCase();
        String osName = rawOsName.substring(0, 3);
        boolean is64bit = System.getProperty("os.arch").indexOf("64") >= 0;

        if (osName.equals("lin")) {
            osName += is64bit ? "64" : "32";
        }
        return osName;
    }
    
}