Skip to content

Commit 3985d04

Browse files
committed
use std log
1 parent 4ad4e83 commit 3985d04

3 files changed

Lines changed: 39 additions & 118 deletions

File tree

src/test/java/org/htmlunit/html/HtmlLink2Test.java

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ public void onLoadDynamic() throws Exception {
263263
= "<html>\n"
264264
+ "<head>\n"
265265
+ " <script>\n"
266+
+ LOG_TEXTAREA_FUNCTION
266267
+ " function test() {\n"
267268
+ " var dynLink = document.createElement('link');\n"
268269
+ " dynLink.rel = 'stylesheet';\n"
@@ -272,21 +273,14 @@ public void onLoadDynamic() throws Exception {
272273
+ " dynLink.onerror = function (e) { log(\"onError \" + e) };\n"
273274
+ " document.head.appendChild(dynLink);\n"
274275
+ " }\n"
275-
276-
+ " function log(x) {\n"
277-
+ " document.getElementById('log').value += x + '\\n';\n"
278-
+ " }\n"
279276
+ " </script>\n"
280277
+ "</head>\n"
281278
+ "<body onload='test()'></body>\n"
282-
+ " <textarea id='log' cols='80' rows='40'></textarea>\n"
279+
+ LOG_TEXTAREA
283280
+ "</body>\n"
284281
+ "</html>";
285282

286-
final WebDriver driver = loadPage2(html);
287-
Thread.sleep(200);
288-
final String text = driver.findElement(By.id("log")).getAttribute("value").trim().replaceAll("\r", "");
289-
assertEquals(String.join("\n", getExpectedAlerts()), text);
283+
loadPageVerifyTextArea2(html);
290284
}
291285

292286
/**
@@ -300,6 +294,7 @@ public void onLoadDynamicUnknown() throws Exception {
300294
= "<html>\n"
301295
+ "<head>\n"
302296
+ " <script>\n"
297+
+ LOG_TEXTAREA_FUNCTION
303298
+ " function test() {\n"
304299
+ " var dynLink = document.createElement('link');\n"
305300
+ " dynLink.rel = 'stylesheet';\n"
@@ -309,22 +304,15 @@ public void onLoadDynamicUnknown() throws Exception {
309304
+ " dynLink.onerror = function (e) { log(\"onError \" + e) };\n"
310305
+ " document.head.appendChild(dynLink);\n"
311306
+ " }\n"
312-
313-
+ " function log(x) {\n"
314-
+ " document.getElementById('log').value += x + '\\n';\n"
315-
+ " }\n"
316307
+ " </script>\n"
317308
+ "</head>\n"
318309
+ "<body onload='test()'></body>\n"
319-
+ " <textarea id='log' cols='80' rows='40'></textarea>\n"
310+
+ LOG_TEXTAREA
320311
+ "</body>\n"
321312
+ "</html>";
322313
getMockWebConnection().setDefaultResponse("Error: not found", 404, "Not Found", MimeType.TEXT_HTML);
323314

324-
final WebDriver driver = loadPage2(html);
325-
Thread.sleep(200);
326-
final String text = driver.findElement(By.id("log")).getAttribute("value").trim().replaceAll("\r", "");
327-
assertEquals(String.join("\n", getExpectedAlerts()), text);
315+
loadPageVerifyTextArea2(html);
328316
}
329317

330318
/**

src/test/java/org/htmlunit/javascript/host/Window2Test.java

Lines changed: 24 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,25 +1519,19 @@ public void setTimeout() throws Exception {
15191519
final String html = "<html>\n"
15201520
+ "<head>\n"
15211521
+ " <script>\n"
1522+
+ LOG_TEXTAREA_FUNCTION
15221523
+ " function test() {\n"
15231524
+ " var id = window.setTimeout( function() { log('result'); }, 20);\n"
15241525
+ " log(typeof id);\n"
15251526
+ " log('done');\n"
15261527
+ " }\n"
1527-
+ "\n"
1528-
+ " function log(x) {\n"
1529-
+ " document.getElementById('log').value += x + '\\n';\n"
1530-
+ " }\n"
15311528
+ "</script></head>\n"
15321529
+ "<body onload='test()'>\n"
1533-
+ " <textarea id='log' cols='80' rows='40'></textarea>\n"
1530+
+ LOG_TEXTAREA
15341531
+ "</body>\n"
15351532
+ "</html>\n";
15361533

1537-
final WebDriver driver = loadPage2(html);
1538-
Thread.sleep(200);
1539-
final String text = driver.findElement(By.id("log")).getAttribute("value").trim().replaceAll("\r", "");
1540-
assertEquals(String.join("\n", getExpectedAlerts()), text);
1534+
loadPageVerifyTextArea2(html);
15411535
}
15421536

15431537
/**
@@ -1549,25 +1543,19 @@ public void setTimeoutWithParams() throws Exception {
15491543
final String html = "<html>\n"
15501544
+ "<head>\n"
15511545
+ " <script>\n"
1546+
+ LOG_TEXTAREA_FUNCTION
15521547
+ " function test() {\n"
15531548
+ " var id = window.setTimeout( function(p1) { log(p1); }, 20, 42);\n"
15541549
+ " log(typeof id);\n"
15551550
+ " log('done');\n"
15561551
+ " }\n"
1557-
+ "\n"
1558-
+ " function log(x) {\n"
1559-
+ " document.getElementById('log').value += x + '\\n';\n"
1560-
+ " }\n"
15611552
+ "</script></head>\n"
15621553
+ "<body onload='test()'>\n"
1563-
+ " <textarea id='log' cols='80' rows='40'></textarea>\n"
1554+
+ LOG_TEXTAREA
15641555
+ "</body>\n"
15651556
+ "</html>\n";
15661557

1567-
final WebDriver driver = loadPage2(html);
1568-
Thread.sleep(200);
1569-
final String text = driver.findElement(By.id("log")).getAttribute("value").trim().replaceAll("\r", "");
1570-
assertEquals(String.join("\n", getExpectedAlerts()), text);
1558+
loadPageVerifyTextArea2(html);
15711559
}
15721560

15731561
/**
@@ -1579,26 +1567,20 @@ public void setTimeoutCode() throws Exception {
15791567
final String html = "<html>\n"
15801568
+ "<head>\n"
15811569
+ " <script>\n"
1570+
+ LOG_TEXTAREA_FUNCTION
15821571
+ " function test() {\n"
15831572
+ " try{\n"
15841573
+ " var id = window.setTimeout('log(7)');\n"
15851574
+ " log('done 2');\n"
15861575
+ " } catch(e) { log(e); }\n"
15871576
+ " }\n"
1588-
+ "\n"
1589-
+ " function log(x) {\n"
1590-
+ " document.getElementById('log').value += x + '\\n';\n"
1591-
+ " }\n"
15921577
+ "</script></head>\n"
15931578
+ "<body onload='test()'>\n"
1594-
+ " <textarea id='log' cols='80' rows='40'></textarea>\n"
1579+
+ LOG_TEXTAREA
15951580
+ "</body>\n"
15961581
+ "</html>\n";
15971582

1598-
final WebDriver driver = loadPage2(html);
1599-
Thread.sleep(200);
1600-
final String text = driver.findElement(By.id("log")).getAttribute("value").trim().replaceAll("\r", "");
1601-
assertEquals(String.join("\n", getExpectedAlerts()), text);
1583+
loadPageVerifyTextArea2(html);
16021584
}
16031585

16041586
/**
@@ -1610,26 +1592,20 @@ public void setTimeoutWrongParams() throws Exception {
16101592
final String html = "<html>\n"
16111593
+ "<head>\n"
16121594
+ " <script>\n"
1595+
+ LOG_TEXTAREA_FUNCTION
16131596
+ " function test() {\n"
16141597
+ " try{\n"
16151598
+ " window.setTimeout();\n"
16161599
+ " log('done');\n"
16171600
+ " } catch(e) { log(e instanceof TypeError); }\n"
16181601
+ " }\n"
1619-
+ "\n"
1620-
+ " function log(x) {\n"
1621-
+ " document.getElementById('log').value += x + '\\n';\n"
1622-
+ " }\n"
16231602
+ "</script></head>\n"
16241603
+ "<body onload='test()'>\n"
1625-
+ " <textarea id='log' cols='80' rows='40'></textarea>\n"
1604+
+ LOG_TEXTAREA
16261605
+ "</body>\n"
16271606
+ "</html>\n";
16281607

1629-
final WebDriver driver = loadPage2(html);
1630-
Thread.sleep(200);
1631-
final String text = driver.findElement(By.id("log")).getAttribute("value").trim().replaceAll("\r", "");
1632-
assertEquals(String.join("\n", getExpectedAlerts()), text);
1608+
loadPageVerifyTextArea2(html);
16331609
}
16341610

16351611
/**
@@ -1674,26 +1650,20 @@ public void setInterval() throws Exception {
16741650
final String html = "<html>\n"
16751651
+ "<head>\n"
16761652
+ " <script>\n"
1653+
+ LOG_TEXTAREA_FUNCTION
16771654
+ " var id;\n"
16781655
+ " function test() {\n"
16791656
+ " id = window.setInterval( function() { log('result'); clearInterval(id); }, 20);\n"
16801657
+ " log(typeof id);\n"
16811658
+ " log('done');\n"
16821659
+ " }\n"
1683-
+ "\n"
1684-
+ " function log(x) {\n"
1685-
+ " document.getElementById('log').value += x + '\\n';\n"
1686-
+ " }\n"
16871660
+ "</script></head>\n"
16881661
+ "<body onload='test()'>\n"
1689-
+ " <textarea id='log' cols='80' rows='40'></textarea>\n"
1662+
+ LOG_TEXTAREA
16901663
+ "</body>\n"
16911664
+ "</html>\n";
16921665

1693-
final WebDriver driver = loadPage2(html);
1694-
Thread.sleep(200);
1695-
final String text = driver.findElement(By.id("log")).getAttribute("value").trim().replaceAll("\r", "");
1696-
assertEquals(String.join("\n", getExpectedAlerts()), text);
1666+
loadPageVerifyTextArea2(html);
16971667
}
16981668

16991669
/**
@@ -1705,26 +1675,20 @@ public void setIntervalWithParams() throws Exception {
17051675
final String html = "<html>\n"
17061676
+ "<head>\n"
17071677
+ " <script>\n"
1678+
+ LOG_TEXTAREA_FUNCTION
17081679
+ " var id;\n"
17091680
+ " function test() {\n"
17101681
+ " id = window.setInterval( function(p1) { log(p1); clearInterval(id); }, 20, 42);\n"
17111682
+ " log(typeof id);\n"
17121683
+ " log('done');\n"
17131684
+ " }\n"
1714-
+ "\n"
1715-
+ " function log(x) {\n"
1716-
+ " document.getElementById('log').value += x + '\\n';\n"
1717-
+ " }\n"
17181685
+ "</script></head>\n"
17191686
+ "<body onload='test()'>\n"
1720-
+ " <textarea id='log' cols='80' rows='40'></textarea>\n"
1687+
+ LOG_TEXTAREA
17211688
+ "</body>\n"
17221689
+ "</html>\n";
17231690

1724-
final WebDriver driver = loadPage2(html);
1725-
Thread.sleep(200);
1726-
final String text = driver.findElement(By.id("log")).getAttribute("value").trim().replaceAll("\r", "");
1727-
assertEquals(String.join("\n", getExpectedAlerts()), text);
1691+
loadPageVerifyTextArea2(html);
17281692
}
17291693

17301694
/**
@@ -1736,27 +1700,21 @@ public void setIntervalCode() throws Exception {
17361700
final String html = "<html>\n"
17371701
+ "<head>\n"
17381702
+ " <script>\n"
1703+
+ LOG_TEXTAREA_FUNCTION
17391704
+ " var id;\n"
17401705
+ " function test() {\n"
17411706
+ " try{\n"
17421707
+ " id = window.setInterval('log(7); clearInterval(id);' );\n"
17431708
+ " log('done 2');\n"
17441709
+ " } catch(e) { log(e); }\n"
17451710
+ " }\n"
1746-
+ "\n"
1747-
+ " function log(x) {\n"
1748-
+ " document.getElementById('log').value += x + '\\n';\n"
1749-
+ " }\n"
17501711
+ "</script></head>\n"
17511712
+ "<body onload='test()'>\n"
1752-
+ " <textarea id='log' cols='80' rows='40'></textarea>\n"
1713+
+ LOG_TEXTAREA
17531714
+ "</body>\n"
17541715
+ "</html>\n";
17551716

1756-
final WebDriver driver = loadPage2(html);
1757-
Thread.sleep(200);
1758-
final String text = driver.findElement(By.id("log")).getAttribute("value").trim().replaceAll("\r", "");
1759-
assertEquals(String.join("\n", getExpectedAlerts()), text);
1717+
loadPageVerifyTextArea2(html);
17601718
}
17611719

17621720
/**
@@ -1768,26 +1726,20 @@ public void setIntervalWrongParams() throws Exception {
17681726
final String html = "<html>\n"
17691727
+ "<head>\n"
17701728
+ " <script>\n"
1729+
+ LOG_TEXTAREA_FUNCTION
17711730
+ " function test() {\n"
17721731
+ " try{\n"
17731732
+ " window.setInterval();\n"
17741733
+ " log('done');\n"
17751734
+ " } catch(e) { log(e instanceof TypeError); }\n"
17761735
+ " }\n"
1777-
+ "\n"
1778-
+ " function log(x) {\n"
1779-
+ " document.getElementById('log').value += x + '\\n';\n"
1780-
+ " }\n"
17811736
+ "</script></head>\n"
17821737
+ "<body onload='test()'>\n"
1783-
+ " <textarea id='log' cols='80' rows='40'></textarea>\n"
1738+
+ LOG_TEXTAREA
17841739
+ "</body>\n"
17851740
+ "</html>\n";
17861741

1787-
final WebDriver driver = loadPage2(html);
1788-
Thread.sleep(200);
1789-
final String text = driver.findElement(By.id("log")).getAttribute("value").trim().replaceAll("\r", "");
1790-
assertEquals(String.join("\n", getExpectedAlerts()), text);
1742+
loadPageVerifyTextArea2(html);
17911743
}
17921744

17931745
/**

src/test/java/org/htmlunit/javascript/host/Window3Test.java

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import org.junit.runner.RunWith;
3838
import org.openqa.selenium.By;
3939
import org.openqa.selenium.WebDriver;
40-
import org.openqa.selenium.WebElement;
4140

4241
/**
4342
* Tests for {@link Window}.
@@ -424,10 +423,7 @@ public void scrollEvents() throws Exception {
424423
+ "<html>\n"
425424
+ "<head>\n"
426425
+ "<script>\n"
427-
+ " function log(msg) {\n"
428-
+ " var ta = document.getElementById('myTextArea');\n"
429-
+ " ta.value += msg + '; ';\n"
430-
+ " }\n"
426+
+ LOG_TEXTAREA_FUNCTION
431427
+ " function test() {\n"
432428
+ " document.addEventListener('scroll', function(e) { log(\"document\") });\n"
433429
+ " window.scroll(10, 20);\n"
@@ -436,14 +432,11 @@ public void scrollEvents() throws Exception {
436432
+ "</head>\n"
437433
+ "<body onload='test()' onscroll='log(\"body\")'>\n"
438434
+ " <div onscroll='log(\"div\")' style='height: 1000px;'></div>\n"
439-
440-
+ " <textarea id='myTextArea' cols='80' rows='30'></textarea>\n"
435+
+ LOG_TEXTAREA
441436
+ "</body>\n"
442437
+ "</html>";
443-
final WebDriver driver = loadPage2(html);
444438

445-
final WebElement textArea = driver.findElement(By.id("myTextArea"));
446-
assertEquals(String.join("; ", getExpectedAlerts()) + "; ", textArea.getAttribute("value"));
439+
loadPageVerifyTextArea2(html);
447440
}
448441

449442
/**
@@ -472,10 +465,7 @@ public void scrollByEvents() throws Exception {
472465
+ "<html>\n"
473466
+ "<head>\n"
474467
+ "<script>\n"
475-
+ " function log(msg) {\n"
476-
+ " var ta = document.getElementById('myTextArea');\n"
477-
+ " ta.value += msg + '; ';\n"
478-
+ " }\n"
468+
+ LOG_TEXTAREA_FUNCTION
479469
+ " function test() {\n"
480470
+ " document.addEventListener('scroll', function(e) { log(\"document\") });\n"
481471
+ " window.scrollBy(10, 20);\n"
@@ -484,14 +474,11 @@ public void scrollByEvents() throws Exception {
484474
+ "</head>\n"
485475
+ "<body onload='test()' onscroll='log(\"body\")'>\n"
486476
+ " <div onscroll='log(\"div\")' style='height: 1000px;'></div>\n"
487-
488-
+ " <textarea id='myTextArea' cols='80' rows='30'></textarea>\n"
477+
+ LOG_TEXTAREA
489478
+ "</body>\n"
490479
+ "</html>";
491-
final WebDriver driver = loadPage2(html);
492480

493-
final WebElement textArea = driver.findElement(By.id("myTextArea"));
494-
assertEquals(String.join("; ", getExpectedAlerts()) + "; ", textArea.getAttribute("value"));
481+
loadPageVerifyTextArea2(html);
495482
}
496483

497484
/**
@@ -562,10 +549,7 @@ public void scrollToEvents() throws Exception {
562549
+ "<html>\n"
563550
+ "<head>\n"
564551
+ "<script>\n"
565-
+ " function log(msg) {\n"
566-
+ " var ta = document.getElementById('myTextArea');\n"
567-
+ " ta.value += msg + '; ';\n"
568-
+ " }\n"
552+
+ LOG_TEXTAREA_FUNCTION
569553
+ " function test() {\n"
570554
+ " document.addEventListener('scroll', function(e) { log(\"document\") });\n"
571555
+ " window.scrollTo(10, 20);\n"
@@ -574,14 +558,11 @@ public void scrollToEvents() throws Exception {
574558
+ "</head>\n"
575559
+ "<body onload='test()' onscroll='log(\"body\")'>\n"
576560
+ " <div onscroll='log(\"div\")' style='height: 1000px;'></div>\n"
577-
578-
+ " <textarea id='myTextArea' cols='80' rows='30'></textarea>\n"
561+
+ LOG_TEXTAREA
579562
+ "</body>\n"
580563
+ "</html>";
581-
final WebDriver driver = loadPage2(html);
582564

583-
final WebElement textArea = driver.findElement(By.id("myTextArea"));
584-
assertEquals(String.join("; ", getExpectedAlerts()) + "; ", textArea.getAttribute("value"));
565+
loadPageVerifyTextArea2(html);
585566
}
586567

587568
/**

0 commit comments

Comments
 (0)