8000 Updated test @Rule/@ClassRule system to enable handling JS-only tests · seleniumQuery/seleniumQuery@09ff0a5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 09ff0a5

Browse files
committed
Updated test @Rule/@ClassRule system to enable handling JS-only tests
1 parent f027a38 commit 09ff0a5

File tree

8 files changed

+207
-40
lines changed

8 files changed

+207
-40
lines changed

src/test/java/infrastructure/IntegrationTestUtils.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public static NegativeAbleTest t(String assertionName, String selector, int expe
3939

4040
public static String classNameToTestFileUrl(Class<?> clazz) {
4141
String classFullName = clazz.getName();
42+
return classNameToTestFileUrl(classFullName);
43+
}
44+
45+
public static String classNameToTestFileUrl(String classFullName) {
4246
String classPath = classFullName.replace('.', '/');
4347
String htmlPath = TEST_SRC_FOLDER + classPath + ".html";
4448
return new File(htmlPath).toURI().toString();

src/test/java/infrastructure/junitrule/DriverToRunTestsIn.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ public enum DriverToRunTestsIn {
99
HEADLESS_DRIVERS_WITH_JS_OFF_AS_WELL(true, false, false, false, true, true),
1010
NON_HEADLESS_DRIVERS(false, true, true, true, false, false),
1111
HTMLUNIT(true, false, false, false, false, false),
12-
HTMLUNIT_ONLY_ONCE(true, false, false, false, false, false),
13-
HTMLUNIT_WITH_JS_OFF_AS_WELL(true, false, false, false, true, true),
12+
HTMLUNIT_WITH_JS_OFF_AS_WELL(true, false, false, false, false, true),
13+
HTMLUNIT_CHROME(true, false, false, false, false, false),
14+
HTMLUNIT_CHROME_WITH_JS_OFF_AS_WELL(true, false, false, false, false, true),
1415
CHROME(false, false, true, false, false, false),
1516
FIREFOX(false, true, false, false, false, false),
1617
FIREFOX_WITH_JS_OFF_AS_WELL(false, true, false, false, false, true),
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package infrastructure.junitrule;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
* Specifies that the test method should only be run when the driver has enabled JavaScript.
10+
*/
11+
@Retention(RetentionPolicy.RUNTIME)
12+
@Target({ElementType.METHOD})
13+
public @interface JavaScriptOnly {
14+
15+
}

src/test/java/infrastructure/junitrule/RunTestMethodsInChosenDrivers.java

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import static io.github.seleniumquery.SeleniumQuery.$;
88

9+
@SuppressWarnings("deprecation")
910
public class RunTestMethodsInChosenDrivers extends Statement {
1011

1112
private final TestMethodsRunner testMethodsRunner;
@@ -27,19 +28,19 @@ public void evaluate() throws Throwable {
2728
testMethodsRunner.reportFailures();
2829
}
2930

30-
@SuppressWarnings("deprecation")
3131
private void executeTestOnHtmlUnits() {
3232
if (!driverToRunTestsIn.canRunHtmlUnit()) {
3333
return;
3434
}
35-
if (driverToRunTestsIn != DriverToRunTestsIn.HTMLUNIT_ONLY_ONCE) {
36-
executeOnHtmlUnit(BrowserVersion.FIREFOX_17);
37-
executeOnHtmlUnit(BrowserVersion.FIREFOX_24);
38-
executeOnHtmlUnit(BrowserVersion.INTERNET_EXPLORER_8);
39-
executeOnHtmlUnit(BrowserVersion.INTERNET_EXPLORER_9);
40-
executeOnHtmlUnit(BrowserVersion.INTERNET_EXPLORER_11);
35+
if (driverToRunTestsIn != DriverToRunTestsIn.HTMLUNIT_CHROME &&
36+
driverToRunTestsIn != DriverToRunTestsIn.HTMLUNIT_CHROME_WITH_JS_OFF_AS_WELL) {
37+
executeOnHtmlUnitEmulatingFirefox17();
38+
executeOnHtmlUnitEmulatingFirefox24();
39+
executeOnHtmlUnitEmulatingIE8();
40+
executeOnHtmlUnitEmulatingIE9();
41+
executeOnHtmlUnitEmulatingIE11();
4142
}
42-
executeOnHtmlUnit(BrowserVersion.CHROME);
43+
executeOnHtmlUnitEmulatingChrome();
4344
}
4445

4546
private void executeTestOnChrome() {
@@ -90,6 +91,30 @@ private void executeTestOnPhantomJS() {
9091
$.quit();
9192
}
9293

94+
private void executeOnHtmlUnitEmulatingChrome() {
95+
executeOnHtmlUnit(BrowserVersion.CHROME);
96+
}
97+
98+
private void executeOnHtmlUnitEmulatingIE11() {
99+
executeOnHtmlUnit(BrowserVersion.INTERNET_EXPLORER_11);
100< 10000 span class="diff-text-marker">+
}
101+
102+
private void executeOnHtmlUnitEmulatingIE9() {
103+
executeOnHtmlUnit(BrowserVersion.INTERNET_EXPLORER_9);
104+
}
105+
106+
private void executeOnHtmlUnitEmulatingIE8() {
107+
executeOnHtmlUnit(BrowserVersion.INTERNET_EXPLORER_8);
108+
}
109+
110+
private void executeOnHtmlUnitEmulatingFirefox24() {
111+
executeOnHtmlUnit(BrowserVersion.FIREFOX_24);
112+
}
113+
114+
private void executeOnHtmlUnitEmulatingFirefox17() {
115+
executeOnHtmlUnit(BrowserVersion.FIREFOX_17);
116+
}
117+
93118
private void executeOnHtmlUnit(BrowserVersion browserVersion) {
94119
System.out.println("@## > Instantiating HtmlUnit ("+browserVersion+") Driver - JavaScript ON");
95120
$.driver().use(createHtmlUnitDriverWithJavasCript(browserVersion, true));
@@ -125,19 +150,11 @@ public TestMethodsRunner(Statement base, String url) {
125150
this.url = url;
126151
}
127152

128-
public void openTestPage() {
129-
$.url(url);
130-
}
131-
132-
public void runTests() throws Throwable {
133-
base.evaluate();
134-
}
135-
136153
public void executeMethodForDriver(String driver) {
137154
System.out.println(" @## >>> Running on "+driver);
138-
openTestPage();
155+
$.url(url); // this wont be needed when everyone use this both as @Rule and @ClassRule
139156
try {
140-
runTests();
157+
base.evaluate();
141158
} catch (Throwable t) {
142159
if (this.firstFailure == null) {
143160
this.firstFailure = t;
@@ -149,7 +166,7 @@ public void executeMethodForDriver(String driver) {
149166
}
150167

151168
public void reportFailures() throws Throwable {
152-
if (!failed.isEmpty()) {
169+
if (this.firstFailure != null) {
153170
throw new AssertionError("There are test failures in some drivers: "+failed, this.firstFailure);
154171
}
155172
}

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

Lines changed: 111 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,125 @@
44
import org.junit.rules.TestRule;
55
import org.junit.runner.Description;
66
import org.junit.runners.model.Statement;
7+
import org.openqa.selenium.WebDriver;
8+
import org.openqa.selenium.firefox.FirefoxDriver;
9+
import org.openqa.selenium.firefox.FirefoxProfile;
10+
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
11+
import org.openqa.selenium.remote.CommandExecutor;
12+
import org.openqa.selenium.remote.RemoteWebDriver;
713

14+
import java.lang.reflect.Field;
15+
import java.util.Map;
16+
17+
import static io.github.seleniumquery.SeleniumQuery.$;
18+
19+
@SuppressWarnings("unused")
820
public class SetUpAndTearDownDriver implements TestRule {
921

10-
private static final DriverToRunTestsIn driverToRunTestsIn = DriverToRunTestsIn.HEADLESS_DRIVERS;
22+
private static final DriverToRunTestsIn driverToRunTestsIn = DriverToRunTestsIn.HTMLUNIT_CHROME_WITH_JS_OFF_AS_WELL;
23+
private static final String NOT_SPECIFIED = null;
1124

12-
private final Class<?> htmlTestUrlClass;
25+
private final String testUrl;
26+
27+
public SetUpAndTearDownDriver() {
28+
this.testUrl = NOT_SPECIFIED;
29+
}
1330

1431
public SetUpAndTearDownDriver(Class<?> htmlTestUrlClass) {
15-
this.htmlTestUrlClass = htmlTestUrlClass;
32+
this.testUrl = IntegrationTestUtils.classNameToTestFileUrl(htmlTestUrlClass);
1633
}
1734

35+
private String url(Description description) {
36+
//noinspection StringEquality
37+
if (this.testUrl == NOT_SPECIFIED) {
38+
return IntegrationTestUtils.classNameToTestFileUrl(description.getTestClass());
39+
}
40+
return this.testUrl;
41+
}
42+
43+
private boolean browserWasStarted = false;
44+
1845
@Override
19-
public Statement apply(final Statement base, Description description) {
20-
System.out.println("BASE: "+base);
21-
System.out.println("description: "+description);
22-
String url = IntegrationTestUtils.classNameToTestFileUrl(htmlTestUrlClass);
23-
return new RunTestMethodsInChosenDrivers(driverToRunTestsIn, base, url);
46+
public Statement apply(final Statement base, final Description description) {
47+
if (description.isSuite()) {
48+
browserWasStarted = true;
49+
return new RunTestMethodsInChosenDrivers(driverToRunTestsIn, base, url(description));
50+
/*return new Statement() {
51+
@Override
52+
public void evaluate() throws Throwable {
53+
beforeClass();
54+
base.evaluate();
55+
afterClass();
56+
}
57+
};*/
58+
}
59+
Statement runTestMethodStatement = new Statement() {
60+
@Override
61+
public void evaluate() throws Throwable {
62+
boolean isJavaScriptOnlyTest = description.getAnnotation(JavaScriptOnly.class) != null;
63+
if (isJavaScriptOnlyTest && !isJavaScriptOn()) {
64+
System.out.println("\t\t-> Skipping JavaScript-only test: " + description);
65+
return;
66+
}
67+
beforeMethod(description);
68+
base.evaluate();
69+
afterMethod(description);
70+
}
71+
};
72+
if (!browserWasStarted) { // the test class has this as @Rule only and not as @ClassRule/@Rule, so we restart the browser every method
73+
return new RunTestMethodsInChosenDrivers(driverToRunTestsIn, runTestMethodStatement, url(description));
74+
}
75+
return runTestMethodStatement;
76+
}
77+
78+
private void beforeClass() { }
79+
private void afterClass() { }
80+
private void beforeMethod(Description description) {
81+
$.url(url(description));
82+
}
83+
private void afterMethod(Description description) { }
84+
85+
private boolean isJavaScriptOn() {
86+
WebDriver webDriver = $.driver().get();
87+
if (webDriver instanceof HtmlUnitDriver) {
88+
return ((HtmlUnitDriver) webDriver).isJavascriptEnabled();
89+
}
90+
if (webDriver instanceof FirefoxDriver) {
91+
try {
92+
CommandExecutor commandExecutor = ((RemoteWebDriver) webDriver).getCommandExecutor();
93+
Class<?> innerClass = FirefoxDriver.class.getDeclaredClasses()[0];
94+
Field profileField = innerClass.getDeclaredField("profile");
95+
profileField.setAccessible(true);
96+
FirefoxProfile firefoxProfile = (FirefoxProfile) profileField.get(commandExecutor);
97+
98+
Field additionalPrefsField = FirefoxProfile.class.getDeclaredField("additionalPrefs");
99+
additionalPrefsField.setAccessible(true);
100+
Object additionalPrefs = additionalPrefsField.get(firefoxProfile);
101+
102+
Field allPrefsField = additionalPrefs.getClass().getDeclaredField("allPrefs");
103+
allPrefsField.setAccessible(true);
104+
@SuppressWarnings("unchecked")
105+
Map<String, Object> allPrefs = (Map<String, Object>) allPrefsField.get(additionalPrefs);
106+
Object javaScriptEnabledPref = allPrefs.get("javascript.enabled");
107+
return javaScriptEnabledPref == null || !javaScriptEnabledPref.toString().equals("false");
108+
} catch (Exception e) {
109+
System.out.println("Unable to determine if JS is on or OFF in Firefox. Returning IS ON.");
110+
return true;
111+
}
112+
}
113+
return true;
114+
}
115+
116+
private void dump(Description description) {
117+
final String methodName = description.getMethodName();
118+
System.out.println("***************************************************");
119+
System.out.println(" - isSuite: "+description.isSuite());
120+
System.out.println(" - isTest: "+description.isTest());
121+
System.out.println(" - annotations: "+description.getAnnotations());
122+
System.out.println(" - TestClass: "+description.getTestClass());
123+
System.out.println(" - class name: "+description.getClassName());
124+
System.out.println(" - method name: "+description.getMethodName());
125+
System.out.println("###################################################");
24126
}
25-
127+
26128
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package infrastructure.junitrule;
2+
3+
import org.junit.ClassRule;
4+
import org.junit.Rule;
5+
import org.junit.Test;
6+
7+
public class SetUpAndTearDownDriverTest {
8+
9+
@ClassRule public static SetUpAndTearDownDriver setUpAndTearDownDriverRule = new SetUpAndTearDownDriver();
10+
@Rule public SetUpAndTearDownDriver setUpAndTearDownDriverRuleInstance = setUpAndTearDownDriverRule;
11+
12+
@Test @JavaScriptOnly
13+
public void a() throws Exception {
14+
System.out.println("a");
15+
}
16+
17+
@Test
18+
public void b() throws Exception {
19+
System.out.println("b");
20+
}
21+
22+
}

src/test/java/integration/functions/jquery/attributes/PropFunctionTest.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,21 @@
44
import static org.hamcrest.Matchers.is;
55
import static org.hamcrest.Matchers.nullValue;
66
import static org.junit.Assert.assertThat;
7+
8+
import infrastructure.junitrule.JavaScriptOnly;
79
import infrastructure.junitrule.SetUpAndTearDownDriver;
810

911
import org.junit.ClassRule;
12+
import org.junit.Rule;
1013
import org.junit.Test;
1114

1215
public class PropFunctionTest {
13-
14-
@ClassRule
15-
public static SetUpAndTearDownDriver setUpAndTearDownDriverRule = new SetUpAndTearDownDriver(PropFunctionTest.class);
16+
17+
@ClassRule public static SetUpAndTearDownDriver setUpAndTearDownDriverRule = new SetUpAndTearDownDriver();
18+
@Rule public SetUpAndTearDownDriver setUpAndTearDownDriverRuleInstance = setUpAndTearDownDriverRule;
1619

1720
// http://jsbin.com/zofekalo/1/edit
18-
@Test
21+
@Test @JavaScriptOnly
1922
public void prop_function__getting() throws Exception {
2023
assertThat($("#chk_checked").<Boolean>prop("checked"), is(true));
2124
assertThat($("#chk_not_checked").<Boolean>prop("checked"), is(false));
@@ -37,7 +40,7 @@ public void prop_function__getting() throws Exception {
3740
}
3841

3942
// http://jsbin.com/ceqijima/2/edit
40-
@Test
43+
@Test @JavaScriptOnly
4144
public void prop_function__setting() throws Exception {
4245
setPropAndVerify(true, true);
4346
// setPropAndVerify(1, true);

src/test/java/integration/functions/jquery/attributes/RemoveAttrFunctionTest.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,23 @@
44
import static org.hamcrest.Matchers.is;
55
import static org.hamcrest.Matchers.nullValue;
66
import static org.junit.Assert.assertThat;
7+
8+
import infrastructure.junitrule.JavaScriptOnly;
79
import infrastructure.junitrule.SetUpAndTearDownDriver;
810

11+
import org.junit.ClassRule;
912
import org.junit.Rule;
1013
import org.junit.Test;
1114

1215
public class RemoveAttrFunctionTest {
13-
14-
@Rule
15-
public SetUpAndTearDownDriver setUpAndTearDownDriverRule = new SetUpAndTearDownDriver(getClass());
16+
17+
@ClassRule public static SetUpAndTearDownDriver setUpAndTearDownDriverRule = new SetUpAndTearDownDriver();
18+
@Rule public SetUpAndTearDownDriver setUpAndTearDownDriverRuleInstance = setUpAndTearDownDriverRule;
1619

1720
// http://jsbin.com/lexuyesu/1/edit
18-
@Test
21+
@Test @JavaScriptOnly
1922
public void removeAttr_function() throws Exception {
20-
assertThat($("#chk1").attr("data-ball"), is("yo"));
23+
assertThat($("#chk1").attr("data-ball"), is("yo"));
2124
$("#chk1").attr("data-ball", "");
2225
assertThat($("#chk1").attr("data-ball"), is(""));
2326
$("#chk1").removeAttr("data-ball");

0 commit comments

Comments
 (0)
0