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