jwebunit assertSubmitButtonPresent not finding button

Discussions

Web tier: servlets, JSP, Web frameworks: jwebunit assertSubmitButtonPresent not finding button

  1. any jwebunit users out there? i'm trying to test my very simple login page using the jwebunit WebTestCase class. here's my setup:

    i have a very simple login form page with the usual two input fields: email and password

    i also have two submit buttons: login and cancel

    Here's the jsp snippet for my form:

    <html:form action="/login">
      Username: <html:text property="username"><bean:write name="user" property="username"/></html:text> <br/>
      Password: <html:password property="password"><bean:write name="user" property="password"/></html:password><br/>
      
      <html:submit property="direction">
        <bean:message key="button.login"/>
      </html:submit>
      <html:submit property="direction">
        <bean:message key="button.cancel"/>
      </html:submit>
    </html:form>

    Note: I'm using the LookupDispatchAction for my Action class, hence the property="direction" in the submit buttons

    In my jwebunit test case I have a class:

    public class LoginJspTest extends WebTestCase {
    ...
        public void setUp() {
            getTestContext().setBaseUrl(baseUrl);
        }
    ...

        public void testLogin() {
            beginAt("/login.do");

            assertFormElementPresent("email"); //ok
            assertFormElementPresent("password"); // ok
            populateLoginForm(email,password); // ok
            
            assertSubmitButtonPresent("Login"); // cannot find!
            assertSubmitButtonPresent("Cancel");
            submit("Login");
            String title = "Logged in Successfully";
            log.debug("testLogin looking for title: " + title);
            assertTitleEquals(title);
        }
    ...

    Any ideas why I keep getting the error:
    AssertionFailedError: Submit Button[Login] not found.

    tia,
    Patrick

    Threaded Messages (4)

  2. Here is my suggestion[ Go to top ]

    First,Submit button's part in your jsp pages should be modified as below:
      <html:submit>
        <bean:message key="button.login"/>
      </html:submit>
      <html:submit>
        <bean:message key="button.cancel"/>
      </html:submit>
      That is to say you should remove the configure about attribute "property",because in struts property="direction" is parsed into <input name="direction"> by SubmitTag.You can
    see this point from the source of SubmitTag.

    Then, you can add test code for the submit button as below:
      this.assertKeyPresent("button.login");
    or
      this.assertSubmitButtonPresent("Login");

    That's all.

    Jones lee: wzlee@126.com
  3. Sorry, something is wrong.[ Go to top ]

    Sorry, I mistake some usage of assertKeyPresent,it is used to check the present of message.

    So, the right answer is you must specific the property attribut for every <html:submit>,then you should
    use the attribute's value to test the present of
    submit button.See below for details, plz.

    First,Submit button's part in your jsp pages should be modified as below:
      <html:submit property="Login">
        <bean:message key="button.login"/>
      </html:submit>
      <html:submit property="Cancel">
        <bean:message key="button.cancel"/>
      </html:submit>
      You must specific property attribute for every submit button, because in struts property="Login" is parsed into <input name="Login"> by SubmitTag.You can
    see this point from the source of SubmitTag.

    Then, you can add test code for the submit button as below:
      this.assertSubmitButtonPresent("Login");

    I think assertSubmitButtonPresent will check the "name" attribute of <input type="submit"> which is in the compiled pages.

    That's all.

    Jones lee: wzlee@126.com
  4. Fix coming in jWebUnit 1.3[ Go to top ]

    Quite late for this poster, but hopefully others won't worry too much. If you're using LookupDispatchAction in Struts & wanting to use jWebUnit to test it, you'll run into this problem.

    The solution appears to be integrated in the next release of jWebUnit:

    http://sourceforge.net/tracker/index.php?func=detail&aid=800660&group_id=61302&atid=497984

    So, you can either wait for that to be released, or grab the source from CVS and build it yourself.

    HTH,

    Peter
  5. ... or do it yourself[ Go to top ]

    Just a follow-up: here's a method I'm using as a work-around to not being able to provide both the name & value of the button to submit. Typical usage would be:

    submit("method", getMessage("some.button.key"));


    HTH,

    Peter




      /**
       * This method is a work-around for a limitation in jWebUnit 1.2. It allows
       * you to specify the button to submit with by specifying the button name
       * as well as the button value (the label). jWebUnit 1.3 will provide this
       * ability out of the box.
       *
       * @param buttonName The name of the button (typically 'method' in our
       * pages)
       * @param buttonValue The value of the button
       */
      protected void submit(String buttonName, String buttonValue) {

        WebTester tester = getTester();
        HttpUnitDialog dialog = tester.getDialog();

        WebForm form = dialog.getForm();
        Button[] buttons = form.getButtons();

        for (int i = 0; i < buttons.length; i++) {
          Button current = buttons[i];

          if (current.getName().equals(buttonName) &&
              current.getValue().equals(buttonValue)) {
            try {
              current.click();
            } catch (Exception ex) {
              throw new IllegalArgumentException(
                      "Failed to click the button " + buttonName +
                      " with value " + buttonValue);
            }

            // Successful return
            return;
          }
        }

        // This should never get reached, unless on test
        // developer error!
        throw new IllegalStateException("jWebUnit missed a button!");
      }