[Jsimplebrowser-svn] SF.net SVN: jsimplebrowser: [45] trunk
Status: Alpha
Brought to you by:
rdimarco
From: <rdi...@us...> - 2007-08-11 20:44:16
|
Revision: 45 http://jsimplebrowser.svn.sourceforge.net/jsimplebrowser/?rev=45&view=rev Author: rdimarco Date: 2007-08-11 13:44:05 -0700 (Sat, 11 Aug 2007) Log Message: ----------- Got a script generated Selenium IDE to actually work! Modified Paths: -------------- trunk/client/src/main/java/com/software416/jsimplebrowser/client/SimpleClient.java trunk/core/src/main/java/com/software416/jsimplebrowser/impl/BrowserImpl.java trunk/core/src/main/java/com/software416/jsimplebrowser/impl/WindowImpl.java trunk/core/src/main/java/com/software416/jsimplebrowser/impl/WindowProvider.java trunk/selenium-client/src/main/java/com/software416/jsimplebrowser/seleniumclient/SeleniumClient.java trunk/selenium-client/src/test/java/com/software416/jsimplebrowser/seleniumclient/SeleniumClientTest.java Added Paths: ----------- trunk/selenium-client/src/main/java/com/software416/jsimplebrowser/seleniumclient/SeleniumClientException.java trunk/selenium-client/src/main/java/com/software416/jsimplebrowser/seleniumclient/SeleniumCommand.java trunk/selenium-client/src/main/java/com/software416/jsimplebrowser/seleniumclient/SeleniumHtmlRunner.java trunk/selenium-client/src/main/java/com/software416/jsimplebrowser/seleniumclient/VerificationFailureEvent.java trunk/selenium-client/src/main/java/com/software416/jsimplebrowser/seleniumclient/VerificationFailureListener.java trunk/selenium-client/src/test/java/com/software416/jsimplebrowser/seleniumclient/SeleniumHtmlRunnerTest.java trunk/selenium-client/src/test/resources/testSeleniumHtmlScript.html Modified: trunk/client/src/main/java/com/software416/jsimplebrowser/client/SimpleClient.java =================================================================== --- trunk/client/src/main/java/com/software416/jsimplebrowser/client/SimpleClient.java 2007-08-11 17:36:52 UTC (rev 44) +++ trunk/client/src/main/java/com/software416/jsimplebrowser/client/SimpleClient.java 2007-08-11 20:44:05 UTC (rev 45) @@ -9,10 +9,13 @@ import javax.xml.xpath.XPathFactory; import org.apache.commons.collections.MultiHashMap; +import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.w3c.dom.Attr; import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; import org.w3c.dom.NodeList; import com.google.inject.Guice; @@ -20,6 +23,7 @@ import com.software416.jsimplebrowser.Browser; import com.software416.jsimplebrowser.BrowserException; import com.software416.jsimplebrowser.Window; +import com.software416.jsimplebrowser.util.ElementLocator; public class SimpleClient { @SuppressWarnings("unused") @@ -93,7 +97,46 @@ getCurrentWindow().submitForm(id, new MultiHashMap()); } -// public String[] getAllWindowTitles() { + public void type(String id, String value) { + Element e = new ElementLocator(getDocument()).findElement(id); + if (e != null) { + e.setAttribute("value", value); + } + } + + public void click(String id) throws BrowserException { + Element e = new ElementLocator(getDocument()).findElement(id); + if (e != null) { + if (e.getTagName().equalsIgnoreCase("a")) { + // Logic should move to JavaScriptImpl layer to handle other events... + String href = e.getAttribute("href"); + String target = e.getAttribute("target"); + if (!StringUtils.isEmpty(href)) { + if (StringUtils.isEmpty(target)) { + open(href); + } else { + openWindow(target, href); + } + } + } else if (e.getTagName().equalsIgnoreCase("input") && "submit".equalsIgnoreCase(e.getAttribute("type"))) { + Node n = e.getParentNode(); + while (n != null) { + if (n.getNodeType() == Node.ELEMENT_NODE) { + Element parent = (Element)n; + if ("form".equalsIgnoreCase(parent.getTagName())) { + String formId = parent.getAttribute("id"); + String formName = parent.getAttribute("name"); + submitForm(StringUtils.isEmpty(formId) ? formName : formId); + break; + } + } + n = n.getParentNode(); + } + } + } + } + + // public String[] getAllWindowTitles() { // throw new UnsupportedOperationException(); // } // @@ -243,11 +286,6 @@ // // } // -// public void type(String arg0, String arg1) { -// throw new UnsupportedOperationException(); -// -// } -// // public void typeKeys(String arg0, String arg1) { // throw new UnsupportedOperationException(); // @@ -276,11 +314,8 @@ // public void check(String id) { // throw new UnsupportedOperationException(); // } -// public void click(String id) { -// throw new UnsupportedOperationException(); -// } // public void clickAt(String arg0, String arg1) { // throw new UnsupportedOperationException(); // } - +// } Modified: trunk/core/src/main/java/com/software416/jsimplebrowser/impl/BrowserImpl.java =================================================================== --- trunk/core/src/main/java/com/software416/jsimplebrowser/impl/BrowserImpl.java 2007-08-11 17:36:52 UTC (rev 44) +++ trunk/core/src/main/java/com/software416/jsimplebrowser/impl/BrowserImpl.java 2007-08-11 20:44:05 UTC (rev 45) @@ -63,10 +63,9 @@ public synchronized Window getWindow(String windowName) { if (!_browserWindows.containsKey(windowName)) { - ThreadLocal<Browser> tl = new ThreadLocal<Browser>(); - tl.set(this); + WindowProvider.CURRENT_BROWSER.set(this); _browserWindows.put(windowName, _windowProvider.get()); - tl.remove(); + WindowProvider.CURRENT_BROWSER.remove(); } return _browserWindows.get(windowName); } Modified: trunk/core/src/main/java/com/software416/jsimplebrowser/impl/WindowImpl.java =================================================================== --- trunk/core/src/main/java/com/software416/jsimplebrowser/impl/WindowImpl.java 2007-08-11 17:36:52 UTC (rev 44) +++ trunk/core/src/main/java/com/software416/jsimplebrowser/impl/WindowImpl.java 2007-08-11 20:44:05 UTC (rev 45) @@ -74,7 +74,7 @@ try { HttpMethod m = buildMethodForForm(formName, userSpecifiedParameterMap); if (m == null) { - throw new BrowserException("Could not submit form as it does not exist!!"); + throw new BrowserException("Could not submit form '" + formName + "' as it does not exist!!"); } makeRequest(m); } catch (IOException ex) { @@ -101,7 +101,7 @@ @SuppressWarnings("unchecked") protected HttpMethod buildMethodForForm(String formName, MultiMap userParamMap) throws URIException { - Element formElement = findFormElementByFormName(formName); + Element formElement = findFormElementByFormNameOrId(formName); if (formElement == null) { return null; } @@ -205,12 +205,13 @@ } } - protected Element findFormElementByFormName(String name) { + protected Element findFormElementByFormNameOrId(String name) { NodeList formElements=getDocument().getElementsByTagName("form"); for (int i = 0; i < formElements.getLength(); i++) { Element formElement=(Element)formElements.item(i); String formName=formElement.getAttribute("name"); - if (name.equals(formName)) { + String formId =formElement.getAttribute("id"); + if (name.equals(formName) || name.equals(formId)) { return formElement; } } Modified: trunk/core/src/main/java/com/software416/jsimplebrowser/impl/WindowProvider.java =================================================================== --- trunk/core/src/main/java/com/software416/jsimplebrowser/impl/WindowProvider.java 2007-08-11 17:36:52 UTC (rev 44) +++ trunk/core/src/main/java/com/software416/jsimplebrowser/impl/WindowProvider.java 2007-08-11 20:44:05 UTC (rev 45) @@ -5,9 +5,13 @@ import com.software416.jsimplebrowser.Window; public class WindowProvider implements Provider<Window> { - + protected static final ThreadLocal<Browser> CURRENT_BROWSER = new ThreadLocal<Browser>(); public Window get() { - return new WindowImpl(new ThreadLocal<Browser>().get()); + Browser b = CURRENT_BROWSER.get(); + if (b == null) { + throw new IllegalStateException("Browser cannot be null"); + } + return new WindowImpl(b); } } Modified: trunk/selenium-client/src/main/java/com/software416/jsimplebrowser/seleniumclient/SeleniumClient.java =================================================================== --- trunk/selenium-client/src/main/java/com/software416/jsimplebrowser/seleniumclient/SeleniumClient.java 2007-08-11 17:36:52 UTC (rev 44) +++ trunk/selenium-client/src/main/java/com/software416/jsimplebrowser/seleniumclient/SeleniumClient.java 2007-08-11 20:44:05 UTC (rev 45) @@ -1,5 +1,7 @@ package com.software416.jsimplebrowser.seleniumclient; +import java.util.ArrayList; +import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -17,7 +19,16 @@ public class SeleniumClient implements Selenium { private static final Log LOG = LogFactory.getLog(SeleniumClient.class); private SimpleClient _simpleClient = new SimpleClient(); + private String _baseUrl; + public String getBaseUrl() { + return _baseUrl; + } + + public void setBaseUrl(String baseUrl) { + _baseUrl = baseUrl; + } + public String getHtmlSource() { return _simpleClient.getHtmlSource(); } @@ -186,14 +197,23 @@ throw new UnsupportedOperationException(); } public void click(String id) { - throw new UnsupportedOperationException(); + try { + _simpleClient.click(id); + } catch (BrowserException ex) { + LOG.warn("Problem clicking id: " + id, ex); + } } public void clickAt(String arg0, String arg1) { throw new UnsupportedOperationException(); } + public void clickAndWait(String id, String time) { + click(id); + waitForPageToLoad(time); + } public String getText(String id) { - return _simpleClient.getDocument().getElementById(id).getTextContent(); + Element e = _simpleClient.getDocument().getElementById(id); + return e != null ? e.getTextContent() : null; } public String getTitle() { @@ -252,12 +272,20 @@ public void open(String url) { try { + url = buildUrl(url); _simpleClient.open(url); } catch (BrowserException ex) { LOG.warn("Problem loading page", ex); } } + protected String buildUrl(String url) { + if (!url.matches("^https?\\://.*")) { + url = _baseUrl + (!_baseUrl.endsWith("/") && !url.startsWith("/") ? "/" : "") + url; + } + return url; + } + public void openWindow(String windowName, String url) { try { _simpleClient.openWindow(windowName, url); @@ -320,9 +348,8 @@ } } - public void type(String arg0, String arg1) { - throw new UnsupportedOperationException(); - + public void type(String id, String value) { + _simpleClient.type(id, value); } public void typeKeys(String arg0, String arg1) { @@ -344,11 +371,11 @@ } public void waitForFrameToLoad(String arg0, String arg1) { - // noop + // noop right now until we make requests asynchronous... } public void waitForPageToLoad(String arg0) { - // noop + // noop right now until we make requests asynchronous... } // @@ -606,6 +633,30 @@ public void setSimpleClient(SimpleClient simpleClient) { _simpleClient = simpleClient; } + + private List<VerificationFailureListener> _verificationFailureListeners = new ArrayList<VerificationFailureListener>(); - + public void addVerificationFailureListener(VerificationFailureListener listener) { + _verificationFailureListeners.add(listener); + } + + public void removeVerificationFailureListener(VerificationFailureListener listener) { + _verificationFailureListeners.remove(listener); + } + + protected void createVerificationFailureEvent(String command, String... args) { + SeleniumCommand sc = new SeleniumCommand(); + sc.setCommand(command); + sc.setArguments(args); + VerificationFailureEvent e = new VerificationFailureEvent(sc, this); + for (VerificationFailureListener listener : _verificationFailureListeners) { + listener.handleVerificationFailureEvent(e); + } + } + + public void verifyTextPresent(String text) { + if (!getHtmlSource().contains(text)) { + createVerificationFailureEvent("verifyTextPresent", text); + } + } } Added: trunk/selenium-client/src/main/java/com/software416/jsimplebrowser/seleniumclient/SeleniumClientException.java =================================================================== --- trunk/selenium-client/src/main/java/com/software416/jsimplebrowser/seleniumclient/SeleniumClientException.java (rev 0) +++ trunk/selenium-client/src/main/java/com/software416/jsimplebrowser/seleniumclient/SeleniumClientException.java 2007-08-11 20:44:05 UTC (rev 45) @@ -0,0 +1,17 @@ +package com.software416.jsimplebrowser.seleniumclient; + +public class SeleniumClientException extends Exception { + private static final long serialVersionUID = 20070811L; + public SeleniumClientException() { + super(); + } + public SeleniumClientException(String msg) { + super(msg); + } + public SeleniumClientException(Throwable t) { + super(t); + } + public SeleniumClientException(String msg, Throwable t) { + super(msg, t); + } +} Added: trunk/selenium-client/src/main/java/com/software416/jsimplebrowser/seleniumclient/SeleniumCommand.java =================================================================== --- trunk/selenium-client/src/main/java/com/software416/jsimplebrowser/seleniumclient/SeleniumCommand.java (rev 0) +++ trunk/selenium-client/src/main/java/com/software416/jsimplebrowser/seleniumclient/SeleniumCommand.java 2007-08-11 20:44:05 UTC (rev 45) @@ -0,0 +1,28 @@ +package com.software416.jsimplebrowser.seleniumclient; + +public class SeleniumCommand { + private String _command; + private String[] _arguments; + + public SeleniumCommand() { + // no arg constructor + } + + public SeleniumCommand(String command, String... args) { + _command = command; + _arguments = args; + } + + public String[] getArguments() { + return _arguments; + } + public void setArguments(String[] arguments) { + _arguments = arguments; + } + public String getCommand() { + return _command; + } + public void setCommand(String command) { + _command = command; + } +} \ No newline at end of file Added: trunk/selenium-client/src/main/java/com/software416/jsimplebrowser/seleniumclient/SeleniumHtmlRunner.java =================================================================== --- trunk/selenium-client/src/main/java/com/software416/jsimplebrowser/seleniumclient/SeleniumHtmlRunner.java (rev 0) +++ trunk/selenium-client/src/main/java/com/software416/jsimplebrowser/seleniumclient/SeleniumHtmlRunner.java 2007-08-11 20:44:05 UTC (rev 45) @@ -0,0 +1,170 @@ +package com.software416.jsimplebrowser.seleniumclient; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; + +import com.software416.jsimplebrowser.HtmlParseException; +import com.software416.jsimplebrowser.util.HtmlToDomConverter; + +public class SeleniumHtmlRunner { + private static final Log LOG = LogFactory.getLog(SeleniumHtmlRunner.class); + private static final Map<String, Method> CLIENT_METHOD_MAP = new HashMap<String, Method>(); + private SeleniumClient _client = new SeleniumClient(); + private List<SeleniumCommand> _commands = new ArrayList<SeleniumCommand>(); + private boolean _ignoreFailures; + + static { + Method[] methods = SeleniumClient.class.getMethods(); + for (Method method : methods) { + CLIENT_METHOD_MAP.put(method.getName(), method); + } + } + + public List<SeleniumCommand> getCommands() { + return _commands; + } + + public void setCommands(List<SeleniumCommand> commands) { + _commands = commands; + } + + public String getBaseUrl() { + return _client.getBaseUrl(); + } + + public void setBaseUrl(String baseUrl) { + _client.setBaseUrl(baseUrl); + } + + public void execute() throws SeleniumClientException { + _client.start(); + for (SeleniumCommand cmd : _commands) { + executeCommand(cmd); + } + _client.stop(); + } + + protected void executeCommand(SeleniumCommand cmd) throws SeleniumClientException { + Method m = CLIENT_METHOD_MAP.get(cmd.getCommand()); + if (m == null) { + LOG.warn("Command not allowed to be executed: " + cmd.getCommand()); + return; + } + Object[] args = new Object[m.getParameterTypes().length]; + for (int x = 0; x < args.length; x++) { + args[x] = x < cmd.getArguments().length ? cmd.getArguments()[x] : null; + } + try { + if (LOG.isDebugEnabled()) { + LOG.debug("Invoking command '" + cmd.getCommand() + "' with args '" + Arrays.toString(args) + "'"); + } + m.invoke(_client, args); + } catch (IllegalArgumentException ex) { + if (_ignoreFailures) { + LOG.warn("Problem executing command: " + cmd.getCommand(), ex); + } else { + throw new SeleniumClientException(ex.getCause()); + } + } catch (IllegalAccessException ex) { + if (_ignoreFailures) { + LOG.warn("Problem executing command: " + cmd.getCommand(), ex); + } else { + throw new SeleniumClientException(ex.getCause()); + } + } catch (InvocationTargetException ex) { + if (_ignoreFailures) { + LOG.warn("Problem executing command: " + cmd.getCommand(), ex); + } else { + throw new SeleniumClientException(ex.getCause()); + } + } + } + + public void loadTestHtmlFromFile(File f) throws SeleniumClientException { + InputStream in = null; + try { + in = new FileInputStream(f); + loadTestHtmlFromInputStream(in); + } catch (FileNotFoundException ex) { + throw new SeleniumClientException("Could not find file : " + f.getAbsolutePath(), ex); + } finally { + IOUtils.closeQuietly(in); + } + } + + public void loadTestHtmlFromInputStream(InputStream in) throws SeleniumClientException{ + Document doc; + try { + doc = new HtmlToDomConverter().getDocumentFromHtml(IOUtils.toByteArray(in)); + } catch (HtmlParseException ex) { + throw new SeleniumClientException(ex); + } catch (IOException ex) { + throw new SeleniumClientException(ex); + } + NodeList nl = doc.getElementsByTagName("table"); + if (nl.getLength() != 1) { + throw new SeleniumClientException("Invalid number of tables in HTML. Expected 1 but found :" + nl.getLength()); + } + Element table = (Element)nl.item(0); + nl = table.getElementsByTagName("tr"); + + // Start at second row as first row just has test name information + for (int x = 1 ; x < nl.getLength(); x++) { + NodeList cells = ((Element)nl.item(x)).getElementsByTagName("td"); + if (cells.getLength() == 0) { + LOG.warn("Unusual situation...No cells for row " + x); + continue; + } + SeleniumCommand cmd = new SeleniumCommand(); + _commands.add(cmd); + String[] args = new String[ cells.getLength() - 1]; + cmd.setArguments(args); + for (int y = 0; y < cells.getLength(); y++) { + if (y == 0) { + cmd.setCommand(cells.item(y).getTextContent()); + } else { + args[y - 1] = cells.item(y).getTextContent(); + } + } + } + } + + public static void main(String[] args) throws SeleniumClientException { + SeleniumHtmlRunner str = new SeleniumHtmlRunner(); + str.loadTestHtmlFromFile(new File(args[0])); + str.execute(); + } + + public boolean isIgnoreFailures() { + return _ignoreFailures; + } + + public void addVerificationFailureListener(VerificationFailureListener listener) { + _client.addVerificationFailureListener(listener); + } + + public void removeVerificationFailureListener(VerificationFailureListener listener) { + _client.removeVerificationFailureListener(listener); + } + + public void setIgnoreFailures(boolean ignoreFailures) { + _ignoreFailures = ignoreFailures; + } +} Added: trunk/selenium-client/src/main/java/com/software416/jsimplebrowser/seleniumclient/VerificationFailureEvent.java =================================================================== --- trunk/selenium-client/src/main/java/com/software416/jsimplebrowser/seleniumclient/VerificationFailureEvent.java (rev 0) +++ trunk/selenium-client/src/main/java/com/software416/jsimplebrowser/seleniumclient/VerificationFailureEvent.java 2007-08-11 20:44:05 UTC (rev 45) @@ -0,0 +1,31 @@ +package com.software416.jsimplebrowser.seleniumclient; + +public class VerificationFailureEvent { + private SeleniumCommand _command; + private SeleniumClient _client; + + public VerificationFailureEvent() { + // default constructor + } + + public VerificationFailureEvent(SeleniumCommand cmd, SeleniumClient client) { + _command=cmd; + _client=client; + } + + public SeleniumCommand getCommand() { + return _command; + } + + public void setCommand(SeleniumCommand command) { + _command = command; + } + + public SeleniumClient getClient() { + return _client; + } + + public void setClient(SeleniumClient client) { + _client = client; + } +} Added: trunk/selenium-client/src/main/java/com/software416/jsimplebrowser/seleniumclient/VerificationFailureListener.java =================================================================== --- trunk/selenium-client/src/main/java/com/software416/jsimplebrowser/seleniumclient/VerificationFailureListener.java (rev 0) +++ trunk/selenium-client/src/main/java/com/software416/jsimplebrowser/seleniumclient/VerificationFailureListener.java 2007-08-11 20:44:05 UTC (rev 45) @@ -0,0 +1,5 @@ +package com.software416.jsimplebrowser.seleniumclient; + +public interface VerificationFailureListener { + public void handleVerificationFailureEvent(VerificationFailureEvent e); +} Modified: trunk/selenium-client/src/test/java/com/software416/jsimplebrowser/seleniumclient/SeleniumClientTest.java =================================================================== --- trunk/selenium-client/src/test/java/com/software416/jsimplebrowser/seleniumclient/SeleniumClientTest.java 2007-08-11 17:36:52 UTC (rev 44) +++ trunk/selenium-client/src/test/java/com/software416/jsimplebrowser/seleniumclient/SeleniumClientTest.java 2007-08-11 20:44:05 UTC (rev 45) @@ -31,6 +31,22 @@ assertNull(sc.getTable("foo1.100")); } + @Test public void testBuildUrl() { + SeleniumClient sc = new SeleniumClient(); + sc.setBaseUrl("http://www.google.com"); + assertEquals("http://www.google.com/", sc.buildUrl("/")); + assertEquals("http://www.google.com/", sc.buildUrl("")); + assertEquals("http://www.google.com/foo", sc.buildUrl("/foo")); + assertEquals("http://www.google.com/foo", sc.buildUrl("foo")); + sc.setBaseUrl("http://www.google.com/"); + assertEquals("http://www.google.com//", sc.buildUrl("/")); + assertEquals("http://www.google.com/", sc.buildUrl("")); + assertEquals("http://www.google.com//foo", sc.buildUrl("/foo")); + assertEquals("http://www.google.com/foo", sc.buildUrl("foo")); + assertEquals("http://www.416software.com/foo", sc.buildUrl("http://www.416software.com/foo")); + assertEquals("https://www.416software.com/foo", sc.buildUrl("https://www.416software.com/foo")); + } + private static class TestSimpleClient extends SimpleClient { private Document _document; Added: trunk/selenium-client/src/test/java/com/software416/jsimplebrowser/seleniumclient/SeleniumHtmlRunnerTest.java =================================================================== --- trunk/selenium-client/src/test/java/com/software416/jsimplebrowser/seleniumclient/SeleniumHtmlRunnerTest.java (rev 0) +++ trunk/selenium-client/src/test/java/com/software416/jsimplebrowser/seleniumclient/SeleniumHtmlRunnerTest.java 2007-08-11 20:44:05 UTC (rev 45) @@ -0,0 +1,53 @@ +package com.software416.jsimplebrowser.seleniumclient; + +import static org.junit.Assert.*; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.junit.BeforeClass; +import org.junit.Test; + +public class SeleniumHtmlRunnerTest implements VerificationFailureListener { + @BeforeClass public static void setUpLog() { + System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog"); + System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true"); + System.setProperty("org.apache.commons.logging.simplelog.log.com.software416", "debug"); + // + // HTTP Client Debugging... +// System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire.header", "debug"); +// System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire.content", "debug"); +// System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient", "debug"); + } + + @Test public void testLoadingScript() throws SeleniumClientException { + SeleniumHtmlRunner shr = new SeleniumHtmlRunner(); + shr.setBaseUrl("http://www.google.com"); + shr.loadTestHtmlFromInputStream(Thread.currentThread().getContextClassLoader().getResourceAsStream("testSeleniumHtmlScript.html")); + } + + @Test public void testRunningCommands() throws SeleniumClientException { + SeleniumHtmlRunner shr = new SeleniumHtmlRunner(); + shr.setBaseUrl("http://www.google.com"); + List<SeleniumCommand> cmds = new ArrayList<SeleniumCommand>(); + cmds.add(buildCommand("open", "/ig?hl=en", "")); + cmds.add(buildCommand("type", "q", "Java SDK")); + cmds.add(buildCommand("clickAndWait", "btnG", "")); + cmds.add(buildCommand("verifyTextPresent", "Java", "")); + shr.setCommands(cmds); + shr.addVerificationFailureListener(this); + shr.execute(); + } + + private SeleniumCommand buildCommand(String cmd, String... args) { + SeleniumCommand sc = new SeleniumCommand(); + sc.setCommand(cmd); + sc.setArguments(args); + return sc; + } + + public void handleVerificationFailureEvent(VerificationFailureEvent e) { + fail("Could not " + e.getCommand().getCommand() + " with args " + Arrays.toString(e.getCommand().getArguments())); + } +} Added: trunk/selenium-client/src/test/resources/testSeleniumHtmlScript.html =================================================================== --- trunk/selenium-client/src/test/resources/testSeleniumHtmlScript.html (rev 0) +++ trunk/selenium-client/src/test/resources/testSeleniumHtmlScript.html 2007-08-11 20:44:05 UTC (rev 45) @@ -0,0 +1,34 @@ +<html> +<head> +<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> +<title>test</title> +</head> +<body> +<table cellpadding="1" cellspacing="1" border="1"> +<thead> +<tr><td rowspan="1" colspan="3">test</td></tr> +</thead><tbody> +<tr> + <td>open</td> + <td>/ig?hl=en</td> + <td></td> +</tr> +<tr> + <td>type</td> + <td>q</td> + <td>Java SDK</td> +</tr> +<tr> + <td>clickAndWait</td> + <td>btnG</td> + <td></td> +</tr> +<tr> + <td>verifyTextPresent</td> + <td>Download Java 2 Platform, Standard Edition, v 1.4.2 (J2SE)</td> + <td></td> +</tr> + +</tbody></table> +</body> +</html> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |