ImmoSearch test

git-svn-id: svn://10.0.0.236/trunk@228744 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
edburns%acm.org 2007-06-26 07:17:26 +00:00
parent ba5d9225d5
commit 630d50f8f7
3 changed files with 215 additions and 6 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 immosearch;
/**
*
* @author edburns
*/
public class ImmoSearch {
/** Creates a new instance of CarDemo */
public ImmoSearch() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
}
}

View File

@ -0,0 +1,120 @@
/*
* $Id: ImmoSearchTest.java,v 1.1 2007-06-26 07:17:25 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 <edburns@acm.org>
*/
package immosearch;
import java.util.BitSet;
import java.util.Map;
import junit.framework.TestFailure;
import org.mozilla.mcp.AjaxListener;
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;
/**
*
* @author edburns
*/
public class ImmoSearchTest extends WebclientTestCase {
private MCP mcp = null;
public ImmoSearchTest(String testName) {
super(testName);
}
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 testTrue() throws Exception {
assertTrue(true);
}
public void testPlzAutoComplete() throws Exception {
mcp.setBounds(30, 30, 960, 960);
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());
}
}
};
mcp.addAjaxListener(listener);
final Thread mainThread = Thread.currentThread();
TimeoutHandler timeoutHandler = new TimeoutHandler() {
public void timeout() {
super.timeout();
mainThread.interrupt();
fail("Action timed out");
}
};
mcp.setTimeoutHandler(timeoutHandler);
// Load the main page of the app
mcp.blockingLoad("http://immo.search.ch/");
// Wait for the instructions to appear
mcp.waitUntilTextPresent("Bedienung");
// Get the Postleitzahl text field
Element plzInput = mcp.findElement("basefield");
assertNotNull(plzInput);
mcp.setCurrentElementText("80");
boolean keepWaiting = true;
while (keepWaiting) {
Thread.currentThread().sleep(1000);
}
mcp.deleteBrowserControl();
}
}

View File

@ -1,5 +1,5 @@
/*
* $Id: MCP.java,v 1.13 2007-06-19 20:18:12 edburns%acm.org Exp $
* $Id: MCP.java,v 1.14 2007-06-26 07:17:26 edburns%acm.org Exp $
*/
/*
@ -30,6 +30,7 @@ import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseListener;
import java.io.FileNotFoundException;
@ -94,6 +95,8 @@ public class MCP {
private TimeoutHandler timeoutHandler = null;
private long Timeout = 30000L;
private Element currentElement;
private void createLatch() {
if (null != latch) {
IllegalStateException ise = new IllegalStateException("Trying to set latch when latch is already set!");
@ -400,19 +403,54 @@ public class MCP {
*/
public Element findElement(String id) {
Element result = null;
Document dom = getCurrentPage().getDOM();
try {
result = dom.getElementById(id);
currentElement = dom.getElementById(id);
}
catch (Exception e) {
}
if (null == result) {
result = getDOMTreeDumper().findFirstElementWithName(dom, id);
if (null == currentElement) {
currentElement = getDOMTreeDumper().findFirstElementWithName(dom, id);
}
return currentElement;
}
public void setCurrentElementText(String newValue) {
if (null == currentElement) {
throw new IllegalStateException("You must find an element before you can set its text.");
}
String
screenX = currentElement.getAttribute("screenX"),
screenY = currentElement.getAttribute("screenY");
int i,len,x,y;
if (null != screenX && null != screenY) {
try {
requestFocus();
x = Integer.valueOf(screenX).intValue();
y = Integer.valueOf(screenY).intValue();
} catch (NumberFormatException ex) {
LOGGER.throwing(this.getClass().getName(), "setElementText", ex);
throw new IllegalStateException(ex);
}
Robot robot = getRobot();
robot.mouseMove(x, y);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
len = newValue.length();
for (i = 0; i < len; i++) {
}
// PENDING(edburns): make it so each character in newValue
// is translated into a keyCode.
robot.keyPress(KeyEvent.VK_8);
robot.keyRelease(KeyEvent.VK_8);
robot.keyPress(KeyEvent.VK_0);
robot.keyRelease(KeyEvent.VK_0);
}
return result;
}
/**
@ -628,6 +666,28 @@ public class MCP {
requestFocus();
}
public void waitUntilTextPresent(String textToFind) {
boolean found = false;
long
maxWait = getTimeout(),
timeWaited = 0,
waitInterval = getTimeoutWaitInterval();
while (!(found = getCurrentPage().find(textToFind, true, true))) {
try {
Thread.currentThread().sleep(waitInterval);
timeWaited += waitInterval;
if (maxWait < timeWaited) {
getTimeoutHandler().timeout();
}
} catch (InterruptedException ex) {
if (LOGGER.isLoggable(Level.WARNING)) {
LOGGER.log(Level.WARNING, "waitUntilTextPresent", ex);
}
}
}
}
private void requestFocus() {
boolean result = getBrowserControlCanvas().doRequestFocus(false);
if (result) {