This commit is contained in:
edburns%acm.org 2007-06-26 12:39:04 +00:00
Родитель eec2a7932b
Коммит e1f44a679f
3 изменённых файлов: 146 добавлений и 32 удалений

Просмотреть файл

@ -1,5 +1,5 @@
/*
* $Id: ImmoSearchTest.java,v 1.2 2007-06-26 11:29:27 edburns%acm.org Exp $
* $Id: ImmoSearchTest.java,v 1.3 2007-06-26 12:39:01 edburns%acm.org Exp $
*/
/*
@ -25,16 +25,15 @@
*/
package immosearch;
import java.awt.event.KeyEvent;
import java.util.BitSet;
import java.util.Map;
import junit.framework.TestFailure;
import org.mozilla.mcp.AjaxListener;
import org.mozilla.mcp.Condition;
import org.mozilla.mcp.MCP;
import org.mozilla.mcp.TimeoutHandler;
import org.mozilla.mcp.junit.WebclientTestCase;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.Document;
/**
*
@ -77,10 +76,11 @@ public class ImmoSearchTest extends WebclientTestCase {
}
public void testPlzAutoComplete() throws Exception {
int len,i;
mcp.setBounds(30, 30, 960, 960);
mcp.getRealizedVisibleBrowserWindow();
final BitSet bitSet = new BitSet();
AjaxListener listener = new AjaxListener() {
AjaxListener autocompleteListener = new AjaxListener() {
public void endAjax(Map eventMap) {
bitSet.set(TestFeature.RECEIVED_END_AJAX_EVENT.ordinal(),
true);
@ -127,7 +127,7 @@ public class ImmoSearchTest extends WebclientTestCase {
LOGGER.info("Received Ajax ResponseText: " + responseText);
}
};
mcp.addAjaxListener(listener);
mcp.addAjaxListener(autocompleteListener);
final Thread mainThread = Thread.currentThread();
@ -144,7 +144,16 @@ public class ImmoSearchTest extends WebclientTestCase {
mcp.blockingLoad("http://immo.search.ch/");
// Wait for the instructions to appear
mcp.waitUntilTextPresent("Bedienung");
boolean conditionMet =
mcp.waitUntilConditionMet(new Condition() {
public boolean isConditionMet() {
if (!(conditionMet = (null != ImmoSearchTest.this.mcp.findElementById("statusfield")))) {
conditionMet = ImmoSearchTest.this.mcp.findInPage("Bedienung");
}
return conditionMet;
}
});
assertTrue(conditionMet);
// Get the Postleitzahl text field
Element plzInput = mcp.findElement("basefield");
@ -153,20 +162,55 @@ public class ImmoSearchTest extends WebclientTestCase {
// Append "8" into the text field
bitSet.clear();
mcp.appendToCurrentElementText("8");
mcp.appendKeyCodeToCurrentElementText(KeyEvent.VK_8);
makeAutocompleteAjaxAssertions(bitSet);
for (i = 0; i < 10; i++) {
assertNotNull(mcp.findElement("basefield_ce" + i));
}
makeAjaxAssertions(bitSet);
// Append "0" into the text field.
bitSet.clear();
mcp.appendToCurrentElementText("0");
mcp.appendKeyCodeToCurrentElementText(KeyEvent.VK_0);
makeAutocompleteAjaxAssertions(bitSet);
for (i = 0; i < 10; i++) {
assertNotNull(mcp.findElement("basefield_ce" + i));
}
mcp.removeAjaxListener(autocompleteListener);
// Select the first autocomplete suggestion
AjaxListener trefferUpdateListener = new AjaxListener() {
public void endAjax(Map eventMap) {
bitSet.set(TestFeature.RECEIVED_END_AJAX_EVENT.ordinal(),
true);
if (null != eventMap) {
bitSet.set(TestFeature.HAS_MAP.ordinal(), true);
}
String readyState = (String) eventMap.get("readyState");
bitSet.set(TestFeature.HAS_VALID_READYSTATE.ordinal(),
null != readyState && readyState.equals("4"));
String responseText = (String) eventMap.get("responseText");
if (-1 != responseText.indexOf("8050 Z\u00fcrich")) {
bitSet.set(TestFeature.STOP_WAITING.ordinal(), true);
}
LOGGER.info("Received Ajax ResponseText: " + responseText);
}
};
bitSet.clear();
mcp.addAjaxListener(trefferUpdateListener);
mcp.appendKeyCodeToCurrentElementText(KeyEvent.VK_DOWN);
mcp.appendKeyCodeToCurrentElementText(KeyEvent.VK_ENTER);
makeTrefferUpdateAjaxAssertions(bitSet);
makeAjaxAssertions(bitSet);
mcp.deleteBrowserControl();
}
private void makeAjaxAssertions(BitSet bitSet) throws Exception {
private void makeAutocompleteAjaxAssertions(BitSet bitSet) throws Exception {
// Artifically wait for the ajax transaction to complete, or the timeout to be reached.
int i = 0;
while (true) {
@ -185,6 +229,18 @@ public class ImmoSearchTest extends WebclientTestCase {
assertTrue(bitSet.get(TestFeature.HAS_VALID_READYSTATE.ordinal()));
}
private void makeTrefferUpdateAjaxAssertions(BitSet bitSet) throws Exception {
// Artifically wait for the ajax transaction to complete, or the timeout to be reached.
int i = 0;
while (true) {
if (bitSet.get(TestFeature.STOP_WAITING.ordinal())) {
break;
}
i++;
Thread.currentThread().sleep(mcp.getTimeoutWaitInterval());
}
}
}

Просмотреть файл

@ -0,0 +1,54 @@
/*
* ConditionMet.java
*
* Created on June 26, 2007, 2:12 PM
*
*/
/*
* $Id: Condition.java,v 1.1 2007-06-26 12:39:04 edburns%acm.org Exp $
*/
/*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the License). You may not use this file except in
* compliance with the License.
*
* You can obtain a copy of the License at
* https://javaserverfaces.dev.java.net/CDDL.html or
* legal/CDDLv1.0.txt.
* See the License for the specific language governing
* permission and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* Header Notice in each file and include the License file
* at legal/CDDLv1.0.txt.
* If applicable, add the following below the CDDL Header,
* with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* [Name of File] [ver.__] [Date]
*
* Copyright 2005 Sun Microsystems Inc. All Rights Reserved
*/
package org.mozilla.mcp;
/**
*
* @author edburns
*/
public class Condition {
protected boolean conditionMet = false;
public boolean isConditionMet() {
return conditionMet;
}
public void setConditionMet(boolean newValue) {
conditionMet = newValue;
}
}

