From: Berg Klaus-P. <kla...@si...> - 2004-09-02 14:57:47
|
Hi all, I have tried to run htmlunit with javascript but encountered that the expected javascript method (doSearch() in this case) will not be = called. Perhaps, someone has an idea what went wrong... Here is the code snipped: public class WebUITest3 extends TestCase { private WebClient webClient; protected void setUp() throws Exception { webClient =3D new = WebClient(BrowserVersion.INTERNET_EXPLORER_6_0); webClient.setRedirectEnabled(true); webClient.setJavaScriptEnabled(true); } public void testSendLogonPage() throws Exception { URL url =3D new URL("http://xyz/index.html"); Page page =3D webClient.getPage(url); final HtmlPage logonPage =3D (HtmlPage) page; =20 final HtmlForm form =3D logonPage.getFormByName("sform"); // Now change the desired input field final HtmlTextInput textField =3D (HtmlTextInput) form.getInputByName("SearchWhat"); textField.setValueAttribute("test"); HtmlInput searchButton =3D form.getInputByName("submit"); assertTrue("Search button is not an image", searchButton instanceof HtmlImageInput); final Page resultPage =3D searchButton.click(); // emulate user clicking "start search" image button // verify search result ... } } and part of the HTML page being called: function checkQueryText (textElement) { if (textElement.length < 2) { return false } else { return true } } function checkTheQuery (myform) { var queryOK; queryOK =3D checkQueryText (myform ["SearchWhat"].value); if (! queryOK) { alertText =3D "Bitte geben Sie einen Suchbegriff ein, der l=E4nger 1 Zeichen ist."; //alertText =3D "Please enter a search term longer than 1 character."; alert (alertText); }=20 return (queryOK) } function doSearch (myform) { v_url =3D v_url + "pg=3Daq";// pg=3Daq (advanced query) v_url =3D v_url + "&catfilter=3Duser";// catfilter=3Duser (use the = filter from customcategorieval) queryString =3D myform ["SearchWhat"].value; v_url =3D v_url + "&customcategorieval=3D" + webify (website); v_url =3D v_url + "&customcategoriedisp=3D" + webify (categorie); v_url =3D v_url + "&q=3D" + webify (queryString); v_url =3D v_url + "&nolanguage=3DTrue"; v_url =3D v_url + "&noquicktips=3DTrue"; v_url =3D v_url + "&nokeywordindex=3DTrue"; v_url =3D v_url + "&uil=3Dde"; window.location.href =3D v_url;=20 } <form action=3D"javascript:doSearch (document.sform);" method=3D"post" name=3D"sform" id=3D"sform" onSubmit=3D"return checkTheQuery (this);"> <td width=3D"*" colspan=3D"2" bgcolor=3D"#ffcc99"><table border=3D"0" cellspacing=3D"0" cellpadding=3D"0"> <tr> <td><img src=3D"/cti/images/common/1x1_blank.gif" height=3D"1" width=3D"18" border=3D"0"></td> <td nowrap><input type=3D"text" name=3D"SearchWhat" maxlength=3D"40" value=3D"search for?" size=3D"8" style=3D"height:20px;width:90px;font-size:12px;"></td> <td nowrap align=3D"right"><input type=3D"image" name=3D"submit" src=3D"/images/start_search_button.gif" width=3D"74" height=3D"36" border=3D"0"></td> </tr></table> </td><input type=3D"hidden" name=3D"document" value=3D""> </form> Software development is fundamentally hard. -- Grady Booch |
From: Andrey S. <asu...@oi...> - 2004-09-03 05:35:30
|
Accodring to HTML DOM all attribute names must be lowercase. Try "onsubmit" instead of "onSubmit". This is the name HtmlUnit looks for in HtmlForm.getOnSubmitAttribute method. > I have tried to run htmlunit with javascript but encountered that the > expected javascript method (doSearch() in this case) will not be called. > Perhaps, someone has an idea what went wrong... ... > <form action="javascript:doSearch (document.sform);" method="post" > name="sform" id="sform" onSubmit="return checkTheQuery (this);"> |
From: Andrey S. <asu...@oi...> - 2004-09-03 08:45:48
|
Er, after taking a second and a third look at your code, I understood that my previous letter missed the point. While onSubmit is still wrong, your problem happens somewhere else. The misnamed onsubmit is simply ignored, and the code should proceed directly to action. What do you mean by "not called"? Does absolutely nothing? Fails with exception? prints something to log? From the first look, the quoted pieces should generally work. In particular, image buttons are supported and, when clicked, should submit the form; form action can be a javascript:foo() url; and assigning new url to document.location.href is supported. I expect that doSearch() itself is called, but an error happens somewhere along the way. I would recommend registering an AlertHandler by calling aWebClient.setAlertHandler(h) that prints alert text to System.out. Then systematically insertng alert() in javascript to pinpoint the error. This is what I do when something strange happens in tested pages (i.e. all the time :). Also make sure that your HtmlUnit logging is enabled. Maybe add assertTrue(aWebClient.getLog().isErrorEnabled()) to your test. Andrey |