Finally, I have an automated test that exercises DynaFaces and jMaki.

If you don't know what those two things are, check out
<http://developers.sun.com/web/swdp/>.

Anyhow, it was a real pain to keep these two things working together as
jMaki rapidly evolved.  Now at least we can have an automated test to
catch regressions.

SECTION: Changes

A dist/mcp-test/src/main/java/jsf_jmaki/JsfjMaki.java
A dist/mcp-test/src/test/java/jsf_jmaki/JsfjMakiTest.java

- The automated test and the netbeans empty main class.

M dom/classes/org/mozilla/dom/util/DOMTreeDumper.java

- Introduce preorder traversal callback facility

M webclient/classes_spec/org/mozilla/mcp/MCP.java

- add methods

+    public List<Element> getAnchors(String id) {

+    public List<Element> getAnchors() {

+    public List<Element> getChildElementsWithTagName(Element root,
+            final String tagName) {

+    public List<Element> getChildElementsWithTagName(String id,
+            final String tagName) {

+    public void clickElement(String id) {

+    public void blockingClickElement(Element element) {


git-svn-id: svn://10.0.0.236/trunk@224826 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
edburns%acm.org 2007-04-21 03:25:37 +00:00
parent c384da1ba2
commit 083c1a8f9b
4 changed files with 503 additions and 31 deletions

View File

@ -0,0 +1,29 @@
/*
* CarDemo.java
*
* Created on March 3, 2007, 2:58 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package jsf_jmaki;
/**
*
* @author edburns
*/
public class JsfjMaki {
/** Creates a new instance of CarDemo */
public JsfjMaki() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
}
}

View File

@ -0,0 +1,276 @@
/*
* $Id: JsfjMakiTest.java,v 1.1 2007-04-21 03:25:36 edburns%acm.org Exp $
*/
/*
*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Sun
* Microsystems, Inc. Portions created by Sun are
* Copyright (C) 1999 Sun Microsystems, Inc. All
* Rights Reserved.
*
* Contributor(s): Ed Burns &lt;edburns@acm.org&gt;
*/
package jsf_jmaki;
import java.util.BitSet;
import java.util.List;
import java.util.Map;
import junit.framework.TestFailure;
import org.mozilla.mcp.AjaxListener;
import org.mozilla.mcp.MCP;
import org.mozilla.webclient.WebclientTestCase;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.Document;
/**
*
* @author edburns
*/
public class JsfjMakiTest extends WebclientTestCase {
private MCP mcp = null;
public JsfjMakiTest(String testName) {
super(testName);
}
private int ajaxTimeOut = 60000;
private int ajaxWaitInterval = 5000;
public void setUp() {
super.setUp();
mcp = new MCP();
try {
mcp.setAppData(getBrowserBinDir());
}
catch (Exception e) {
fail();
}
}
enum TestFeature {
RECEIVED_END_AJAX_EVENT,
HAS_MAP,
HAS_VALID_RESPONSE_TEXT,
HAS_VALID_RESPONSE_XML,
HAS_VALID_READYSTATE,
STOP_WAITING
}
public void testInplace() throws Exception {
mcp.getRealizedVisibleBrowserWindow();
final BitSet bitSet = new BitSet();
AjaxListener listener = new AjaxListener() {
public void endAjax(Map eventMap) {
bitSet.flip(TestFeature.RECEIVED_END_AJAX_EVENT.ordinal());
if (null != eventMap) {
bitSet.flip(TestFeature.HAS_MAP.ordinal());
}
// Make some assertions about the response text
String responseText = (String) eventMap.get("responseText");
if (null != responseText) {
if (-1 != responseText.indexOf("<partial-response>") &&
-1 != responseText.indexOf("</partial-response>")) {
bitSet.flip(TestFeature.HAS_VALID_RESPONSE_TEXT.ordinal());
}
}
Document responseXML = (Document)
eventMap.get("responseXML");
Element rootElement = null, element = null;
Node node = null;
String tagName = null;
try {
rootElement = responseXML.getDocumentElement();
tagName = rootElement.getTagName();
if (tagName.equals("partial-response")) {
element = (Element) rootElement.getFirstChild();
tagName = element.getTagName();
if (tagName.equals("components")) {
element = (Element) rootElement.getLastChild();
tagName = element.getTagName();
if (tagName.equals("state")) {
bitSet.flip(TestFeature.
HAS_VALID_RESPONSE_XML.ordinal());
}
}
}
}
catch (Throwable t) {
}
String readyState = (String) eventMap.get("readyState");
bitSet.set(TestFeature.HAS_VALID_READYSTATE.ordinal(),
null != readyState && readyState.equals("4"));
bitSet.flip(TestFeature.STOP_WAITING.ordinal());
}
};
mcp.addAjaxListener(listener);
// Load the main page of the app
mcp.blockingLoad("http://localhost:8080/jsf-jmaki/index-demo.jsf");
// Choose the inplace test
mcp.blockingClickElement("inplace-test");
scrollToBeginningOfResultSet(mcp, bitSet);
Element firstCustomerName = mcp.findElement("form:table:0:j_id_id118");
assertNotNull(firstCustomerName);
// Click the first customer name cell
mcp.clickElement(firstCustomerName);
Thread.currentThread().sleep(1000);
// Get the inplace editor element
Element inplaceEditor =
mcp.findElement("form:table:0:j_id_id118-inplaceeditor");
assertNotNull(inplaceEditor);
// Get the text field and the button within that element
List<Element> inplaceFields =
mcp.getChildElementsWithTagName(inplaceEditor, "input");
assertNotNull(inplaceFields);
assertTrue(2 == inplaceFields.size());
// create a unique value, set it into the text field
String nodeValue = "" + System.currentTimeMillis();
inplaceFields.get(0).setNodeValue(nodeValue);
// click "ok" to save the value via ajax
String textContent = inplaceFields.get(1).getTextContent();
// clear the bit set so we can make assertions about the ajax
// transaction
bitSet.clear();
mcp.clickElement(inplaceFields.get(1));
makeAjaxAssertions(bitSet);
scrollToNextPageOfResultSet(mcp, bitSet);
scrollToPreviousPageOfResultSet(mcp, bitSet);
// Compare the value of the first customerName with
// our local value
firstCustomerName = mcp.findElement("form:table:0:j_id_id118");
assertNotNull(firstCustomerName);
assertEquals(firstCustomerName.getTextContent(),nodeValue);
Thread.currentThread().sleep(10000);
mcp.deleteBrowserControl();
}
private void makeAjaxAssertions(BitSet bitSet) throws Exception {
// Artifically wait for the ajax transaction to complete, or the timeout to be reached.
int i = 0;
while (!bitSet.get(TestFeature.STOP_WAITING.ordinal()) ||
((i * getAjaxWaitInterval()) > getAjaxTimeOut())) {
i++;
Thread.currentThread().sleep(getAjaxWaitInterval());
}
// Ensure the timeout was not reached
assertFalse(((i * getAjaxWaitInterval()) > getAjaxTimeOut()));
// assert that the ajax transaction succeeded
assertTrue(bitSet.get(TestFeature.RECEIVED_END_AJAX_EVENT.ordinal()));
assertTrue(bitSet.get(TestFeature.HAS_MAP.ordinal()));
assertTrue(bitSet.get(TestFeature.HAS_VALID_RESPONSE_TEXT.ordinal()));
assertTrue(bitSet.get(TestFeature.HAS_VALID_RESPONSE_XML.ordinal()));
assertTrue(bitSet.get(TestFeature.HAS_VALID_READYSTATE.ordinal()));
}
private void scrollToBeginningOfResultSet(MCP mcp, BitSet bitSet) throws Exception {
// Put the scroller in a known state by clicking on the second
// link, then scrolling to the beginning.
List<Element> anchors = null;
Element firstElement, secondElement;
String firstElementLinkText;
anchors = mcp.getAnchors("form:subview2");
assertTrue(!anchors.isEmpty());
secondElement = anchors.get(1);
assertNotNull(secondElement);
bitSet.clear();
mcp.clickElement(secondElement);
makeAjaxAssertions(bitSet);
// Scroll to the first page, if necessary
do {
anchors = mcp.getAnchors("form:subview2");
assertTrue(!anchors.isEmpty());
firstElement = anchors.get(0);
firstElementLinkText = firstElement.getTextContent();
secondElement = null;
// Is the link text of the first link "Previous"?
if (null != firstElementLinkText &&
firstElementLinkText.equals("Previous")) {
// If so, click the "second" link, thus scrolling
// one page closer to the beginning of the list.
secondElement = anchors.get(1);
bitSet.clear();
mcp.clickElement(secondElement);
makeAjaxAssertions(bitSet);
}
} while (null != secondElement);
}
private void scrollToNextPageOfResultSet(MCP mcp, BitSet bitSet) throws Exception {
// Scroll to the next page
List<Element> anchors = mcp.getAnchors("form:subview2");
int numAnchors = anchors.size();
assertTrue(!anchors.isEmpty());
assertTrue(2 < numAnchors);
bitSet.clear();
mcp.clickElement(anchors.get(numAnchors - 1));
makeAjaxAssertions(bitSet);
}
private void scrollToPreviousPageOfResultSet(MCP mcp, BitSet bitSet) throws Exception {
// Scroll to the previous page
List<Element> anchors = mcp.getAnchors("form:subview2");
int numAnchors = anchors.size();
assertTrue(!anchors.isEmpty());
assertTrue(2 < numAnchors);
bitSet.clear();
mcp.clickElement(anchors.get(0));
makeAjaxAssertions(bitSet);
}
public int getAjaxTimeOut() {
return ajaxTimeOut;
}
public void setAjaxTimeOut(int ajaxTimeOut) {
this.ajaxTimeOut = ajaxTimeOut;
}
public int getAjaxWaitInterval() {
return ajaxWaitInterval;
}
public void setAjaxWaitInterval(int ajaxWaitInterval) {
this.ajaxWaitInterval = ajaxWaitInterval;
}
}

View File

@ -24,6 +24,7 @@ import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
@ -233,34 +234,60 @@ public class DOMTreeDumper {
dbg("finished dumping...");
return baos.toString();
}
private Element findElementWithName(Element start, String name) {
Element result = null;
Node child = null;
String elementName = start.getAttribute("name");
if (null != elementName && elementName.equals(name)) {
return start;
public void preorderTreeWalk(Node start,
TreeTraversalCallBack callback, Object closure) {
try {
doPreorderTreeWalk(start, callback, closure);
} catch (AbortTraversalException ex) {
return;
}
else {
NodeList children = start.getChildNodes();
int length = 0;
boolean hasChildren = ((children != null) &&
((length = children.getLength()) > 0));
if (!hasChildren) {
return result;
}
for (int i=0; i < length; i++) {
child = children.item(i);
result = null;
if (child instanceof Element) {
result = findElementWithName((Element) child, name);
}
if (null != result) {
break;
}
}
}
private void doPreorderTreeWalk(Node start,
TreeTraversalCallBack callback, Object closure)
throws AbortTraversalException {
callback.takeActionOnNode(start, closure);
NodeList children = start.getChildNodes();
Node child = null;
int length = 0;
boolean hasChildren = ((children != null) &&
((length = children.getLength()) > 0));
if (!hasChildren) {
return;
}
for (int i=0; i < length; i++) {
child = children.item(i);
doPreorderTreeWalk(child, callback, closure);
}
}
private Element findElementWithName(Element start, final String name) {
TreeTraversalCallBack callback = new TreeTraversalCallBack() {
public void takeActionOnNode(Node node, Object closure)
throws AbortTraversalException {
Element element = null;
if (node instanceof Element) {
element = (Element) node;
String elementName = element.getAttribute("name");
if (null != elementName && elementName.equals(name)) {
this.setResult(node);
throw new AbortTraversalException("Found result");
}
}
}
};
Element result = null;
this.preorderTreeWalk(start, callback, name);
Object obj = callback.getResult();
if (obj instanceof Element) {
result = (Element)obj;
}
return result;
}
@ -280,4 +307,29 @@ public class DOMTreeDumper {
System.out.println(name + ": " + str);
}
}
public static abstract class TreeTraversalCallBack {
public abstract void takeActionOnNode(Node node, Object closure)
throws AbortTraversalException;
private Object result;
public Object getResult() {
return result;
}
public void setResult(Object result) {
this.result = result;
}
}
public static class AbortTraversalException extends RuntimeException {
private Object closure;
public AbortTraversalException(Object closure) {
this.closure = closure;
}
public Object getClosure() {
return closure;
}
}
}

View File

@ -1,5 +1,5 @@
/*
* $Id: MCP.java,v 1.8 2007-03-14 21:02:13 edburns%acm.org Exp $
* $Id: MCP.java,v 1.9 2007-04-21 03:25:37 edburns%acm.org Exp $
*/
/*
@ -33,11 +33,15 @@ import java.awt.event.InputEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseListener;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.mozilla.dom.util.DOMTreeDumper;
import org.mozilla.dom.util.DOMTreeDumper.AbortTraversalException;
import org.mozilla.dom.util.DOMTreeDumper.TreeTraversalCallBack;
import org.mozilla.webclient.BrowserControl;
import org.mozilla.webclient.BrowserControlCanvas;
import org.mozilla.webclient.BrowserControlFactory;
@ -49,6 +53,7 @@ import org.mozilla.webclient.PageInfoListener;
import org.mozilla.webclient.WebclientEvent;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
/**
* <p>The main class for the Mozilla Control Program. Please see <a
@ -110,6 +115,9 @@ public class MCP {
}
private void openLatch() {
if (null == latch) {
return;
}
if (null != latch || 1 != latch.getCount()) {
latch.countDown();
latch = null;
@ -405,6 +413,79 @@ public class MCP {
return result;
}
/**
* <p>Given an argument <code>id</code>, return a List of DOM
* <code>Element</code> instances that are HTML anchors that are
* direct or indirect children of the element with that <code>id</code>.
*/
public List<Element> getAnchors(String id) {
List<Element> result = getChildElementsWithTagName(id, "a");
return result;
}
/**
* <p>Return a List of DOM
* <code>Element</code> instances that are HTML anchors that are
* present in the document.
* If no such elements exist,
* <code>null</code> is returned.</p>
*/
public List<Element> getAnchors() {
Element root = getCurrentPage().getDOM().getDocumentElement();
List<Element> result = getChildElementsWithTagName(root, "a");
return result;
}
/**
* <p>Return a List of DOM <code>Element</code> instances whose
* tagName attribute is equal to the argument <code>tagName</code>.
* If no such elements exist,
* <code>null</code> is returned.</p>
*/
public List<Element> getChildElementsWithTagName(Element root,
final String tagName) {
List<Element> results = new ArrayList<Element>();
if (null != root) {
TreeTraversalCallBack callback = new TreeTraversalCallBack() {
public void takeActionOnNode(Node node, Object closure)
throws AbortTraversalException {
List<Element> list = (List<Element>) closure;
Element element = null;
if (node instanceof Element) {
element = (Element) node;
String nodeTagName = element.getTagName();
if (null != nodeTagName && nodeTagName.equalsIgnoreCase(tagName)){
list.add(element);
}
}
}
};
getDOMTreeDumper().preorderTreeWalk(root, callback, results);
}
return results;
}
/**
* <p>Return a List of DOM <code>Element</code> instances whose
* tagName attribute is equal to the argument <code>tagName</code>.
* If no such elements exist,
* <code>null</code> is returned.</p>
*/
public List<Element> getChildElementsWithTagName(String id,
final String tagName) {
List<Element> results = null;
Element root = findElement(id);
results = getChildElementsWithTagName(root, tagName);
return results;
}
/**
@ -432,9 +513,9 @@ public class MCP {
*/
public void clickElement(String id) {
Element element = findElement(id);
public void clickElement(Element element) {
String clientX = null, clientY = null;
String id = element.getAttribute("id");
if (null != element) {
clientX = element.getAttribute("clientX");
clientY = element.getAttribute("clientY");
@ -444,7 +525,6 @@ public class MCP {
x = Integer.valueOf(clientX).intValue();
y = Integer.valueOf(clientY).intValue();
Robot robot = getRobot();
createLatch();
robot.mouseMove(x, y);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
@ -462,6 +542,23 @@ public class MCP {
}
}
/**
* <p>Find the DOM element within the current page matching the
* argument <code>id</code> using {@link #findElement}. Use
* <code>java.awt.Robot</code> to click the element. Return
* immediately after clicking the element.</p>
*/
public void clickElement(String id) {
Element element = findElement(id);
if (null != element) {
clickElement(element);
}
}
/**
* <p>Find the DOM element within the current page matching the
@ -472,10 +569,11 @@ public class MCP {
*/
public void blockingClickElement(String idOrName) {
public void blockingClickElement(Element element) {
synchronized (this) {
try {
clickElement(idOrName);
createLatch();
clickElement(element);
lockLatch();
}
catch (InterruptedException ie) {
@ -488,6 +586,23 @@ public class MCP {
/**
* <p>Find the DOM element within the current page matching the
* argument <code>id</code> using {@link #findElement}. Use
* <code>java.awt.Robot</code> to click the element. Block until the
* document load triggered by the click has completed.</p>
*/
public void blockingClickElement(String idOrName) {
Element element = findElement(idOrName);
if (null != element) {
blockingClickElement(element);
}
}
/**
* <p>Load the url, blocking until the load has completed.</p>
*/