Project

General

Profile

Download (4.87 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 *
3
 */
4
package eu.etaxonomy.dataportal.selenium;
5

    
6
import java.io.IOException;
7

    
8
import org.apache.log4j.Logger;
9
import org.openqa.selenium.WebDriver;
10
import org.openqa.selenium.chrome.ChromeDriver;
11
import org.openqa.selenium.chrome.ChromeOptions;
12
import org.openqa.selenium.firefox.FirefoxDriver;
13
import org.openqa.selenium.firefox.FirefoxProfile;
14
import org.openqa.selenium.ie.InternetExplorerDriver;
15

    
16
import eu.etaxonomy.dataportal.Browser;
17
import eu.etaxonomy.dataportal.SystemUtils;
18
import eu.etaxonomy.dataportal.junit.CdmDataPortalTestBase;
19

    
20
/**
21
 * @author andreas
22
 *
23
 */
24
public class WebDriverFactory {
25

    
26

    
27
    public static final Logger logger = Logger.getLogger(WebDriverFactory.class);
28

    
29
    public static final String SYSTEM_PROPERTY_NAME_BROWSER = "browser";
30

    
31
    private static final String FIREBUG_VERSION = "1.10.5";
32
    private static final String FIREXPATH_VERSION = "0.9.7";
33
    private static final String DISABLE_ADD_ON_COMPATIBILITY_CHECKS_VERSION = "1.3";
34

    
35
    private static final long IMPLICIT_WAIT_DEFAULT = 5;
36

    
37

    
38
    public static WebDriver newWebDriver() {
39

    
40
        WebDriver newDriver = null;
41
        try {
42
            Browser browser = Browser.valueOf(System.getProperty(SYSTEM_PROPERTY_NAME_BROWSER, Browser.firefox.name()));
43

    
44
            logger.info("Using browser: " + browser.name());
45
            switch (browser) {
46
            case firefox:
47
                newDriver = initFirefoxDriver();
48
                break;
49
            case chrome:
50
                newDriver = initChromeDriver();
51
                break;
52
            case iexplorer:
53
                newDriver = initInternetExplorerDriver();
54
                break;
55
            }
56

    
57
        } catch (NullPointerException e) {
58
            SystemUtils.handleInvalidSystemProperty(SYSTEM_PROPERTY_NAME_BROWSER, e);
59
        } catch (IllegalArgumentException e) {
60
            SystemUtils.handleInvalidSystemProperty(SYSTEM_PROPERTY_NAME_BROWSER, e);
61
        }
62

    
63
//        newDriver.manage().timeouts().implicitlyWait(IMPLICIT_WAIT_DEFAULT, TimeUnit.SECONDS);
64
//        logger.info("Implicit wait set to : " + IMPLICIT_WAIT_DEFAULT + " " + TimeUnit.SECONDS);
65

    
66
        return newDriver;
67
    }
68

    
69

    
70
    private static WebDriver initChromeDriver() {
71

    
72
        ChromeOptions options = new ChromeOptions();
73

    
74
        String chromeBinary = System.getProperty("webdriver.chrome.bin");
75
        if(chromeBinary != null) {
76
            CdmDataPortalTestBase.logger.info(("webdriver.chrome.bin = " + chromeBinary));
77
            options.setBinary(chromeBinary);
78

    
79
        }
80
        return new ChromeDriver(options);
81
    }
82

    
83
    private static WebDriver initInternetExplorerDriver() {
84
        return new InternetExplorerDriver();
85
    }
86

    
87
    /**
88
     * -Dwebdriver.firefox.bin=/usr/lib/iceweasel/firefox-bin
89
     *
90
     * See http://code.google.com/p/selenium/wiki/FirefoxDriverInternals
91
     *
92
     */
93
    private static WebDriver initFirefoxDriver() {
94

    
95
        WebDriver driver;
96

    
97
        CdmDataPortalTestBase.logger.info(("webdriver.firefox.bin = " + System.getProperty("webdriver.firefox.bin")));
98
        CdmDataPortalTestBase.logger.info(("webdriver.firefox.library.path = " + System.getProperty("webdriver.firefox.library.path")));
99

    
100
        FirefoxProfile firefoxProfile = new FirefoxProfile();
101

    
102
        boolean addons = true;
103

    
104
        if(addons){
105
            try {
106
                firefoxProfile.addExtension(CdmDataPortalTestBase.class, "/org/mozilla/addons/disable_add_on_compatibility_checks-" + DISABLE_ADD_ON_COMPATIBILITY_CHECKS_VERSION + ".xpi");
107
                firefoxProfile.setPreference("extensions.checkCompatibility", "false");
108

    
109
//                CdmDataPortalTestBase.logger.debug("FirefoxProfile: " + firefoxProfile.getClass().getSimpleName() + "(" + firefoxProfile.hashCode() + ")");
110
//                firefoxProfile.addExtension(CdmDataPortalTestBase.class, "/org/mozilla/addons/firebug-" + FIREBUG_VERSION + ".xpi");
111
//                firefoxProfile.setPreference("extensions.firebug.currentVersion", FIREBUG_VERSION); // avoid displaying first run page
112
//
113
//                firefoxProfile.addExtension(CdmDataPortalTestBase.class, "/org/mozilla/addons/firepath-" + FIREXPATH_VERSION + "-fx.xpi");
114

    
115
            } catch (IOException e) {
116
                CdmDataPortalTestBase.logger.error(e);
117
                System.exit(-1);
118
            }
119
        }
120

    
121
        // NativeEvents can only be used with official binary releases of firefox !!!
122
        // firefoxProfile.setEnableNativeEvents(true);
123
        // see http://groups.google.com/group/webdriver/browse_thread/thread/ab68c413f17ae1ba/b5cbdcebe859aa56?lnk=gst&q=setEnableNativeEvents+firefox#msg_339ac1870da6d975
124

    
125
        driver = new FirefoxDriver(firefoxProfile);
126

    
127
        return driver;
128
    }
129

    
130
     public static void main(String[] args){
131

    
132
         initFirefoxDriver();
133
         try {
134
            Thread.currentThread().sleep(1000);
135
        } catch (InterruptedException e) {
136
            System.err.println("InterruptedException");
137
        }
138

    
139
    }
140

    
141

    
142

    
143

    
144
}
(7-7/7)