Commit 5bc796f336257268cfc9d5378e9fe32eba829219

Authored by bernard
1 parent 1031a1b2

Added loggers for alerts,domChangeEvents,htmlAttributeChangeEvents,request/response.

Added SandboxTest.
Implemented switchBetwenScheduleAndTableMode test.
implemented selectStudentSuggestionByTab test.
... ... @@ -10,9 +10,13 @@
10 10 <version>1.0-SNAPSHOT</version>
11 11 <name>${project.artifactId} : ${project.version}</name>
12 12 <description>Htmlunit sandbox</description>
13   - <url></url>
14   - <packaging>pom</packaging>
  13 + <packaging>jar</packaging>
15 14
  15 + <properties>
  16 + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  17 + <project.build.outputEncoding>UTF-8</project.build.outputEncoding>
  18 + <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  19 + </properties>
16 20
17 21 <dependencies>
18 22 <dependency>
... ... @@ -43,5 +47,12 @@
43 47 </plugins>
44 48 </build>
45 49
  50 + <distributionManagement>
  51 + <repository>
  52 + <id>bernard.labno.pl</id>
  53 + <name>MyCo Internal Repository</name>
  54 + <url>http://bernard.labno.pl/artifactory/libs-snapshot-local</url>
  55 + </repository>
  56 + </distributionManagement>
46 57
47 58 </project>
... ...
  1 +package pl.labno.bernard.htmlunified;
  2 +
  3 +import com.gargoylesoftware.htmlunit.AlertHandler;
  4 +import com.gargoylesoftware.htmlunit.Page;
  5 +import org.apache.commons.logging.Log;
  6 +import org.apache.commons.logging.LogFactory;
  7 +
  8 +public class AlertLogger implements AlertHandler {
  9 +
  10 + private static final Log LOG = LogFactory.getLog(AlertLogger.class);
  11 +
  12 + public void handleAlert(Page page, String message) {
  13 + LOG.info("Alert message:" + message);
  14 + }
  15 +}
... ...
  1 +package pl.labno.bernard.htmlunified;
  2 +
  3 +import com.gargoylesoftware.htmlunit.html.DomChangeEvent;
  4 +import com.gargoylesoftware.htmlunit.html.DomChangeListener;
  5 +import com.gargoylesoftware.htmlunit.html.DomNode;
  6 +import org.apache.commons.logging.Log;
  7 +import org.apache.commons.logging.LogFactory;
  8 +import org.w3c.dom.Node;
  9 +
  10 +public class DomChangeLogger implements DomChangeListener {
  11 +
  12 + private static final Log LOG = LogFactory.getLog(DomChangeLogger.class);
  13 +
  14 + public void nodeAdded(DomChangeEvent event) {
  15 + logEvent(event, "added");
  16 + }
  17 +
  18 + public void nodeDeleted(DomChangeEvent event) {
  19 + logEvent(event, "removed");
  20 + }
  21 +
  22 + private void logEvent(DomChangeEvent event, String action) {
  23 + String changed;
  24 + final DomNode changedNode = event.getChangedNode();
  25 + try {
  26 + changed = changedNode.toString();
  27 + } catch (Exception e) {
  28 + changed = changedNode.getNodeName() + "[id=" + changedNode.getAttributes().getNamedItem("id") + ("#text".equals(changedNode.getNodeName()) ? ";value=" + changedNode.getTextContent() : "") + "]";
  29 + }
  30 + String parent;
  31 + final DomNode parentNode = event.getParentNode();
  32 + try {
  33 + parent = parentNode.toString();
  34 + } catch (Exception e) {
  35 + parent = parentNode.getNodeName() + "[id=" + parentNode.getAttributes().getNamedItem("id") + ("#text".equals(parentNode.getNodeName()) ? ";value=" + parentNode.getTextContent() : "") + "]";
  36 + }
  37 + LOG.info("DomChangeEvent[action=" + action + ";changedElement=" + changed + ";source=" + event.getSource() + ";parent=" + parent + "]");
  38 + final Node id = event.getChangedNode().getAttributes().getNamedItem("id");
  39 + if (id != null && "searchClass:searchStudent".equals(id.getTextContent())) {
  40 + LOG.info(event.getChangedNode().asXml());
  41 + }
  42 + }
  43 +}
