2014-11-03 5 views
0

JWebunit 테스트 클래스를 통해 로그인 화면을 확인해야합니다.
페이지에 어떤 움찔한 단추가 없습니다. 우리는 href 태그를 사용하고 있습니다.
href를 클릭하면 goPasswordPage 메소드 스크립트로 이동합니다. 이 스크립트는 해당 서블릿 LoginServelet.java을 호출합니다.JWebunit에서 javascript 메서드를 호출하는 방법

프로세스 세부 Home.jsp --> LoginServlet.java --> password.jsp

home.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> 

LoginServlet.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 {   
} 
} 

password.jsp

,745,
... 
... 
<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"); 
} 
} 

제발 도와주세요. 사전에 감사합니다.

답변

0

페이지에 둘 이상의 링크가있는 인덱스가있는 경우. 그렇지 않으면 색인을 사용할 필요가 없습니다. 클릭 링크와 함께 익명 텍스트 방법

@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"); 
} 
관련 문제