JWebUnit使用:jWebUnit是基于Java的Web应用程序的测试框架 .

1什么是JWebUnit 
jWebUnit是基于Java的Web应用程序的测试框架。 它包装现有的测试框架如 HtmlUnit 和 Selenium,用一个统一的,简单的测试界面,让您可以快速测试您的Web应用程序的正确性。 


2JWebUnit的作用 
JWebUnit提供一个高层次的Java API,用于向导的Web应用程序结合的断言,以验证应用程序的正确性。 这包括通过链接,表单输入和提交,验证表的内容,和其他典型的商业Web应用程序的功能向导。 
简单的向导方法和准备使用的断言允许超过只使用快速测试的创建JUnit或HtmlUnit 。 



3使用JWebUnit HelloWorld 
http://jwebunit.sourceforge.net/apidocs/index.html  

测试版本:jwebunit-3.0-release 
导入jar:jwebunit-3.0-release\lib 
(servlet-api-2.5.jar一般你如果建立web工程就会已经有了) 

HelloWorld: 
  1. jsp:  
  2. index.jsp  
  3.   <body>  
  4.     <a href="login.jsp" id="login">login</a>  
  5.   </body>  
  6.     
  7. login.jsp  
  8. <html>  
  9.   <head>  
  10.     <title>Login</title>  
  11.   </head>  
  12.     
  13.   <body>  
  14.     <form action="servlet/LoginServlet" method="post">  
  15.         username:<input type="text" name="username"/><br/>  
  16.         password:<input type="password" name="pass"/>  
  17.         <input type="submit"/>  
  18.     </form>  
  19.       
  20.   </body>  
  21. </html>    
  22.   
  23. welcome.jsp  
  24. <html>  
  25.   <head>  
  26.     <title>Welcome</title>  
  27.   </head>  
  28.     
  29.   <body>  
  30.     welcome! <br>  
  31.   </body>  
  32. </html>  
  33.   
  34. LoginServlet  
  35. public class LoginServlet extends HttpServlet {  
  36.   
  37.     @Override  
  38.     protected void service(HttpServletRequest request, HttpServletResponse response)  
  39.             throws ServletException, IOException {  
  40.         String username = request.getParameter("username");  
  41.         String passsword = request.getParameter("pass");  
  42.         System.out.println(username + " login ,pass is" + passsword);  
  43.         String path = request.getContextPath();  
  44.         response.sendRedirect(path + "/welcome.jsp");  
  45.     }  
  46.       
  47. }  
  48.   
  49. LoginServletTest  
  50. package com.partner4java.servlet;  
  51.   
  52. import static net.sourceforge.jwebunit.junit.JWebUnit.*;  
  53.   
  54. import org.junit.Before;  
  55. import org.junit.Test;  
  56.   
  57. public class LoginServletTest {  
  58.       
  59.     @Before  
  60.     public void prepare(){  
  61.         setBaseUrl("http://localhost:8080/jwebunit");  
  62.     }  
  63.       
  64.     @Test  
  65.     public void testLogin(){  
  66.         beginAt("index.jsp");  
  67.         clickLink("login");  
  68.         assertTitleEquals("Login");  
  69.         setTextField("username""hello");  
  70.         setTextField("pass""world");  
  71.         submit();  
  72.         assertTitleEquals("Welcome");  
  73.     }  
  74. }  
  75.   
  76.   
  77. Junit3:  
  78. import net.sourceforge.jwebunit.junit.WebTestCase;  
  79.   
  80. public class ExampleWebTestCase extends WebTestCase {  
  81.       
  82.     public void setUp() {  
  83.         super.setUp();  
  84.         setBaseUrl("http://localhost:8080/test");  
  85.     }  
  86.   
  87.     public void test1() {  
  88.         beginAt("home.xhtml"); //Open the browser on http://localhost:8080/test/home.xhtml  
  89.         clickLink("login");  
  90.         assertTitleEquals("Login");  
  91.         setTextField("username""test");  
  92.         setTextField("password""test123");  
  93.         submit();  
  94.         assertTitleEquals("Welcome, test!");  
  95.     }  
  96. }  
  97.   
  98.       




