Menu

Problem with form.submit()

2006-05-22
2013-04-26
  • Miroslav Šulc

    Miroslav Šulc - 2006-05-22

    Hi,

    I am very new to httpunit so I might miss something obvious. I have problem with testing my login HTML page and I cannot find out where the problem is. I just load the login page, set login information and use form.submit() to log in. But wc.getCurrentPage() still returns the login page instead of the page that should be displayed when user successfully logs in (I test whether the after-login page contains login form). If I do the test manually on the page, I successfully log in.

    I have spent hours trying to make it work but didn't succeed. I would appreciate any suggestion or help. Below is my test code + the login HTML page. The HTML page is XHTML valid.

        public void testTest3()
            throws MalformedURLException, IOException, SAXException {
            WebConversation wc = new WebConversation();
            WebResponse response =
                wc.getResponse("http://localhost:8080/intranet/");
            WebForm form = response.getFormWithID("frmLogin");
            form.setParameter("UserName", "user");
            form.setParameter("Password", "password");
            form.submit();
            response = wc.getCurrentPage();
            form = response.getFormWithID("frmLogin");
            assertNull("login form", form);
        }

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cs" lang="cs">
        <head>
            <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
            <meta name="Author" content="StartNet s.r.o."/>
            <meta name="copyright" content="Copyright 1997-2006 StartNet s.r.o. http://www.startnet.cz/ e-mail: info@startnet.cz"/>
            <title>StartNet® Intranet™ 4.0.0</title>
            <link rel="stylesheet" type="text/css" href="/intranet/cai/css/main_linux_gecko.css" title="main"/>
        </head>

        <body>
    <table class="TopBar">
        <colgroup>
            <col width="33%"/>
            <col width="34%"/>
            <col width="33%"/>
        </colgroup>
        <tbody>
            <tr>

                <td class="TopBarUserInfo">Uživatel: nepřihlášen</td>
                <td class="TopBarModuleInfo">StartNet® Intranet™ 4.0.0</td>
                <td class="TopBarLanguages">
                   
                    <form id="frmLocale" name="frmLocale" method="post" action="">
                        <select id="locale" name="locale" class="LanguageSelection" style="background-image: url('images/languages/cs.png')" onchange="JavaScript: frmLocale.submit()"><option class="LanguageItem" value="cs" style="background-image: url('images/languages/cs.png')" selected="selected">čeština</option><option class="LanguageItem" value="en" style="background-image: url('images/languages/en.png')">English</option></select>
                    </form>
                   
                </td>

            </tr>
        </tbody>
    </table>
    <table class="LoginPage">
        <colgroup>
            <col/>
            <col width="300px"/>
            <col/>
        </colgroup>
        <tbody>

            <tr>
                <td> </td>
                <td>
                    <form id="frmLogin" name="frmLogin" class="LoginForm" method="post" enctype="multipart/form-data" accept-charset="UTF-8" action="">
                        <table class="LoginFrame">
                            <colgroup>
                                <col span="2" width="50%"/>
                            </colgroup>

                            <tbody>
                                <tr>
                                    <td class="FormFieldLabel">
                                        <label>Uživatelské jméno:</label>
                                    </td>
                                    <td class="FormField">
                                        <input id="UserName" name="UserName" type="text" style="width: 120px" maxlength="20" value=""/>
                                    </td>

                                </tr>
                                <tr>
                                    <td class="FormFieldLabel">
                                        <label>Heslo:</label>
                                    </td>
                                    <td class="FormField">
                                        <input id="Password" name="Password" type="password" style="width: 120px" maxlength="20"/>
                                    </td>

                                </tr>
                                <tr>
                                    <td class="FormField Center" colspan="2">
                                        <button id="btnLogin" name="btnLogin" type="submit">Přihlásit se</button>
                                    </td>
                                </tr>
                            </tbody>
                        </table>

                    </form>
                </td>
                <td> </td>
            </tr>
        </tbody>
    </table>
    <script language="JavaScript" type="text/javascript">document.frmLogin.UserName.focus()</script>
    </body></html>

     
    • Jovic Quinto

      Jovic Quinto - 2006-06-19

      Hello,

      In the line:
      WebForm form = response.getFormWithID("frmLogin");

      Heve you already tried using getFormWithName() instead, such as:
      WebForm form = resp.getFormWithName("frmLogin");

      Regards,
      Jovic Quinto

       
    • xzwsun

      xzwsun - 2007-01-25

      which class include the method assertNull! My progrem could not be compile because it ,and let me create the method!I would appreciate any suggestion or help!

       
    • Jeff

      Jeff - 2007-02-28

      One thing that I ahve noticed when working with httpunit over the last few months is that filling in a form for the login function sometimes seems to behave different to that of just filling and submitting a normal form.

      Try something like the below and let me know how you get on:

      WebConversation wc = new WebConversation();
      WebResponse resp = wc.getResponse("http://localhost:8080/intranet/");

      WebForm formLogin = resp.getForms()[0];
      assertEquals("UserName","",formLogin.getParameterValue("UserName"));
         
      SubmitButton login = formLogin.getSubmitButtons()[0];
      assertEquals("submit","",login.getValue());
         
      //Log in to site
      WebRequest request = formLogin.getRequest(login);
      request.setParameter("UserName",user);
      System.out.println("Entering username: " + formLogin.getParameterValue("UserName"));
         
      request.setParameter("Password",password);
      System.out.println("Entering password: " +formLogin.getParameterValue("Password"));
         
      WebResponse response = wc.getResponse(request);

      Then try something like the below or the getCurrentPage() function to check the page:

      String title = response.getTitle();
      assertEquals("Title",titleString,title);

       

Log in to post a comment.

Monday.com Logo