... ...
  1 +package pl.labno.bernard.htmlunified;
  2 +
  3 +import com.gargoylesoftware.htmlunit.html.HtmlAttributeChangeEvent;
  4 +import com.gargoylesoftware.htmlunit.html.HtmlAttributeChangeListener;
  5 +import org.apache.commons.logging.Log;
  6 +import org.apache.commons.logging.LogFactory;
  7 +
  8 +public class HtmlAttributeChangeLogger implements HtmlAttributeChangeListener {
  9 +
  10 + private static final Log LOG = LogFactory.getLog(HtmlAttributeChangeLogger.class);
  11 +
  12 + public void attributeAdded(HtmlAttributeChangeEvent event) {
  13 + log(event, "added");
  14 + }
  15 +
  16 + public void attributeRemoved(HtmlAttributeChangeEvent event) {
  17 + log(event, "removed");
  18 + }
  19 +
  20 + public void attributeReplaced(HtmlAttributeChangeEvent event) {
  21 + log(event, "replaced");
  22 + }
  23 +
  24 + private void log(HtmlAttributeChangeEvent event, String action) {
  25 + LOG.info("HtmlAttributeChangeEvent[" + action + ":" + event.getName() + "=" + event.getValue() + ";source=" + event.getSource() + "]");
  26 + }
  27 +}
\ No newline at end of file
... ...
  1 +package pl.labno.bernard.htmlunified;
  2 +
  3 +import com.gargoylesoftware.htmlunit.WebClient;
  4 +import com.gargoylesoftware.htmlunit.WebRequest;
  5 +import com.gargoylesoftware.htmlunit.WebResponse;
  6 +import com.gargoylesoftware.htmlunit.util.WebConnectionWrapper;
  7 +import org.apache.commons.logging.Log;
  8 +import org.apache.commons.logging.LogFactory;
  9 +
  10 +import java.io.IOException;
  11 +
  12 +public class RequestResponseLogger extends WebConnectionWrapper {
  13 +
  14 + private static final Log LOG = LogFactory.getLog(RequestResponseLogger.class);
  15 +
  16 + private boolean active = true;
  17 +
  18 + public RequestResponseLogger(WebClient webClient) throws IllegalArgumentException {
  19 + super(webClient);
  20 + }
  21 +
  22 + @Override
  23 + public WebResponse getResponse(WebRequest request) throws IOException {
  24 + final WebResponse response = super.getResponse(request);
  25 + if (active) {
  26 + LOG.info("request=" + request.getRequestBody() + "\nresponse=" + response.getContentAsString());
  27 + }
  28 + return response;
  29 + }
  30 +
  31 + public void off() {
  32 + active = false;
  33 + }
  34 +
  35 + public void on() {
  36 + active = true;
  37 + }
  38 +}
