0

我需要通过 JWebunit 测试类检查我的登录屏幕。
该页面没有任何 sumbit 按钮。我们正在使用 href 标签。
当我们单击 href 时,进程转到goPasswordPage方法脚本。此脚本将调用相应的 servlet LoginServelet.java

工艺细节Home.jsp --> LoginServlet.java --> password.jsp

主页.jsp

<head>
...
...
<title>Home</title>
<script type="text/javascript">
        function goToPasswordPage() {
             var mainForm1 = document.forms["mainForm"];
             mainForm1.submit();
        }
</script>
</head>
<body>
      <form id="mainForm" method="GET" action="LoginServlet">
            <table cellpadding="10" cellspacing="10">
               <tr>
                   <td>UserName:</td>
                   <td><input id="username" name="username" type="text" /></td>
               </tr>
               <tr>
                   <td><a href='javascript:goToPasswordPage()'>Go Password Page</a>
               </tr>
             </table>
      </form></body></html>

登录Servlet.java

public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

public LoginServlet() {
    super();
}

protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {

    String userName = request.getParameter("username");

    /**
     * Here we did some back end validation.
     * Based on the validation, 
     * decided to navigate: go to the password page or same home page
     */     
    request.getRequestDispatcher("/password.jsp").forward(request, response);
}


protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {        
}
}

密码.jsp

...
...
<head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Password</title>
</head><body>
    <%
      String username = request.getParameter("username");
    %>
    <p>Welcome <%=username%>!!!</p>
</body></html>

JWebUnit 测试类

BasicWebAppTest.java

package com.jwebunit.test;

import org.junit.Before;
import org.junit.Test;
import static net.sourceforge.jwebunit.junit.JWebUnit.*;

public class BasicWebAppTest {
@Before
public void setUp() throws Exception {
    setBaseUrl("http://localhost:7070/BasicWebApp");
}

@Test
public void testJSFDemoMethod() {

    beginAt("/home.jsp");

    assertTitleEquals("Home");
    setTextField("username", "Jack123");

    /**
     * Here i have to write the code for 
     * calling the javascript or href tag action
     */

    assertTitleEquals("Password");
}
}

请帮我。提前致谢。

4

1 回答 1

0

如果页面有多个链接使用索引。否则不需要在clickLinkWithExactText方法 中使用索引

@Test
public void testJSFDemoMethod() 
{
  beginAt("/home.jsp");
  assertTitleEquals("Home");
  setTextField("username", "Jack123");

  clickLinkWithExactText("Go Password Page",0);  //This is the answer for my question

  assertTitleEquals("Password");
}
于 2014-11-05T10:42:21.380 回答