From: <rb...@us...> - 2014-03-06 20:22:32
|
Revision: 9157 http://sourceforge.net/p/htmlunit/code/9157 Author: rbri Date: 2014-03-06 20:22:28 +0000 (Thu, 06 Mar 2014) Log Message: ----------- Fix handling of coordinate parameters when clicking on an image input. Some test cases migrated to webdriver and retested. Fix the javascript configuration for Chrome. Modified Paths: -------------- trunk/htmlunit/src/changes/changes.xml trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/BrowserVersionFeatures.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlImageInput.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLElement.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlImageInput2Test.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlImageInputTest.java Modified: trunk/htmlunit/src/changes/changes.xml =================================================================== --- trunk/htmlunit/src/changes/changes.xml 2014-03-06 07:24:13 UTC (rev 9156) +++ trunk/htmlunit/src/changes/changes.xml 2014-03-06 20:22:28 UTC (rev 9157) @@ -8,6 +8,9 @@ <body> <release version="2.15" date="???" description="Bugfixes"> + <action type="fix" dev="rbri" issue="1581"> + JavaScript: Fix handling of coordinate parameters when clicking on an image input. + </action> <action type="fix" dev="asashour"> JavaScript: fix setting value to toString() function. </action> Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/BrowserVersionFeatures.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/BrowserVersionFeatures.java 2014-03-06 07:24:13 UTC (rev 9156) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/BrowserVersionFeatures.java 2014-03-06 20:22:28 UTC (rev 9157) @@ -547,8 +547,8 @@ @BrowserFeature({ @WebBrowser(FF), @WebBrowser(CHROME), @WebBrowser(value = IE, minVersion = 11) }) HTMLIFRAME_IGNORE_SELFCLOSING, - /** */ - @BrowserFeature({ @WebBrowser(FF), @WebBrowser(CHROME) }) + /** Clicking an image input submits the value as param if defined. */ + @BrowserFeature(@WebBrowser(CHROME)) HTMLIMAGE_NAME_VALUE_PARAMS, /** Setting defaultValue updates the value also. */ Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlImageInput.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlImageInput.java 2014-03-06 07:24:13 UTC (rev 9156) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlImageInput.java 2014-03-06 20:22:28 UTC (rev 9157) @@ -24,6 +24,7 @@ import com.gargoylesoftware.htmlunit.ElementNotFoundException; import com.gargoylesoftware.htmlunit.Page; import com.gargoylesoftware.htmlunit.SgmlPage; +import com.gargoylesoftware.htmlunit.javascript.host.Event; import com.gargoylesoftware.htmlunit.util.NameValuePair; /** @@ -127,15 +128,33 @@ * @exception ElementNotFoundException If a particular XML element could not be found in the DOM model */ @Override - @SuppressWarnings("unchecked") public <P extends Page> P click(final int x, final int y) throws IOException, ElementNotFoundException { wasPositionSpecified_ = true; xPosition_ = x; yPosition_ = y; - return (P) super.click(); + return super.click(); } /** + * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br/> + * + * Simulates clicking on this element, returning the page in the window that has the focus + * after the element has been clicked. Note that the returned page may or may not be the same + * as the original page, depending on the type of element being clicked, the presence of JavaScript + * action listeners, etc. + * + * @param event the click event used + * @param <P> the page type + * @return the page contained in the current window as returned by {@link WebClient#getCurrentWindow()} + * @exception IOException if an IO error occurs + */ + @Override + public <P extends Page> P click(final Event event) throws IOException { + wasPositionSpecified_ = true; + return super.click(event); + } + + /** * {@inheritDoc} Also sets the value to the new default value, just like IE. * @see SubmittableElement#setDefaultValue(String) */ Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLElement.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLElement.java 2014-03-06 07:24:13 UTC (rev 9156) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/html/HTMLElement.java 2014-03-06 20:22:28 UTC (rev 9157) @@ -2120,7 +2120,7 @@ * Click this element. This simulates the action of the user clicking with the mouse. * @throws IOException if this click triggers a page load that encounters problems */ - @JsxFunction({ @WebBrowser(IE), @WebBrowser(value = FF, minVersion = 10) }) + @JsxFunction({ @WebBrowser(IE), @WebBrowser(value = FF, minVersion = 10), @WebBrowser(CHROME) }) public void click() throws IOException { getDomNodeOrDie().click(); } Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlImageInput2Test.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlImageInput2Test.java 2014-03-06 07:24:13 UTC (rev 9156) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlImageInput2Test.java 2014-03-06 20:22:28 UTC (rev 9157) @@ -14,17 +14,26 @@ */ package com.gargoylesoftware.htmlunit.html; +import static com.gargoylesoftware.htmlunit.BrowserRunner.Browser.CHROME; +import static com.gargoylesoftware.htmlunit.BrowserRunner.Browser.IE; + import org.junit.Test; import org.junit.runner.RunWith; +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; import com.gargoylesoftware.htmlunit.BrowserRunner; import com.gargoylesoftware.htmlunit.BrowserRunner.Alerts; +import com.gargoylesoftware.htmlunit.BrowserRunner.NotYetImplemented; import com.gargoylesoftware.htmlunit.WebDriverTestCase; /** * Tests for {@link HtmlImageInput}. * * @version $Revision$ + * @author <a href="mailto:mb...@Ga...">Mike Bowler</a> + * @author Marc Guillemot + * @author Ahmed Ashour * @author Ronald Brill */ @RunWith(BrowserRunner.class) @@ -34,6 +43,52 @@ * @throws Exception if the test fails */ @Test + @Alerts(DEFAULT = "§§URL§§?button.x=0&button.y=0", + CHROME = "§§URL§§?button.x=10&button.y=10&button=foo", + IE8 = "§§URL§§?button.x=15&button.y=16", + IE11 = "§§URL§§?button.x=14&button.y=15") + @NotYetImplemented({ CHROME, IE }) + public void testClick_NoPosition() throws Exception { + final String html + = "<html><head><title>foo</title></head><body>\n" + + "<form id='form1'>\n" + + "<input type='image' name='aButton' value='foo'/>\n" + + "<input type='image' name='button' value='foo'/>\n" + + "<input type='image' name='anotherButton' value='foo'/>\n" + + "</form></body></html>"; + final WebDriver webDriver = loadPage2(html); + webDriver.findElement(By.name("button")).click(); + + expandExpectedAlertsVariables(URL_FIRST); + assertEquals(getExpectedAlerts()[0], webDriver.getCurrentUrl()); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts(DEFAULT = "§§URL§§?button.x=0&button.y=0", + CHROME = "§§URL§§?button.x=24&button.y=10", + IE8 = "§§URL§§?button.x=15&button.y=16", + IE11 = "§§URL§§?button.x=14&button.y=15") + @NotYetImplemented({ CHROME, IE }) + public void testClick_NoPosition_NoValue() throws Exception { + final String html + = "<html><head><title>foo</title></head><body>\n" + + "<form id='form1'>\n" + + "<input type='image' name='button'>\n" + + "</form></body></html>"; + final WebDriver webDriver = loadPage2(html); + webDriver.findElement(By.name("button")).click(); + + expandExpectedAlertsVariables(URL_FIRST); + assertEquals(getExpectedAlerts()[0], webDriver.getCurrentUrl()); + } + + /** + * @throws Exception if the test fails + */ + @Test @Alerts({ "-", "-", "-" }) public void defaultValues() throws Exception { final String html = "<html><head><title>foo</title>\n" @@ -201,4 +256,72 @@ loadPageWithAlerts2(html); } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("§§URL§§?imageInput.x=0&imageInput.y=0") + public void javascriptClick() throws Exception { + final String html = "<html><head><title>foo</title>\n" + + "</head><body>\n" + + "<form>\n" + + " <input type='image' name='imageInput'>\n" + + " <input type='button' id='submit' value='submit' " + + "onclick='document.getElementsByName(\"imageInput\")[0].click()'>\n" + + "</form>\n" + + "</body></html>"; + + final WebDriver webDriver = loadPage2(html); + webDriver.findElement(By.id("submit")).click(); + + expandExpectedAlertsVariables(URL_FIRST); + assertEquals(getExpectedAlerts()[0], webDriver.getCurrentUrl()); + } + + /** + * Test for bug: http://sourceforge.net/p/htmlunit/bugs/646/. + * @throws Exception if an error occurs + */ + @Test + @Alerts("1") + public void testClickFiresOnMouseDown() throws Exception { + final String html = "<html><body><input type='image' src='x.png' id='i' onmousedown='alert(1)'></body></html>"; + + final WebDriver webDriver = loadPage2(html); + webDriver.findElement(By.id("i")).click(); + + assertEquals(getExpectedAlerts(), getCollectedAlerts(webDriver)); + } + + /** + * Test for bug: http://sourceforge.net/p/htmlunit/bugs/646/. + * @throws Exception if an error occurs + */ + @Test + @Alerts("1") + public void testClickFiresOnMouseUp() throws Exception { + final String html = "<html><body><input type='image' src='x.png' id='i' onmouseup='alert(1)'></body></html>"; + final WebDriver webDriver = loadPage2(html); + webDriver.findElement(By.id("i")).click(); + + assertEquals(getExpectedAlerts(), getCollectedAlerts(webDriver)); + } + + /** + * @throws Exception if the test fails + */ + @Test + @Alerts("1") + public void testOutsideForm() throws Exception { + final String html = + "<html><head></head>\n" + + "<body>\n" + + "<input id='myInput' type='image' src='test.png' onclick='alert(1)'>\n" + + "</body></html>"; + final WebDriver webDriver = loadPage2(html); + webDriver.findElement(By.id("myInput")).click(); + + assertEquals(getExpectedAlerts(), getCollectedAlerts(webDriver)); + } } Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlImageInputTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlImageInputTest.java 2014-03-06 07:24:13 UTC (rev 9156) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlImageInputTest.java 2014-03-06 20:22:28 UTC (rev 9157) @@ -43,59 +43,6 @@ * @throws Exception if the test fails */ @Test - public void testClick_NoPosition() throws Exception { - final String htmlContent - = "<html><head><title>foo</title></head><body>\n" - + "<form id='form1' method='post'>\n" - + "<input type='image' name='aButton' value='foo'/>\n" - + "<input type='image' name='button' value='foo'/>\n" - + "<input type='image' name='anotherButton' value='foo'/>\n" - + "</form></body></html>"; - final HtmlPage page = loadPageWithAlerts(htmlContent); - final MockWebConnection webConnection = getMockConnection(page); - - final HtmlForm form = page.getHtmlElementById("form1"); - - final HtmlImageInput imageInput = form.getInputByName("button"); - final HtmlPage secondPage = (HtmlPage) imageInput.click(); - assertNotNull(secondPage); - - final List<NameValuePair> expectedPairs = new ArrayList<NameValuePair>(); - expectedPairs.add(new NameValuePair("button.x", "0")); - expectedPairs.add(new NameValuePair("button.y", "0")); - if (getBrowserVersion().isFirefox()) { - expectedPairs.add(new NameValuePair("button", "foo")); - } - - assertEquals( - expectedPairs, - webConnection.getLastParameters()); - } - - /** - * @throws Exception if the test fails - */ - @Test - public void testClick_NoPosition_NoValue() throws Exception { - final String htmlContent - = "<html><head><title>foo</title></head><body>\n" - + "<form id='form1'>\n" - + "<input type='image' name='button'>\n" - + "</form></body></html>"; - final HtmlPage page = loadPageWithAlerts(htmlContent); - getMockConnection(page).setDefaultResponse(htmlContent); - final HtmlForm form = page.getHtmlElementById("form1"); - - final HtmlImageInput imageInput = form.getInputByName("button"); - final HtmlPage secondPage = (HtmlPage) imageInput.click(); - final String url = secondPage.getUrl().toExternalForm(); - assertTrue(url.endsWith("?button.x=0&button.y=0")); - } - - /** - * @throws Exception if the test fails - */ - @Test public void testClick_WithPosition() throws Exception { final String html = "<html><head><title>foo</title></head><body>\n" @@ -116,9 +63,6 @@ final List<NameValuePair> expectedPairs = new ArrayList<NameValuePair>(); expectedPairs.add(new NameValuePair("button.x", "100")); expectedPairs.add(new NameValuePair("button.y", "200")); - if (getBrowserVersion().isFirefox()) { - expectedPairs.add(new NameValuePair("button", "foo")); - } assertEquals( expectedPairs, @@ -155,52 +99,4 @@ expectedPairs, webConnection.getLastParameters()); } - - /** - * @throws Exception if the test fails - */ - @Test - public void testOutsideForm() throws Exception { - final String html = - "<html><head></head>\n" - + "<body>\n" - + "<input id='myInput' type='image' src='test.png' onclick='alert(1)'>\n" - + "</body></html>"; - - final String[] expectedAlerts = {"1"}; - final List<String> collectedAlerts = new ArrayList<String>(); - final HtmlPage page = loadPage(getBrowserVersion(), html, collectedAlerts); - final HtmlImageInput input = page.getHtmlElementById("myInput"); - input.click(); - - assertEquals(expectedAlerts, collectedAlerts); - } - - /** - * Test for bug: http://sourceforge.net/p/htmlunit/bugs/646/. - * @throws Exception if an error occurs - */ - @Test - public void testClickFiresOnMouseDown() throws Exception { - final String s = "<html><body><input type='image' src='x.png' id='i' onmousedown='alert(1)'></body></html>"; - final String[] expectedAlerts = {"1"}; - final List<String> collectedAlerts = new ArrayList<String>(); - final HtmlPage page = loadPage(getBrowserVersion(), s, collectedAlerts); - page.getHtmlElementById("i").click(); - assertEquals(expectedAlerts, collectedAlerts); - } - - /** - * Test for bug: http://sourceforge.net/p/htmlunit/bugs/646/. - * @throws Exception if an error occurs - */ - @Test - public void testClickFiresOnMouseUp() throws Exception { - final String s = "<html><body><input type='image' src='x.png' id='i' onmouseup='alert(1)'></body></html>"; - final String[] expectedAlerts = {"1"}; - final List<String> collectedAlerts = new ArrayList<String>(); - final HtmlPage page = loadPage(getBrowserVersion(), s, collectedAlerts); - page.getHtmlElementById("i").click(); - assertEquals(expectedAlerts, collectedAlerts); - } } |