Просмотреть файл

@ -1,5 +1,5 @@
/*
* $Id: MCP.java,v 1.15 2007-06-26 11:29:27 edburns%acm.org Exp $
* $Id: MCP.java,v 1.16 2007-06-26 12:39:04 edburns%acm.org Exp $
*/
/*
@ -403,6 +403,17 @@ public class MCP {
*/
public Element findElement(String id) {
currentElement = findElementById(id);
if (null == currentElement) {
Document dom = getCurrentPage().getDOM();
currentElement = getDOMTreeDumper().findFirstElementWithName(dom, id);
}
return currentElement;
}
public Element findElementById(String id) {
Document dom = getCurrentPage().getDOM();
try {
currentElement = dom.getElementById(id);
@ -410,10 +421,6 @@ public class MCP {
catch (Exception e) {
}
if (null == currentElement) {
currentElement = getDOMTreeDumper().findFirstElementWithName(dom, id);
}
return currentElement;
}
@ -440,27 +447,17 @@ public class MCP {
}
public void appendToCurrentElementText(String toAppend) {
public void appendKeyCodeToCurrentElementText(int keyCode) {
if (null == currentElement) {
throw new IllegalStateException("You must find an element before you can set its text.");
}
int i,len,x,y;
Robot robot = getRobot();
len = toAppend.length();
for (i = 0; i < len; i++) {
}
// PENDING(edburns): make it so each character in toAppend
// is translated into a keyCode.
if (toAppend.equals("8")) {
robot.keyPress(KeyEvent.VK_8);
robot.keyRelease(KeyEvent.VK_8);
}
if (toAppend.equals("0")) {
robot.keyPress(KeyEvent.VK_0);
robot.keyRelease(KeyEvent.VK_0);
}
robot.keyPress(keyCode);
robot.keyRelease(keyCode);
}
/**
@ -676,14 +673,20 @@ public class MCP {
requestFocus();
}
public void waitUntilTextPresent(String textToFind) {
public boolean waitUntilConditionMet(Condition condition) {
boolean found = false;
long
maxWait = getTimeout(),
timeWaited = 0,
waitInterval = getTimeoutWaitInterval();
try {
Thread.currentThread().sleep(3000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
while (!(found = getCurrentPage().find(textToFind, true, true))) {
while (!(found = condition.isConditionMet())) {
try {
Thread.currentThread().sleep(waitInterval);
timeWaited += waitInterval;
@ -696,6 +699,7 @@ public class MCP {
}
}
}
return found;
}
private void requestFocus() {