Skip to content

Commit 0a7d0b9

Browse files
feat: add configurable script timeout (dequelabs#28)
This patch adds a `Builder#setTimeout` method for setting a configurable script timeout. Prior to this, we'd always timeout after 30 seconds. Closes dequelabs#17.
1 parent 3617578 commit 0a7d0b9

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

src/main/java/com/deque/axe/AXE.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ public static class Builder {
248248
private final List<String> excludes = new ArrayList<String>();
249249
private String options = "{}";
250250
private Boolean skipFrames = false;
251+
private int timeout = 30;
251252

252253
/**
253254
* Initializes the Builder class to chain configuration before analyzing pages.
@@ -259,6 +260,15 @@ public Builder(final WebDriver driver, final URL script) {
259260
this.script = script;
260261
}
261262

263+
/**
264+
* Set the script timeout.
265+
* @param timeout
266+
*/
267+
public Builder setTimeout(int timeout) {
268+
this.timeout = timeout;
269+
return this;
270+
}
271+
262272
/**
263273
* Skips iframe checks
264274
*/
@@ -357,7 +367,7 @@ public JSONObject analyze(final WebElement context) {
357367
private JSONObject execute(final String command, final Object... args) {
358368
AXE.inject(this.driver, this.script, this.skipFrames);
359369

360-
this.driver.manage().timeouts().setScriptTimeout(30, TimeUnit.SECONDS);
370+
this.driver.manage().timeouts().setScriptTimeout(this.timeout, TimeUnit.SECONDS);
361371

362372
Object response = ((JavascriptExecutor) this.driver).executeAsyncScript(command, args);
363373
JSONObject result = new JSONObject((Map) response);

src/test/java/com/deque/axe/ExampleTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,26 @@ public void testAccessibilityWithOptions() {
111111
}
112112
}
113113

114+
@Test
115+
public void testCustomTimeout() {
116+
driver.get("http://localhost:5005");
117+
118+
boolean didTimeout = false;
119+
try {
120+
new AXE.Builder(driver, ExampleTest.class.getResource("/timeout.js"))
121+
.setTimeout(1)
122+
.analyze();
123+
} catch (Exception e) {
124+
String msg = e.getMessage();
125+
if (msg.indexOf("1 seconds") == -1) {
126+
assertTrue("Did not error with timeout message", msg.indexOf("1 seconds") != -1);
127+
}
128+
didTimeout = true;
129+
}
130+
131+
assertTrue("Did set custom timeout", didTimeout);
132+
}
133+
114134
/**
115135
* Test a specific selector or selectors
116136
*/

src/test/resources/timeout.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// A fake axe-core that takes 5 minutes to finish
2+
window.axe = {
3+
run: function (context, options, callback) {
4+
setTimeout(function () {
5+
callback()
6+
}, 1000 * 60 * 5)
7+
}
8+
}

0 commit comments

Comments
 (0)