Skip to content

Commit eeedf42

Browse files
committed
InternetExplorerDriverBuilder complete - see seleniumQuery#65
1 parent 52fa247 commit eeedf42

9 files changed

Lines changed: 133 additions & 124 deletions

File tree

src/main/java/io/github/seleniumquery/browser/driver/builders/ChromeDriverBuilder.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import java.io.File;
1111

12+
import static io.github.seleniumquery.browser.driver.builders.DriverInstantiationUtils.executableExistsInClasspath;
1213
import static io.github.seleniumquery.browser.driver.builders.DriverInstantiationUtils.getFullPath;
1314
import static io.github.seleniumquery.browser.driver.builders.DriverInstantiationUtils.getFullPathForFileInClasspath;
1415

@@ -70,7 +71,7 @@ protected WebDriver build() {
7071

7172
if (customPathWasProvidedAndExecutableExistsThere()) {
7273
System.setProperty(CHROME_DRIVER_EXECUTABLE_SYSTEM_PROPERTY, getFullPath(this.customPathToChromeDriverExe));
73-
} else if (executableExistsInClasspath()) {
74+
} else if (executableExistsInClasspath(CHROMEDRIVER_EXE)) {
7475
System.setProperty(CHROME_DRIVER_EXECUTABLE_SYSTEM_PROPERTY, getFullPathForFileInClasspath(CHROMEDRIVER_EXE));
7576
}
7677
try {
@@ -92,22 +93,12 @@ private boolean customPathWasProvidedAndExecutableExistsThere() {
9293
return false;
9394
}
9495
File driverServerExecutableFile = new File(this.customPathToChromeDriverExe);
95-
if (!isValidFile(driverServerExecutableFile)) {
96+
if (!DriverInstantiationUtils.isValidFile(driverServerExecutableFile)) {
9697
throw new SeleniumQueryException(
97-
"No ChromeDriver Server executable file was not found (or is a directory) at \"" +
98+
"The ChromeDriver Server executable file was not found (or is a directory) at \"" +
9899
getFullPath(this.customPathToChromeDriverExe) + "\"." + EXCEPTION_MESSAGE);
99100
}
100101
return true;
101102
}
102103

103-
private boolean executableExistsInClasspath() {
104-
String strPath = getFullPathForFileInClasspath(CHROMEDRIVER_EXE);
105-
File driverServerExecutableFile = new File(strPath);
106-
return isValidFile(driverServerExecutableFile);
107-
}
108-
109-
private boolean isValidFile(File driverServerExecutableFile) {
110-
return driverServerExecutableFile.exists() && !driverServerExecutableFile.isDirectory();
111-
}
112-
113104
}

src/main/java/io/github/seleniumquery/browser/driver/builders/DriverInstantiationUtils.java

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,6 @@
1616
*/
1717
class DriverInstantiationUtils {
1818

19-
static WebDriver instantiateDriverWithPath(String pathToDriverServerExecutable,
20-
String driverServerFileDescription, String driverServerDownloadPage,
21-
String alternativeMethod, String driverServerSystemPropertyPath,
22-
Class<? extends WebDriver> driverClass) {
23-
try {
24-
File driverServerExecutableFile = new File(pathToDriverServerExecutable);
25-
String driverServerExecutableFilePath = driverServerExecutableFile.getCanonicalPath();
26-
27-
if (!driverServerExecutableFile.exists() || driverServerExecutableFile.isDirectory()) {
28-
throw new RuntimeException("No " + driverServerFileDescription + " file was found at '" +
29-
driverServerExecutableFilePath + "'. Download the latest release at " + driverServerDownloadPage
30-
+ " and place it there or specify a different path using " + alternativeMethod + ".");
31-
}
32-
System.setProperty(driverServerSystemPropertyPath, driverServerExecutableFilePath);
33-
return driverClass.newInstance();
34-
} catch (RuntimeException e) {
35-
throw e;
36-
} catch (Exception e) {
37-
throw new RuntimeException(e);
38-
}
39-
}
40-
4119
static String getFullPathForFileInClasspath(String executableFileName) {
4220
String slashExecutableFileName = "/" + executableFileName;
4321
URL executableFileInTheClassPathUrl = DriverInstantiationUtils.class.getResource(slashExecutableFileName);
@@ -56,4 +34,13 @@ static String getFullPath(String file) {
5634
}
5735
}
5836

37+
public static boolean isValidFile(File driverServerExecutableFile) {
38+
return driverServerExecutableFile.exists() && !driverServerExecutableFile.isDirectory();
39+
}
40+
41+
public static boolean executableExistsInClasspath(String file) {
42+
String strPath = getFullPathForFileInClasspath(file);
43+
File driverServerExecutableFile = new File(strPath);
44+
return isValidFile(driverServerExecutableFile);
45+
}
5946
}

src/main/java/io/github/seleniumquery/browser/driver/builders/InternetExplorerDriverBuilder.java

Lines changed: 44 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22

33
import io.github.seleniumquery.SeleniumQueryException;
44
import 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;
95
import org.openqa.selenium.WebDriver;
106
import org.openqa.selenium.ie.InternetExplorerDriver;
7+
import org.openqa.selenium.remote.DesiredCapabilities;
118
import org.openqa.selenium.remote.SessionNotFoundException;
129

1310
import 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.
@@ -21,7 +22,15 @@
2122
*/
2223
public 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 = " \nDownload 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
}

