Recently, when I adopt the HtmlUnit into testing, I encountered a weird problem.
In the page, there is a BUTTON element. Whenever the button is clicked, a value will be submitted to server, result in a change in content.
However, HtmlUnit won't let me see the new content.
The code look like this:
WebClient wc = new WebClient();
HtmlPage page = (HtmlPage) wc.getPage("http://localhost/doSomething");
HtmlButton button = (HtmlButton) page.getByXPath(...);
HtmlPage page2 = button.click();
As that step, if I make an evaluation in my debugger, then page==page2 will result in true (normally, it would be false). So, I come to a conclusion that HtmlUnit didn't submit the form when I clicked on the button.
So, I replaced [BUTTON] element with [INPUT type="submit"] and the page==page2 will now result in false, the page is properly submitted.
Oh, sh*t, I whispered, a bug in the HtmlUnit code base....
But well, thing isn't that bad. After looking around for more information about BUTTON element, I found out that BUTTON have an attribute named type, which can be set to either "submit|button|reset". So, I gave it a try, add [type="button"] to the [BUTTON] element and BOOM, my code worked.
Hmm, a little difference with the expectation but thing is fine. After all, I learned a lesson: When in doubt, make all properties explicit.
HtmlUnit is great, though.
Thứ Hai, 22 tháng 3, 2010
Đăng ký:
Đăng Nhận xét (Atom)
Không có nhận xét nào:
Đăng nhận xét