[JWebUnit-development] SF.net SVN: jwebunit:[924] trunk
Brought to you by:
henryju
|
From: <he...@us...> - 2011-09-23 14:56:58
|
Revision: 924
http://jwebunit.svn.sourceforge.net/jwebunit/?rev=924&view=rev
Author: henryju
Date: 2011-09-23 14:56:50 +0000 (Fri, 23 Sep 2011)
Log Message:
-----------
Lot of work on WebDriver testing engine. Many tests are passing.
Modified Paths:
--------------
trunk/jwebunit-commons-tests/src/main/resources/logback-test.xml
trunk/jwebunit-webdriver-plugin/src/main/java/net/sourceforge/jwebunit/webdriver/WebDriverTestingEngineImpl.java
trunk/jwebunit-webdriver-plugin/src/test/java/net/sourceforge/jwebunit/webdriver/JWebUnitTest.java
Added Paths:
-----------
trunk/jwebunit-webdriver-plugin/src/main/java/net/sourceforge/jwebunit/webdriver/FixedHtmlUnitDriver.java
trunk/jwebunit-webdriver-plugin/src/main/java/net/sourceforge/jwebunit/webdriver/FixedHtmlUnitWebElement.java
trunk/jwebunit-webdriver-plugin/src/main/java/net/sourceforge/jwebunit/webdriver/WebDriverElementImpl.java
Removed Paths:
-------------
trunk/jwebunit-webdriver-plugin/src/main/java/net/sourceforge/jwebunit/webdriver/WebdriverElementImpl.java
Modified: trunk/jwebunit-commons-tests/src/main/resources/logback-test.xml
===================================================================
--- trunk/jwebunit-commons-tests/src/main/resources/logback-test.xml 2011-09-23 14:52:27 UTC (rev 923)
+++ trunk/jwebunit-commons-tests/src/main/resources/logback-test.xml 2011-09-23 14:56:50 UTC (rev 924)
@@ -40,6 +40,8 @@
<logger name="org.eclipse.jetty" level="ERROR"/>
<logger name="ch.qos.logback" level="ERROR"/>
+
+ <logger name="org.browsermob.proxy" level="ERROR"/>
<root level="DEBUG">
<appender-ref ref="consoleAppender"/>
Added: trunk/jwebunit-webdriver-plugin/src/main/java/net/sourceforge/jwebunit/webdriver/FixedHtmlUnitDriver.java
===================================================================
--- trunk/jwebunit-webdriver-plugin/src/main/java/net/sourceforge/jwebunit/webdriver/FixedHtmlUnitDriver.java (rev 0)
+++ trunk/jwebunit-webdriver-plugin/src/main/java/net/sourceforge/jwebunit/webdriver/FixedHtmlUnitDriver.java 2011-09-23 14:56:50 UTC (rev 924)
@@ -0,0 +1,20 @@
+package net.sourceforge.jwebunit.webdriver;
+
+import org.openqa.selenium.Capabilities;
+
+import com.gargoylesoftware.htmlunit.html.HtmlElement;
+import org.openqa.selenium.WebElement;
+import org.openqa.selenium.htmlunit.HtmlUnitDriver;
+
+
+public class FixedHtmlUnitDriver extends HtmlUnitDriver {
+
+ public FixedHtmlUnitDriver(Capabilities capabilities) {
+ super(capabilities);
+ }
+
+ protected WebElement newHtmlUnitWebElement(HtmlElement element) {
+ return new FixedHtmlUnitWebElement(this, element);
+ }
+
+}
Property changes on: trunk/jwebunit-webdriver-plugin/src/main/java/net/sourceforge/jwebunit/webdriver/FixedHtmlUnitDriver.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added: trunk/jwebunit-webdriver-plugin/src/main/java/net/sourceforge/jwebunit/webdriver/FixedHtmlUnitWebElement.java
===================================================================
--- trunk/jwebunit-webdriver-plugin/src/main/java/net/sourceforge/jwebunit/webdriver/FixedHtmlUnitWebElement.java (rev 0)
+++ trunk/jwebunit-webdriver-plugin/src/main/java/net/sourceforge/jwebunit/webdriver/FixedHtmlUnitWebElement.java 2011-09-23 14:56:50 UTC (rev 924)
@@ -0,0 +1,65 @@
+package net.sourceforge.jwebunit.webdriver;
+
+import com.gargoylesoftware.htmlunit.html.DomElement;
+import com.gargoylesoftware.htmlunit.html.HtmlPage;
+import com.gargoylesoftware.htmlunit.html.HtmlSelect;
+import com.gargoylesoftware.htmlunit.html.HtmlTextArea;
+import java.net.MalformedURLException;
+import java.util.List;
+
+import com.gargoylesoftware.htmlunit.html.HtmlInput;
+import com.gargoylesoftware.htmlunit.html.HtmlOption;
+
+import com.gargoylesoftware.htmlunit.html.HtmlElement;
+import org.openqa.selenium.htmlunit.HtmlUnitDriver;
+
+import org.openqa.selenium.htmlunit.HtmlUnitWebElement;
+
+/**
+ * Fix for issue http://code.google.com/p/selenium/issues/detail?id=2295
+ *
+ * @author julien, 23 sept. 2011
+ */
+public class FixedHtmlUnitWebElement extends HtmlUnitWebElement {
+
+ public FixedHtmlUnitWebElement(HtmlUnitDriver parent, HtmlElement element) {
+ super(parent, element);
+ }
+
+ @Override
+ public boolean isSelected() {
+ assertElementNotStale();
+
+ if (element instanceof HtmlInput) {
+ return ((HtmlInput) element).isChecked();
+ }
+ else if (element instanceof HtmlOption) {
+ return ((HtmlOption) element).isSelected();
+ }
+
+ throw new UnsupportedOperationException("Unable to determine if element is selected. Tag name is: " + element.getTagName());
+ }
+
+ @Override
+ public String getAttribute(String name) {
+ assertElementNotStale();
+
+ final String lowerName = name.toLowerCase();
+
+ if (element instanceof HtmlOption && "selected".equals(lowerName)) {
+ return ((HtmlOption) element).isSelected() ? "true" : null;
+ }
+
+ if ("value".equals(lowerName)) {
+ if (element instanceof HtmlTextArea) {
+ return ((HtmlTextArea) element).getText();
+ }
+
+ if (element instanceof HtmlOption) {
+ return ((HtmlOption) element).getValueAttribute();
+ }
+ }
+
+ return super.getAttribute(name);
+ }
+}
Property changes on: trunk/jwebunit-webdriver-plugin/src/main/java/net/sourceforge/jwebunit/webdriver/FixedHtmlUnitWebElement.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Copied: trunk/jwebunit-webdriver-plugin/src/main/java/net/sourceforge/jwebunit/webdriver/WebDriverElementImpl.java (from rev 913, trunk/jwebunit-webdriver-plugin/src/main/java/net/sourceforge/jwebunit/webdriver/WebdriverElementImpl.java)
===================================================================
--- trunk/jwebunit-webdriver-plugin/src/main/java/net/sourceforge/jwebunit/webdriver/WebDriverElementImpl.java (rev 0)
+++ trunk/jwebunit-webdriver-plugin/src/main/java/net/sourceforge/jwebunit/webdriver/WebDriverElementImpl.java 2011-09-23 14:56:50 UTC (rev 924)
@@ -0,0 +1,198 @@
+/**
+ * Copyright (c) 2010, JWebUnit team.
+ *
+ * This file is part of JWebUnit.
+ *
+ * JWebUnit is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * JWebUnit is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with JWebUnit. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/**
+ *
+ */
+package net.sourceforge.jwebunit.webdriver;
+
+import org.openqa.selenium.JavascriptExecutor;
+
+import org.openqa.selenium.By;
+
+import org.openqa.selenium.WebElement;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import net.sourceforge.jwebunit.api.IElement;
+
+import com.gargoylesoftware.htmlunit.html.DomNode;
+import com.gargoylesoftware.htmlunit.html.HtmlElement;
+import com.gargoylesoftware.htmlunit.html.HtmlInput;
+import com.gargoylesoftware.htmlunit.html.HtmlTextArea;
+
+/**
+ * Webdriver implementation of IElement wrapper.
+ *
+ * @author henryju
+ *
+ */
+public class WebDriverElementImpl implements IElement {
+
+ /**
+ * The wrapped element.
+ */
+ private WebElement element;
+
+ public WebDriverElementImpl(WebElement element) {
+ if (element == null)
+ throw new NullPointerException("Cannot create an IElement for a null element.");
+ this.element = element;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see net.sourceforge.jwebunit.api.IElement#attribute(java.lang.String)
+ */
+ public String getAttribute(String name) {
+ return element.getAttribute(name);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see net.sourceforge.jwebunit.api.IElement#name()
+ */
+ public String getName() {
+ return element.getTagName();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see net.sourceforge.jwebunit.api.IElement#getChildren()
+ */
+ public List<IElement> getChildren() {
+ List<IElement> children = new ArrayList<IElement>();
+ for (WebElement e : element.findElements(By.xpath("child::*"))) {
+ if (e != null)
+ children.add(new WebDriverElementImpl(e));
+ }
+ return children;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see net.sourceforge.jwebunit.api.IElement#getParent()
+ */
+ public IElement getParent() {
+ return new WebDriverElementImpl(element.findElement(By.xpath("//parent::*")));
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see net.sourceforge.jwebunit.api.IElement#getTextContent()
+ */
+ public String getTextContent() {
+ return element.getText();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see net.sourceforge.jwebunit.api.IElement#getElement(java.lang.String)
+ */
+ public IElement getElement(String xpath) {
+ return new WebDriverElementImpl((WebElement) element.findElement(By.xpath(xpath)));
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see net.sourceforge.jwebunit.api.IElement#getElements(java.lang.String)
+ */
+ public List<IElement> getElements(String xpath) {
+ List<IElement> elements = new ArrayList<IElement>();
+ for (WebElement o : element.findElements(By.xpath(xpath))) {
+ elements.add(new WebDriverElementImpl(o));
+ }
+ return elements;
+ }
+
+ public String toString() {
+ return "IElement[name=" + getName() + " wrapped=" + element + "]";
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see net.sourceforge.jwebunit.api.IElement#setAttribute(java.lang.String)
+ */
+ public void setAttribute(String string) {
+ throw new UnsupportedOperationException("Not supported yet.");
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see net.sourceforge.jwebunit.api.IElement#setAttribute(java.lang.String, java.lang.String)
+ */
+ public void setAttribute(String string, String value) {
+ throw new UnsupportedOperationException("Not supported yet.");
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see net.sourceforge.jwebunit.api.IElement#setTextContent(java.lang.String)
+ */
+ public void setTextContent(String value) {
+ throw new UnsupportedOperationException("Not supported yet.");
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 37;
+ int result = 1;
+ result = prime * result + ((element == null) ? 0 : element.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ final WebDriverElementImpl other = (WebDriverElementImpl) obj;
+ if (element == null) {
+ if (other.element != null)
+ return false;
+ }
+ else if (!element.equals(other.element))
+ return false;
+ return true;
+ }
+
+ /**
+ * Return the unwrapped Webdriver element that this IElement represents.
+ *
+ * @return the Webdriver element this IElement represents.
+ */
+ public WebElement getHtmlElement() {
+ return element;
+ }
+
+}
Property changes on: trunk/jwebunit-webdriver-plugin/src/main/java/net/sourceforge/jwebunit/webdriver/WebDriverElementImpl.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Modified: trunk/jwebunit-webdriver-plugin/src/main/java/net/sourceforge/jwebunit/webdriver/WebDriverTestingEngineImpl.java
===================================================================
--- trunk/jwebunit-webdriver-plugin/src/main/java/net/sourceforge/jwebunit/webdriver/WebDriverTestingEngineImpl.java 2011-09-23 14:52:27 UTC (rev 923)
+++ trunk/jwebunit-webdriver-plugin/src/main/java/net/sourceforge/jwebunit/webdriver/WebDriverTestingEngineImpl.java 2011-09-23 14:56:50 UTC (rev 924)
@@ -18,12 +18,16 @@
*/
package net.sourceforge.jwebunit.webdriver;
-import org.apache.regexp.RE;
-import org.apache.regexp.RESyntaxException;
+import org.apache.http.conn.BasicManagedEntity;
-import com.gargoylesoftware.htmlunit.html.HtmlElement;
+import com.gargoylesoftware.htmlunit.util.NameValuePair;
-import com.gargoylesoftware.htmlunit.BrowserVersion;
+import java.io.ByteArrayInputStream;
+
+import com.gargoylesoftware.htmlunit.WebClient;
+import com.gargoylesoftware.htmlunit.WebResponse;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
@@ -33,6 +37,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
+import java.util.Random;
import java.util.Set;
import net.sourceforge.jwebunit.api.HttpHeader;
import net.sourceforge.jwebunit.api.IElement;
@@ -41,19 +46,39 @@
import net.sourceforge.jwebunit.exception.ExpectedJavascriptConfirmException;
import net.sourceforge.jwebunit.exception.ExpectedJavascriptPromptException;
import net.sourceforge.jwebunit.exception.TestingEngineResponseException;
+import net.sourceforge.jwebunit.html.Cell;
+import net.sourceforge.jwebunit.html.Row;
import net.sourceforge.jwebunit.html.Table;
import net.sourceforge.jwebunit.javascript.JavascriptAlert;
import net.sourceforge.jwebunit.javascript.JavascriptConfirm;
import net.sourceforge.jwebunit.javascript.JavascriptPrompt;
import net.sourceforge.jwebunit.util.TestContext;
+import org.apache.commons.io.input.ProxyInputStream;
+import org.apache.http.Header;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpException;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpRequestInterceptor;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpResponseInterceptor;
+import org.apache.http.HttpStatus;
+import org.apache.http.entity.HttpEntityWrapper;
+import org.apache.http.message.BasicHttpResponse;
+import org.apache.http.protocol.HttpContext;
+import org.apache.regexp.RE;
+import org.apache.regexp.RESyntaxException;
+import org.browsermob.proxy.ProxyServer;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.NoSuchWindowException;
+import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
+import org.openqa.selenium.remote.CapabilityType;
+import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.Select;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -70,47 +95,234 @@
* Logger for this class.
*/
private final Logger logger = LoggerFactory.getLogger(WebDriverTestingEngineImpl.class);
+ private ProxyServer proxyServer;
private WebDriver driver;
private TestContext testContext;
+ private static final int TRY_COUNT = 50;
+ private static final int DEFAULT_PORT = 8183;
+ private static final Random RANDOM = new Random();
+ private HttpResponse response;
+ private BasicBufferedHttpEntity entity;
// The xpath string that identifie the current form
// ie : @name='myForm'
private String formIdent;
+ private boolean throwExceptionOnScriptError = true;//TODO
+ private boolean ignoreFailingStatusCodes = false;
+ private Map<String, String> requestHeaders;
+ /**
+ * Is Javascript enabled.
+ */
+ private boolean jsEnabled = true;
public WebDriverTestingEngineImpl() {
}
public void beginAt(URL aInitialURL, TestContext aTestContext) throws TestingEngineResponseException {
this.setTestContext(aTestContext);
- driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_3);
- ((HtmlUnitDriver) driver).setJavascriptEnabled(true);
+ // start the proxy
+ Proxy proxy = startBrowserMobProxy();
+ DesiredCapabilities capabilities = new DesiredCapabilities();
+ capabilities.setCapability(CapabilityType.PROXY, proxy);
+ capabilities.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, jsEnabled);
+ capabilities.setBrowserName("htmlunit");
+ capabilities.setVersion("firefox");
+
+ driver = new FixedHtmlUnitDriver(capabilities);
//Reset form
formIdent = null;
+ // Deal with cookies
+ for (javax.servlet.http.Cookie c : aTestContext.getCookies()) {
+ //FIXME Hack for BrowserMob
+ String domain = c.getDomain();
+ if ("localhost".equals(domain)) {
+ domain = null;
+ }
+ if ("".equals(domain)) {
+ domain = null;
+ }
+ Date expiry = null;
+ if (c.getMaxAge() != -1) {
+ Calendar cal = Calendar.getInstance();
+ cal.add(Calendar.SECOND, c.getMaxAge());
+ expiry = cal.getTime();
+ }
+ driver.manage().addCookie(
+ new org.openqa.selenium.Cookie(c
+ .getName(), c.getValue(), domain, c.getPath() != null ? c
+ .getPath() : "", expiry, c.getSecure()));
+ }
+ // Deal with custom request header
+ this.requestHeaders = aTestContext.getRequestHeaders();
+
gotoPage(aInitialURL);
}
+
+ private Proxy startBrowserMobProxy() {
+ for (int i = 1; i <= TRY_COUNT; i++) {
+ int port = getRandomPort();
+ proxyServer = new ProxyServer(port);
+ try {
+ proxyServer.start();
+ proxyServer.addResponseInterceptor(new HttpResponseInterceptor() {
+ public void process(HttpResponse response, HttpContext context) throws HttpException, IOException {
+ WebDriverTestingEngineImpl.this.response = response;
+ if (response instanceof BasicHttpResponse) {
+ BasicHttpResponse basicResponse = (BasicHttpResponse) response;
+ WebDriverTestingEngineImpl.this.entity = new BasicBufferedHttpEntity(response.getEntity());
+ basicResponse.setEntity(WebDriverTestingEngineImpl.this.entity);
+ }
+ else {
+ logger.error("Response is of type {}. Impossible to buffer content.", response.getClass().getSimpleName());
+ }
+ }
+ });
+ proxyServer.addRequestInterceptor(new HttpRequestInterceptor() {
+ public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
+ for (Map.Entry<String, String> requestHeader : requestHeaders.entrySet()) {
+ request.addHeader(requestHeader.getKey(), requestHeader.getValue());
+ }
+ }
+ });
+ return proxyServer.seleniumProxy();
+ }
+ catch (Exception e) {
+ if (i<TRY_COUNT) {
+ logger.error("Error while starting BrowserMob proxy. Retry...", e);
+ continue;
+ }
+ }
+ }
+ throw new RuntimeException("Unable to start BrowserMob proxy after " + TRY_COUNT + " retries");
+ }
+
+ private class BasicBufferedHttpEntity extends HttpEntityWrapper {
+
+ private BufferedInputStream bis;
+ public BasicBufferedHttpEntity(HttpEntity wrapped) {
+ super(wrapped);
+ }
+
+ @Override
+ public InputStream getContent() throws IOException {
+ if (bis == null) {
+ bis = new BufferedInputStream(super.getContent());
+ }
+ return bis;
+ }
+
+ public byte[] getBufferedContent() {
+ return bis.getBuffer();
+ }
+
+ }
+
+ private class BufferedInputStream extends ProxyInputStream {
+
+ private ByteArrayOutputStream bos = new ByteArrayOutputStream();
+
+ public BufferedInputStream(InputStream proxy) {
+ super(proxy);
+ }
+
+
+ public byte[] getBuffer() {
+ return bos.toByteArray();
+ }
+
+ @Override
+ public int read() throws IOException {
+ int b = super.read();
+ if (b != -1) {
+ bos.write(b);
+ }
+ return b;
+ }
+
+ @Override
+ public int read(byte[] array) throws IOException {
+ int len = super.read(array);
+ if (len > 0) {
+ bos.write(array, 0, len);
+ }
+ return len;
+ }
+
+ @Override
+ public int read(byte[] array, int off, int len) throws IOException {
+ int len2 = super.read(array, off, len);
+ if (len2 > 0) {
+ bos.write(array, 0, len2);
+ }
+ return len2;
+ }
+
+ }
+
+ private static int getRandomPort() {
+ synchronized (RANDOM) {
+ return DEFAULT_PORT + RANDOM.nextInt(1000);
+ }
+ }
+
public void setTestContext(TestContext testContext) {
this.testContext = testContext;
}
public void closeBrowser() throws ExpectedJavascriptAlertException, ExpectedJavascriptConfirmException, ExpectedJavascriptPromptException {
formIdent = null;
- driver.close();
+ if (driver != null) {
+ driver.quit();
+ driver = null;
+ }
+ if (proxyServer != null) {
+ try {
+ proxyServer.stop();
+ proxyServer = null;
+ }
+ catch (Exception e) {
+ logger.error("Error while stopping proxy", e);
+ throw new RuntimeException("Error while stopping proxy", e);
+ }
+ }
}
public void gotoPage(URL url) throws TestingEngineResponseException {
formIdent = null;
- driver.get(url.toString());
+ //Big hack for browsermob
+ String urlStr = url.toString().replace("http://localhost", "http://127.0.0.1");
+ driver.get(urlStr);
+ throwFailingHttpStatusCodeExceptionIfNecessary(
+ response.getStatusLine().getStatusCode(), urlStr);
}
+
+ /**
+ * Copied from {@link WebClient#throwFailingHttpStatusCodeExceptionIfNecessary(WebResponse)}
+ *
+ * @param webResponse
+ */
+ private void throwFailingHttpStatusCodeExceptionIfNecessary(int statusCode, String url) {
+ final boolean successful = (statusCode >= HttpStatus.SC_OK && statusCode < HttpStatus.SC_MULTIPLE_CHOICES)
+ || statusCode == HttpStatus.SC_USE_PROXY
+ || statusCode == HttpStatus.SC_NOT_MODIFIED;
+ if (!this.ignoreFailingStatusCodes && !successful) {
+ throw new TestingEngineResponseException(statusCode,
+ "unexpected status code [" + statusCode + "] at URL: [" + url + "]");
+ }
+ }
public void setScriptingEnabled(boolean value) {
- if (driver instanceof HtmlUnitDriver) {
+ // This variable is used to set Javascript before wc is instancied
+ jsEnabled = value;
+ if (driver != null && driver instanceof HtmlUnitDriver) {
((HtmlUnitDriver) driver).setJavascriptEnabled(value);
}
}
public void setThrowExceptionOnScriptError(boolean value) {
+ this.throwExceptionOnScriptError = true;
}
public List<javax.servlet.http.Cookie> getCookies() {
@@ -171,19 +383,21 @@
String current = driver.getWindowHandle();
for (String handle : driver.getWindowHandles()) {
driver.switchTo().window(handle);
- if (driver.getTitle().equals(title)) {
+ if (title.equals(driver.getTitle())) {
return;
}
}
driver.switchTo().window(current);
+ throw new RuntimeException("No window found with title [" + title + "]");
}
public void gotoWindow(int windowID) {
- throw new UnsupportedOperationException("Not supported yet.");
+ Set<String> handles = driver.getWindowHandles();
+ driver.switchTo().window(handles.toArray(new String[handles.size()])[windowID]);
}
public void gotoRootWindow() {
- throw new UnsupportedOperationException("Not supported yet.");
+ driver.switchTo().defaultContent();
}
public int getWindowCount() {
@@ -192,11 +406,18 @@
public void closeWindow() {
formIdent = null;
- driver.close();
+ driver.close();//FIXME Issue 1466
}
public boolean hasFrame(String frameNameOrId) {
- throw new UnsupportedOperationException("Not supported yet.");
+ List<WebElement> frameset = driver.findElements(By.tagName("frame"));
+ for (WebElement frame : frameset) {
+ if (frameNameOrId.equals(frame.getAttribute("name"))
+ || frameNameOrId.equals(frame.getAttribute("id"))) {
+ return true;
+ }
+ }
+ return false;
}
public void gotoFrame(String frameNameOrId) {
@@ -209,7 +430,7 @@
public void setWorkingForm(String nameOrId, int index) {
if (nameOrId != null) {
- formIdent = "(@name='" + nameOrId + "' or @id='" + nameOrId + "')][position()="
+ formIdent = "(@name=" + escapeQuotes(nameOrId) + " or @id=" + escapeQuotes(nameOrId) + ")][position()="
+ (index + 1);
} else {
formIdent = null;
@@ -228,20 +449,20 @@
}
public boolean hasForm(String nameOrID) {
- return hasElementByXPath("//form[@name='" + nameOrID + "' or @id='" + nameOrID + "']");
+ return hasElementByXPath("//form[@name=" + escapeQuotes(nameOrID) + " or @id=" + escapeQuotes(nameOrID) + "]");
}
public boolean hasFormParameterNamed(String paramName) {
- return hasElementByXPath(formSelector() + "//*[@name='" + paramName + "']");
+ return getWebElementByXPath("//*[@name=" + escapeQuotes(paramName) + "]", false, true) != null;
}
- private WebElement getWebElementByXPath(String xpathAfterForm, boolean onlyInCurrentForm) {
+ private WebElement getWebElementByXPath(String xpathAfterForm, boolean searchOnlyInCurrentForm, boolean overrideWorkingForm) {
//First try the current form
if (formIdent != null) {
try {
return driver.findElement(By.xpath("//form[" + formIdent + "]" + xpathAfterForm));
} catch (NoSuchElementException ex) {
- if (onlyInCurrentForm) {
+ if (searchOnlyInCurrentForm) {
return null;
}
}
@@ -252,7 +473,9 @@
for (WebElement f : forms) {
try {
WebElement e = driver.findElement(By.xpath("//form[position()=" + (index + 1) + "]" + xpathAfterForm));
- setWorkingForm(index);
+ if (overrideWorkingForm) {
+ setWorkingForm(index);
+ }
return e;
} catch (NoSuchElementException ex) {
index ++;
@@ -275,29 +498,40 @@
}
public String getTextFieldValue(String paramName) {
- WebElement e = getWebElementByXPath("//input[@type='text' and @name='" + paramName + "']", false);
- return e.getValue();
+ WebElement e = getTextField(paramName);
+ return e.getAttribute("value");
}
public String getHiddenFieldValue(String paramName) {
- WebElement e = getWebElementByXPath("//input[@type='hidden' and @name='" + paramName + "']", false);
- return e.getValue();
+ WebElement e = getWebElementByXPath("//input[@type='hidden' and @name=" + escapeQuotes(paramName) + "]", false, true);
+ return e.getAttribute("value");
}
public void setTextField(String inputName, String text) {
- WebElement e = getWebElementByXPath("//input[@type='text' and @name='" + inputName + "']", false);
+ WebElement e = getTextField(inputName);
+ e.clear();
+ e.sendKeys(text);
+ }
+
+ /**
+ * Look for any text field (input text, input password, textarea, file input).
+ */
+ private WebElement getTextField(String paramName) {
+ WebElement e = getWebElementByXPath("//input[@type='text' and @name=" + escapeQuotes(paramName) + "]", false, true);
if (e == null) {
- e = getWebElementByXPath("//textarea[@name='" + inputName + "']", false);
+ e = getWebElementByXPath("//textarea[@name=" + escapeQuotes(paramName) + "]", false, true);
}
if (e == null) {
- e = getWebElementByXPath("//input[@type='file' and @name='" + inputName + "']", false);
+ e = getWebElementByXPath("//input[@type='file' and @name=" + escapeQuotes(paramName) + "]", false, true);
}
- e.clear();
- e.sendKeys(text);
+ if (e == null) {
+ e = getWebElementByXPath("//input[@type='password' and @name=" + escapeQuotes(paramName) + "]", false, true);
+ }
+ return e;
}
public void setHiddenField(String inputName, String text) {
- WebElement e = getWebElementByXPath("//input[@type='hidden' and @name='" + inputName + "']", false);
+ WebElement e = getWebElementByXPath("//input[@type='hidden' and @name=" + escapeQuotes(inputName) + "]", false, true);
((JavascriptExecutor)driver).executeScript("arguments[0].value=" + escapeQuotes(text), e);
}
@@ -306,10 +540,10 @@
}
public String[] getSelectOptionValues(String selectName, int index) {
- Select select = new Select(getWebElementByXPath("//select[@name='" + selectName + "' and position()=" + (index + 1) + "]", true));
+ Select select = new Select(getWebElementByXPath("//select[@name=" + escapeQuotes(selectName) + " and position()=" + (index + 1) + "]", true, true));
ArrayList<String> result = new ArrayList<String>();
for (WebElement opt : select.getOptions()) {
- result.add(opt.getValue());
+ result.add(opt.getAttribute("value"));
}
return result.toArray(new String[result.size()]);
}
@@ -319,23 +553,24 @@
}
private String[] getSelectedOptions(Select select) {
+ //FIXME http://code.google.com/p/selenium/issues/detail?id=2295
String[] result = new String[select.getAllSelectedOptions().size()];
int i = 0;
for (WebElement opt : select.getAllSelectedOptions()) {
- result[i++] = opt.getValue();
+ result[i++] = opt.getAttribute("value");
}
return result;
}
public String[] getSelectedOptions(String selectName, int index) {
- Select select = new Select(getWebElementByXPath("//select[@name='" + selectName + "' and position()=" + (index + 1) + "]", true));
+ Select select = new Select(getWebElementByXPath("//select[@name='" + selectName + "' and position()=" + (index + 1) + "]", true, true));
return getSelectedOptions(select);
}
private String getSelectOptionValueForLabel(Select select, String label) {
for (WebElement opt : select.getOptions()) {
if (opt.getText().equals(label)) {
- return opt.getValue();
+ return opt.getAttribute("value");
}
}
throw new RuntimeException("Unable to find option " + label);
@@ -343,7 +578,7 @@
private String getSelectOptionLabelForValue(Select select, String value) {
for (WebElement opt : select.getOptions()) {
- if (opt.getValue().equals(value)) {
+ if (opt.getAttribute("value").equals(value)) {
return opt.getText();
}
}
@@ -351,22 +586,22 @@
}
public String getSelectOptionLabelForValue(String selectName, String optionValue) {
- Select select = new Select(getWebElementByXPath("//select[@name='" + selectName + "']", true));
+ Select select = new Select(getWebElementByXPath("//select[@name='" + selectName + "']", true, true));
return getSelectOptionLabelForValue(select, optionValue);
}
public String getSelectOptionLabelForValue(String selectName, int index, String optionValue) {
- Select select = new Select(getWebElementByXPath("//select[@name='" + selectName + "' and position()=" + (index + 1) + "]", true));
+ Select select = new Select(getWebElementByXPath("//select[@name='" + selectName + "' and position()=" + (index + 1) + "]", true, true));
return getSelectOptionLabelForValue(select, optionValue);
}
public String getSelectOptionValueForLabel(String selectName, String optionLabel) {
- Select select = new Select(getWebElementByXPath("//select[@name='" + selectName + "']", true));
+ Select select = new Select(getWebElementByXPath("//select[@name='" + selectName + "']", true, true));
return getSelectOptionValueForLabel(select, optionLabel);
}
public String getSelectOptionValueForLabel(String selectName, int index, String optionLabel) {
- Select select = new Select(getWebElementByXPath("//select[@name='" + selectName + "' and position()=" + (index + 1) + "]", true));
+ Select select = new Select(getWebElementByXPath("//select[@name='" + selectName + "' and position()=" + (index + 1) + "]", true, true));
return getSelectOptionValueForLabel(select, optionLabel);
}
@@ -375,13 +610,13 @@
}
public void selectOptions(String selectName, int index, String[] optionValues) {
- Select select = new Select(getWebElementByXPath("//select[@name='" + selectName + "' and position()=" + (index + 1) + "]", true));
+ Select select = new Select(getWebElementByXPath("//select[@name='" + selectName + "' and position()=" + (index + 1) + "]", true, true));
if (!select.isMultiple() && optionValues.length > 1)
throw new RuntimeException("Multiselect not enabled");
for (String option : optionValues) {
boolean found = false;
for (WebElement opt : select.getOptions()) {
- if (opt.getValue().equals(option)) {
+ if (opt.getAttribute("value").equals(option)) {
select.selectByValue(option);
found = true;
break;
@@ -399,13 +634,13 @@
}
public void unselectOptions(String selectName, int index, String[] optionValues) {
- Select select = new Select(getWebElementByXPath("//select[@name='" + selectName + "' and position()=" + (index + 1) + "]", true));
+ Select select = new Select(getWebElementByXPath("//select[@name='" + selectName + "' and position()=" + (index + 1) + "]", true, true));
if (!select.isMultiple() && optionValues.length > 1)
throw new RuntimeException("Multiselect not enabled");
for (String option : optionValues) {
boolean found = false;
for (WebElement opt : select.getOptions()) {
- if (opt.getValue().equals(option)) {
+ if (opt.getAttribute("value").equals(option)) {
select.deselectByValue(option);
found = true;
break;
@@ -427,7 +662,7 @@
}
public boolean hasSelectOption(String selectName, int index, String optionLabel) {
- Select select = new Select(getWebElementByXPath("//select[@name='" + selectName + "' and position()=" + (index + 1) + "]", true));
+ Select select = new Select(getWebElementByXPath("//select[@name=" + escapeQuotes(selectName) + " and position()=" + (index + 1) + "]", true, true));
for (WebElement opt : select.getOptions()) {
if (opt.getText().equals(optionLabel)) {
return true;
@@ -437,9 +672,9 @@
}
public boolean hasSelectOptionValue(String selectName, int index, String optionValue) {
- Select select = new Select(getWebElementByXPath("//select[@name='" + selectName + "' and position()=" + (index + 1) + "]", true));
+ Select select = new Select(getWebElementByXPath("//select[@name=" + escapeQuotes(selectName) + " and position()=" + (index + 1) + "]", true, true));
for (WebElement opt : select.getOptions()) {
- if (opt.getValue().equals(optionValue)) {
+ if (opt.getAttribute("value").equals(optionValue)) {
return true;
}
}
@@ -447,125 +682,131 @@
}
public boolean isCheckboxSelected(String checkBoxName) {
- WebElement e = getWebElementByXPath("//input[@type='checkbox' and @name='" + checkBoxName + "']", true);
+ WebElement e = getWebElementByXPath("//input[@type='checkbox' and @name=" + escapeQuotes(checkBoxName) + "]", true, true);
return e.isSelected();
}
public boolean isCheckboxSelected(String checkBoxName, String checkBoxValue) {
- WebElement e = getWebElementByXPath("//input[@type='checkbox' and @name='" + checkBoxName + "' and @value='" + checkBoxValue + "']", true);
+ WebElement e = getWebElementByXPath("//input[@type='checkbox' and @name=" + escapeQuotes(checkBoxName) + " and @value=" + escapeQuotes(checkBoxValue) + "]", true, true);
return e.isSelected();
}
public void checkCheckbox(String checkBoxName) {
- WebElement e = getWebElementByXPath("//input[@type='checkbox' and @name='" + checkBoxName + "']", true);
+ WebElement e = getWebElementByXPath("//input[@type='checkbox' and @name=" + escapeQuotes(checkBoxName) + "]", true, true);
if (!e.isSelected()) {
- e.toggle();
+ e.click();
}
}
public void checkCheckbox(String checkBoxName, String checkBoxValue) {
- WebElement e = getWebElementByXPath("//input[@type='checkbox' and @name='" + checkBoxName + "' and @value='" + checkBoxValue + "']", true);
+ WebElement e = getWebElementByXPath("//input[@type='checkbox' and @name=" + escapeQuotes(checkBoxName) + " and @value=" + escapeQuotes(checkBoxValue) + "]", true, true);
if (!e.isSelected()) {
- e.toggle();
+ e.click();
}
}
public void uncheckCheckbox(String checkBoxName) {
- WebElement e = getWebElementByXPath("//input[@type='checkbox' and @name='" + checkBoxName + "']", true);
+ WebElement e = getWebElementByXPath("//input[@type='checkbox' and @name=" + escapeQuotes(checkBoxName) + "]", true, true);
if (e.isSelected()) {
- e.toggle();
+ e.click();
}
}
public void uncheckCheckbox(String checkBoxName, String value) {
- WebElement e = getWebElementByXPath("//input[@type='checkbox' and @name='" + checkBoxName + "' and @value='" + value + "']", true);
+ WebElement e = getWebElementByXPath("//input[@type='checkbox' and @name=" + escapeQuotes(checkBoxName) + " and @value=" + escapeQuotes(value) + "]", true, true);
if (e.isSelected()) {
- e.toggle();
+ e.click();
}
}
public void clickRadioOption(String radioGroup, String radioOptionValue) {
- WebElement e = getWebElementByXPath("//input[@type='radio' and @name='" + radioGroup + "' and @value='" + radioOptionValue + "']", false);
+ WebElement e = getWebElementByXPath("//input[@type='radio' and @name=" + escapeQuotes(radioGroup) + " and @value=" + escapeQuotes(radioOptionValue) + "]", false, true);
e.click();
}
public boolean hasRadioOption(String radioGroup, String radioOptionValue) {
- WebElement e = getWebElementByXPath("//input[@type='radio' and @name='" + radioGroup + "' and @value='" + radioOptionValue + "']", false);
+ WebElement e = getWebElementByXPath("//input[@type='radio' and @name=" + escapeQuotes(radioGroup) + " and @value=" + escapeQuotes(radioOptionValue) + "]", false, true);
return e != null;
}
public String getSelectedRadio(String radioGroup) {
- List<WebElement> radios = getWebElementsByXPath("/input[@type='radio' and @name='" + radioGroup + "']");
+ List<WebElement> radios = getWebElementsByXPath("/input[@type='radio' and @name=" + escapeQuotes(radioGroup) + "]");
for (WebElement r : radios) {
if (r.isSelected()) {
- return r.getValue();
+ return r.getAttribute("value");
}
}
return null;
}
public boolean hasSubmitButton() {
- return (getWebElementByXPath("//input[@type='submit' or @type='image']", true) != null) || (getWebElementByXPath("//button[@type='submit']", false) != null);
+ return (getWebElementByXPath("//input[@type='submit' or @type='image']", true, true) != null) || (getWebElementByXPath("//button[@type='submit']", false, true) != null);
}
public boolean hasSubmitButton(String nameOrID) {
- return (getWebElementByXPath("//input[(@type='submit' or @type='image') and (@name='" + nameOrID + "' or @id='" + nameOrID + "')]", true) != null)
- || (getWebElementByXPath("//button[@type='submit' and (@name='" + nameOrID + "' or @id='" + nameOrID + "')]", true) != null);
+ return (getWebElementByXPath("//input[(@type='submit' or @type='image') and (@name=" + escapeQuotes(nameOrID) + " or @id=" + nameOrID + ")]", true, true) != null)
+ || (getWebElementByXPath("//button[@type='submit' and (@name=" + escapeQuotes(nameOrID) + " or @id=" + escapeQuotes(nameOrID) + ")]", true, true) != null);
}
public boolean hasSubmitButton(String nameOrID, String value) {
- return (getWebElementByXPath("//input[(@type='submit' or @type='image') and (@name='" + nameOrID + "' or @id='" + nameOrID + "') and @value='" + value + "']", true) != null)
- || (getWebElementByXPath("//button[@type='submit' and (@name='" + nameOrID + "' or @id='" + nameOrID + "') and @value='" + value + "']", true) != null);
+ return (getWebElementByXPath("//input[(@type='submit' or @type='image') and (@name=" + escapeQuotes(nameOrID) + " or @id=" + escapeQuotes(nameOrID) + ") and @value=" + escapeQuotes(value) + "]", true, true) != null)
+ || (getWebElementByXPath("//button[@type='submit' and (@name=" + escapeQuotes(nameOrID) + " or @id=" + escapeQuotes(nameOrID) + ") and @value=" + escapeQuotes(value) + "]", true, true) != null);
}
public void submit() {
- WebElement e = getWebElementByXPath("//input[@type='submit' or @type='image']", true);
+ WebElement e = getWebElementByXPath("//input[@type='submit' or @type='image']", true, true);
if (e == null) {
- e = getWebElementByXPath("//button[@type='submit']", true);
+ e = getWebElementByXPath("//button[@type='submit']", true, true);
}
e.submit();
+ throwFailingHttpStatusCodeExceptionIfNecessary(
+ response.getStatusLine().getStatusCode(), driver.getCurrentUrl());
}
public void submit(String nameOrID) {
- WebElement e = getWebElementByXPath("//input[(@type='submit' or @type='image') and (@name='" + nameOrID + "' or @id='" + nameOrID + "')]", true);
+ WebElement e = getWebElementByXPath("//input[(@type='submit' or @type='image') and (@name=" + escapeQuotes(nameOrID) + " or @id=" + escapeQuotes(nameOrID) + ")]", true, true);
if (e == null) {
- e = getWebElementByXPath("//button[@type='submit' and (@name='" + nameOrID + "' or @id='" + nameOrID + "')]", true);
+ e = getWebElementByXPath("//button[@type='submit' and (@name=" + escapeQuotes(nameOrID) + " or @id=" + escapeQuotes(nameOrID) + ")]", true, true);
}
e.submit();
+ throwFailingHttpStatusCodeExceptionIfNecessary(
+ response.getStatusLine().getStatusCode(), driver.getCurrentUrl());
}
public void submit(String buttonName, String buttonValue) {
- WebElement e = getWebElementByXPath("//input[(@type='submit' or @type='image') and (@name='" + buttonName + "' or @id='" + buttonName + "') and @value='" + buttonValue + "']", true);
+ WebElement e = getWebElementByXPath("//input[(@type='submit' or @type='image') and (@name=" + escapeQuotes(buttonName) + " or @id=" + escapeQuotes(buttonName) + ") and @value=" + escapeQuotes(buttonValue) + "]", true, true);
if (e == null) {
- e = getWebElementByXPath("//button[@type='submit' and (@name='" + buttonName + "' or @id='" + buttonName + "') and @value='" + buttonValue + "']", true);
+ e = getWebElementByXPath("//button[@type='submit' and (@name=" + escapeQuotes(buttonName) + " or @id=" + escapeQuotes(buttonName) + ") and @value=" + escapeQuotes(buttonValue) + "]", true, true);
}
e.submit();
+ throwFailingHttpStatusCodeExceptionIfNecessary(
+ response.getStatusLine().getStatusCode(), driver.getCurrentUrl());
}
public boolean hasResetButton() {
- return getWebElementByXPath("//input[@type='reset']", false) != null;
+ return getWebElementByXPath("//input[@type='reset']", false, true) != null;
}
public boolean hasResetButton(String nameOrID) {
- return getWebElementByXPath("//input[@type='reset' and (@name='" + nameOrID + "' or @id='" + nameOrID + "')]", true) != null;
+ return getWebElementByXPath("//input[@type='reset' and (@name=" + escapeQuotes(nameOrID) + " or @id=" + escapeQuotes(nameOrID) + ")]", true, true) != null;
}
public void reset() {
- getWebElementByXPath("//input[@type='reset']", true).click();;
+ getWebElementByXPath("//input[@type='reset']", true, true).click();;
}
private WebElement getButton(String nameOrID) {
- WebElement e = getWebElementByXPath("//input[(@type='submit' or @type='image' or @type='reset' or @type='button') and (@name='" + nameOrID + "' or @id='" + nameOrID + "')]", false);
+ WebElement e = getWebElementByXPath("//input[(@type='submit' or @type='image' or @type='reset' or @type='button') and (@name=" + escapeQuotes(nameOrID) + " or @id=" + escapeQuotes(nameOrID) + ")]", false, true);
if (e == null) {
- e = getWebElementByXPath("//button[@type='submit' and (@name='" + nameOrID + "' or @id='" + nameOrID + "')]", false);
+ e = getWebElementByXPath("//button[@type='submit' and (@name=" + escapeQuotes(nameOrID) + " or @id=" + escapeQuotes(nameOrID) + ")]", false, true);
}
return e;
}
private WebElement getButtonWithText(String text) {
- WebElement e = getWebElementByXPath("//input[(@type='submit' or @type='reset' or @type='button') and contains(@value," + escapeQuotes(text) + ")]", false);
+ WebElement e = getWebElementByXPath("//input[(@type='submit' or @type='reset' or @type='button') and contains(@value," + escapeQuotes(text) + ")]", false, true);
if (e == null) {
- e = getWebElementByXPath("//button[contains(.," + escapeQuotes(text) + ")]", false);
+ e = getWebElementByXPath("//button[contains(.," + escapeQuotes(text) + ")]", false, true);
}
return e;
}
@@ -595,11 +836,32 @@
}
public String getPageText() {
- return driver.findElement(By.xpath("//body")).getText();
+ try {
+ return driver.findElement(By.xpath("//body")).getText();
+ } catch (Exception e) {
+ //Maybe this is not HTML
+ return driver.getPageSource();
+ }
}
public String getPageSource() {
- return driver.getPageSource();
+ String encoding = "ISO-8859-1";
+ if (response.getEntity().getContentEncoding() != null) {
+ encoding = response.getEntity().getContentEncoding().getValue();
+ }
+
+ if (response.getEntity() instanceof BasicBufferedHttpEntity) {
+ try {
+ return new String(((BasicBufferedHttpEntity) response.getEntity()).getBufferedContent(),
+ encoding);
+ }
+ catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ else {
+ return driver.getPageSource();
+ }
}
public String getPageTitle() {
@@ -611,6 +873,9 @@
}
public InputStream getInputStream() {
+ if (entity != null) {
+ return new ByteArrayInputStream(entity.getBufferedContent());
+ }
throw new UnsupportedOperationException("Not supported yet.");
}
@@ -619,12 +884,55 @@
}
public boolean hasTable(String tableSummaryNameOrId) {
- throw new UnsupportedOperationException("Not supported yet.");
+ return getHtmlTable(tableSummaryNameOrId) != null;
}
public Table getTable(String tableSummaryNameOrId) {
- throw new UnsupportedOperationException("Not supported yet.");
+ WebElement table = getHtmlTable(tableSummaryNameOrId);
+ Table result = new Table();
+ List<WebElement> trs = table.findElements(By.xpath("tr | tbody/tr"));
+ for (int i = 0; i < trs.size(); i++) {
+ Row newRow = new Row();
+ WebElement tr = trs.get(i);
+ List<WebElement> tds = tr.findElements(By.xpath("td | th"));
+ for (WebElement td : tds) {
+ int colspan;
+ try {
+ colspan = Integer.valueOf(td.getAttribute("colspan"));
+ }
+ catch (NumberFormatException e) {
+ colspan = 1;
+ }
+ int rowspan;
+ try {
+ rowspan = Integer.valueOf(td.getAttribute("rowspan"));
+ }
+ catch (NumberFormatException e) {
+ rowspan = 1;
+ }
+ newRow.appendCell(new Cell(td.getText(),
+ colspan,
+ rowspan));
+ }
+ result.appendRow(newRow);
+ }
+ return result;
}
+
+ /**
+ * Return the Webdriver WebElement object representing a specified table in the current response. Null is returned if a
+ * parsing exception occurs looking for the table or no table with the id or summary could be found.
+ *
+ * @param tableSummaryOrId summary or id of the table to return.
+ */
+ private WebElement getHtmlTable(String tableSummaryOrId) {
+ try {
+ return driver.findElement(By.xpath("(//table[@id="
+ + escapeQuotes(tableSummaryOrId) + " or @summary=" + escapeQuotes(tableSummaryOrId) + "])"));
+ } catch (NoSuchElementException e) {
+ return null;
+ }
+ }
private WebElement getLinkWithImage(String filename, int index) {
try {
@@ -679,11 +987,21 @@
}
public void clickLinkWithText(String linkText, int index) {
- throw new UnsupportedOperationException("Not supported yet.");
+ WebElement link = getLinkWithText(linkText, index);
+ if (link == null) {
+ throw new RuntimeException("No Link found for \"" + linkText
+ + "\" with index " + index);
+ }
+ link.click();
}
public void clickLinkWithExactText(String linkText, int index) {
- throw new UnsupportedOperationException("Not supported yet.");
+ WebElement link = getLinkWithExactText(linkText, index);
+ if (link == null) {
+ throw new RuntimeException("No Link found for \"" + linkText
+ + "\" with index " + index);
+ }
+ link.click();
}
public void clickLink(String anID) {
@@ -691,9 +1009,14 @@
}
public void clickLinkWithImage(String imageFileName, int index) {
- throw new UnsupportedOperationException("Not supported yet.");
+ WebElement link = getLinkWithImage(imageFileName, index);
+ if (link == null) {
+ throw new RuntimeException("No Link found with filename \""
+ + imageFileName + "\" and index " + index);
+ }
+ link.click();
}
-
+
public boolean hasElement(String anID) {
try {
driver.findElement(By.id(anID));
@@ -759,7 +1082,9 @@
} catch (RESyntaxException e) {
throw new RuntimeException(e);
}
- } public void setExpectedJavaScriptAlert(JavascriptAlert[] alerts) throws ExpectedJavascriptAlertException {
+ }
+
+ public void setExpectedJavaScriptAlert(JavascriptAlert[] alerts) throws ExpectedJavascriptAlertException {
throw new UnsupportedOperationException("Not supported yet.");
}
@@ -772,31 +1097,50 @@
}
public IElement getElementByXPath(String xpath) {
- throw new UnsupportedOperationException("Not supported yet.");
+ try {
+ return new WebDriverElementImpl(driver.findElement(By.xpath(xpath)));
+ }
+ catch (NoSuchElementException e) {
+ return null;
+ }
}
public IElement getElementByID(String id) {
- throw new UnsupportedOperationException("Not supported yet.");
+ try {
+ return new WebDriverElementImpl(driver.findElement(By.id(id)));
+ }
+ catch (NoSuchElementException e) {
+ return null;
+ }
}
public List<IElement> getElementsByXPath(String xpath) {
- throw new UnsupportedOperationException("Not supported yet.");
+ List<IElement> result = new ArrayList<IElement>();
+ List<WebElement> elements = driver.findElements(By.xpath(xpath));
+ for (WebElement child : elements) {
+ result.add(new WebDriverElementImpl(child));
+ }
+ return result;
}
public int getServerResponseCode() {
- throw new UnsupportedOperationException("Not supported yet.");
+ return response.getStatusLine().getStatusCode();
}
public String getHeader(String name) {
- throw new UnsupportedOperationException("Not supported yet.");
+ return response.getHeaders(name).length>0?response.getHeaders(name)[0].getValue():null;
}
public Map<String, String> getAllHeaders() {
- throw new UnsupportedOperationException("Not supported yet.");
+ Map<String, String> map = new java.util.HashMap<String, String>();
+ for (Header header : response.getAllHeaders()) {
+ map.put(header.getName(), header.getValue());
+ }
+ return map;
}
public void setIgnoreFailingStatusCodes(boolean ignore) {
- throw new UnsupportedOperationException("Not supported yet.");
+ this.ignoreFailingStatusCodes = ignore;
}
public List<String> getComments() {
@@ -808,8 +1152,11 @@
}
public List<HttpHeader> getResponseHeaders() {
- // TODO Auto-generated method stub
- return null;
+ List<HttpHeader> result = new LinkedList<HttpHeader>();
+ for (Header header : response.getAllHeaders()) {
+ result.add(new HttpHeader(header.getName(), header.getValue()));
+ }
+ return result;
}
/**
Deleted: trunk/jwebunit-webdriver-plugin/src/main/java/net/sourceforge/jwebunit/webdriver/WebdriverElementImpl.java
===================================================================
--- trunk/jwebunit-webdriver-plugin/src/main/java/net/sourceforge/jwebunit/webdriver/WebdriverElementImpl.java 2011-09-23 14:52:27 UTC (rev 923)
+++ trunk/jwebunit-webdriver-plugin/src/main/java/net/sourceforge/jwebunit/webdriver/WebdriverElementImpl.java 2011-09-23 14:56:50 UTC (rev 924)
@@ -1,198 +0,0 @@
-/**
- * Copyright (c) 2010, JWebUnit team.
- *
- * This file is part of JWebUnit.
- *
- * JWebUnit is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * JWebUnit is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with JWebUnit. If not, see <http://www.gnu.org/licenses/>.
- */
-
-/**
- *
- */
-package net.sourceforge.jwebunit.webdriver;
-
-import org.openqa.selenium.JavascriptExecutor;
-
-import org.openqa.selenium.By;
-
-import org.openqa.selenium.WebElement;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import net.sourceforge.jwebunit.api.IElement;
-
-import com.gargoylesoftware.htmlunit.html.DomNode;
-import com.gargoylesoftware.htmlunit.html.HtmlElement;
-import com.gargoylesoftware.htmlunit.html.HtmlInput;
-import com.gargoylesoftware.htmlunit.html.HtmlTextArea;
-
-/**
- * Webdriver implementation of IElement wrapper.
- *
- * @author henryju
- *
- */
-public class WebdriverElementImpl implements IElement {
-
- /**
- * The wrapped element.
- */
- private WebElement element;
-
- public WebdriverElementImpl(WebElement element) {
- if (element == null)
- throw new NullPointerException("Cannot create an IElement for a null element.");
- this.element = element;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see net.sourceforge.jwebunit.api.IElement#attribute(java.lang.String)
- */
- public String getAttribute(String name) {
- return element.getAttribute(name);
- }
-
- /*
- * (non-Javadoc)
- *
- * @see net.sourceforge.jwebunit.api.IElement#name()
- */
- public String getName() {
- return element.getTagName();
- }
-
- /*
- * (non-Javadoc)
- *
- * @see net.sourceforge.jwebunit.api.IElement#getChildren()
- */
- public List<IElement> getChildren() {
- List<IElement> children = new ArrayList<IElement>();
- for (WebElement e : element.findElements(By.xpath("/"))) {
- if (e != null)
- children.add(new WebdriverElementImpl(e));
- }
- return children;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see net.sourceforge.jwebunit.api.IElement#getParent()
- */
- public IElement getParent() {
- throw new UnsupportedOperationException("Not supported yet.");
- }
-
- /*
- * (non-Javadoc)
- *
- * @see net.sourceforge.jwebunit.api.IElement#getTextContent()
- */
- public String getTextContent() {
- return element.getText();
- }
-
- /*
- * (non-Javadoc)
- *
- * @see net.sourceforge.jwebunit.api.IElement#getElement(java.lang.String)
- */
- public IElement getElement(String xpath) {
- return new WebdriverElementImpl((WebElement) element.findElement(By.xpath(xpath)));
- }
-
- /*
- * (non-Javadoc)
- *
- * @see net.sourceforge.jwebunit.api.IElement#getElements(java.lang.String)
- */
- public List<IElement> getElements(String xpath) {
- List<IElement> elements = new ArrayList<IElement>();
- for (WebElement o : element.findElements(By.xpath(xpath))) {
- elements.add(new WebdriverElementImpl(o));
- }
- return elemen...
[truncated message content] |