... ...
... ... @@ -30,24 +30,24 @@ public class WebClientUtils {
30 30 }
31 31
32 32 public static int waitForJSJob(WebClient webClient) {
33   - return waitForJSJob(webClient, webClient.waitForBackgroundJavaScript(10) - 1, defaultCheckInterval, defaultTimeout);
  33 + return waitForJSJob(webClient, webClient.waitForBackgroundJavaScript(10) - 1);
34 34 }
35 35
36 36 public static int waitForJSJob(WebClient webClient, int initialJobCount) {
37   - return waitForJSJob(webClient, initialJobCount, defaultCheckInterval, defaultTimeout);
  37 + return waitForJSJob(webClient, initialJobCount, defaultTimeout);
38 38 }
39 39
40 40 public static int waitForJSJob(WebClient webClient, int initialJobCount, long timeout) {
41   - return waitForJSJob(webClient, initialJobCount, defaultCheckInterval, timeout);
  41 + return waitForJSJob(webClient, initialJobCount, timeout, defaultCheckInterval);
42 42 }
43 43
44   - public static int waitForJSJob(WebClient webClient, int initialJobCount, long checkInterval, long timeout) {
  44 + public static int waitForJSJob(WebClient webClient, int initialJobCount, long timeout, long checkInterval) {
45 45 int jobs;
46 46 long startTime = System.currentTimeMillis();
47 47 do {
48 48 jobs = webClient.waitForBackgroundJavaScript(checkInterval);
49 49 if (startTime + timeout < System.currentTimeMillis()) {
50   - throw new RuntimeException("Number of JavaScript jobs doesn't drop to initial level for 10000 seconds. It's memory leak in your JavaScript rather then request taking so long!");
  50 + throw new RuntimeException("Number of JavaScript jobs doesn't drop to initial level for " + timeout + " seconds. It's memory leak in your JavaScript rather then request taking so long!");
51 51 }
52 52 } while (jobs > initialJobCount);
53 53 System.out.println("Waiting took: " + (System.currentTimeMillis() - startTime) + "ms");
... ... @@ -74,4 +74,18 @@ public class WebClientUtils {
74 74 }
75 75 return suggestions;
76 76 }
  77 +
  78 + public static void forceWait(int timeout) {
  79 + final long startTime = System.currentTimeMillis();
  80 + do {
  81 + try {
  82 + final long millis = startTime + timeout - System.currentTimeMillis();
  83 + if (millis > 0) {
  84 + Thread.sleep(millis);
  85 + }
  86 + } catch (InterruptedException ignore) {
  87 + } catch (IllegalArgumentException ignore) {
  88 + }
  89 + } while (startTime + timeout > System.currentTimeMillis());
  90 + }
77 91 }
... ...
  1 +package pl.labno.bernard.htmlunified;
  2 +
  3 +import com.gargoylesoftware.htmlunit.BrowserVersion;
  4 +import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
  5 +import com.gargoylesoftware.htmlunit.WebClient;
  6 +import com.gargoylesoftware.htmlunit.html.HtmlPage;
  7 +import org.junit.Assert;
  8 +import org.junit.Test;
  9 +
  10 +import java.io.IOException;
  11 +
  12 +public class SandboxTest {
  13 +
  14 + @Test
  15 + public void test() throws IOException {
  16 + final WebClient client = new WebClient(BrowserVersion.FIREFOX_3_6);
  17 + client.setAjaxController(new NicelyResynchronizingAjaxController());
  18 + HtmlPage page = (HtmlPage) client.getPage("file:./target/test-classes/sandbox.html");
  19 + Assert.assertEquals("elementToHide", page.getElementById("elementToHide").asText());
  20 + page.getElementById("close").click();
  21 + Assert.assertEquals("", page.getElementById("elementToHide").asText());
  22 + }
  23 +}
... ...
1 1 package pl.labno.bernard.htmlunified;
2 2
3 3 import com.gargoylesoftware.htmlunit.BrowserVersion;
4   -import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
5 4 import com.gargoylesoftware.htmlunit.WebClient;
6 5 import com.gargoylesoftware.htmlunit.html.HtmlElement;
7 6 import com.gargoylesoftware.htmlunit.html.HtmlInput;
... ... @@ -16,83 +15,137 @@ import java.util.ArrayList;
16 15 import java.util.List;
17 16 import java.util.Map;
18 17
  18 +/**
  19 + * NicelyResynchronizingAjaxController is EVIL! Don't use it!
  20 + */