4选择您要使用的插件 
JWebUnit可以使用不同的插件来执行你写的测试。通过一个设置来进行区分: 
  1. import net.sourceforge.jwebunit.util.TestingEngineRegistry;  
  2. import org.junit.Before;  
  3.   
  4. import static net.sourceforge.jwebunit.junit.JWebUnit.*;  
  5.   
  6. public class ExampleWebTestCase {  
  7.   
  8.     @Before  
  9.     public void prepare() {  
  10.         setTestingEngineKey(TestingEngineRegistry.TESTING_ENGINE_HTMLUNIT);    // use HtmlUnit  
  11.         setTestingEngineKey(TestingEngineRegistry.TESTING_ENGINE_SELENIUM);    // use Selenium  
  12.     }  
  13. }  


如果在你的环境变量里面就一种插件,上面的设置是可以省略的,JWebUnit能够自动寻找到这个唯一的插件。 


5部署你的测试向导上下文 
JWebUnit允许您测试您的Web应用程序的主要方式,是通过应用程序本身的向导。 通过测试向导的管理,可以在每个测试用例里面取得测试环境。 
第一步是就是在一个统一资源管理的地方为测试向导指明向导的资源位置。 
  1. @Before  
  2.    public void prepare() {  
  3.        setBaseUrl("http://myserver:8080/myapp");  
  4.    }  
一般我们的地址就是一个测试服务器的地址。 
现在,测试环境里面就存在了一个测试向导,测试向导管理了你的应用资源,那么你就可以查询这里面的一些资源是否存在或者是否正确。 
例如向下面这样:设置一个导航起点,并且判断每步的内容是否正确。 
  1. @Test  
  2. public void testIndexLogin() {  
  3.     beginAt("index.html");        // start at index.html  
  4.     assertTitleEquals("Home");     // the home page should be titled "Home"  
  5.     assertLinkPresent("Login");    // there should be a "Login" link  
  6.     clickLink("Login");            // click the link  
  7.     assertTitleEquals("Login");    // we should now be on the login page  
  8. }  

assertLinkPresent()搜索一个字符串ID的链接; assertLinkPresentWithText()链接包含一个文本字符串搜索。 
具体查阅:http://jwebunit.sourceforge.net/apidocs/index.html 



6使用表单 
现在,我们可以访问登录页面,我们可以使用JWebUnit填写表格,并断言,它的结果如预期。 
  1. @Test  
  2. public void testFormSubmission() {  
  3.     beginAt("login.html");  
  4.     assertTitleEquals("Login");    // we should be on the login page  
  5.       
  6.     // fill out the form  
  7.     assertLinkNotPresent("Logout");     // we should not be logged in  
  8.     assertFormPresent("login_form");  
  9.     assertFormElementPresent("username");  
  10.     assertFormElementPresent("password");  
  11.     setTextField("username""test");  
  12.     setTextField("password""test123");  
  13.     assertFormElementEquals("username""test");  
  14.     submit();  
  15.       
  16.     // now that we have filled out the form,  
  17.     // we can assert that we can logout  
  18.     assertLinkPresent("Logout");        // we should now be logged in  
  19. }  

JWebUnit通过 HtmlUnit/Selenium,自动保持跟踪cookies和Web应用程序指定的会话变量,允许您遍历访问您的网站,因为你就像一个普通用户 
如果一个网页里面有多个form,JWebUnit还可以通过form的id或者name进行区分: 
  1. @Test  
  2. public void testBottomFormSubmission() {  
  3.     beginAt("twoForm.html");  
  4.     setWorkingForm("bottomForm");  
  5.     submit();  
  6. }  


你还可以触发非提交按键 (type='button'): 
  1. @Test  
  2. public void testPopupButton() {  
  3.     beginAt("info.html");  
  4.     assertButtonPresent("popupButtonId"); // clickButton() will also check this  
  5.     clickButton("popupButtonId");  
  6.     assertWindowPresent("popupWindow");  
  7. }  




