8000 ChromeDriver and PhantomJS builders now also look for linux executabl… · seleniumQuery/seleniumQuery@0439e10 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0439e10

Browse files
committed
ChromeDriver and PhantomJS builders now also look for linux executables in the classpath - closes #81
1 parent cc0383e commit 0439e10

File tree

13 files changed

+303
-21
lines changed

13 files changed

+303
-21
lines changed

src/main/java/io/github/seleniumquery/SeleniumQueryObject.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import io.github.seleniumquery.functions.jquery.attributes.RemoveAttrFunction;
1010
import io.github.seleniumquery.functions.jquery.events.ClickFunction;
1111
import io.github.seleniumquery.functions.jquery.forms.FocusFunction;
12+
import io.github.seleniumquery.functions.jquery.forms.SubmitFunction;
1213
import io.github.seleniumquery.functions.jquery.forms.ValFunction;
1314
import io.github.seleniumquery.functions.jquery.manipulation.HtmlFunction;
1415
import io.github.seleniumquery.functions.jquery.manipulation.TextFunction;
@@ -606,4 +607,21 @@ public String toString() {
606607
return this.by.toString();
607608
}
608609

610+
/**
611+
* <p>Attempts to submits, in the current order, every element in the matched set.</p>
612+
* <p>If a matched element is a form, or an element within a form, then it will be submitted.</p>
613+
* <p><b>If submitting an element causes the current page to change, then this method will block until
614+
* the new page is loaded; as consequence, not all elements may get to be submitted - and will be ignored.</b></p>
615+
*
616+
* @throws org.openqa.selenium.NoSuchElementException If a submitted element is not a, or within a, form.
617+
*
618+
* @return A self reference.
619+
*
620+
* @since 0.9.0
621+
*/
622+
public SeleniumQueryObject submit() {
623+
SubmitFunction.submit(this);
624+
return this;
625+
}
626+
609627
}

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
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;
57
import org.openqa.selenium.WebDriver;
68
import org.openqa.selenium.chrome.ChromeDriver;
79
import org.openqa.selenium.chrome.ChromeOptions;
@@ -21,6 +23,8 @@
2123
*/
2224
public class ChromeDriverBuilder extends DriverBuilder<ChromeDriverBuilder> {
2325

26+
private static final Log LOGGER = LogFactory.getLog(ChromeDriverBuilder.class);
27+
2428
private static final String CHROME_DRIVER_EXECUTABLE_SYSTEM_PROPERTY = "webdriver.chrome.driver";
2529

2630
private static final String EXCEPTION_MESSAGE = " \nDownload the latest release at http://chromedriver.storage.googleapis.com/index.html and place it: \n" +
@@ -32,7 +36,9 @@ public class ChromeDriverBuilder extends DriverBuilder<ChromeDriverBuilder> {
3236

3337
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;
3438

35-
static String CHROMEDRIVER_EXE = "chromedriver.exe"; // package visibility so it can be changed during test
39+
// package visibility so they can be changed during test
40+
static String CHROMEDRIVER_EXECUTABLE_WINDOWS = "chromedriver.exe";
41+
static String CHROMEDRIVER_EXECUTABLE_LINUX = "chromedriver";
3642

3743

3844
private String customPathToChromeDriver;
@@ -74,9 +80,11 @@ protected WebDriver build() {
7480
overwriteCapabilityIfValueNotNull(capabilities, ChromeOptions.CAPABILITY, this.chromeOptions);
7581

7682
if (customPathWasProvidedAndExecutableExistsThere(this.customPathToChromeDriver, BAD_PATH_PROVIDED_EXCEPTION_MESSAGE)) {
77-
System.setProperty(CHROME_DRIVER_EXECUTABLE_SYSTEM_PROPERTY, getFullPath(this.customPathToChromeDriver));
78-
} else if (executableExistsInClasspath(CHROMEDRIVER_EXE)) {
79-
System.setProperty(CHROME_DRIVER_EXECUTABLE_SYSTEM_PROPERTY, getFullPathForFileInClasspath(CHROMEDRIVER_EXE));
83+
setExecutableSystemProperty(getFullPath(this.customPathToChromeDriver));
84+
} else if (executableExistsInClasspath(CHROMEDRIVER_EXECUTABLE_WINDOWS)) {
85+
setExecutableSystemProperty(getFullPathForFileInClasspath(CHROMEDRIVER_EXECUTABLE_WINDOWS));
86+
} else if (executableExistsInClasspath(CHROMEDRIVER_EXECUTABLE_LINUX)) {
87+
setExecutableSystemProperty(getFullPathForFileInClasspath(CHROMEDRIVER_EXECUTABLE_LINUX));
8088
}
8189
try {
8290
return new ChromeDriver(capabilities);
@@ -86,12 +94,17 @@ protected WebDriver build() {
8694
}
8795
}
8896

97+
private void setExecutableSystemProperty(String executableFullPath) {
98+
LOGGER.debug("Loading ChromeDriver executable from "+executableFullPath);
99+
System.setProperty(CHROME_DRIVER_EXECUTABLE_SYSTEM_PROPERTY, executableFullPath);
100+
}
101+
89102
private void throwCustomExceptionIfExecutableWasNotFound(IllegalStateException e) {
90103
if (e.getMessage().contains("path to the driver executable must be set")) {
91104
throw new SeleniumQueryException(
92105
format(
93-
"The ChromeDriver server executable (%s) was not found in the classpath, in the \"%s\" system property or in the system's PATH variable. %s",
94-
CHROMEDRIVER_EXE, CHROME_DRIVER_EXECUTABLE_SYSTEM_PROPERTY, EXCEPTION_MESSAGE
106+
"The ChromeDriver server executable (%s/%s) was not found in the classpath, in the \"%s\" system property or in the system's PATH variable. %s",
107+
CHROMEDRIVER_EXECUTABLE_WINDOWS, CHROMEDRIVER_EXECUTABLE_LINUX, CHROME_DRIVER_EXECUTABLE_SYSTEM_PROPERTY, EXCEPTION_MESSAGE
95108
), e);
96109
}
97110
}

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
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;
57
import org.openqa.selenium.WebDriver;
68
import org.openqa.selenium.phantomjs.PhantomJSDriver;
79
import org.openqa.selenium.remote.DesiredCapabilities;
@@ -18,6 +20,8 @@
1820
*/
1921
public class PhantomJSDriverBuilder extends DriverBuilder<PhantomJSDriverBuilder> {
2022

23+
private static final Log LOGGER = LogFactory.getLog(PhantomJSDriverBuilder.class);
24+
2125
private static final String PHANTOMJS_EXECUTABLE_SYSTEM_PROPERTY = "phantomjs.binary.path";
2226

2327
private static final String EXCEPTION_MESSAGE = " \nDownload the latest release at http://phantomjs.org/download.html and place it: \n" +
@@ -30,7 +34,9 @@ public class PhantomJSDriverBuilder extends DriverBuilder<PhantomJSDriverBuilder
3034

3135
private static final String BAD_PATH_PROVIDED_EXCEPTION_MESSAGE = "The PhantomJS executable file was not found (or is a directory) at \"%s\"." + EXCEPTION_MESSAGE;
3236

33-
static String PHANTOMJS_EXE = "phantomjs.exe"; // package visibility so it can be changed during test
37+
// package visibility so they can be changed during test
38+
static String PHANTOMJS_EXECUTABLE_WINDOWS = "phantomjs.exe";
39+
static String PHANTOMJS_EXECUTABLE_LINUX = "phantomjs";
3440

3541
private String customPathToPhantomJs;
3642

@@ -49,9 +55,11 @@ protected WebDriver build() {
4955
DesiredCapabilities capabilities = capabilities(new DesiredCapabilities());
5056

5157
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));
58+
setExecutableSystemProperty(getFullPath(this.customPathToPhantomJs));
59+
} else if (executableExistsInClasspath(PHANTOMJS_EXECUTABLE_WINDOWS)) {
60+
setExecutableSystemProperty(getFullPathForFileInClasspath(PHANTOMJS_EXECUTABLE_WINDOWS));
61+
} else if (executableExistsInClasspath(PHANTOMJS_EXECUTABLE_LINUX)) {
62+
setExecutableSystemProperty(getFullPathForFileInClasspath(PHANTOMJS_EXECUTABLE_LINUX));
5563
}
5664
try {
5765
return new PhantomJSDriver(capabilities);
@@ -61,12 +69,17 @@ protected WebDriver build() {
6169
}
6270
}
6371

72+
private void setExecutableSystemProperty(String executableFullPath) {
73+
LOGGER.debug("Loading PhantomJS executable from "+executableFullPath);
74+
System.setProperty(PHANTOMJS_EXECUTABLE_SYSTEM_PROPERTY, executableFullPath);
75+
}
76+
6477
private void throwCustomExceptionIfExecutableWasNotFound(IllegalStateException e) {
6578
if (e.getMessage().contains("path to the driver executable must be set")) {
6679
throw new SeleniumQueryException(
6780
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
81+
"The PhantomJS executable (%s/%s) was not found in the classpath, in the \"%s\" system property or in the system's PATH variable. %s",
82+
PHANTOMJS_EXECUTABLE_WINDOWS, PHANTOMJS_EXECUTABLE_LINUX, PHANTOMJS_EXECUTABLE_SYSTEM_PROPERTY, EXCEPTION_MESSAGE
7083
), e);
7184
}
7285
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package io.github.seleniumquery.functions.jquery.forms;
2+
3+
import io.github.seleniumquery.SeleniumQueryObject;
4+
import org.apache.commons.logging.Log;
5+
import org.apache.commons.logging.LogFactory;
6+
import org.openqa.selenium.StaleElementReferenceException;
7+
import org.openqa.selenium.WebElement;
8+
9+
/**
10+
* <pre>
11+
* $("selector").submit();
12+
* </pre>
13+
*
14+
* @author acdcjunior
15+
*
16+
* @since 0.9.0
17+
*/
18+
public class SubmitFunction {
19+
20+
private static final Log LOGGER = LogFactory.getLog(SubmitFunction.class);
21+
22+
public static void submit(SeleniumQueryObject seleniumQueryObject) {
23+
for (WebElement webElement : seleniumQueryObject) {
24+
try {
25+
webElement.submit();
26+
} catch (StaleElementReferenceException e) {
27+
LOGGER.warn(".submit() was called on an element that is not present anymore. Ignoring further elements on the matched set. (Maybe the .submit() execution on previous elements changed the page.)");
28+
LOGGER.debug(".submit() exception follows.", e);
29+
return;
30+
}
31+
}
32+
}
33+
34+
}

src/test/java/infrastructure/junitrule/SetUpAndTearDownDriver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
public class SetUpAndTearDownDriver implements TestRule {
99

10-
private static final DriverToRunTestsIn driverToRunTestsIn = DriverToRunTestsIn.ALL_DRIVERS_WITH_JS_OFF_AS_WELL;
10+
private static final DriverToRunTestsIn driverToRunTestsIn = DriverToRunTestsIn.FIREFOX_WITH_JS_OFF_AS_WELL;
1111

1212
private final Class<?> htmlTestUrlClass;
1313

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head><meta charset="utf-8"><title>.submit() test page - http://jsbin.com/bovusorofo/3/edit?html,output</title>
4+
</head>
5+
<body>
6+
<script>
7+
var addEvent = (function( window, document ) {
8+
if ( document.addEventListener ) {
9+
return function( elem, type, cb ) {
10+
if ( (elem && !elem.length) || elem === window ) {
11+
elem.addEventListener(type, cb, false );
12+
}
13+
else if ( elem && elem.length ) {
14+
var len = elem.length;
15+
for ( var i = 0; i < len; i++ ) {
16+
addEvent( elem[i], type, cb );
17+
}
18+
}
19+
};
20+
}
21+
else if ( document.attachEvent ) {
22+
return function ( elem, type, cb ) {
23+
if ( (elem && !elem.length) || elem === window ) {
24+
elem.attachEvent( 'on' + type, function() { return cb.call(elem, window.event); } );
25+
}
26+
else if ( elem.length ) {
27+
var len = elem.length;
28+
for ( var i = 0; i < len; i++ ) {
29+
addEvent( elem[i], type, cb );
30+
}
31+
}
32+
};
33+
}
34+
})( this, document );
35+
36+
37+
function pl(str) { document.getElementById("out").innerHTML += str; }
38+
function submitA() { pl("a"); return false; }
39+
function submitB() { pl("b"); return false; }
40+
</script>
41+
42+
<form onsubmit="return submitA();">
43+
<input type="text" id="input-a">
44+
<div id="div-a"></div>
45+
</form>
46+
47+
<form onsubmit="return submitB();">
48+
<input type="text" id="input-b">
49+
<div id="div-b"></div>
50+
</form>
51+
52+
<button id="go">GO</button>
53+
54+
<pre id="out"></pre>
55+
56+
</body>
57+
</html>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package integration.functions.jquery.forms;
2+
3+
import infrastructure.junitrule.SetUpAndTearDownDriver;
4+
import org.junit.Assert;
5+
import org.junit.ClassRule;
6+
import org.junit.Rule;
7+
import org.junit.Test;
8+
9+
import static infrastructure.IntegrationTestUtils.equal;
10+
import static infrastructure.IntegrationTestUtils.id;
11+
import static infrastructure.IntegrationTestUtils.ids;
12+
import static io.github.seleniumquery.SeleniumQuery.$;
13+
import static io.github.seleniumquery.SeleniumQuery.jQuery;
14+
import static java.util.Arrays.asList;
15+
import static org.hamcrest.CoreMatchers.is;
16+
import static org.junit.Assert.assertThat;
17+
18+
public class SubmitFunctionTest {
19+
20+
@ClassRule
21+
public static SetUpAndTearDownDriver setUpAndTearDownDriverRule = new SetUpAndTearDownDriver(SubmitFunctionTest.class);
22+
23+
@Test
24+
public void submit_function() {
25+
assertOutput("");
26+
$("#input-a").submit();
27+
assertOutput("a");
28+
$("#div-a").submit();
29+
assertOutput("aa");
30+
$("#input-b").submit();
31+
assertOutput("aab");
32+
$("#div-b").submit();
33+
assertOutput("aabb");
34+
$("input").submit();
35+
assertOutput("aabbab");
36+
$("div").submit();
37+
assertOutput("aabbabab");
38+
}
39+
40+
private void assertOutput(String value) {
41+
assertThat($("#out").text(), is(value));
42+
}
43+
44+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head><meta charset="utf-8"><title>.submit() WITHOUT JS test page</title>
4+
</head>
5+
<body>
6+
<form action="SubmitFunctionWithoutJSTest_2.html">
7+
<input type="text" id="input-a" name="aName" value="aValue">
8+
<div id="div-a"></div>
9+
<button type="submit">GO A</button>
10+
</form>
11+
<form action="SubmitFunctionWithoutJSTest_3.html">
12+
<input type="text" id="input-b" name="bName" value="bValue">
13+
<div id="div-b"></div>
14+
<button type="submit">GO B</button>
15+
</form>
16+
</body>
17+
</html>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package integration.functions.jquery.forms;
2+
3+
import infrastructure.junitrule.SetUpAndTearDownDriver;
4+
import org.junit.ClassRule;
5+
import org.junit.Test;
6+
7+
import static io.github.seleniumquery.SeleniumQuery.$;
8+
import static org.hamcrest.CoreMatchers.endsWith;
9+
import static org.hamcrest.CoreMatchers.is;
10+
import static org.junit.Assert.assertThat;
11+
12+
public class SubmitFunctionWithoutJSTest {
13+
14+
@ClassRule
15+
public static SetUpAndTearDownDriver setUpAndTearDownDriverRule = new SetUpAndTearDownDriver(SubmitFunctionWithoutJSTest.class);
16+
17+
@Test
18+
public void submit_function_without_js() {
19+
String url = $.url();
20+
assertThat(url, endsWith("WithoutJSTest.html"));
21+
22+
$("#input-a").submit();
23+
assertThat($.url(), endsWith("WithoutJSTest_2.html?aName=aValue"));
24+
25+
$.url(url);
26+
$("#div-a").submit();
27+
assertThat($.url(), endsWith("WithoutJSTest_2.html?aName=aValue"));
28+
29+
$.url(url);
30+
$("#input-b").submit();
31+
assertThat($.url(), endsWith("WithoutJSTest_3.html?bName=bValue"));
32+
33+
$.url(url);
34+
$("#div-b").submit();
35+
assertThat($.url(), endsWith("WithoutJSTest_3.html?bName=bValue"));
36+
37+
$.url(url);
38+
$("input").submit();
39+
assertThat($.url(), endsWith("WithoutJSTest_2.html?aName=aValue"));
40+
41+
$.url(url);
42+
$("div").submit();
43+
assertThat($.url(), endsWith("WithoutJSTest_2.html?aName=aValue"));
44+
}
45+
46+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head><meta charset="utf-8"><title>.submit() test page</title>
4+
</head>
5+
<body>
6+
.submit() without js test page 2
7+
</body>
8+
</html>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head><meta charset="utf-8"><title>.submit() test page</title>
4+
</head>
5+
<body>
6+
.submit() without js test page 3
7+
</body>
8+
</html>

0 commit comments

Comments
 (0)
0