10000 PhantomJSDriverBuilder is complete - closes #65 · seleniumQuery/seleniumQuery@46e7852 · GitHub
[go: up one dir, main page]

Skip to content

Commit 46e7852

Browse files
committed
PhantomJSDriverBuilder is complete - closes #65
1 parent 2aa361e commit 46e7852

File tree

7 files changed

+191
-105
lines changed

7 files changed

+191
-105
lines changed

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

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77
import org.openqa.selenium.chrome.ChromeOptions;
88
import org.openqa.selenium.remote.DesiredCapabilities;
99

10-
import java.io.File;
10+
import java.lang.IllegalStateException;
1111

12-
import static io.github.seleniumquery.browser.driver.builders.DriverInstantiationUtils.executableExistsInClasspath;
13-
import static io.github.seleniumquery.browser.driver.builders.DriverInstantiationUtils.getFullPath;
14-
import static io.github.seleniumquery.browser.driver.builders.DriverInstantiationUtils.getFullPathForFileInClasspath;
12+
import static io.github.seleniumquery.browser.driver.builders.DriverInstantiationUtils.*;
13+
import static java.lang.String.format;
1514

1615
/**
1716
* Builds ChromeDriver instances for SeleniumQueryDriver.
@@ -23,13 +22,16 @@
2322
public class ChromeDriverBuilder extends DriverBuilder<ChromeDriverBuilder> {
2423

2524
private static final String CHROME_DRIVER_EXECUTABLE_SYSTEM_PROPERTY = "webdriver.chrome.driver";
25+
2626
private static final String EXCEPTION_MESSAGE = " \nDownload the latest release at http://chromedriver.storage.googleapis.com/index.html and place it: \n" +
2727
"(1) on the classpath of this project; or\n" +
2828
"(2) on the path specified by the \"" + CHROME_DRIVER_EXECUTABLE_SYSTEM_PROPERTY + "\" system property; or\n" +
2929
"(3) on a folder in the system's PATH variable; or\n" +
3030
"(4) wherever and set the path via $.driver().useChrome().withPathToChromeDriverExe(\"other/path/to/chromedriver.exe\").\n" +
3131
"For more information, see https://github.com/seleniumQuery/seleniumQuery/wiki/seleniumQuery-and-Chrome-Driver";
3232

33+
private static final String BAD_PATH_PROVIDED_EXCEPTION_MESSAGE = "The ChromeDriver Server executable file was not found (or is a directory) at \"%s\"." + EXCEPTION_MESSAGE;
34+
3335
static String CHROMEDRIVER_EXE = "chromedriver.exe"; // package visibility so it can be changed during test
3436

3537

@@ -69,36 +71,27 @@ protected WebDriver build() {
6971
DesiredCapabilities capabilities = capabilities(DesiredCapabilities.chrome());
7072
overwriteCapabilityIfValueNotNull(capabilities, ChromeOptions.CAPABILITY, this.chromeOptions);
7173

72-
if (customPathWasProvidedAndExecutableExistsThere()) {
74+
if (customPathWasProvidedAndExecutableExistsThere(this.customPathToChromeDriverExe, BAD_PATH_PROVIDED_EXCEPTION_MESSAGE)) {
7375
System.setProperty(CHROME_DRIVER_EXECUTABLE_SYSTEM_PROPERTY, getFullPath(this.customPathToChromeDriverExe));
7476
} else if (executableExistsInClasspath(CHROMEDRIVER_EXE)) {
7577
System.setProperty(CHROME_DRIVER_EXECUTABLE_SYSTEM_PROPERTY, getFullPathForFileInClasspath(CHROMEDRIVER_EXE));
7678
}
7779
try {
7880
return new ChromeDriver(capabilities);
79-
} catch (java.lang.IllegalStateException e) {
80-
if (e.getMessage().contains("path to the driver executable must be set")) {
81-
throw new SeleniumQueryException(
82-
"The ChromeDriver server executable ("+CHROMEDRIVER_EXE+") was not found in the classpath," +
83-
" in the \""+CHROME_DRIVER_EXECUTABLE_SYSTEM_PROPERTY+"\" system property or in the system's PATH variable."
84-
+EXCEPTION_MESSAGE, e);
85-
}
81+
} catch (IllegalStateException e) {
82+
throwCustomExceptionIfExecutableWasNotFound(e);
8683
throw e;
8784
}
8885
}
8986

90-
private boolean customPathWasProvidedAndExecutableExistsThere() {
91-
boolean customPathWasProvided = this.customPathToChromeDriverExe != null;
92-
if (!customPathWasProvided) {
93-
return false;
94-
}
95-
File driverServerExecutableFile = new File(this.customPathToChromeDriverExe);
96-
if (!DriverInstantiationUtils.isValidFile(driverServerExecutableFile)) {
87+
private void throwCustomExceptionIfExecutableWasNotFound(IllegalStateException e) {
88+
if (e.getMessage().contains("path to the driver executable must be set")) {
9789
throw new SeleniumQueryException(
98-
"The ChromeDriver Server executable file was not found (or is a directory) at \"" +
99-
getFullPath(this.customPathToChromeDriverExe) + "\"." + EXCEPTION_MESSAGE);
90+
format(
91+
"The ChromeDriver server executable (%s) was not found in the classpath, in the \"%s\" system property or in the system's PATH variable. %s",
92+
CHROMEDRIVER_EXE, CHROME_DRIVER_EXECUTABLE_SYSTEM_PROPERTY, EXCEPTION_MESSAGE
93+
), e);
10094
}
101-
return true;
10295
}
10396

10497
}

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package io.github.seleniumquery.browser.driver.builders;
22

33
import io.github.seleniumquery.SeleniumQueryException;
4-
import org.openqa.selenium.WebDriver;
54

65
import java.io.File;
76
import java.io.IOException;
87
import java.net.URL;
98

9+
import static java.lang.String.format;
10+
1011
/**
1112
* Utilities for instantiating drivers.
1213
*
@@ -34,13 +35,26 @@ static String getFullPath(String file) {
3435
}
3536
}
3637

37-
public static boolean isValidFile(File driverServerExecutableFile) {
38+
static boolean isValidFile(File driverServerExecutableFile) {
3839
return driverServerExecutableFile.exists() && !driverServerExecutableFile.isDirectory();
3940
}
4041

41-
public static boolean executableExistsInClasspath(String file) {
42+
static boolean executableExistsInClasspath(String file) {
4243
String strPath = getFullPathForFileInClasspath(file);
4344
File driverServerExecutableFile = new File(strPath);
4445
return isValidFile(driverServerExecutableFile);
4546
}
47+
48+
static boolean customPathWasProvidedAndExecutableExistsThere(String pathToExecutable, String exceptionMessage) {
49+
boolean customPathWasProvided = pathToExecutable != null;
50+
if (!customPathWasProvided) {
51+
return false;
52+
}
53+
File driverServerExecutableFile = new File(pathToExecutable);
54+
if (!isValidFile(driverServerExecutableFile)) {
55+
throw new SeleniumQueryException(format(exceptionMessage, getFullPath(pathToExecutable)));
56+
}
57+
return true;
58+
}
59+
4660
}

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

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
import org.openqa.selenium.remote.DesiredCapabilities;
88
import org.openqa.selenium.remote.SessionNotFoundException;
99

10-
import java.io.File;
1110
import java.lang.IllegalStateException;
1211

12+
import static io.github.seleniumquery.browser.driver.builders.DriverInstantiationUtils.customPathWasProvidedAndExecutableExistsThere;
1313
import static io.github.seleniumquery.browser.driver.builders.DriverInstantiationUtils.getFullPath;
1414
import static io.github.seleniumquery.browser.driver.builders.DriverInstantiationUtils.getFullPathForFileInClasspath;
15+
import static java.lang.String.format;
1516

1617
/**
1718
* Builds InternetExplorerDriver instances for SeleniumQueryDriver.
@@ -23,13 +24,16 @@
2324
public class InternetExplorerDriverBuilder extends DriverBuilder<InternetExplorerDriverBuilder> {
2425

2526
private static final String IE_DRIVER_EXECUTABLE_SYSTEM_PROPERTY = "webdriver.ie.driver";
27+
2628
private static final String EXCEPTION_MESSAGE = " \nDownload the latest release at http://selenium-release.storage.googleapis.com/index.html and place it: \n" +
2729
"(1) on the classpath of this project; or\n" +
2830
"(2) on the path specified by the \"" + IE_DRIVER_EXECUTABLE_SYSTEM_PROPERTY + "\" system property; or\n" +
2931
"(3) on a folder in the system's PATH variable; or\n" +
3032
"(4) wherever and set the path via $.driver().useInternetExplorer().withPathToIEDriverServerExe(\"other/path/to/IEDriverServer.exe\").\n" +
3133
"For more information, see https://github.com/seleniumQuery/seleniumQuery/wiki/seleniumQuery-and-IE-Driver";
3234

35+
private static final String BAD_PATH_PROVIDED_EXCEPTION_MESSAGE = "The IEDriverServer executable file was not found (or is a directory) at \"%s\"." + EXCEPTION_MESSAGE;
36+
3337
static String IEDRIVERSERVER_EXE = "IEDriverServer.exe"; // package visibility so it can be changed during test
3438

3539
private String customPathToIEDriverServerExe;
@@ -54,46 +58,41 @@ public InternetExplorerDriverBuilder withPathToIEDriverServerExe(String pathToIE
5458
protected WebDriver build() {
5559
DesiredCapabilities capabilities = capabilities(DesiredCapabilities.chrome());
5660

57-
if (customPathWasProvidedAndExecutableExistsThere()) {
61+
if (customPathWasProvidedAndExecutableExistsThere(this.customPathToIEDriverServerExe , BAD_PATH_PROVIDED_EXCEPTION_MESSAGE)) {
5862
System.setProperty(IE_DRIVER_EXECUTABLE_SYSTEM_PROPERTY, getFullPath(this.customPathToIEDriverServerExe));
5963
} else if (DriverInstantiationUtils.executableExistsInClasspath(IEDRIVERSERVER_EXE)) {
6064
System.setProperty(IE_DRIVER_EXECUTABLE_SYSTEM_PROPERTY, getFullPathForFileInClasspath(IEDRIVERSERVER_EXE));
6165
}
6266
try {
6367
return new InternetExplorerDriver(capabilities);
6468
} 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-
}
69+
throwCustomExceptionIfExecutableNotFound(e);
7170
throw e;
7271
} catch (SessionNotFoundException e) {
73-
String message = e.getLocalizedMessage();
74-
if (message != null && message.contains("Protected Mode")) {
75-
throw new SeleniumQueryException("IE Driver requires Protected Mode settings to be the same for all zones. Go to\n\t\t" +
76-
"'Tools' -> 'Internet Options' -> 'Security Tab', and set all zones to the same protected mode," +
77-
" be it enabled or disabled, does not matter.\n\t\t" +
78-
"If this does not solve the problem, or for more info, check our IE Driver wiki page at: " +
79-
"https://github.com/seleniumQuery/seleniumQuery/wiki/seleniumQuery-and-IE-Driver", e);
80-
}
72+
throwCustomExceptionIfProtectedModeMustBeTheSame(e);
8173
throw e;
8274
}
8375
}
8476

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)) {
77+
private void throwCustomExceptionIfExecutableNotFound(IllegalStateException e) {
78+
if (e.getMessage().contains("path to the driver executable must be set")) {
9279
throw new SeleniumQueryException(
93-
"The IEDriverServer executable file was not found (or is a directory) at \"" +
94-
getFullPath(this.customPathToIEDriverServerExe) + "\"." + EXCEPTION_MESSAGE);
80+
format(
81+
"The IEDriverServer executable (%s) was not found in the classpath, in the \"%s\" system property or in the system's PATH variable.%s",
82+
IEDRIVERSERVER_EXE, IE_DRIVER_EXECUTABLE_SYSTEM_PROPERTY, EXCEPTION_MESSAGE
83+
), e);
84+
}
85+
}
86+
87+
private void throwCustomExceptionIfProtectedModeMustBeTheSame(SessionNotFoundException e) {
88+
String message = e.getLocalizedMessage();
89+
if (message != null && message.contains("Protected Mode")) {
90+
throw new SeleniumQueryException("IE Driver requires Protected Mode settings to be the same for all zones. Go to\n\t\t" +
91+
"'Tools' -> 'Internet Options' -> 'Security Tab', and set all zones to the same protected mode," +
92+
" be it enabled or disabled, does not matter.\n\t\t" +
93+
"If this does not solve the problem, or for more info, check our IE Driver wiki page at: " +
94+
"https://github.com/seleniumQuery/seleniumQuery/wiki/seleniumQuery-and-IE-Driver", e);
9595
}
96-
return true;
9796
}
9897

9998
}
Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package io.github.seleniumquery.browser.driver.builders;
22

3+
import io.github.seleniumquery.SeleniumQueryException;
34
import io.github.seleniumquery.browser.driver.DriverBuilder;
45
import org.openqa.selenium.WebDriver;
56
import org.openqa.selenium.phantomjs.PhantomJSDriver;
7+
import org.openqa.selenium.remote.DesiredCapabilities;
68

7-
import java.io.File;
9+
import static io.github.seleniumquery.browser.driver.builders.DriverInstantiationUtils.*;
10+
import static java.lang.String.format;
811

912
/**
1013
* Builds PhantomJSDriver instances for SeleniumQueryDriver.
@@ -15,6 +18,20 @@
1518
*/
1619
public class PhantomJSDriverBuilder extends DriverBuilder<PhantomJSDriverBuilder> {
1720

21+
private static final String PHANTOMJS_EXECUTABLE_SYSTEM_PROPERTY = "phantomjs.binary.path";
22+
23+
private static final String EXCEPTION_MESSAGE = " \nDownload the latest release at http://phantomjs.org/download.html and place it: \n" +
24+
"(1) on the classpath of this project; or\n" +
25+
"(2) on the path specified by the \"" + PHANTOMJS_EXECUTABLE_SYSTEM_PROPERTY + "\" system property; or\n" +
26+
"(3) on a folder in the system's PATH variable; or\n" +
27+
"(4) on the path set in the \""+PHANTOMJS_EXECUTABLE_SYSTEM_PROPERTY+"\" capability; or\n" +
28+
"(5) wherever and set the path via $.driver.usePhantomJS().withPathToPhantomJsExe(\"other/path/to/phantomjs.exe\").\n" +
29+
"For more information, see https://github.com/seleniumQuery/seleniumQuery/wiki/seleniumQuery-and-PhantomJS-Driver";
30+
31+
private static final String BAD_PATH_PROVIDED_EXCEPTION_MESSAGE = "The PhantomJS executable file was not found (or is a directory) at \"%s\"." + EXCEPTION_MESSAGE;
32+
33+
static String PHANTOMJS_EXE = "phantomjs.exe"; // package visibility so it can be changed during test
34+
1835
private String customPathToPhantomJs;
1936

2037
/**
@@ -29,33 +46,29 @@ public PhantomJSDriverBuilder withPathToPhantomJsExe(String pathToPhantomJs) {
2946

3047
@Override
3148
protected WebDriver build() {
32-
if (this.customPathToPhantomJs != null) {
33-
return instantiatePhantomJsDriverWithPath(this.customPathToPhantomJs);
34-
}
35-
return instantiatePhantomJsDriverWithoutPath();
36-
}
49+
DesiredCapabilities capabilities = capabilities(new DesiredCapabilities());
3750

38-
private WebDriver instantiatePhantomJsDriverWithPath(String pathToPhantomJs) {
51+
if (customPathWasProvidedAndExecutableExistsThere(this.customPathToPhantomJs, BAD_PATH_PROVIDED_EXCEPTION_MESSAGE)) {
52+
System.setProperty(PHANTOMJS_EXECUTABLE_SYSTEM_PROPERTY, getFullPath(this.customPathToPhantomJs));
53+
} else if (executableExistsInClasspath(PHANTOMJS_EXE)) {
54+
System.setProperty(PHANTOMJS_EXECUTABLE_SYSTEM_PROPERTY, getFullPathForFileInClasspath(PHANTOMJS_EXE));
55+
}
3956
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) {
57+
return new PhantomJSDriver(capabilities);
58+
} catch (IllegalStateException e) {
59+
throwCustomExceptionIfExecutableWasNotFound(e);
5160
throw e;
52-
} catch (Exception e) {
53-
throw new RuntimeException(e);
5461
}
5562
}
5663

57-
private WebDriver instantiatePhantomJsDriverWithoutPath() {
58-
return instantiatePhantomJsDriverWithPath(DriverInstantiationUtils.getFullPathForFileInClasspath("phantomjs.exe"));
64+
private void throwCustomExceptionIfExecutableWasNotFound(IllegalStateException e) {
65+
if (e.getMessage().contains("path to the driver executable must be set")) {
66+
throw new SeleniumQueryException(
67+
format(
68+
"The PhantomJS executable (%s) was not found in the classpath, in the \"%s\" system property or in the system's PATH variable. %s",
69+
PHANTOMJS_EXE, PHANTOMJS_EXECUTABLE_SYSTEM_PROPERTY, EXCEPTION_MESSAGE
70+
), e);
71+
}
5972
}
6073

6174
}
Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.github.seleniumquery.browser.driver.builders;
22

3+
import org.junit.After;
34
import org.junit.Test;
45
import org.openqa.selenium.chrome.ChromeOptions;
56
import org.openqa.selenium.remote.DesiredCapabilities;
@@ -12,20 +13,21 @@
1213

1314
public class ChromeDriverBuilderTest {
1415

16+
@After
17+
public void tearDown() throws Exception {
18+
$.quit();
19+
}
20+
1521
@Test
1622
public void withOptions() {
23+
// given
1724
ChromeOptions options = new ChromeOptions();
1825
options.addArguments("start-maximized");
19-
26+
// when
2027
$.driver().useChrome().withOptions(options);
21-
28+
// then
2229
$.url(classNameToTestFileUrl(ChromeDriverBuilderTest.class));
23-
24-
try {
25-
assertThat($("#isMaximized").text(), is("yes"));
26-
} finally {
27-
$.quit();
28-
}
30+
assertThat($("#isMaximized").text(), is("yes"));
2931
}
3032

3133
@Test
@@ -39,11 +41,7 @@ public void withCapabilities() {
3941
$.driver().useChrome().withCapabilities(capabilities);
4042
// then
4143
$.url(classNameToTestFileUrl(ChromeDriverBuilderTest.class));
42-
try {
43-
assertThat($("#isMaximized").text(), is("yes"));
44-
} finally {
45-
$.quit();
46-
}
44+
assertThat($("#isMaximized").text(), is("yes"));
4745
}
4846

4947
@Test
@@ -53,21 +51,24 @@ public void withCapabilities__should_return_the_current_ChromeDriverBuilder_inst
5351

5452
@Test
5553
public void withPathToChromeDriverExe() {
54+
// given
5655
$.driver().useChrome().withPathToChromeDriverExe("src/test/resources/chromedriver.exe");
57-
$.url(classNameToTestFileUrl(ChromeDriverBuilderTest.class)); // just opening a page should work
58-
$.quit();
56+
// when
57+
$.url(classNameToTestFileUrl(ChromeDriverBuilderTest.class));
58+
// then
59+
// no exception is thrown while opening a page
5960
}
6061

6162
@Test
6263
public void useChrome__should_fall_back_to_systemProperty_when_executable_not_found_in_classpath() {
6364
// given
6465
ChromeDriverBuilder.CHROMEDRIVER_EXE = "not-in-classpath.exe";
65-
// when
6666
System.setProperty("webdriver.chrome.driver", getFullPathForFileInClasspath("chromedriver.exe"));
67+
// when
6768
$.driver().useChrome();
69+
$.url(classNameToTestFileUrl(ChromeDriverBuilderTest.class));
6870
// then
69-
$.url(classNameToTestFileUrl(ChromeDriverBuilderTest.class)); // just opening a page should work
70-
$.quit();
71+
// no exception is thrown while opening a page
7172
}
7273

7374
}

0 commit comments

Comments
 (0)
0