src/main/java/io/github/seleniumquery/browser/driver/builders/PhantomJSDriverBuilder.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import org.openqa.selenium.WebDriver;
55
import org.openqa.selenium.phantomjs.PhantomJSDriver;
66

7+
import java.io.File;
8+
79
/**
810
* Builds PhantomJSDriver instances for SeleniumQueryDriver.
911
*
@@ -34,12 +36,22 @@ protected WebDriver build() {
3436
}
3537

3638
private WebDriver instantiatePhantomJsDriverWithPath(String pathToPhantomJs) {
37-
return DriverInstantiationUtils.instantiateDriverWithPath(pathToPhantomJs,
38-
"PhantomJS Executable",
39-
"http://phantomjs.org/download.html",
40-
"$.driver.usePhantomJS().withPath(\"other/path/to/PhantomJS.exe\")",
41-
"phantomjs.binary.path",
42-
PhantomJSDriver.class);
39+
try {
40+
File driverServerExecutableFile = new File(pathToPhantomJs);
41+
String driverServerExecutableFilePath = driverServerExecutableFile.getCanonicalPath();
42+
43+
if (!driverServerExecutableFile.exists() || driverServerExecutableFile.isDirectory()) {
44+
throw new RuntimeException("No " + "PhantomJS Executable" + " file was found at '" +
45+
driverServerExecutableFilePath + "'. Download the latest release at " + "http://phantomjs.org/download.html"
46+
+ " and place it there or specify a different path using " + "$.driver.usePhantomJS().withPath(\"other/path/to/PhantomJS.exe\")" + ".");
47+
}
48+
System.setProperty("phantomjs.binary.path", driverServerExecutableFilePath);
49+
return PhantomJSDriver.class.newInstance();
50+
} catch (RuntimeException e) {
51+
throw e;
52+
} catch (Exception e) {
53+
throw new RuntimeException(e);
54+
}
4355
}
4456

4557
private WebDriver instantiatePhantomJsDriverWithoutPath() {

src/main/resources/ie-activex.html

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/main/resources/ie.html

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package io.github.seleniumquery.browser.driver.builders;
2+
3+
import org.junit.Test;
4+
import org.openqa.selenium.remote.DesiredCapabilities;
5+
6+
import static infrastructure.IntegrationTestUtils.classNameToTestFileUrl;
7+
import static io.github.seleniumquery.SeleniumQuery.$;
8+
import static io.github.seleniumquery.browser.driver.builders.DriverInstantiationUtils.getFullPathForFileInClasspath;
9+
import static org.hamcrest.CoreMatchers.is;
10+
import static org.junit.Assert.assertThat;
11+
12+
public class InternetExplorerDriverBuilderTest {
13+
14+
@Test
15+
public void withCapabilities() {
16+
// given
17+
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
18+
String fileUrl = classNameToTestFileUrl(ChromeDriverBuilderTest.class);
19+
capabilities.setCapability("initialBrowserUrl", fileUrl);
20+
try {
21+
// when
22+
$.driver().useInternetExplorer().withCapabilities(capabilities);
23+
// then
24+
// should open at the page specified by the capabilities
25+
assertThat($.url(), is(fileUrl.replace("/", "\\").replace("file:\\", "file://")));
26+
} finally {
27+
$.quit();
28+
}
29+
}
30+
31+
@Test
32+
public void withCapabilities__should_return_the_current_InternetExplorerDriverBuilder_instance_to_allow_further_chaining() {
33+
$.driver().useInternetExplorer().withCapabilities(null).withPathToIEDriverServerExe(null); // should compile
34+
}
35+
36+
@Test
37+
public void withPathToIEDriverServerExe() {
38+
$.driver().useInternetExplorer().withPathToIEDriverServerExe("src/test/resources/IEDriverServer.exe");
39+
$.url(classNameToTestFileUrl(ChromeDriverBuilderTest.class)); // just opening a page should work
40+
$.quit();
41+
}
42+
43+
@Test
44+
public void useInternetExplorer__should_fall_back_to_systemProperty_when_executable_not_found_in_classpath() {
45+
// given
46+
InternetExplorerDriverBuilder.IEDRIVERSERVER_EXE = "not-in-classpath.exe";
47+
// when
48+
System.setProperty("webdriver.ie.driver", getFullPathForFileInClasspath("IEDriverServer.exe"));
49+
$.driver().useInternetExplorer();
50+
// then
51+
$.url(classNameToTestFileUrl(ChromeDriverBuilderTest.class)); // just opening a page should work
52+
$.quit();
53+
}
54+
55+
}
-51.5 KB
Binary file not shown.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
File: IEDriverServer_Win32_2.41.0.zip
2-
Downloaded from: http://selenium-release.storage.googleapis.com/2.41/IEDriverServer_Win32_2.41.0.zip
3-
When: 20/05/2014
1+
File: IEDriverServer_Win32_2.44.0.zip
2+
Downloaded from: http://selenium-release.storage.googleapis.com/2.44/IEDriverServer_Win32_2.44.0.zip
3+
When: 30/11/2014

0 commit comments

Comments
 (0)