Commit 7d1f860d7c8c2eefe3215362826c54293a7f3c1d
0 parents
Initial import of htmlunit-sandbox project.
Showing
3 changed files
with
222 additions
and
0 deletions
pom.xml
0 → 100644
1 | +<?xml version="1.0" encoding="UTF-8"?> | ||
2 | +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
3 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
4 | + | ||
5 | + <modelVersion>4.0.0</modelVersion> | ||
6 | + | ||
7 | + | ||
8 | + <groupId>pl.labno.bernard</groupId> | ||
9 | + <artifactId>htmlunit-sandbox</artifactId> | ||
10 | + <version>1.0-SNAPSHOT</version> | ||
11 | + <name>${project.artifactId} : ${project.version}</name> | ||
12 | + <description>Htmlunit sandbox</description> | ||
13 | + <url></url> | ||
14 | + <packaging>pom</packaging> | ||
15 | + | ||
16 | + | ||
17 | + <dependencies> | ||
18 | + <dependency> | ||
19 | + <groupId>net.sourceforge.htmlunit</groupId> | ||
20 | + <artifactId>htmlunit</artifactId> | ||
21 | + <version>2.8</version> | ||
22 | + </dependency> | ||
23 | + <dependency> | ||
24 | + <groupId>junit</groupId> | ||
25 | + <artifactId>junit</artifactId> | ||
26 | + <version>4.8.2</version> | ||
27 | + </dependency> | ||
28 | + </dependencies> | ||
29 | + | ||
30 | + <build> | ||
31 | + <plugins> | ||
32 | + <plugin> | ||
33 | + <groupId>org.apache.maven.plugins</groupId> | ||
34 | + <artifactId>maven-compiler-plugin</artifactId> | ||
35 | + <configuration> | ||
36 | + <source>1.5</source> | ||
37 | + <target>1.5</target> | ||
38 | + <debug>true</debug> | ||
39 | + <optimize>false</optimize> | ||
40 | + </configuration> | ||
41 | + </plugin> | ||
42 | + | ||
43 | + </plugins> | ||
44 | + </build> | ||
45 | + | ||
46 | + | ||
47 | +</project> |
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.HtmlElement; | ||
7 | +import com.gargoylesoftware.htmlunit.html.HtmlInput; | ||
8 | +import com.gargoylesoftware.htmlunit.html.HtmlPage; | ||
9 | +import com.gargoylesoftware.htmlunit.html.HtmlSpan; | ||
10 | +import com.gargoylesoftware.htmlunit.html.HtmlTableCell; | ||
11 | +import org.junit.Assert; | ||
12 | +import org.junit.Test; | ||
13 | + | ||
14 | +import java.io.IOException; | ||
15 | +import java.util.ArrayList; | ||
16 | +import java.util.List; | ||
17 | +import java.util.Map; | ||
18 | + | ||
19 | +public class ScheduleTest { | ||
20 | + | ||
21 | + // @Test | ||
22 | + public void accessEventTitles() throws IOException { | ||
23 | + 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 | + | ||
35 | + final List<HtmlSpan> titleElements = (List<HtmlSpan>) page.getElementById("results:schedule").getByXPath(".//span[@class='fc-event-title']"); | ||
36 | + List<String> titles = new ArrayList<String>(titleElements.size()); | ||
37 | + for (HtmlSpan o : titleElements) { | ||
38 | + titles.add(o.asText()); | ||
39 | + } | ||
40 | + Assert.assertEquals(2, titles.size()); | ||
41 | + } | ||
42 | + | ||
43 | + @Test | ||
44 | + public void clickSuggestionBox() throws IOException { | ||
45 | + 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 | + | ||
60 | + studentInput.type('w'); | ||
61 | + studentInput.type('i'); | ||
62 | + Assert.assertEquals(client.getCurrentWindow().getEnclosedPage(), page); | ||
63 | +// Here the suggestionBox is still hidden | ||
64 | +// System.out.println(page.asXml()); | ||
65 | + | ||
66 | + int jobs = WebClientUtils.waitForJSJob(client, 0, 30000); | ||
67 | +// 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 | + 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 | + Map<String, HtmlTableCell> suggestions = WebClientUtils.getSuggestions(page.getElementById("searchClass:sS"), 0); | ||
81 | + Assert.assertEquals(1, suggestions.size()); | ||
82 | + System.out.println(suggestions); | ||
83 | + HtmlTableCell cell = suggestions.get("Willis Bruce"); | ||
84 | + Assert.assertNotNull(cell); | ||
85 | + initialJobs = client.waitForBackgroundJavaScript(10); | ||
86 | + System.out.println("initial jobs:"+initialJobs); | ||
87 | + 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()); | ||
96 | + } | ||
97 | + | ||
98 | +} |
1 | +package pl.labno.bernard.htmlunified; | ||
2 | + | ||
3 | +import com.gargoylesoftware.htmlunit.WebClient; | ||
4 | +import com.gargoylesoftware.htmlunit.html.DomNodeList; | ||
5 | +import com.gargoylesoftware.htmlunit.html.HtmlElement; | ||
6 | +import com.gargoylesoftware.htmlunit.html.HtmlTableCell; | ||
7 | + | ||
8 | +import java.util.HashMap; | ||
9 | +import java.util.Map; | ||
10 | + | ||
11 | +public class WebClientUtils { | ||
12 | + | ||
13 | + private static long defaultCheckInterval = 500; | ||
14 | + private static long defaultTimeout = 10000; | ||
15 | + | ||
16 | + public static long getDefaultCheckInterval() { | ||
17 | + return defaultCheckInterval; | ||
18 | + } | ||
19 | + | ||
20 | + public static void setDefaultCheckInterval(long defaultCheckInterval) { | ||
21 | + WebClientUtils.defaultCheckInterval = defaultCheckInterval; | ||
22 | + } | ||
23 | + | ||
24 | + public static long getDefaultTimeout() { | ||
25 | + return defaultTimeout; | ||
26 | + } | ||
27 | + | ||
28 | + public static void setDefaultTimeout(long defaultTimeout) { | ||
29 | + WebClientUtils.defaultTimeout = defaultTimeout; | ||
30 | + } | ||
31 | + | ||
32 | + public static int waitForJSJob(WebClient webClient) { | ||
33 | + return waitForJSJob(webClient, webClient.waitForBackgroundJavaScript(10) - 1, defaultCheckInterval, defaultTimeout); | ||
34 | + } | ||
35 | + | ||
36 | + public static int waitForJSJob(WebClient webClient, int initialJobCount) { | ||
37 | + return waitForJSJob(webClient, initialJobCount, defaultCheckInterval, defaultTimeout); | ||
38 | + } | ||
39 | + | ||
40 | + public static int waitForJSJob(WebClient webClient, int initialJobCount, long timeout) { | ||
41 | + return waitForJSJob(webClient, initialJobCount, defaultCheckInterval, timeout); | ||
42 | + } | ||
43 | + | ||
44 | + public static int waitForJSJob(WebClient webClient, int initialJobCount, long checkInterval, long timeout) { | ||
45 | + int jobs; | ||
46 | + long startTime = System.currentTimeMillis(); | ||
47 | + do { | ||
48 | + jobs = webClient.waitForBackgroundJavaScript(checkInterval); | ||
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!"); | ||
51 | + } | ||
52 | + } while (jobs > initialJobCount); | ||
53 | + System.out.println("Waiting took: " + (System.currentTimeMillis() - startTime) + "ms"); | ||
54 | + return jobs; | ||
55 | + } | ||
56 | + | ||
57 | + /** | ||
58 | + * Returns list of suggestions from rich:suggestionBox | ||
59 | + * | ||
60 | + * @param suggestion suggestionBox element | ||
61 | + * @param column column of suggestionBox to extract text from | ||
62 | + * @return list of suggestions | ||
63 | + */ | ||
64 | + public static Map<String, HtmlTableCell> getSuggestions(HtmlElement suggestion, int column) { | ||
65 | + final Map<String, HtmlTableCell> suggestions = new HashMap<String, HtmlTableCell>(); | ||
66 | + final HtmlElement suggestElement = suggestion.getElementById(suggestion.getId() + ":suggest"); | ||
67 | + @SuppressWarnings("unchecked") | ||
68 | + final DomNodeList<HtmlElement> suggestionRows = suggestElement.getElementsByTagName("tr"); | ||
69 | + for (HtmlElement row : suggestionRows) { | ||
70 | + @SuppressWarnings("unchecked") | ||
71 | + final DomNodeList<HtmlElement> cells = row.getElementsByTagName("td"); | ||
72 | + final HtmlTableCell cell = (HtmlTableCell) cells.get(column + 1); | ||
73 | + suggestions.put(cell.asText(), cell); | ||
74 | + } | ||
75 | + return suggestions; | ||
76 | + } | ||
77 | +} |
Please
register
or
login
to post a comment