ReRenderedJavaScriptTest.java
7.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
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.DomNodeList;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import junit.framework.Assert;
import junit.framework.AssertionFailedError;
import org.junit.Test;
import java.io.IOException;
public class ReRenderedJavaScriptTest {
/**
* This test checks if results can be paginated properly.
* On first page there are 6 people and datascroller.
* After clicking page 2 the results should be re-rendered with
* response from ajax request. Datascroller should also be re-rendered.
* In order for datascroller to work the javascript that is rendered below it
* should be evaluated.
* This test fails cause HtmlUnit has problems with script elements from XHttpRequest
*/
@Test(expected = AssertionFailureCaughtException.class)
public void testPagination() throws IOException {
final WebClient client = new WebClient(BrowserVersion.FIREFOX_3_6);
client.setAjaxController(new NicelyResynchronizingAjaxController());
HtmlPage page = client.getPage("http://mamywladze.pl/ludzie");
HtmlElement table = page.getElementById("filterForm:j_id101:j_id113_table");
Assert.assertNotNull(table);
DomNodeList<HtmlElement> cells = table.getElementsByTagName("td");
Assert.assertNotNull(cells);
Assert.assertEquals(5, cells.size());
/**
* Current page has rich-datascr-act style class.
*/
String styleClass = cells.get(0).getAttribute("class");
Assert.assertNotNull(styleClass);
Assert.assertTrue("Page 1 is not the current page", styleClass.contains("rich-datascr-act"));
final HtmlElement cell2 = cells.get(1);
Assert.assertNotNull(cell2);
new RequestResponseLogger(client);
/**
* Now this click will trigger ajax request which response will be used to update results and datascroller.
*/
cell2.click();
/**
* Notice that output of following line should contain this:
* <script type="text/javascript">
* //<![CDATA[
* //
* //]]>
* </script>
* Its cause HtmlScript contains 3 children in stead of 1 and HtmlScript class uses only first child
* Child 1:
* //
* Child 2:
* Event.observe('filterForm:j_id101:j_id113', 'rich:datascroller:onscroll', function(event){A4J.AJAX.Submit('_viewRoot','filterForm',event,{'ignoreDupResponses':true,'parameters':{'filterForm:j_id101:j_id113':event.memo.page} ,'eventsQueue':'filterForm:j_id101:j_id113','actionUrl':'/ludzie'} ); return false;});
*
* Child 3:
* //
*
* And actual part of Xhttp response was:
* <script type="text/javascript">//<![CDATA[
* Event.observe('filterForm:j_id101:j_id113', 'rich:datascroller:onscroll', function(event){A4J.AJAX.Submit('_viewRoot','filterForm',event,{'ignoreDupResponses':true,'parameters':{'filterForm:j_id101:j_id113':event.memo.page} ,'eventsQueue':'filterForm:j_id101:j_id113','actionUrl':'/ludzie'} ); return false;});
* //]]>
* </script>
*
* It is fault of the parser that splits script's contents into 3 child nodes. It happends both in HtmlUnit and in FireFox. So i guess it is the nature
* of parser.
*/
System.out.println(page.getElementById("filterForm:personList").asXml());
table = page.getElementById("filterForm:j_id101:j_id113_table");
Assert.assertNotNull(table);
cells = table.getElementsByTagName("td");
Assert.assertNotNull(cells);
Assert.assertEquals(6, cells.size());
styleClass = cells.get(2).getAttribute("class"); //If you wonder why 2 in stead of 1, it's cause there is extra cell that navigates to the start
Assert.assertNotNull(styleClass);
Assert.assertTrue("Page 2 is not the current page", styleClass.contains("rich-datascr-act"));
/**
* This click however will not trigger ajax request cause script that was binding appropriate event listeners was not executed.
* (it came from previous ajax request)
*/
cells.get(1).click();
System.out.println(page.getElementById("filterForm:personList").asXml());
table = page.getElementById("filterForm:j_id101:j_id113_table");
Assert.assertNotNull(table);
cells = table.getElementsByTagName("td");
Assert.assertNotNull(cells);
/**
* This assertion should fail.
*/
try {
Assert.assertEquals(5, cells.size());
} catch (AssertionFailedError e) {
throw new AssertionFailureCaughtException();
}
styleClass = cells.get(0).getAttribute("class");
Assert.assertNotNull(styleClass);
Assert.assertTrue("Page 2 is not the current page", styleClass.contains("rich-datascr-act"));
}
/**
* This test is the same as testPagination but should pass cause it uses my nice hack.
*/
@Test
public void testPaginationSuccessfully() throws IOException {
final WebClient client = new WebClient(BrowserVersion.FIREFOX_3_6);
client.setAjaxController(new NicelyResynchronizingAjaxController());
HtmlPage page = client.getPage("http://mamywladze.pl/ludzie");
HtmlElement table = page.getElementById("filterForm:j_id101:j_id113_table");
Assert.assertNotNull(table);
DomNodeList<HtmlElement> cells = table.getElementsByTagName("td");
Assert.assertNotNull(cells);
Assert.assertEquals(5, cells.size());
/**
* Current page has rich-datascr-act style class.
*/
String styleClass = cells.get(0).getAttribute("class");
Assert.assertNotNull(styleClass);
Assert.assertTrue("Page 1 is not the current page", styleClass.contains("rich-datascr-act"));
final HtmlElement cell2 = cells.get(1);
Assert.assertNotNull(cell2);
new RequestResponseLogger(client);
/**
* Now this click will trigger ajax request which response will be used to update results and datascroller.
*/
cell2.click();
System.out.println(page.getElementById("filterForm:personList").asXml());
table = page.getElementById("filterForm:j_id101:j_id113_table");
Assert.assertNotNull(table);
cells = table.getElementsByTagName("td");
Assert.assertNotNull(cells);
Assert.assertEquals(6, cells.size());
styleClass = cells.get(2).getAttribute("class"); //If you wonder why 2 in stead of 1, it's cause there is extra cell that navigates to the start
Assert.assertNotNull(styleClass);
Assert.assertTrue("Page 2 is not the current page", styleClass.contains("rich-datascr-act"));
/**
* This click however will not trigger ajax request cause script that was binding appropriate event listeners was not executed.
* (it came from previous ajax request)
*/
cells.get(1).click();
/**
* Here is the hack.
*/
WebClientUtils.executeAjaxReRenderedScripts(page);
System.out.println(page.getElementById("filterForm:personList").asXml());
table = page.getElementById("filterForm:j_id101:j_id113_table");
Assert.assertNotNull(table);
cells = table.getElementsByTagName("td");
Assert.assertNotNull(cells);
/**
* This time the assertion should be ok
*/
Assert.assertEquals(5, cells.size());
styleClass = cells.get(0).getAttribute("class");
Assert.assertNotNull(styleClass);
Assert.assertTrue("Page 2 is not the current page", styleClass.contains("rich-datascr-act"));
}
public class AssertionFailureCaughtException extends RuntimeException {
}
}