首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从JWebunit调用javascript方法

如何从JWebunit调用javascript方法
EN

Stack Overflow用户
提问于 2014-11-03 13:45:40
回答 1查看 538关注 0票数 0

我需要通过JWebunit测试类检查我的登录屏幕。

这页没有任何求和按钮。我们用的是href标签。

当我们单击href时,进程将转到goPasswordPage方法脚本。此脚本将调用相应的servlet LoginServelet.java.

过程细节Home.jsp --> LoginServlet.java --> password.jsp

home.jsp

代码语言:javascript
复制
<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

代码语言:javascript
复制
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

代码语言:javascript
复制
...
...
<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

代码语言:javascript
复制
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");
}
}

请帮帮我。提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2014-11-05 10:42:21

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

代码语言:javascript
复制
@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");
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26715653

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档