Selenium WebDriver using ruby with HTMLunit driver

1,049 views
Skip to first unread message

Michael

unread,
Apr 27, 2011, 11:37:06 AM4/27/11
to Selenium Users
Hello,

I am in the midst of tracing an issue discovered using watir-
webdriver, but have been able to reproduce the same issue with
selenium-webdriver. Here's a quick summary. I'm looking for insight
into how all these components communicate and link together -
specifically how selenium-webdriver communicates with the headless
HTMLunit browser via selenium-server-standalone.jar.

Repro:
- I have a simple HTML page with one form and a single image input
element. The form method is get and loads another static HTML page.
This is sufficient to see the issue. Included at bottom of this
messgae.
- The test code is at the bottom of this message.

Results are as follows:
htmlunit: http://path.to.server/htmlUnitTestPost.html?input-image-test=
firefox: http://path.to.server/htmlUnitTestPost.html?input-image-test.x=0&input-image-test.y=0
ie: http://path.to.server/htmlUnitTestPost.html?input-image-test.x=13&input-image-test.y=14

Analysis:
When selenium-webdriver (via remote htmlunit) clicks on a input image
it does not provide a default x/y coordinate, and as a result the page
is submitted as if a submit button was clicked not an input button. In
addition to not providing a default x/y, webdriver itself does not
provide a clickAt equivalent.

Discussion:
I initially assumed it was HTMLunit that was the cause and that it
didn't behave like other browsers. So I then tried to directly control
HTMLunit with Java - it behaves correctly returning:
http://path.to.server/htmlUnitTestPost.html?input-image-test.x=0&input-image-test.y=0

Which confused me: I checked out the read-only selenium source code,
and looked through HTMLunit, it does correctly implement the click
method providing a position and yielding the correct result (as
demonstrated above). Why doesn't selenium webdriver correctly wrap the
HTMLunit click method to make it behave in the expected way for input
images? How does webdriver interact with elements in the headless
HTMLunit browser since it doesn't seem to call the native element
methods?

I'm happy to submit the code patch once I figure out exactly where the
disconnect is occurring. Any thoughts or suggestions?

Thanks,
Michael



Ruby test:

require "test/unit"
require "rubygems"
require "selenium-webdriver"

class HtmlUnitTest < Test::Unit::TestCase

def test_html_unit
caps =
Selenium::WebDriver::Remote::Capabilities.htmlunit(:javascript_enabled
=> true)
preform_test(caps)
end

def test_firefox
caps =
Selenium::WebDriver::Remote::Capabilities.firefox(:javascript_enabled
=> true)
preform_test(caps)
end

def test_internet_explorer
caps =
Selenium::WebDriver::Remote::Capabilities.ie(:javascript_enabled =>
true)
preform_test(caps)
end

def preform_test(caps)
driver = Selenium::WebDriver.for(
:remote,
:url => "http://127.0.0.1:4444/wd/hub",
:desired_capabilities => caps
)

driver.navigate.to "http://path.to.server/htmlUnitTest.html"
driver.find_element(:name, 'input-image-test').click
puts driver.current_url
end

end # end HtmlUnitTest class


Java to directly control HTMLunit (snippet):

final WebClient webClient = new WebClient();
final HtmlPage page = webClient.getPage("http://path.to.server/
htmlUnitTest.html");
HtmlForm form = page.getFormByName("theform");
final HtmlImageInput button = form.getInputByName("input-image-
test");
button.click();
System.out.println(page.getUrl());


HTML Code (snippet):

<form name="theform" action="htmlUnitTestPost.html" method="get">
<input type="image" name="input-image-test" />
</form>

Jari Bakken

unread,
Apr 30, 2011, 6:50:55 AM4/30/11
to seleniu...@googlegroups.com
On Wed, Apr 27, 2011 at 5:37 PM, Michael <mmcwi...@gmail.com> wrote:
>
> Discussion:
> I initially assumed it was HTMLunit that was the cause and that it
> didn't behave like other browsers. So I then tried to directly control
> HTMLunit with Java - it behaves correctly returning:
> http://path.to.server/htmlUnitTestPost.html?input-image-test.x=0&input-image-test.y=0
>

Does WebDriver use the same version of HtmlUnit that you used to run this test?

Jari Bakken

unread,
Apr 30, 2011, 7:29:44 AM4/30/11
to seleniu...@googlegroups.com
On Wed, Apr 27, 2011 at 5:37 PM, Michael <mmcwi...@gmail.com> wrote:
>      final WebClient webClient = new WebClient();
>      final HtmlPage page = webClient.getPage("http://path.to.server/
> htmlUnitTest.html");
>          HtmlForm form = page.getFormByName("theform");
>      final HtmlImageInput button = form.getInputByName("input-image-
> test");
>      button.click();
>      System.out.println(page.getUrl());
>
>

Think I figured this out. WebDriver uses a slightly different
mechanism for clicking elements in HtmlUnit, to support holding down
modifier keys:

http://code.google.com/p/selenium/source/browse/trunk/java/client/src/org/openqa/selenium/htmlunit/HtmlUnitMouse.java#78
http://htmlunit.sourceforge.net/apidocs/com/gargoylesoftware/htmlunit/html/HtmlElement.html#click(boolean,
boolean, boolean)

If you change your Java code to use that, you'll be able to reproduce
the problem with HtmlUnit directly:

button.click(false, false, false)

I don't see any reason why the behaviour here should be different from
a normal click(), so you should definitely file a bug with HtmlUnit
after verifying it's not already fixed in trunk.

Jari

Reply all
Reply to author
Forward
0 new messages