Package org.mozilla.mcp

Mozilla Control Program

See:
          Description

Class Summary
AjaxListener  
MCP The main class for the Mozilla Control Program.
 

Package org.mozilla.mcp Description

Mozilla Control Program

Master Control Program, from TRON

This package is a simple layer on top of the Webclient API to enable automated testing of web applications using software such as JUnit or TestNG. It fulfills a similar role as HtmlUnit or HttpUnit, but unlike those two, MCP has a real browser behind it. MCP also fulfills a similar role as Selenium. However, unlike Selenium, MCP allows you to make assertions about the response content of actual XMLHttpRequest responses, including getting an actual DOM instance of the response XML.

The main class in the package is MCP. An instance of MCP corresponds to one browser window. Methods doing the following obvious tasks are provided:


Here is a code excerpt that illustrates the use of MCP and JUnit to test a JSF application that uses Project Dynamic Faces for Ajax.

  1. public void testCardemo() throws Exception {
  2.     mcp.getRealizedVisibleBrowserWindow();
  3.     final BitSet bitSet = new BitSet();
  4.     AjaxListener listener = new AjaxListener() {
  5.       public void endAjax(Map eventMap) {
  6.           bitSet.flip(TestFeature.RECEIVED_END_AJAX_EVENT.ordinal());
  7.           if (null != eventMap) {
  8.               bitSet.flip(TestFeature.HAS_MAP.ordinal());
  9.           }
  10.           // Make some assertions about the response text
  11.           String responseText = (String) eventMap.get("responseText");
  12.           if (null != responseText) {
  13.               if (-1 != responseText.indexOf("<partial-response>") &&
  14.                   -1 != responseText.indexOf("</partial-response>")) {
  15.                 bitSet.flip(TestFeature.HAS_VALID_RESPONSE_TEXT.ordinal());
  16.               }
  17.           }
  18.           Document responseXML = (Document)
  19.               eventMap.get("responseXML");
  20.           Element rootElement = null, element = null;
  21.           Node node = null;
  22.           String tagName = null;
  23.           try {
  24.               rootElement = responseXML.getDocumentElement();
  25.               tagName = rootElement.getTagName();
  26.               if (tagName.equals("partial-response")) {
  27.                   element = (Element) rootElement.getFirstChild();
  28.                   tagName = element.getTagName();
  29.                   if (tagName.equals("components")) {
  30.                       element = (Element) rootElement.getLastChild();
  31.                       tagName = element.getTagName();
  32.                       if (tagName.equals("state")) {
  33.                           bitSet.flip(TestFeature.
  34.                                       HAS_VALID_RESPONSE_XML.ordinal());
  35.                       }
  36.                   }
  37.               }
  38.           }
  39.           catch (Throwable t) {
  40.               
  41.           }
  42.           
  43.           String readyState = (String) eventMap.get("readyState");
  44.           bitSet.set(TestFeature.HAS_VALID_READYSTATE.ordinal(),
  45.                      null != readyState && readyState.equals("4"));
  46.           bitSet.flip(TestFeature.STOP_WAITING.ordinal());
  47.           
  48.       }
  49.     };
  50.     mcp.addAjaxListener(listener);
  51.    
  52.     // Load the main page of the app
  53.     mcp.blockingLoad("http://javaserver.org/jsf-ajax-cardemo/faces/chooseLocale.jsp");
  54.     // Choose the "German" language button
  55.     mcp.blockingClickElement("Germany");
  56.     // Choose the roadster
  57.     mcp.blockingClickElement("roadsterButton");
  58.     // Sample the Basis-Preis and Ihr Preis before the ajax transaction
  59.     Element pricePanel = mcp.findElement("zone1");
  60.     assertNotNull(pricePanel);
  61.     String pricePanelText = pricePanel.getTextContent();
  62.    
  63.     assertNotNull(pricePanelText);
  64.     assertTrue(pricePanelText.matches("(?s).*Basis-Preis\\s*15700.*"));
  65.     assertTrue(pricePanelText.matches("(?s).*Ihr Preis\\s*15700.*"));
  66.    
  67.     // Choose the "Tempomat" checkbox
  68.     bitSet.clear();
  69.     mcp.clickElement("cruiseControlCheckbox");
  70.    
  71.     while (!bitSet.get(TestFeature.STOP_WAITING.ordinal())) {
  72.         Thread.currentThread().sleep(5000);
  73.     }
  74.    
  75.     // assert that the ajax transaction succeeded
  76.     assertTrue(bitSet.get(TestFeature.RECEIVED_END_AJAX_EVENT.ordinal()));
  77.     assertTrue(bitSet.get(TestFeature.HAS_MAP.ordinal()));
  78.     assertTrue(bitSet.get(TestFeature.HAS_VALID_RESPONSE_TEXT.ordinal()));
  79.     assertTrue(bitSet.get(TestFeature.HAS_VALID_RESPONSE_XML.ordinal()));
  80.     assertTrue(bitSet.get(TestFeature.HAS_VALID_READYSTATE.ordinal()));
  81.     bitSet.clear();
  82.    
  83.     // Sample the Basis-Preis and Ihr-Preis after the ajax transaction
  84.     pricePanel = mcp.findElement("zone1");
  85.     assertNotNull(pricePanel);
  86.     pricePanelText = pricePanel.getTextContent();
  87.    
  88.     assertNotNull(pricePanelText);
  89.     assertTrue(pricePanelText.matches("(?s).*Basis-Preis\\s*15700.*"));
  90.     assertTrue(pricePanelText.matches("(?s).*Ihr Preis\\s*16600.*"));
  91.    
  92.     mcp.deleteBrowserControl();
  93. }



Copyright © 2002-2007 Mozilla.org All Rights Reserved.