Skip to content

Commit 99af8a7

Browse files
committed
last test migrated
1 parent 73a52d8 commit 99af8a7

File tree

1 file changed

+78
-32
lines changed

1 file changed

+78
-32
lines changed

src/test/java/org/openqa/selenium/htmlunit/ExecutingAsyncJavascriptTest.java

Lines changed: 78 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,19 @@
1717

1818
package org.openqa.selenium.htmlunit;
1919

20+
import java.io.IOException;
21+
import java.io.Writer;
2022
import java.time.Duration;
23+
import java.util.HashMap;
2124
import java.util.Iterator;
2225
import java.util.List;
26+
import java.util.Map;
27+
28+
import javax.servlet.Servlet;
29+
import javax.servlet.ServletException;
30+
import javax.servlet.http.HttpServlet;
31+
import javax.servlet.http.HttpServletRequest;
32+
import javax.servlet.http.HttpServletResponse;
2333

2434
import org.htmlunit.corejs.javascript.JavaScriptException;
2535
import org.junit.Assert;
@@ -343,40 +353,76 @@ public void shouldBeAbleToPassMultipleArgumentsToAsyncScripts() throws Exception
343353
assertEquals(3, result.intValue());
344354
}
345355

346-
/* TODO
347356
@Test
348-
void shouldBeAbleToMakeXMLHttpRequestsAndWaitForTheResponse() {
349-
String script =
350-
"var url = arguments[0];"
351-
+ "var callback = arguments[arguments.length - 1];"
352-
+
353-
// Adapted from http://www.quirksmode.org/js/xmlhttp.html
354-
"var XMLHttpFactories = ["
355-
+ " function () {return new XMLHttpRequest()},"
356-
+ " function () {return new ActiveXObject('Msxml2.XMLHTTP')},"
357-
+ " function () {return new ActiveXObject('Msxml3.XMLHTTP')},"
358-
+ " function () {return new ActiveXObject('Microsoft.XMLHTTP')}"
359-
+ "];"
360-
+ "var xhr = false;"
361-
+ "while (!xhr && XMLHttpFactories.length) {"
362-
+ " try {"
363-
+ " xhr = XMLHttpFactories.shift().call();"
364-
+ " } catch (e) {}"
365-
+ "}"
366-
+ "if (!xhr) throw Error('unable to create XHR object');"
367-
+ "xhr.open('GET', url, true);"
368-
+ "xhr.onreadystatechange = function() {"
369-
+ " if (xhr.readyState == 4) callback(xhr.responseText);"
370-
+ "};"
371-
+ "xhr.send('');"; // empty string to stop firefox 3 from choking
372-
373-
driver.get(pages.ajaxyPage);
374-
driver.manage().timeouts().scriptTimeout(Duration.ofSeconds(3));
375-
String response = (String) executor.executeAsyncScript(script, pages.sleepingPage + "?time=2");
376-
assertThat(response.trim())
377-
.isEqualTo("<html><head><title>Done</title></head><body>Slept for 2s</body></html>");
357+
public void shouldBeAbleToMakeXMLHttpRequestsAndWaitForTheResponse() throws Exception {
358+
final Map<String, Class<? extends Servlet>> servlets = new HashMap<>();
359+
servlets.put("/sleep", SleepServlet.class);
360+
361+
final String html = getFileContent("ajax_page.html");
362+
final WebDriver driver = loadPage2(html, servlets);
363+
driver.manage().timeouts().scriptTimeout(Duration.ofSeconds(3));
364+
365+
final JavascriptExecutor executor = (JavascriptExecutor) driver;
366+
final String script =
367+
"var url = arguments[0];"
368+
+ "var callback = arguments[arguments.length - 1];"
369+
+
370+
// Adapted from http://www.quirksmode.org/js/xmlhttp.html
371+
"var XMLHttpFactories = ["
372+
+ " function () {return new XMLHttpRequest()},"
373+
+ " function () {return new ActiveXObject('Msxml2.XMLHTTP')},"
374+
+ " function () {return new ActiveXObject('Msxml3.XMLHTTP')},"
375+
+ " function () {return new ActiveXObject('Microsoft.XMLHTTP')}"
376+
+ "];"
377+
+ "var xhr = false;"
378+
+ "while (!xhr && XMLHttpFactories.length) {"
379+
+ " try {"
380+
+ " xhr = XMLHttpFactories.shift().call();"
381+
+ " } catch (e) {}"
382+
+ "}"
383+
+ "if (!xhr) throw Error('unable to create XHR object');"
384+
+ "xhr.open('GET', url, true);"
385+
+ "xhr.onreadystatechange = function() {"
386+
+ " if (xhr.readyState == 4) callback(xhr.responseText);"
387+
+ "};"
388+
+ "xhr.send('');"; // empty string to stop firefox 3 from choking
389+
390+
final String response = (String) executor.executeAsyncScript(script, "sleep?time=2");
391+
assertEquals("<html><head><title>Done</title></head><body>Slept for 2s</body></html>", response);
392+
}
393+
394+
/**
395+
* Servlet for {@link #patch()}.
396+
*/
397+
public static class SleepServlet extends HttpServlet {
398+
private static final String RESPONSE_STRING_FORMAT =
399+
"<html><head><title>Done</title></head><body>Slept for %ss</body></html>";
400+
401+
@Override
402+
protected void service(final HttpServletRequest req,
403+
final HttpServletResponse resp) throws ServletException, IOException {
404+
final String duration = req.getParameter("time");
405+
final long timeout = Long.parseLong(duration) * 1000;
406+
407+
reallySleep(timeout);
408+
409+
final Writer writer = resp.getWriter();
410+
writer.write(String.format(RESPONSE_STRING_FORMAT, duration));
411+
}
412+
413+
private static void reallySleep(final long timeout) {
414+
final long start = System.currentTimeMillis();
415+
try {
416+
Thread.sleep(timeout);
417+
while ((System.currentTimeMillis() - start) < timeout) {
418+
Thread.sleep(20);
419+
}
420+
}
421+
catch (final InterruptedException ignore) {
422+
// ignore
423+
}
424+
}
378425
}
379-
*/
380426

381427
@Test
382428
public void throwsIfScriptTriggersAlert() throws Exception {

0 commit comments

Comments
 (0)