19 21 public class ScheduleTest {
20 22
21   - // @Test
  23 + @Test
22 24 public void accessEventTitles() throws IOException {
23 25 WebClient client = new WebClient(BrowserVersion.FIREFOX_3_6);
24   - //login-----------------------------------
25   - HtmlPage page = (HtmlPage) client.getPage("http://localhost:8080/schoolmanager/view/class/current.seam?networkId=salsafactory");
26   - ((HtmlInput) page.getElementById("loginForm:email")).setValueAttribute("s4237@pjwstk.edu.pl");
27   - ((HtmlInput) page.getElementById("loginForm:password")).setValueAttribute("aaaaa");
28   - HtmlPage oldPage = page;
29   - page = page.getElementById("loginForm:submit").click();
30   -// This assert would fail (when page redidrects than you need to obtain new page)
31   -// Assert.assertEquals(page,oldPage);
32   - Assert.assertEquals(client.getCurrentWindow().getEnclosedPage(), page);
33   - //----------------------------------------
34   -
  26 + HtmlPage page = login(client);
  27 + @SuppressWarnings("unchecked")
35 28 final List<HtmlSpan> titleElements = (List<HtmlSpan>) page.getElementById("results:schedule").getByXPath(".//span[@class='fc-event-title']");
36 29 List<String> titles = new ArrayList<String>(titleElements.size());
37 30 for (HtmlSpan o : titleElements) {
38 31 titles.add(o.asText());
39 32 }
40   - Assert.assertEquals(2, titles.size());
  33 + Assert.assertEquals(4, titles.size());
41 34 }
42 35
43 36 @Test
44   - public void clickSuggestionBox() throws IOException {
  37 + public void clickStudentSuggestionBox() throws IOException {
45 38 WebClient client = new WebClient(BrowserVersion.FIREFOX_3_6);
46   - client.setAjaxController(new NicelyResynchronizingAjaxController());
47   - //login-----------------------------------
48   - HtmlPage page = (HtmlPage) client.getPage("http://localhost:8080/schoolmanager/view/class/current.seam?networkId=salsafactory");
49   - ((HtmlInput) page.getElementById("loginForm:email")).setValueAttribute("s4237@pjwstk.edu.pl");
50   - ((HtmlInput) page.getElementById("loginForm:password")).setValueAttribute("aaaaa");
51   - HtmlPage oldPage = page;
52   - page = page.getElementById("loginForm:submit").click();
53   -// This assert would fail (when page redidrects than you need to obtain new page)
54   -// Assert.assertEquals(page,oldPage);
55   - Assert.assertEquals(client.getCurrentWindow().getEnclosedPage(), page);
56   - //----------------------------------------
57   - final HtmlInput studentInput = (HtmlInput) page.getElementById("searchClass:student_i");
58   - int initialJobs = client.waitForBackgroundJavaScript(10);
59   -
  39 + HtmlPage page = login(client);
  40 + page.addDomChangeListener(new DomChangeLogger());
  41 + page.getElementById("searchClass:sS").addHtmlAttributeChangeListener(new HtmlAttributeChangeLogger());
  42 + HtmlInput studentInput = (HtmlInput) page.getElementById("searchClass:student_i");
60 43 studentInput.type('w');
61 44 studentInput.type('i');
62 45 Assert.assertEquals(client.getCurrentWindow().getEnclosedPage(), page);
63 46 // Here the suggestionBox is still hidden
64   -// System.out.println(page.asXml());
65   -
66   - int jobs = WebClientUtils.waitForJSJob(client, 0, 30000);
  47 + WebClientUtils.waitForJSJob(client, 0, 30000);
67 48 // Here suggestionBox is visible
68   -// System.out.println(page.asXml());
69   - System.out.println(page.getElementById("searchClass:sS").asXml());
70   - System.out.println("initialJobs:" + initialJobs);
71   - System.out.println("jobs:" + jobs);
72 49 Assert.assertEquals(client.getCurrentWindow().getEnclosedPage(), page);
73   - synchronized (page) {
74   - try {
75   - page.wait(1000);
76   - } catch (InterruptedException e) {
77   - e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
78   - }
79   - }
80 50 Map<String, HtmlTableCell> suggestions = WebClientUtils.getSuggestions(page.getElementById("searchClass:sS"), 0);
81 51 Assert.assertEquals(1, suggestions.size());
82 52 System.out.println(suggestions);
83 53 HtmlTableCell cell = suggestions.get("Willis Bruce");
84 54 Assert.assertNotNull(cell);
85   - initialJobs = client.waitForBackgroundJavaScript(10);
86   - System.out.println("initial jobs:"+initialJobs);
87 55 cell.click();
88   - System.out.println("Starting jobs:"+client.waitForBackgroundJavaScriptStartingBefore(3000));
89   - System.out.println("Running jobs:"+client.waitForBackgroundJavaScript(10));
90   -// studentInput.type('\t');
91   - jobs = WebClientUtils.waitForJSJob(client,initialJobs, 30000);
92   - System.out.println("initialJobs:" + initialJobs);
93   - System.out.println("jobs:" + jobs);
94   - System.out.println(page.asXml());
95   - Assert.assertEquals("Willis Bruce",((HtmlElement)page.getByXPath("//*[@id='searchClass:searchStudent']").get(0)).asText());
  56 + WebClientUtils.waitForJSJob(client, 0, 30000);
  57 + Assert.assertEquals("Willis Bruce", ((HtmlElement) page.getByXPath("//*[@id='searchClass:searchStudent']").get(0)).asText());
  58 + System.out.println("Success!");
  59 +
  60 +
  61 +// page = (HtmlPage) client.getPage("http://localhost:8080/schoolmanager/view/class/current.seam?networkId=salsafactory");
  62 + WebClientUtils.waitForJSJob(client, 0, 30000);
  63 + Assert.assertEquals(client.getCurrentWindow().getEnclosedPage(), page);
  64 + studentInput = (HtmlInput) page.getElementById("searchClass:student_i");
  65 + studentInput.setValueAttribute("");
  66 + studentInput.type('l');
  67 + studentInput.type('i');
  68 + Assert.assertEquals(client.getCurrentWindow().getEnclosedPage(), page);
  69 +// WebClientUtils.forceWait(2000);
  70 +// Here the suggestionBox is still hidden
  71 + WebClientUtils.waitForJSJob(client, 0, 30000);
  72 +// Here suggestionBox is visible
  73 + Assert.assertEquals(client.getCurrentWindow().getEnclosedPage(), page);
  74 + suggestions = WebClientUtils.getSuggestions(page.getElementById("searchClass:sS"), 0);
  75 + Assert.assertEquals(1, suggestions.size());
  76 + System.out.println(suggestions);
  77 + cell = suggestions.get("Linda Bogusław");
  78 + Assert.assertNotNull(cell);
  79 + cell.click();
  80 + WebClientUtils.waitForJSJob(client, 0, 30000);
  81 + Assert.assertEquals("Linda Bogusław", ((HtmlElement) page.getByXPath("//*[@id='searchClass:searchStudent']").get(0)).asText());
  82 + System.out.println("Success2!");
  83 + }
  84 +
  85 + @Test
  86 + public void switchBetwenScheduleAndTableMode() throws IOException {
  87 + WebClient client = new WebClient(BrowserVersion.FIREFOX_3_6);
  88 + HtmlPage page = login(client);
  89 + page.getElementById("j_id2118:j_id2119:0").click();
  90 + page.getElementById("j_id2118:j_id2119:0").blur();
  91 + WebClientUtils.waitForJSJob(client, 0, 30000);
  92 +// System.out.println(page.getElementById("results").asXml());
  93 + Assert.assertNotNull(page.getElementById("results:table"));
  94 + page.getElementById("j_id2118:j_id2119:1").click();
  95 + page.getElementById("j_id2118:j_id2119:1").blur();
  96 + WebClientUtils.waitForJSJob(client, 0, 30000);
  97 +// System.out.println(page.getElementById("results").asXml());
  98 + Assert.assertNotNull(page.getElementById("results:schedule"));
  99 + page.getElementById("j_id2118:j_id2119:0").click();
  100 + page.getElementById("j_id2118:j_id2119:0").blur();
  101 + WebClientUtils.waitForJSJob(client, 0, 30000);
  102 +// System.out.println(page.getElementById("results").asXml());
  103 + Assert.assertNotNull(page.getElementById("results:table"));
  104 + }
  105 +
  106 + @Test
  107 + public void selectStudentSuggestionByTab() throws IOException {
  108 + final WebClient client = new WebClient(BrowserVersion.FIREFOX_3_6);
  109 + final RequestResponseLogger requestResponseLogger = new RequestResponseLogger(client);
  110 + requestResponseLogger.off();
  111 + HtmlPage page = login(client);
  112 + final HtmlInput studentInput = (HtmlInput) page.getElementById("searchClass:student_i");
  113 + studentInput.type('w');
  114 + studentInput.type('i');
  115 + Assert.assertEquals(client.getCurrentWindow().getEnclosedPage(), page);
  116 +// Here the suggestionBox is still hidden
  117 + WebClientUtils.waitForJSJob(client, 0, 30000);
  118 +// Here suggestionBox is visible
  119 + Map<String, HtmlTableCell> suggestions = WebClientUtils.getSuggestions(page.getElementById("searchClass:sS"), 0);
  120 + Assert.assertEquals(1, suggestions.size());
  121 +// System.out.println(suggestions);
  122 + HtmlTableCell cell = suggestions.get("Willis Bruce");
  123 + Assert.assertNotNull(cell);
  124 + studentInput.type('\t');
  125 + WebClientUtils.waitForJSJob(client, 0, 30000);
  126 + Assert.assertEquals("Willis Bruce", page.getElementById("searchClass:searchStudent").asText());
  127 +
  128 +// requestResponseLogger.on();
  129 +// client.setAlertHandler(new AlertLogger());
  130 +// page.addDomChangeListener(new DomChangeLogger());
  131 + final HtmlElement element = page.getElementById("searchClass:searchStudent").getElementsByTagName("img").get(0);
  132 + element.click();
  133 + WebClientUtils.waitForJSJob(client, 0, 30000);
  134 + Assert.assertEquals(client.getCurrentWindow().getEnclosedPage(), page);
  135 + Assert.assertEquals("", page.getElementById("searchClass:searchStudent").asText());
  136 +
  137 + }
  138 +
  139 + private HtmlPage login(WebClient client) throws IOException {
  140 + HtmlPage page = (HtmlPage) client.getPage("http://localhost:8080/schoolmanager/view/class/current.seam?networkId=salsafactory");
  141 + ((HtmlInput) page.getElementById("loginForm:email")).setValueAttribute("s4237@pjwstk.edu.pl");
  142 + ((HtmlInput) page.getElementById("loginForm:password")).setValueAttribute("aaaaa");
  143 + HtmlPage oldPage = page;
  144 + page = page.getElementById("loginForm:submit").click();
  145 +// This assert would fail (when page redidrects than you need to obtain new page)
  146 +// Assert.assertEquals(page,oldPage);
  147 + Assert.assertEquals(client.getCurrentWindow().getEnclosedPage(), page);
  148 + return page;
96 149 }
97 150
98 151 }
... ...
  1 +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  2 + "http://www.w3.org/TR/html4/loose.dtd">
  3 +<html>
  4 +<head>
  5 + <title></title>
  6 +</head>
  7 +<body>
  8 +<img id="close" onclick="var e = document.getElementById('elementToHide');e.removeChild(e.firstChild)" alt="close"/>
  9 +
  10 +<div id="elementToHide">elementToHide</div>
  11 +</body>
  12 +</html>
\ No newline at end of file
... ...
Please register or login to post a comment