[Jsimplebrowser-svn] SF.net SVN: jsimplebrowser: [44] trunk
Status: Alpha
Brought to you by:
rdimarco
From: <rdi...@us...> - 2007-08-11 17:36:54
|
Revision: 44 http://jsimplebrowser.svn.sourceforge.net/jsimplebrowser/?rev=44&view=rev Author: rdimarco Date: 2007-08-11 10:36:52 -0700 (Sat, 11 Aug 2007) Log Message: ----------- Refactored base client package out of selenium-client so that users do not have to use Selenium. Introduced Guice to better support testing and swapping out interface implementations. Modified Paths: -------------- trunk/core/pom.xml trunk/core/src/main/java/com/software416/jsimplebrowser/Browser.java trunk/core/src/main/java/com/software416/jsimplebrowser/HttpResponse.java trunk/core/src/main/java/com/software416/jsimplebrowser/Window.java trunk/core/src/main/java/com/software416/jsimplebrowser/impl/BrowserImpl.java trunk/core/src/main/java/com/software416/jsimplebrowser/impl/HttpResponseImpl.java trunk/core/src/main/java/com/software416/jsimplebrowser/impl/WindowImpl.java trunk/core/src/test/java/com/software416/jsimplebrowser/util/BrowserHelperTest.java trunk/pom.xml trunk/selenium-client/pom.xml 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/client/ trunk/client/pom.xml trunk/client/src/ trunk/client/src/main/ trunk/client/src/main/java/ trunk/client/src/main/java/com/ trunk/client/src/main/java/com/software416/ trunk/client/src/main/java/com/software416/jsimplebrowser/ trunk/client/src/main/java/com/software416/jsimplebrowser/client/ trunk/client/src/main/java/com/software416/jsimplebrowser/client/SimpleClient.java trunk/client/src/test/ trunk/client/src/test/java/ trunk/client/src/test/java/com/ trunk/client/src/test/java/com/software416/ trunk/client/src/test/java/com/software416/jsimplebrowser/ trunk/client/src/test/java/com/software416/jsimplebrowser/client/ trunk/client/src/test/java/com/software416/jsimplebrowser/client/SimpleClientTest.java trunk/core/src/main/java/com/software416/jsimplebrowser/impl/WindowProvider.java Property changes on: trunk/client ___________________________________________________________________ Name: svn:ignore + .classpath .project .settings target Added: trunk/client/pom.xml =================================================================== --- trunk/client/pom.xml (rev 0) +++ trunk/client/pom.xml 2007-08-11 17:36:52 UTC (rev 44) @@ -0,0 +1,24 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>com.software416.jsimplebrowser</groupId> + <artifactId>jsimplebrowser</artifactId> + <version>1.0.0-SNAPSHOT</version> + </parent> + <name>JSimpleBrowser Selenium Client</name> + <artifactId>jsimplebrowser-client</artifactId> + <version>1.0.0-SNAPSHOT</version> + <packaging>jar</packaging> + <dependencies> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>com.software416.jsimplebrowser</groupId> + <artifactId>jsimplebrowser-core</artifactId> + <version>${project.version}</version> + </dependency> + </dependencies> +</project> Added: trunk/client/src/main/java/com/software416/jsimplebrowser/client/SimpleClient.java =================================================================== --- trunk/client/src/main/java/com/software416/jsimplebrowser/client/SimpleClient.java (rev 0) +++ trunk/client/src/main/java/com/software416/jsimplebrowser/client/SimpleClient.java 2007-08-11 17:36:52 UTC (rev 44) @@ -0,0 +1,286 @@ +package com.software416.jsimplebrowser.client; + +import java.util.ArrayList; +import java.util.List; + +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpressionException; +import javax.xml.xpath.XPathFactory; + +import org.apache.commons.collections.MultiHashMap; +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.NodeList; + +import com.google.inject.Guice; +import com.google.inject.Inject; +import com.software416.jsimplebrowser.Browser; +import com.software416.jsimplebrowser.BrowserException; +import com.software416.jsimplebrowser.Window; + +public class SimpleClient { + @SuppressWarnings("unused") + private static final Log LOG = LogFactory.getLog(SimpleClient.class); + + @Inject + private Browser _browser; + private String _currentWindowName = Browser.MAIN_BROWSER_WINDOW_NAME; + + public static SimpleClient newInstance() { + SimpleClient sc = new SimpleClient(); + Guice.createInjector().injectMembers(sc); + return sc; + } + + public String getHtmlSource() { + return getCurrentWindow().getSource(); + } + + public String getLocation() { + return getCurrentWindow().getLocation(); + } + + public void close() { + _browser.closeWindow(_currentWindowName); + _currentWindowName = Browser.MAIN_BROWSER_WINDOW_NAME; + } + + public String[] getAllElements(String xpath) { + XPath xp = XPathFactory.newInstance().newXPath(); + NodeList nl; + try { + nl = (NodeList)xp.evaluate(xpath, getDocument(), XPathConstants.NODESET); + } catch (XPathExpressionException ex) { + throw new RuntimeException("Developer screwed up the xpath: " + xpath, ex); + } + List<String> links = new ArrayList<String>(); + for (int x = 0; x < nl.getLength(); x++) { + Attr a = (Attr)nl.item(x); + links.add(a.getValue()); + } + return links.toArray(new String[links.size()]); + } + + public String getText(String id) { + return getDocument().getElementById(id).getTextContent(); + } + + public Document getDocument() { + return getCurrentWindow().getDocument(); + } + public Window getCurrentWindow() { + return _browser.getWindow(_currentWindowName); + } + + public String[] getAllWindowNames() { + List<String> windowNames = _browser.getWindowNames(); + return windowNames.toArray(new String[windowNames.size()]); + } + + public void open(String url) throws BrowserException { + getCurrentWindow().open(url); + } + + public void openWindow(String windowName, String url) throws BrowserException { + _currentWindowName = windowName; + getCurrentWindow().open(url); + } + + public void submitForm(String id) throws BrowserException { + getCurrentWindow().submitForm(id, new MultiHashMap()); + } + +// public String[] getAllWindowTitles() { +// throw new UnsupportedOperationException(); +// } +// +// public String getAttribute(String arg0) { +// throw new UnsupportedOperationException(); +// } +// +// public String[] getAttributeFromAllWindows(String arg0) { +// throw new UnsupportedOperationException(); +// } +// +// public String getCookie() { +// throw new UnsupportedOperationException(); +// } +// +// +// public String[] getSelectOptions(String arg0) { +// throw new UnsupportedOperationException(); +// +// } +// +// public String getSelectedId(String arg0) { +// throw new UnsupportedOperationException(); +// +// } +// +// public String[] getSelectedIds(String arg0) { +// throw new UnsupportedOperationException(); +// +// } +// +// public String getSelectedIndex(String arg0) { +// throw new UnsupportedOperationException(); +// +// } +// +// public String[] getSelectedIndexes(String arg0) { +// throw new UnsupportedOperationException(); +// +// } +// +// public String getSelectedLabel(String arg0) { +// throw new UnsupportedOperationException(); +// +// } +// +// public String[] getSelectedLabels(String arg0) { +// throw new UnsupportedOperationException(); +// +// } +// +// public String getSelectedValue(String arg0) { +// throw new UnsupportedOperationException(); +// +// } +// +// public String[] getSelectedValues(String arg0) { +// throw new UnsupportedOperationException(); +// +// } +// public String getTitle() { +// throw new UnsupportedOperationException(); +// +// } +// +// public String getValue(String arg0) { +// throw new UnsupportedOperationException(); +// } +// +// public boolean getWhetherThisFrameMatchFrameExpression(String arg0, String arg1) { +// throw new UnsupportedOperationException(); +// } +// +// public boolean getWhetherThisWindowMatchWindowExpression(String arg0, String arg1) { +// throw new UnsupportedOperationException(); +// } +// +// public Number getXpathCount(String arg0) { +// throw new UnsupportedOperationException(); +// +// } +// +// public void goBack() { +// throw new UnsupportedOperationException(); +// +// } +// +// public boolean isChecked(String id) { +// throw new UnsupportedOperationException(); +// } +// +// public boolean isEditable(String id) { +// throw new UnsupportedOperationException(); +// } +// +// public boolean isElementPresent(String id) { +// return getDocument().getElementById(id) != null; +// } +// +// public boolean isOrdered(String arg0, String arg1) { +// throw new UnsupportedOperationException(); +// } +// +// public boolean isSomethingSelected(String arg0) { +// throw new UnsupportedOperationException(); +// } +// +// public boolean isTextPresent(String text) { +// return getCurrentWindow().getSource().contains(text); +// } +// +// public boolean isVisible(String arg0) { +// throw new UnsupportedOperationException(); +// } +// +// public void refresh() { +// open(getCurrentWindow().getLocation()); +// } +// +// public void removeAllSelections(String arg0) { +// throw new UnsupportedOperationException(); +// +// } +// +// public void removeSelection(String arg0, String arg1) { +// throw new UnsupportedOperationException(); +// +// } +// +// public void select(String arg0, String arg1) { +// throw new UnsupportedOperationException(); +// +// } +// +// public void selectFrame(String arg0) { +// throw new UnsupportedOperationException(); +// +// } +// +// public void selectWindow(String arg0) { +// throw new UnsupportedOperationException(); +// +// } +// +// public void setContext(String arg0) { +// throw new UnsupportedOperationException(); +// +// } +// +// public void type(String arg0, String arg1) { +// throw new UnsupportedOperationException(); +// +// } +// +// public void typeKeys(String arg0, String arg1) { +// throw new UnsupportedOperationException(); +// +// } +// public void setTimeout(String arg0) { +// throw new UnsupportedOperationException(); +// } +// +// public void uncheck(String arg0) { +// throw new UnsupportedOperationException(); +// +// } +// +// public void waitForCondition(String arg0, String arg1) { +// throw new UnsupportedOperationException(); +// +// } +// +// public void waitForFrameToLoad(String arg0, String arg1) { +// // noop +// } +// +// public void waitForPageToLoad(String arg0) { +// // noop +// } +// 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(); +// } + +} Added: trunk/client/src/test/java/com/software416/jsimplebrowser/client/SimpleClientTest.java =================================================================== --- trunk/client/src/test/java/com/software416/jsimplebrowser/client/SimpleClientTest.java (rev 0) +++ trunk/client/src/test/java/com/software416/jsimplebrowser/client/SimpleClientTest.java 2007-08-11 17:36:52 UTC (rev 44) @@ -0,0 +1,18 @@ +package com.software416.jsimplebrowser.client; + +import static org.junit.Assert.*; + +import org.junit.BeforeClass; +import org.junit.Test; + +public class SimpleClientTest { + @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"); + } + + @Test public void testNewInstance() { + assertNotNull(SimpleClient.newInstance()); + } +} Modified: trunk/core/pom.xml =================================================================== --- trunk/core/pom.xml 2007-08-11 15:32:47 UTC (rev 43) +++ trunk/core/pom.xml 2007-08-11 17:36:52 UTC (rev 44) @@ -10,6 +10,10 @@ <version>1.0.0-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> + <dependency> + <groupId>com.google.guice</groupId> + <artifactId>guice</artifactId> + </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> Modified: trunk/core/src/main/java/com/software416/jsimplebrowser/Browser.java =================================================================== --- trunk/core/src/main/java/com/software416/jsimplebrowser/Browser.java 2007-08-11 15:32:47 UTC (rev 43) +++ trunk/core/src/main/java/com/software416/jsimplebrowser/Browser.java 2007-08-11 17:36:52 UTC (rev 44) @@ -2,6 +2,12 @@ import java.util.List; +import org.apache.commons.httpclient.HttpMethod; + +import com.google.inject.ImplementedBy; +import com.software416.jsimplebrowser.impl.BrowserImpl; + +@ImplementedBy(BrowserImpl.class) public interface Browser { public static final String MAIN_BROWSER_WINDOW_NAME = "main"; public void open(String url) throws BrowserException; @@ -10,4 +16,5 @@ public Window getWindow(String windowName); public void closeWindow(String windowName); public List<String> getWindowNames(); + HttpResponse makeRequest(HttpMethod hm) throws BrowserException; } Modified: trunk/core/src/main/java/com/software416/jsimplebrowser/HttpResponse.java =================================================================== --- trunk/core/src/main/java/com/software416/jsimplebrowser/HttpResponse.java 2007-08-11 15:32:47 UTC (rev 43) +++ trunk/core/src/main/java/com/software416/jsimplebrowser/HttpResponse.java 2007-08-11 17:36:52 UTC (rev 44) @@ -7,4 +7,5 @@ public MultiMap getCookies(); public String getCookieValue(String cookieName); public byte[] getResponseBody(); + public String getLocation(); } Modified: trunk/core/src/main/java/com/software416/jsimplebrowser/Window.java =================================================================== --- trunk/core/src/main/java/com/software416/jsimplebrowser/Window.java 2007-08-11 15:32:47 UTC (rev 43) +++ trunk/core/src/main/java/com/software416/jsimplebrowser/Window.java 2007-08-11 17:36:52 UTC (rev 44) @@ -3,6 +3,10 @@ import org.apache.commons.collections.MultiMap; import org.w3c.dom.Document; +import com.google.inject.ProvidedBy; +import com.software416.jsimplebrowser.impl.WindowProvider; + +@ProvidedBy(WindowProvider.class) public interface Window { public void open(String url) throws BrowserException; public void submitForm(String formName, MultiMap parameters) throws BrowserException; 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 15:32:47 UTC (rev 43) +++ trunk/core/src/main/java/com/software416/jsimplebrowser/impl/BrowserImpl.java 2007-08-11 17:36:52 UTC (rev 44) @@ -13,25 +13,20 @@ import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.StringRequestEntity; import org.apache.commons.io.IOUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; +import com.google.inject.Inject; +import com.google.inject.Provider; import com.software416.jsimplebrowser.Browser; import com.software416.jsimplebrowser.BrowserException; import com.software416.jsimplebrowser.Window; public class BrowserImpl implements Browser { - @SuppressWarnings("unused") - private static final Log LOG = LogFactory.getLog(BrowserImpl.class); + @Inject private Provider<Window> _windowProvider; private HttpClient _client = new HttpClient(); - private Map<String, WindowImpl> _browserWindows = new HashMap<String, WindowImpl>(); + private Map<String, Window> _browserWindows = new HashMap<String, Window>(); - public BrowserImpl() { - _browserWindows.put(MAIN_BROWSER_WINDOW_NAME, new WindowImpl(this)); - } - - protected synchronized HttpResponseImpl makeRequest(HttpMethod m) throws BrowserException { + public synchronized HttpResponseImpl makeRequest(HttpMethod m) throws BrowserException { HttpResponseImpl ri = new HttpResponseImpl(); ri.setRequestMethod(m); @@ -68,11 +63,14 @@ public synchronized Window getWindow(String windowName) { if (!_browserWindows.containsKey(windowName)) { - _browserWindows.put(windowName, new WindowImpl(this)); + ThreadLocal<Browser> tl = new ThreadLocal<Browser>(); + tl.set(this); + _browserWindows.put(windowName, _windowProvider.get()); + tl.remove(); } return _browserWindows.get(windowName); } - + public void closeWindow(String windowName) { _browserWindows.remove(windowName); } Modified: trunk/core/src/main/java/com/software416/jsimplebrowser/impl/HttpResponseImpl.java =================================================================== --- trunk/core/src/main/java/com/software416/jsimplebrowser/impl/HttpResponseImpl.java 2007-08-11 15:32:47 UTC (rev 43) +++ trunk/core/src/main/java/com/software416/jsimplebrowser/impl/HttpResponseImpl.java 2007-08-11 17:36:52 UTC (rev 44) @@ -6,7 +6,9 @@ import org.apache.commons.collections.MultiMap; import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpMethod; +import org.apache.commons.httpclient.URIException; +import com.software416.jsimplebrowser.BrowserRuntimeException; import com.software416.jsimplebrowser.HttpResponse; public class HttpResponseImpl implements HttpResponse{ @@ -14,6 +16,13 @@ private int _responseCode; private byte[] _responseBody; + public String getLocation() { + try { + return _requestMethod.getURI().toString(); + } catch (URIException ex) { + throw new BrowserRuntimeException(ex); + } + } public MultiMap getCookies() { MultiHashMap rv = new MultiHashMap(); 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 15:32:47 UTC (rev 43) +++ trunk/core/src/main/java/com/software416/jsimplebrowser/impl/WindowImpl.java 2007-08-11 17:36:52 UTC (rev 44) @@ -18,6 +18,8 @@ import org.w3c.dom.Element; import org.w3c.dom.NodeList; +import com.google.inject.Inject; +import com.software416.jsimplebrowser.Browser; import com.software416.jsimplebrowser.BrowserException; import com.software416.jsimplebrowser.BrowserRuntimeException; import com.software416.jsimplebrowser.History; @@ -27,22 +29,23 @@ import com.software416.jsimplebrowser.util.HtmlToDomConverter; public class WindowImpl implements Window { - private HttpResponseImpl _requestInfo; + private HttpResponse _requestInfo; private Document _responseDocument; - private BrowserImpl _browser; + private Browser _browser; private HtmlToDomConverter _converter = new HtmlToDomConverter(); private boolean _doFollowRedirects = true; - public WindowImpl(BrowserImpl b) { + @Inject + public WindowImpl(Browser b) { _browser = b; } - + public void open(String url) throws BrowserException { try { GetMethod gm; if (_requestInfo != null) { gm = new GetMethod(); - gm.setURI(new URI(_requestInfo.getRequestMethod().getURI(), url, true)); + gm.setURI(new URI(new URI(_requestInfo.getLocation(), true), url, true)); } else { gm = new GetMethod(url); } @@ -56,7 +59,7 @@ handleResponse(_browser.makeRequest(hm)); } - public void handleResponse(HttpResponseImpl requestInfo) throws BrowserException { + public void handleResponse(HttpResponse requestInfo) throws BrowserException { _requestInfo = requestInfo; _responseDocument = null; if (_doFollowRedirects) { @@ -111,7 +114,7 @@ boolean usePost = formMethod != null && formMethod.toUpperCase().equals("POST"); HttpMethod rv = usePost ? new PostMethod() : new GetMethod(); - rv.setURI(new URI(_requestInfo.getRequestMethod().getURI(), new URI(formElement.getAttribute("action"), true))); + rv.setURI(new URI(new URI(_requestInfo.getLocation(), true), new URI(formElement.getAttribute("action"), true))); Collection params = formParameters.values(); if (usePost) { @@ -236,11 +239,7 @@ } public String getLocation() { - try { - return _requestInfo.getRequestMethod().getURI().toString(); - } catch (URIException ex) { - throw new BrowserRuntimeException(ex); - } + return _requestInfo.getLocation(); } public HttpResponse getRequestInfo() { Added: trunk/core/src/main/java/com/software416/jsimplebrowser/impl/WindowProvider.java =================================================================== --- trunk/core/src/main/java/com/software416/jsimplebrowser/impl/WindowProvider.java (rev 0) +++ trunk/core/src/main/java/com/software416/jsimplebrowser/impl/WindowProvider.java 2007-08-11 17:36:52 UTC (rev 44) @@ -0,0 +1,13 @@ +package com.software416.jsimplebrowser.impl; + +import com.google.inject.Provider; +import com.software416.jsimplebrowser.Browser; +import com.software416.jsimplebrowser.Window; + +public class WindowProvider implements Provider<Window> { + + public Window get() { + return new WindowImpl(new ThreadLocal<Browser>().get()); + } + +} Modified: trunk/core/src/test/java/com/software416/jsimplebrowser/util/BrowserHelperTest.java =================================================================== --- trunk/core/src/test/java/com/software416/jsimplebrowser/util/BrowserHelperTest.java 2007-08-11 15:32:47 UTC (rev 43) +++ trunk/core/src/test/java/com/software416/jsimplebrowser/util/BrowserHelperTest.java 2007-08-11 17:36:52 UTC (rev 44) @@ -8,15 +8,15 @@ import org.apache.commons.io.IOUtils; import org.junit.Test; +import com.google.inject.Guice; import com.software416.jsimplebrowser.Browser; import com.software416.jsimplebrowser.BrowserException; -import com.software416.jsimplebrowser.impl.BrowserImpl; import com.software416.jsimplebrowser.impl.HttpResponseImpl; import com.software416.jsimplebrowser.impl.WindowImpl; public class BrowserHelperTest { @Test public void testLinkParsing() throws BrowserException, IOException { - BrowserImpl b = new BrowserImpl(); + Browser b = Guice.createInjector().getInstance(Browser.class); BrowserHelper bh = new BrowserHelper(b); WindowImpl wi = (WindowImpl)b.getWindow(Browser.MAIN_BROWSER_WINDOW_NAME); HttpResponseImpl ri = new HttpResponseImpl(); Modified: trunk/pom.xml =================================================================== --- trunk/pom.xml 2007-08-11 15:32:47 UTC (rev 43) +++ trunk/pom.xml 2007-08-11 17:36:52 UTC (rev 44) @@ -69,6 +69,7 @@ <url>http://jsimplebrowser.sourceforge.net/</url> <modules> <module>core</module> + <module>client</module> <module>selenium-client</module> </modules> <build> @@ -165,6 +166,11 @@ <artifactId>selenium-java-client-driver</artifactId> <version>0.9.2-SNAPSHOT</version> </dependency> + <dependency> + <groupId>com.google.guice</groupId> + <artifactId>guice</artifactId> + <version>1.0</version> + </dependency> </dependencies> </dependencyManagement> <reporting> Modified: trunk/selenium-client/pom.xml =================================================================== --- trunk/selenium-client/pom.xml 2007-08-11 15:32:47 UTC (rev 43) +++ trunk/selenium-client/pom.xml 2007-08-11 17:36:52 UTC (rev 44) @@ -28,5 +28,10 @@ <artifactId>jsimplebrowser-core</artifactId> <version>${project.version}</version> </dependency> + <dependency> + <groupId>com.software416.jsimplebrowser</groupId> + <artifactId>jsimplebrowser-client</artifactId> + <version>${project.version}</version> + </dependency> </dependencies> </project> 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 15:32:47 UTC (rev 43) +++ trunk/selenium-client/src/main/java/com/software416/jsimplebrowser/seleniumclient/SeleniumClient.java 2007-08-11 17:36:52 UTC (rev 44) @@ -1,57 +1,33 @@ package com.software416.jsimplebrowser.seleniumclient; -import java.util.ArrayList; -import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; -import javax.xml.xpath.XPath; -import javax.xml.xpath.XPathConstants; -import javax.xml.xpath.XPathExpressionException; -import javax.xml.xpath.XPathFactory; - -import org.apache.commons.collections.MultiHashMap; 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.software416.jsimplebrowser.Browser; import com.software416.jsimplebrowser.BrowserException; -import com.software416.jsimplebrowser.Window; -import com.software416.jsimplebrowser.impl.BrowserImpl; +import com.software416.jsimplebrowser.client.SimpleClient; import com.software416.jsimplebrowser.util.ElementLocator; import com.thoughtworks.selenium.Selenium; public class SeleniumClient implements Selenium { private static final Log LOG = LogFactory.getLog(SeleniumClient.class); - private BrowserImpl _browser; - private String _currentWindowName = Browser.MAIN_BROWSER_WINDOW_NAME; - - 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(); - } + private SimpleClient _simpleClient = new SimpleClient(); public String getHtmlSource() { - return getCurrentWindow().getSource(); + return _simpleClient.getHtmlSource(); } public String getLocation() { - return getCurrentWindow().getLocation(); + return _simpleClient.getLocation(); } public void close() { - _browser.closeWindow(_currentWindowName); - _currentWindowName = Browser.MAIN_BROWSER_WINDOW_NAME; + _simpleClient.close(); } public void windowFocus() { // noop (although, I would think a window id/name would come in here. @@ -62,40 +38,17 @@ } public String[] getAllButtons() { - return getAllElements("//input[@type='button' or @type='submit' or @type = 'reset']/@id"); + return _simpleClient.getAllElements("//input[@type='button' or @type='submit' or @type = 'reset']/@id"); } public String[] getAllFields() { - return getAllElements("//input[@type='text']/@id"); + return _simpleClient.getAllElements("//input[@type='text']/@id"); } public String[] getAllLinks() { - return getAllElements("//a/@id"); + return _simpleClient.getAllElements("//a/@id"); } - - protected String[] getAllElements(String xpath) { - XPath xp = XPathFactory.newInstance().newXPath(); - NodeList nl; - try { - nl = (NodeList)xp.evaluate(xpath, getDocument(), XPathConstants.NODESET); - } catch (XPathExpressionException ex) { - throw new RuntimeException("Developer screwed up the xpath: " + xpath, ex); - } - List<String> links = new ArrayList<String>(); - for (int x = 0; x < nl.getLength(); x++) { - Attr a = (Attr)nl.item(x); - links.add(a.getValue()); - } - return links.toArray(new String[links.size()]); - } - - protected Document getDocument() { - return getCurrentWindow().getDocument(); - } - private Window getCurrentWindow() { - return _browser.getWindow(_currentWindowName); - } - + public void createCookie(String arg0, String arg1) { throw new UnsupportedOperationException(); } @@ -105,14 +58,17 @@ } public String[] getAllWindowIds() { - return getAllWindowNames(); + return _simpleClient.getAllWindowNames(); } public String[] getAllWindowNames() { - List<String> windowNames = _browser.getWindowNames(); - return windowNames.toArray(new String[windowNames.size()]); + return _simpleClient.getAllWindowNames(); } + public String getBodyText() { + return _simpleClient.getDocument().getElementsByTagName("body").item(0).getTextContent(); + } + public String[] getAllWindowTitles() { throw new UnsupportedOperationException(); } @@ -125,10 +81,6 @@ throw new UnsupportedOperationException(); } - public String getBodyText() { - return getDocument().getElementsByTagName("body").item(0).getTextContent(); - } - public String getCookie() { throw new UnsupportedOperationException(); } @@ -202,7 +154,7 @@ return null; } - Element table = new ElementLocator(getDocument()).findElement(m.group(1)); + Element table = new ElementLocator(_simpleClient.getDocument()).findElement(m.group(1)); if (table != null) { NodeList rows = table.getElementsByTagName("tr"); if (row < rows.getLength()) { @@ -230,13 +182,22 @@ return null; } + 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(); + } + public String getText(String id) { - return getDocument().getElementById(id).getTextContent(); + return _simpleClient.getDocument().getElementById(id).getTextContent(); } public String getTitle() { throw new UnsupportedOperationException(); - } public String getValue(String arg0) { @@ -270,7 +231,7 @@ } public boolean isElementPresent(String id) { - return getDocument().getElementById(id) != null; + return _simpleClient.getDocument().getElementById(id) != null; } public boolean isOrdered(String arg0, String arg1) { @@ -282,7 +243,7 @@ } public boolean isTextPresent(String text) { - return getCurrentWindow().getSource().contains(text); + return _simpleClient.getHtmlSource().contains(text); } public boolean isVisible(String arg0) { @@ -291,7 +252,7 @@ public void open(String url) { try { - getCurrentWindow().open(url); + _simpleClient.open(url); } catch (BrowserException ex) { LOG.warn("Problem loading page", ex); } @@ -299,15 +260,14 @@ public void openWindow(String windowName, String url) { try { - _currentWindowName = windowName; - getCurrentWindow().open(url); + _simpleClient.openWindow(windowName, url); } catch (BrowserException ex) { LOG.warn("Problem loading page", ex); } } public void refresh() { - open(getCurrentWindow().getLocation()); + open(_simpleClient.getLocation()); } public void removeAllSelections(String arg0) { @@ -345,19 +305,19 @@ } public void start() { - _browser = new BrowserImpl(); + _simpleClient = SimpleClient.newInstance(); } public void stop() { - _browser = null; + _simpleClient = null; } public void submit(String id) { try { - getCurrentWindow().submitForm(id, new MultiHashMap()); - } catch (BrowserException ex) { - LOG.warn("Problem loading page", ex); - } + _simpleClient.submitForm(id); + } catch (BrowserException ex ) { + LOG.error("Problem submitting form: " + id, ex); + } } public void type(String arg0, String arg1) { @@ -639,5 +599,13 @@ } + public SimpleClient getSimpleClient() { + return _simpleClient; + } + public void setSimpleClient(SimpleClient simpleClient) { + _simpleClient = simpleClient; + } + + } 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 15:32:47 UTC (rev 43) +++ trunk/selenium-client/src/test/java/com/software416/jsimplebrowser/seleniumclient/SeleniumClientTest.java 2007-08-11 17:36:52 UTC (rev 44) @@ -10,6 +10,7 @@ import org.w3c.dom.Document; import com.software416.jsimplebrowser.BrowserException; +import com.software416.jsimplebrowser.client.SimpleClient; import com.software416.jsimplebrowser.util.HtmlToDomConverter; public class SeleniumClientTest { @@ -20,7 +21,8 @@ } @Test public void testElementSearch() throws IOException, BrowserException { - SeleniumClient sc = new TestSeleniumClient("tableTest.html"); + SeleniumClient sc = new SeleniumClient(); + sc.setSimpleClient(new TestSimpleClient("tableTest.html")); assertEquals("/ig?hl=en", sc.getTable("//table[1].2.1")); assertEquals("/ig?hl=en", sc.getTable("foo.2.1")); assertEquals("Command", sc.getTable("foo.1.0")); @@ -28,11 +30,11 @@ assertNull(sc.getTable("foo1.100.100")); assertNull(sc.getTable("foo1.100")); } - - private static class TestSeleniumClient extends SeleniumClient { + + private static class TestSimpleClient extends SimpleClient { private Document _document; - TestSeleniumClient (String resourceLocation) throws IOException, BrowserException { + TestSimpleClient (String resourceLocation) throws IOException, BrowserException { _document = new HtmlToDomConverter().getDocumentFromHtml(IOUtils.toByteArray(Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceLocation))); } @@ -41,4 +43,5 @@ return _document; } } + } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |