From 7d1f860d7c8c2eefe3215362826c54293a7f3c1d Mon Sep 17 00:00:00 2001
From: Bernard Labno <bernard.labno@itcrowd.pl>
Date: Mon, 31 Jan 2011 10:26:30 +0000
Subject: [PATCH] Initial import of htmlunit-sandbox project.

---
 pom.xml                                                        | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 src/test/java/pl/labno/bernard/htmlunified/ScheduleTest.java   | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/test/java/pl/labno/bernard/htmlunified/WebClientUtils.java | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 222 insertions(+), 0 deletions(-)
 create mode 100644 pom.xml
 create mode 100644 src/test/java/pl/labno/bernard/htmlunified/ScheduleTest.java
 create mode 100644 src/test/java/pl/labno/bernard/htmlunified/WebClientUtils.java

diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..7b0149f
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,47 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+    <modelVersion>4.0.0</modelVersion>
+
+
+    <groupId>pl.labno.bernard</groupId>
+    <artifactId>htmlunit-sandbox</artifactId>
+    <version>1.0-SNAPSHOT</version>
+    <name>${project.artifactId} : ${project.version}</name>
+    <description>Htmlunit sandbox</description>
+    <url></url>
+    <packaging>pom</packaging>
+
+
+    <dependencies>
+        <dependency>
+            <groupId>net.sourceforge.htmlunit</groupId>
+            <artifactId>htmlunit</artifactId>
+            <version>2.8</version>
+        </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>4.8.2</version>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <configuration>
+                    <source>1.5</source>
+                    <target>1.5</target>
+                    <debug>true</debug>
+                    <optimize>false</optimize>
+                </configuration>
+            </plugin>
+
+        </plugins>
+    </build>
+
+
+</project>
diff --git a/src/test/java/pl/labno/bernard/htmlunified/ScheduleTest.java b/src/test/java/pl/labno/bernard/htmlunified/ScheduleTest.java
new file mode 100644
index 0000000..395a95b
--- /dev/null
+++ b/src/test/java/pl/labno/bernard/htmlunified/ScheduleTest.java
@@ -0,0 +1,98 @@
+package pl.labno.bernard.htmlunified;
+
+import com.gargoylesoftware.htmlunit.BrowserVersion;
+import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
+import com.gargoylesoftware.htmlunit.WebClient;
+import com.gargoylesoftware.htmlunit.html.HtmlElement;
+import com.gargoylesoftware.htmlunit.html.HtmlInput;
+import com.gargoylesoftware.htmlunit.html.HtmlPage;
+import com.gargoylesoftware.htmlunit.html.HtmlSpan;
+import com.gargoylesoftware.htmlunit.html.HtmlTableCell;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+public class ScheduleTest {
+
+    //    @Test
+    public void accessEventTitles() throws IOException {
+        WebClient client = new WebClient(BrowserVersion.FIREFOX_3_6);
+        //login-----------------------------------
+        HtmlPage page = (HtmlPage) client.getPage("http://localhost:8080/schoolmanager/view/class/current.seam?networkId=salsafactory");
+        ((HtmlInput) page.getElementById("loginForm:email")).setValueAttribute("s4237@pjwstk.edu.pl");
+        ((HtmlInput) page.getElementById("loginForm:password")).setValueAttribute("aaaaa");
+        HtmlPage oldPage = page;
+        page = page.getElementById("loginForm:submit").click();
+//        This assert would fail (when page redidrects than you need to obtain new page)
+//        Assert.assertEquals(page,oldPage);
+        Assert.assertEquals(client.getCurrentWindow().getEnclosedPage(), page);
+        //----------------------------------------
+
+        final List<HtmlSpan> titleElements = (List<HtmlSpan>) page.getElementById("results:schedule").getByXPath(".//span[@class='fc-event-title']");
+        List<String> titles = new ArrayList<String>(titleElements.size());
+        for (HtmlSpan o : titleElements) {
+            titles.add(o.asText());
+        }
+        Assert.assertEquals(2, titles.size());
+    }
+
+    @Test
+    public void clickSuggestionBox() throws IOException {
+        WebClient client = new WebClient(BrowserVersion.FIREFOX_3_6);
+        client.setAjaxController(new NicelyResynchronizingAjaxController());
+        //login-----------------------------------
+        HtmlPage page = (HtmlPage) client.getPage("http://localhost:8080/schoolmanager/view/class/current.seam?networkId=salsafactory");
+        ((HtmlInput) page.getElementById("loginForm:email")).setValueAttribute("s4237@pjwstk.edu.pl");
+        ((HtmlInput) page.getElementById("loginForm:password")).setValueAttribute("aaaaa");
+        HtmlPage oldPage = page;
+        page = page.getElementById("loginForm:submit").click();
+//        This assert would fail (when page redidrects than you need to obtain new page)
+//        Assert.assertEquals(page,oldPage);
+        Assert.assertEquals(client.getCurrentWindow().getEnclosedPage(), page);
+        //----------------------------------------
+        final HtmlInput studentInput = (HtmlInput) page.getElementById("searchClass:student_i");
+        int initialJobs = client.waitForBackgroundJavaScript(10);
+
+        studentInput.type('w');
+        studentInput.type('i');
+        Assert.assertEquals(client.getCurrentWindow().getEnclosedPage(), page);
+//        Here the suggestionBox is still hidden
+//        System.out.println(page.asXml());
+
+        int jobs = WebClientUtils.waitForJSJob(client, 0, 30000);
+//        Here suggestionBox is visible
+//        System.out.println(page.asXml());
+        System.out.println(page.getElementById("searchClass:sS").asXml());
+        System.out.println("initialJobs:" + initialJobs);
+        System.out.println("jobs:" + jobs);
+        Assert.assertEquals(client.getCurrentWindow().getEnclosedPage(), page);
+         synchronized (page) {
+            try {
+                page.wait(1000);
+            } catch (InterruptedException e) {
+                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
+            }
+        }
+        Map<String, HtmlTableCell> suggestions = WebClientUtils.getSuggestions(page.getElementById("searchClass:sS"), 0);
+        Assert.assertEquals(1, suggestions.size());
+        System.out.println(suggestions);
+        HtmlTableCell cell = suggestions.get("Willis Bruce");
+        Assert.assertNotNull(cell);
+        initialJobs = client.waitForBackgroundJavaScript(10);
+        System.out.println("initial jobs:"+initialJobs);
+        cell.click();
+        System.out.println("Starting jobs:"+client.waitForBackgroundJavaScriptStartingBefore(3000));
+        System.out.println("Running jobs:"+client.waitForBackgroundJavaScript(10));
+//        studentInput.type('\t');
+        jobs = WebClientUtils.waitForJSJob(client,initialJobs, 30000);
+        System.out.println("initialJobs:" + initialJobs);
+        System.out.println("jobs:" + jobs);
+        System.out.println(page.asXml());
+        Assert.assertEquals("Willis Bruce",((HtmlElement)page.getByXPath("//*[@id='searchClass:searchStudent']").get(0)).asText());
+    }
+
+}
diff --git a/src/test/java/pl/labno/bernard/htmlunified/WebClientUtils.java b/src/test/java/pl/labno/bernard/htmlunified/WebClientUtils.java
new file mode 100644
index 0000000..0ff45b3
--- /dev/null
+++ b/src/test/java/pl/labno/bernard/htmlunified/WebClientUtils.java
@@ -0,0 +1,77 @@
+package pl.labno.bernard.htmlunified;
+
+import com.gargoylesoftware.htmlunit.WebClient;
+import com.gargoylesoftware.htmlunit.html.DomNodeList;
+import com.gargoylesoftware.htmlunit.html.HtmlElement;
+import com.gargoylesoftware.htmlunit.html.HtmlTableCell;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class WebClientUtils {
+
+    private static long defaultCheckInterval = 500;
+    private static long defaultTimeout = 10000;
+
+    public static long getDefaultCheckInterval() {
+        return defaultCheckInterval;
+    }
+
+    public static void setDefaultCheckInterval(long defaultCheckInterval) {
+        WebClientUtils.defaultCheckInterval = defaultCheckInterval;
+    }
+
+    public static long getDefaultTimeout() {
+        return defaultTimeout;
+    }
+
+    public static void setDefaultTimeout(long defaultTimeout) {
+        WebClientUtils.defaultTimeout = defaultTimeout;
+    }
+
+    public static int waitForJSJob(WebClient webClient) {
+        return waitForJSJob(webClient, webClient.waitForBackgroundJavaScript(10) - 1, defaultCheckInterval, defaultTimeout);
+    }
+
+    public static int waitForJSJob(WebClient webClient, int initialJobCount) {
+        return waitForJSJob(webClient, initialJobCount, defaultCheckInterval, defaultTimeout);
+    }
+
+    public static int waitForJSJob(WebClient webClient, int initialJobCount, long timeout) {
+        return waitForJSJob(webClient, initialJobCount, defaultCheckInterval, timeout);
+    }
+
+    public static int waitForJSJob(WebClient webClient, int initialJobCount, long checkInterval, long timeout) {
+        int jobs;
+        long startTime = System.currentTimeMillis();
+        do {
+            jobs = webClient.waitForBackgroundJavaScript(checkInterval);
+            if (startTime + timeout < System.currentTimeMillis()) {
+                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!");
+            }
+        } while (jobs > initialJobCount);
+        System.out.println("Waiting took: " + (System.currentTimeMillis() - startTime) + "ms");
+        return jobs;
+    }
+
+    /**
+     * Returns list of suggestions from rich:suggestionBox
+     *
+     * @param suggestion suggestionBox element
+     * @param column     column of suggestionBox to extract text from
+     * @return list of suggestions
+     */
+    public static Map<String, HtmlTableCell> getSuggestions(HtmlElement suggestion, int column) {
+        final Map<String, HtmlTableCell> suggestions = new HashMap<String, HtmlTableCell>();
+        final HtmlElement suggestElement = suggestion.getElementById(suggestion.getId() + ":suggest");
+        @SuppressWarnings("unchecked")
+        final DomNodeList<HtmlElement> suggestionRows = suggestElement.getElementsByTagName("tr");
+        for (HtmlElement row : suggestionRows) {
+            @SuppressWarnings("unchecked")
+            final DomNodeList<HtmlElement> cells = row.getElementsByTagName("td");
+            final HtmlTableCell cell = (HtmlTableCell) cells.get(column + 1);
+            suggestions.put(cell.asText(), cell);
+        }
+        return suggestions;
+    }
+}
--
libgit2 0.24.0