7Working With Frames and Windows 
You can assert the presence of and navigate to windows by name. For instance, if clicking on a button on the root page should open a window, you could test for this and go to the popup window as follows: 
  1.     @Test  
  2.     public void testPopupWindow() {  
  3.         beginAt("rootPage.html");  
  4.         clickLink("popupLink");  
  5.         assertWindowPresent("popupWindow):  // optional - gotoWindow will  
  6.                                             // also perform this assertion.  
  7.         gotoWindow("popupWindow");  
  8.         ...  
  9.         gotoRootWindow();  //Use this method to return to root window.  
  10.     }  
  11. You can work with frames in a similar manner:  
  12.   
  13.     @Test  
  14.     public void testFrame() {  
  15.         beginAt("info.html");  
  16.         assertFramePresent("contentFrame");  
  17.         gotoFrame("contentFrame");  
  18.         ...  
  19.     }  



8验证网页内容 
一旦你已经浏览到的页面,你想测试,你可以调用JWebUnit提供的断言,以验证它的正确性。 
  1. @Test  
  2. public void testCorrectness() {  
  3.      beginAt("mainPage.xhtml");  
  4.      assertTitleEquals("Main Page");  
  5.      assertLinkPresentWithText("Add Widget");  
  6.      clickLinkWithText("Add Widget");  
  7.      setTextField("widgetName""My Widget");  
  8.      submit();  
  9.      assertTextPresent("Widget successfully added."):  
  10. }  



9验证表单的内容 
JWebUnit还提供了验证界面中表单内容的断言。 
你可以验证简单的内容或者布局。HttpUnit可以清楚你表达的空格,便于你的测试。 
The below test validates against this html table (the table id attribute is "ageTable"): 

Name Age 
Jim 30ish 
Wilkes 20ish 
  1. @Test  
  2. public void testAgeTable() {  
  3.     beginAt("agePage.html");  
  4.     // check that table is present  
  5.     assertTablePresent("ageTable");  
  6.       
  7.     // check that a single string is present somewhere in table  
  8.     assertTextInTable("ageTable""Jim");  
  9.       
  10.     // check that a set of strings are present somewhere in table  
  11.     assertTextInTable("ageTable",  
  12.                       new String[] {"Jim""Wilkes"});  
  13.                         
  14.     // check composition of table rows/columns  
  15.     assertTableEquals("ageTable",  
  16.                       new String[][] {{"Name""Age"},  
  17.                                       {"Jim""30ish"},  
  18.                                       {"Wilkes""20ish"}});  
  19. }  



如果您需要验证非空白的表格单元格跨度超过单个列,类提供代表预期的表,行和单元格。 
Age Table 
Name Age 
Jim 30ish 
Wilkes 20ish 
  1. @Test  
  2.   public void testAgeTable() {  
  3.       beginAt("agePage.html");  
  4.       ExpectedTable ageTable = new Table(new Object[][] {  
  5.           {new Cell("Age Table"21)},  
  6.           {"Name""Age"},  
  7.           {"Jim""30ish"},  
  8.           {"Wilkes""20ish"}  
  9.       });  
  10.       assertTableEquals("ageTable", expectedAgeTable);  
  11.   }      




10使用元素ID来验证内容 

JWebUnit允许您检查任何HTML元素的存在,其ID或元素的文本。 这可以是一个有用的技巧,检查存在的一些页面的逻辑部分。 即使在自由浮动的文本的情况下,一个span元素可以用来环绕文本,并给它一个逻辑ID: 
<span id="welcomeMessage">Welcome, Joe User!</span> 
  1. @Test  
  2. public void testWelcomeMessage() {  
  3.     beginAt("mainPage.xhtml");  
  4.       
  5.     // check for presence of welcome message by text  
  6.     assertTextPresent("Welcome, Joe User!");  
  7.       
  8.     // check for presence of welcome message by element id  
  9.     assertElementPresent("welcomeMessage");  
  10.       
  11.     // check for text within an element  
  12.     assertTextInElement("welcomeMessage""Joe User");  
  13. }  



11使用属性文件来验证内容 
JWebUnit提供了一种非硬编码的方式来校验内容,通过来配置属性文件,通过获取property的key来获取值: 
  1. @Before  
  2. public void prepare() {  
  3.     setbaseUrl("http://myserver:8080/myapp");  
  4.     getTestContext().setResourceBundleName("ApplicationResources");  
  5. }  
  6.   
  7. @Test  
  8. public void testMainPage() {  
  9.     beginAt("mainPage.html");  
  10.     assertTitleEqualsKey("title.mainPage");  
  11.     assertKeyPresent("message.welcome");  
  12.     assertKeyInTable("mainPageTable""header.mainPageTable");  
  13. }  


还可以通过WebTester, WebTestCase 和 JWebUnit的getMessage方法获取资源内容绑定到你的测试化境中。 

Whether to check for a given logical chunk of text by hard-coded string, property file lookup, or by use of an element id is up to you. 



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值