Menu

#1772 [HtmlUnitDriver] Form is submitted, even if e.preventDefault() was called

Latest SVN
accepted
nobody
None
1
2016-04-15
2016-04-11
Madis Pärn
No

HtmlTextInput.type("\n") causes form submittal, even if javascript keydown event handler calls event.preventDefault()

Discussion

  • Madis Pärn

    Madis Pärn - 2016-04-11

    Failing testcase

        @Test
        public void noSubmitOnEnterAndPrevented() throws Exception {
            final String html =
                  "<html><head><script>\n"
                + "  function keydown(e) {\n"
                + "    e.preventDefault();\n"
                + "  }\n"
                + "  function submit() {\n"
                + "    throw new Error('Should not submit form');\n"
                + "  }\n"
                + "  function init() {\n"
                + "    document.getElementById('form1').onsubmit = submit;\n"
                + "    document.getElementById('text1').onkeydown = keydown;\n"
                + "  }\n"
                + "</script></head>\n"
                + "<body onload='init()'>\n"
                + "<form id='form1'>\n"
                + "<input id='text1'/>\n"
                + "</form>\n"
                + "</body></html>";
    
            final WebDriver driver = loadPage2(html);
            final WebElement field = driver.findElement(By.id("text1"));
    
            field.sendKeys("\n");
        }
    
     

    Last edit: Madis Pärn 2016-04-11
  • Madis Pärn

    Madis Pärn - 2016-04-11

    I think both org.openqa.selenium.htmlunit.HtmlUnitWebElement.sendKeys(CharSequence...) and com.gargoylesoftware.htmlunit.html.HtmlElement.type(char, boolean, boolean, boolean) need to be modified

     
  • Ahmed Ashour

    Ahmed Ashour - 2016-04-12
    • status: open --> accepted
    • assigned_to: Ahmed Ashour
     
  • Ahmed Ashour

    Ahmed Ashour - 2016-04-12
    • labels: events -->
    • summary: Form is submitted, even if e.preventDefault() was called --> [HtmlUnitDriver] Form is submitted, even if e.preventDefault() was called
    • assigned_to: Ahmed Ashour --> nobody
     
  • Ahmed Ashour

    Ahmed Ashour - 2016-04-12

    This is related to HtmlUnitDriver, which should use the type(Keyboard) instead of sending the characters.

    The below scenario succeess with direct usage of HtmlUnit.

            HtmlPage page = webClient.getPage("http://localhost");
            HtmlElement e = (HtmlElement) page.getElementById("text1");
            e.type(KeyboardEvent.DOM_VK_RETURN);
    
     
  • Madis Pärn

    Madis Pärn - 2016-04-12

    Do you suggest I should be using the HtmlElement.type(int) instead of HtmlElement.type(String) ?

     
  • Ahmed Ashour

    Ahmed Ashour - 2016-04-12

    If you can directly use HtmlUnit (without HtmlUnitDriver), then go ahead and use the type(int).

    On the long term, all should be replaced by type(Keyboard).

     
  • Madis Pärn

    Madis Pärn - 2016-04-12

    When using HtmlElement.type(Keyboard) I also get the unexpected form submittal.

    Keyboard kb = new Keyboard();
    kb.type('\n');
    e.type(kb);
    

    Also some kind of KeyboardBuilder would be nice

     

    Last edit: Madis Pärn 2016-04-12
  • Ahmed Ashour

    Ahmed Ashour - 2016-04-12

    Keyboard should prevent using special 'characters' as '\n'. Please use .press(int) and .release(int).

    Feel free to sumbmit patch for KeybaordBuilder :)

     
  • Madis Pärn

    Madis Pärn - 2016-04-12

    Keyboard as builder patch

     
  • Madis Pärn

    Madis Pärn - 2016-04-12

    But why does the user have to know that '\n' is special character? Keyboard itself could handle the transformation from .type(char) to .press(int) and .release(int).

    Also I would expect the HtmlUnit to submit the form, if the event is not prevented.
    Fixing the com.gargoylesoftware.htmlunit.html.HtmlElement.type(char, boolean, boolean, boolean) seems like correct way to me.

     

    Last edit: Madis Pärn 2016-04-12
  • Ahmed Ashour

    Ahmed Ashour - 2016-04-12

    I just want to hint about few things.

    Previously, it was type(char), and then type(int) was added for special keys. Then came cases where you need to press(shift), type('a'), release(shift), so special characters (alt,shift,ctrl) shouldn't be typed (as the wouldn't make much use in this case), but pressed/released before/after other characters.

    For '\n' I can see you point.

    Please feel free to submit your recommendations.

     
  • Madis Pärn

    Madis Pärn - 2016-04-15

    I have no other suggestions, besides the already attached batch HtmlElement.java.patch

     

Log in to post a comment.