22
33import io .github .seleniumquery .SeleniumQueryException ;
44import io .github .seleniumquery .browser .driver .DriverBuilder ;
5- import org .apache .commons .logging .Log ;
6- import org .apache .commons .logging .LogFactory ;
7- import org .openqa .selenium .By ;
8- import org .openqa .selenium .InvalidSelectorException ;
95import org .openqa .selenium .WebDriver ;
106import org .openqa .selenium .ie .InternetExplorerDriver ;
7+ import org .openqa .selenium .remote .DesiredCapabilities ;
118import org .openqa .selenium .remote .SessionNotFoundException ;
129
1310import java .io .File ;
11+ import java .lang .IllegalStateException ;
12+
13+ import static io .github .seleniumquery .browser .driver .builders .DriverInstantiationUtils .getFullPath ;
14+ import static io .github .seleniumquery .browser .driver .builders .DriverInstantiationUtils .getFullPathForFileInClasspath ;
1415
1516/**
1617 * Builds InternetExplorerDriver instances for SeleniumQueryDriver.
2122 */
2223public class InternetExplorerDriverBuilder extends DriverBuilder <InternetExplorerDriverBuilder > {
2324
24- private static final Log LOGGER = LogFactory .getLog (InternetExplorerDriverBuilder .class );
25+ private static final String IE_DRIVER_EXECUTABLE_SYSTEM_PROPERTY = "webdriver.ie.driver" ;
26+ private static final String EXCEPTION_MESSAGE = " \n Download the latest release at http://selenium-release.storage.googleapis.com/index.html and place it: \n " +
27+ "(1) on the classpath of this project; or\n " +
28+ "(2) on the path specified by the \" " + IE_DRIVER_EXECUTABLE_SYSTEM_PROPERTY + "\" system property; or\n " +
29+ "(3) on a folder in the system's PATH variable; or\n " +
30+ "(4) wherever and set the path via $.driver().useInternetExplorer().withPathToIEDriverServerExe(\" other/path/to/IEDriverServer.exe\" ).\n " +
31+ "For more information, see https://github.com/seleniumQuery/seleniumQuery/wiki/seleniumQuery-and-IE-Driver" ;
32+
33+ static String IEDRIVERSERVER_EXE = "IEDriverServer.exe" ; // package visibility so it can be changed during test
2534
2635 private String customPathToIEDriverServerExe ;
2736
@@ -43,64 +52,48 @@ public InternetExplorerDriverBuilder withPathToIEDriverServerExe(String pathToIE
4352
4453 @ Override
4554 protected WebDriver build () {
46- if (this .customPathToIEDriverServerExe != null ) {
47- return instantiateIeDriverWithPath (this .customPathToIEDriverServerExe );
48- }
49- return instantiateIeDriverWithoutPath ();
50- }
55+ DesiredCapabilities capabilities = capabilities (DesiredCapabilities .chrome ());
5156
52- private WebDriver instantiateIeDriverWithoutPath ( ) {
53- return instantiateIeDriverWithPath ( DriverInstantiationUtils . getFullPathForFileInClasspath ( "IEDriverServer.exe" ));
54- }
55-
56- private WebDriver instantiateIeDriverWithPath ( String pathToIEDriverServerExe ) {
57+ if ( customPathWasProvidedAndExecutableExistsThere () ) {
58+ System . setProperty ( IE_DRIVER_EXECUTABLE_SYSTEM_PROPERTY , getFullPath ( this . customPathToIEDriverServerExe ));
59+ } else if ( DriverInstantiationUtils . executableExistsInClasspath ( IEDRIVERSERVER_EXE )) {
60+ System . setProperty ( IE_DRIVER_EXECUTABLE_SYSTEM_PROPERTY , getFullPathForFileInClasspath ( IEDRIVERSERVER_EXE ));
61+ }
5762 try {
58- WebDriver iEWebDriver = DriverInstantiationUtils .instantiateDriverWithPath (pathToIEDriverServerExe ,
59- "IE Driver Server" ,
60- "http://selenium-release.storage.googleapis.com/index.html" ,
61- "$.driver().useInternetExplorer().withPath(\" other/path/to/IEDriverServer.exe\" )" ,
62- "webdriver.ie.driver" ,
63- InternetExplorerDriver .class );
64-
65- guaranteeActiveXIsNotBlocked (iEWebDriver );
66-
67- return iEWebDriver ;
68- } catch (SessionNotFoundException snfe ) {
69- String message = snfe .getLocalizedMessage ();
63+ return new InternetExplorerDriver (capabilities );
64+ } catch (IllegalStateException e ) {
65+ if (e .getMessage ().contains ("path to the driver executable must be set" )) {
66+ throw new SeleniumQueryException (
67+ "The IEDriverServer executable (" +IEDRIVERSERVER_EXE +") was not found in the classpath," +
68+ " in the \" " +IE_DRIVER_EXECUTABLE_SYSTEM_PROPERTY +"\" system property or in the system's PATH variable."
69+ +EXCEPTION_MESSAGE , e );
70+ }
71+ throw e ;
72+ } catch (SessionNotFoundException e ) {
73+ String message = e .getLocalizedMessage ();
7074 if (message != null && message .contains ("Protected Mode" )) {
7175 throw new SeleniumQueryException ("IE Driver requires Protected Mode settings to be the same for all zones. Go to\n \t \t " +
7276 "'Tools' -> 'Internet Options' -> 'Security Tab', and set all zones to the same protected mode," +
7377 " be it enabled or disabled, does not matter.\n \t \t " +
7478 "If this does not solve the problem, or for more info, check our IE Driver wiki page at: " +
75- "https://github.com/seleniumQuery/seleniumQuery/wiki/seleniumQuery-and-IE-Driver" , snfe );
79+ "https://github.com/seleniumQuery/seleniumQuery/wiki/seleniumQuery-and-IE-Driver" , e );
7680 }
77- throw snfe ;
81+ throw e ;
7882 }
7983 }
8084
81- private void guaranteeActiveXIsNotBlocked (WebDriver iEWebDriver ) {
82- try {
83- iEWebDriver .get (new File (DriverInstantiationUtils .getFullPathForFileInClasspath ("ie.html" )).toURI ().toString ());
84- iEWebDriver .findElements (By .xpath ("/nobody" ));
85- } catch (InvalidSelectorException ise ) {
86- LOGGER .debug ("Failed while testing if ActiveX is enabled in IE Driver." , ise );
87- try {
88- System .out .println ("Your IE Driver is probably blocking ActiveX. Enable it." );
89- iEWebDriver .get (new File (DriverInstantiationUtils .getFullPathForFileInClasspath ("ie-activex.html" )).toURI ().toString ());
90- for (int i = 0 ; i < 45 ; i ++) {
91- try {
92- iEWebDriver .findElements (By .xpath ("/nobody" ));
93- break ;
94- } catch (Exception e ) {
95- LOGGER .debug ("Forcing ActiveX error again[" +i +"]." , e );
96- }
97- Thread .sleep (1000 );
98- }
99- } catch (Exception e ) {
100- e .printStackTrace ();
101- LOGGER .debug ("Failed after giving the user some time to enable ActiveX." , e );
102- }
85+ private boolean customPathWasProvidedAndExecutableExistsThere () {
86+ boolean customPathWasProvided = this .customPathToIEDriverServerExe != null ;
87+ if (!customPathWasProvided ) {
88+ return false ;
89+ }
90+ File driverServerExecutableFile = new File (this .customPathToIEDriverServerExe );
91+ if (!DriverInstantiationUtils .isValidFile (driverServerExecutableFile )) {
92+ throw new SeleniumQueryException (
93+ "The IEDriverServer executable file was not found (or is a directory) at \" " +
94+ getFullPath (this .customPathToIEDriverServerExe ) + "\" ." + EXCEPTION_MESSAGE );
10395 }
96+ return true ;
10497 }
10598
10699}
0 commit comments