packagepl.labno.bernard.htmlunified;importcom.gargoylesoftware.htmlunit.BrowserVersion;importcom.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;importcom.gargoylesoftware.htmlunit.WebClient;importcom.gargoylesoftware.htmlunit.html.DomNodeList;importcom.gargoylesoftware.htmlunit.html.HtmlElement;importcom.gargoylesoftware.htmlunit.html.HtmlPage;importjunit.framework.Assert;importjunit.framework.AssertionFailedError;importorg.junit.Test;importjava.io.IOException;publicclassReRenderedJavaScriptTest{/** * 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)publicvoidtestPagination()throwsIOException{finalWebClientclient=newWebClient(BrowserVersion.FIREFOX_3_6);client.setAjaxController(newNicelyResynchronizingAjaxController());HtmlPagepage=client.getPage("http://mamywladze.pl/ludzie");HtmlElementtable=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. */StringstyleClass=cells.get(0).getAttribute("class");Assert.assertNotNull(styleClass);Assert.assertTrue("Page 1 is not the current page",styleClass.contains("rich-datascr-act"));finalHtmlElementcell2=cells.get(1);Assert.assertNotNull(cell2);newRequestResponseLogger(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 startAssert.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(AssertionFailedErrore){thrownewAssertionFailureCaughtException();}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. */@TestpublicvoidtestPaginationSuccessfully()throwsIOException{finalWebClientclient=newWebClient(BrowserVersion.FIREFOX_3_6);client.setAjaxController(newNicelyResynchronizingAjaxController());HtmlPagepage=client.getPage("http://mamywladze.pl/ludzie");HtmlElementtable=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. */StringstyleClass=cells.get(0).getAttribute("class");Assert.assertNotNull(styleClass);Assert.assertTrue("Page 1 is not the current page",styleClass.contains("rich-datascr-act"));finalHtmlElementcell2=cells.get(1);Assert.assertNotNull(cell2);newRequestResponseLogger(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 startAssert.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"));}publicclassAssertionFailureCaughtExceptionextendsRuntimeException{}}