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. login.jsp
  7. <html>
  8. <head>
  9. <title>Login</title>
  10. </head>
  11. <body>
  12. <form action="servlet/LoginServlet"method="post">
  13. username:<input type="text"name="username"/><br/>
  14. password:<input type="password"name="pass"/>
  15. <input type="submit"/>
  16. </form>
  17. </body>
  18. </html>
  19. welcome.jsp
  20. <html>
  21. <head>
  22. <title>Welcome</title>
  23. </head>
  24. <body>
  25. welcome! <br>
  26. </body>
  27. </html>
  28. LoginServlet
  29. publicclassLoginServletextendsHttpServlet {
  30. @Override
  31. protectedvoidservice(HttpServletRequest request, HttpServletResponse response)
  32. throwsServletException, IOException {
  33. String username = request.getParameter("username");
  34. String passsword = request.getParameter("pass");
  35. System.out.println(username +" login ,pass is"+ passsword);
  36. String path = request.getContextPath();
  37. response.sendRedirect(path +"/welcome.jsp");
  38. }
  39. }
  40. LoginServletTest
  41. packagecom.partner4java.servlet;
  42. importstaticnet.sourceforge.jwebunit.junit.JWebUnit.*;
  43. importorg.junit.Before;
  44. importorg.junit.Test;
  45. publicclassLoginServletTest {
  46. @Before
  47. publicvoidprepare(){
  48. setBaseUrl("http://localhost:8080/jwebunit");
  49. }
  50. @Test
  51. publicvoidtestLogin(){
  52. beginAt("index.jsp");
  53. clickLink("login");
  54. assertTitleEquals("Login");
  55. setTextField("username","hello");
  56. setTextField("pass","world");
  57. submit();
  58. assertTitleEquals("Welcome");
  59. }
  60. }
  61. Junit3:
  62. importnet.sourceforge.jwebunit.junit.WebTestCase;
  63. publicclassExampleWebTestCaseextendsWebTestCase {
  64. publicvoidsetUp() {
  65. super.setUp();
  66. setBaseUrl("http://localhost:8080/test");
  67. }
  68. publicvoidtest1() {
  69. beginAt("home.xhtml");//Open the browser on http://localhost:8080/test/home.xhtml
  70. clickLink("login");
  71. assertTitleEquals("Login");
  72. setTextField("username","test");
  73. setTextField("password","test123");
  74. submit();
  75. assertTitleEquals("Welcome, test!");
  76. }
  77. }




4选择您要使用的插件
JWebUnit可以使用不同的插件来执行你写的测试。通过一个设置来进行区分:
  1. importnet.sourceforge.jwebunit.util.TestingEngineRegistry;
  2. importorg.junit.Before;
  3. importstaticnet.sourceforge.jwebunit.junit.JWebUnit.*;
  4. publicclassExampleWebTestCase {
  5. @Before
  6. publicvoidprepare() {
  7. setTestingEngineKey(TestingEngineRegistry.TESTING_ENGINE_HTMLUNIT);// use HtmlUnit
  8. setTestingEngineKey(TestingEngineRegistry.TESTING_ENGINE_SELENIUM);// use Selenium
  9. }
  10. }


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


5部署你的测试向导上下文
JWebUnit允许您测试您的Web应用程序的主要方式,是通过应用程序本身的向导。 通过测试向导的管理,可以在每个测试用例里面取得测试环境。
第一步是就是在一个统一资源管理的地方为测试向导指明向导的资源位置。
  1. @Before
  2. publicvoidprepare() {
  3. setBaseUrl("http://myserver:8080/myapp");
  4. }
一般我们的地址就是一个测试服务器的地址。
现在,测试环境里面就存在了一个测试向导,测试向导管理了你的应用资源,那么你就可以查询这里面的一些资源是否存在或者是否正确。
例如向下面这样:设置一个导航起点,并且判断每步的内容是否正确。
  1. @Test
  2. publicvoidtestIndexLogin() {
  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. publicvoidtestFormSubmission() {
  3. beginAt("login.html");
  4. assertTitleEquals("Login");// we should be on the login page
  5. // fill out the form
  6. assertLinkNotPresent("Logout");// we should not be logged in
  7. assertFormPresent("login_form");
  8. assertFormElementPresent("username");
  9. assertFormElementPresent("password");
  10. setTextField("username","test");
  11. setTextField("password","test123");
  12. assertFormElementEquals("username","test");
  13. submit();
  14. // now that we have filled out the form,
  15. // we can assert that we can logout
  16. assertLinkPresent("Logout");// we should now be logged in
  17. }

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


你还可以触发非提交按键 (type='button'):
  1. @Test
  2. publicvoidtestPopupButton() {
  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. publicvoidtestPopupWindow() {
  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. @Test
  13. publicvoidtestFrame() {
  14. beginAt("info.html");
  15. assertFramePresent("contentFrame");
  16. gotoFrame("contentFrame");
  17. ...
  18. }



8验证网页内容
一旦你已经浏览到的页面,你想测试,你可以调用JWebUnit提供的断言,以验证它的正确性。
  1. @Test
  2. publicvoidtestCorrectness() {
  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. publicvoidtestAgeTable() {
  3. beginAt("agePage.html");
  4. // check that table is present
  5. assertTablePresent("ageTable");
  6. // check that a single string is present somewhere in table
  7. assertTextInTable("ageTable","Jim");
  8. // check that a set of strings are present somewhere in table
  9. assertTextInTable("ageTable",
  10. newString[] {"Jim","Wilkes"});
  11. // check composition of table rows/columns
  12. assertTableEquals("ageTable",
  13. newString[][] {{"Name","Age"},
  14. {"Jim","30ish"},
  15. {"Wilkes","20ish"}});
  16. }



如果您需要验证非空白的表格单元格跨度超过单个列,类提供代表预期的表,行和单元格。
Age Table
Name Age
Jim 30ish
Wilkes 20ish
  1. @Test
  2. publicvoidtestAgeTable() {
  3. beginAt("agePage.html");
  4. ExpectedTable ageTable =newTable(newObject[][] {
  5. {newCell("Age Table",2,1)},
  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. publicvoidtestWelcomeMessage() {
  3. beginAt("mainPage.xhtml");
  4. // check for presence of welcome message by text
  5. assertTextPresent("Welcome, Joe User!");
  6. // check for presence of welcome message by element id
  7. assertElementPresent("welcomeMessage");
  8. // check for text within an element
  9. assertTextInElement("welcomeMessage","Joe User");
  10. }



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


还可以通过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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值