Skip to content

Commit

Permalink
"FileUploadTest" was the last test that needed to be self contained.
Browse files Browse the repository at this point in the history
Now, all tests dependencies/fixtures are self contained.
This is in preparation of making TravisCI run GhostDriver tests as
part of PhantomJS CI.
  • Loading branch information
detro committed Feb 3, 2014
1 parent df8b69d commit 6b25de5
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 49 deletions.
2 changes: 2 additions & 0 deletions test/java/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ repositories {
mavenCentral()
}

ext.commonsFileUploadVersion = "1.3"
ext.seleniumVersion = "2.39.0"
ext.junitVersion = "4.11"
ext.jettyVersion = "6.1.21"
Expand All @@ -23,6 +24,7 @@ dependencies {
testCompile "com.google.code.findbugs:jsr305:$jsr305Version"
testCompile "junit:junit-dep:$junitVersion"
testCompile "org.mortbay.jetty:jetty:$jettyVersion"
testCompile "commons-fileupload:commons-fileupload:$commonsFileUploadVersion"
testCompile files("../../binding/java/jars/phantomjsdriver-1.1.0.jar")
}

Expand Down
14 changes: 7 additions & 7 deletions test/java/src/test/java/ghostdriver/CookieTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private Cookie[] getCookies() {

@Test
public void gettingAllCookies() {
server.setGetHandler(COOKIE_SETTING_CALLBACK);
server.setHttpHandler("GET", COOKIE_SETTING_CALLBACK);
goToPage();
Cookie[] cookies = getCookies();

Expand All @@ -110,22 +110,22 @@ public void gettingAllCookies() {

@Test
public void gettingAllCookiesOnANonCookieSettingPage() {
server.setGetHandler(EMPTY_CALLBACK);
server.setHttpHandler("GET", EMPTY_CALLBACK);
goToPage();
assertEquals(0, getCookies().length);
}

@Test
public void deletingAllCookies() {
server.setGetHandler(COOKIE_SETTING_CALLBACK);
server.setHttpHandler("GET", COOKIE_SETTING_CALLBACK);
goToPage();
driver.manage().deleteAllCookies();
assertEquals(0, getCookies().length);
}

@Test
public void deletingOneCookie() {
server.setGetHandler(COOKIE_SETTING_CALLBACK);
server.setHttpHandler("GET", COOKIE_SETTING_CALLBACK);
goToPage();

driver.manage().deleteCookieNamed("test");
Expand All @@ -138,7 +138,7 @@ public void deletingOneCookie() {

@Test
public void addingACookie() {
server.setGetHandler(EMPTY_CALLBACK);
server.setHttpHandler("GET", EMPTY_CALLBACK);
goToPage();

driver.manage().addCookie(new Cookie("newCookie", "newValue"));
Expand All @@ -154,7 +154,7 @@ public void addingACookie() {

@Test
public void modifyingACookie() {
server.setGetHandler(COOKIE_SETTING_CALLBACK);
server.setHttpHandler("GET", COOKIE_SETTING_CALLBACK);
goToPage();

driver.manage().addCookie(new Cookie("test", "newValue", "localhost", "/", null, false));
Expand All @@ -176,7 +176,7 @@ public void modifyingACookie() {

@Test
public void shouldRetainCookieInfo() {
server.setGetHandler(EMPTY_CALLBACK);
server.setHttpHandler("GET", EMPTY_CALLBACK);
goToPage();

// Added cookie (in a sub-path - allowed)
Expand Down
8 changes: 4 additions & 4 deletions test/java/src/test/java/ghostdriver/ElementFindingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
public class ElementFindingTest extends BaseTestWithServer {
@Test
public void findChildElement() {
server.setGetHandler(new HttpRequestCallback() {
server.setHttpHandler("GET", new HttpRequestCallback() {
@Override
public void call(HttpServletRequest req, HttpServletResponse res) throws IOException {
res.getOutputStream().println("<div id=\"y-masthead\">" +
Expand All @@ -63,7 +63,7 @@ public void call(HttpServletRequest req, HttpServletResponse res) throws IOExcep

@Test
public void findChildElements() {
server.setGetHandler(new HttpRequestCallback() {
server.setHttpHandler("GET", new HttpRequestCallback() {
@Override
public void call(HttpServletRequest req, HttpServletResponse res) throws IOException {
res.getOutputStream().println("<div id=\"y-masthead\">" +
Expand All @@ -84,7 +84,7 @@ public void call(HttpServletRequest req, HttpServletResponse res) throws IOExcep

@Test
public void findMultipleElements() {
server.setGetHandler(new HttpRequestCallback() {
server.setHttpHandler("GET", new HttpRequestCallback() {
@Override
public void call(HttpServletRequest req, HttpServletResponse res) throws IOException {
res.getOutputStream().println("<div id=\"y-masthead\">" +
Expand Down Expand Up @@ -240,7 +240,7 @@ public void findElementsWithImplicitWait() {
@Test
public void findElementViaXpathLocator() {
// Define HTTP response for test
server.setGetHandler(new HttpRequestCallback() {
server.setHttpHandler("GET", new HttpRequestCallback() {
@Override
public void call(HttpServletRequest req, HttpServletResponse res) throws IOException {
ServletOutputStream out = res.getOutputStream();
Expand Down
32 changes: 16 additions & 16 deletions test/java/src/test/java/ghostdriver/ElementJQueryEventsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,26 +72,26 @@ public ElementJQueryEventsTest(String jQueryVersion) {
public void shouldBeAbleToClickAndEventsBubbleUpUsingJquery() {
final String buttonId = "clickme";

server.setGetHandler(new HttpRequestCallback() {
server.setHttpHandler("GET", new HttpRequestCallback() {
@Override
public void call(HttpServletRequest req, HttpServletResponse res) throws IOException {
res.getOutputStream().println(
"<html>\n" +
"<head>\n" +
"<script src=\"//ajax.googleapis.com/ajax/libs/jquery/"+ mJqueryVersion +"/jquery.min.js\"></script>\n" +
"<script type=\"text/javascript\">\n" +
" var clicked = false;" +
" $(document).ready(function() {" +
" $('#"+buttonId+"').bind('click', function(e) {" +
" clicked = true;" +
" });" +
" });\n" +
"</script>\n" +
"</head>\n" +
"<body>\n" +
" <a href='#' id='"+buttonId+"'>click me</a>\n" +
"</body>\n" +
"</html>");
"<head>\n" +
"<script src=\"//ajax.googleapis.com/ajax/libs/jquery/" + mJqueryVersion + "/jquery.min.js\"></script>\n" +
"<script type=\"text/javascript\">\n" +
" var clicked = false;" +
" $(document).ready(function() {" +
" $('#" + buttonId + "').bind('click', function(e) {" +
" clicked = true;" +
" });" +
" });\n" +
"</script>\n" +
"</head>\n" +
"<body>\n" +
" <a href='#' id='" + buttonId + "'>click me</a>\n" +
"</body>\n" +
"</html>");
}
});

Expand Down
6 changes: 3 additions & 3 deletions test/java/src/test/java/ghostdriver/ElementMethodsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public void shouldWaitForPossiblePageLoadOnlyWhenClickingOnSomeElement() {

@Test
public void shouldWaitForOnClickCallbackToFinishBeforeContinuing() {
server.setGetHandler(new HttpRequestCallback() {
server.setHttpHandler("GET", new HttpRequestCallback() {
@Override
public void call(HttpServletRequest req, HttpServletResponse res) throws IOException {
res.getOutputStream().println("<script type=\"text/javascript\">\n" +
Expand Down Expand Up @@ -173,7 +173,7 @@ public void call(HttpServletRequest req, HttpServletResponse res) throws IOExcep

@Test
public void shouldNotHandleCasesWhenAsyncJavascriptInitiatesAPageLoadFarInTheFuture() {
server.setGetHandler(new HttpRequestCallback() {
server.setHttpHandler("GET", new HttpRequestCallback() {
@Override
public void call(HttpServletRequest req, HttpServletResponse res) throws IOException {
res.getOutputStream().println("<script type=\"text/javascript\">\n" +
Expand Down Expand Up @@ -201,7 +201,7 @@ public void call(HttpServletRequest req, HttpServletResponse res) throws IOExcep
public void shouldHandleCasesWhereJavascriptCodeInitiatesPageLoadsThatFail() throws InterruptedException {
final String crazyUrl = "http://abcdefghilmnopqrstuvz.zvutsr";

server.setGetHandler(new HttpRequestCallback() {
server.setHttpHandler("GET", new HttpRequestCallback() {
@Override
public void call(HttpServletRequest req, HttpServletResponse res) throws IOException {
res.getOutputStream().println("<script type=\"text/javascript\">\n" +
Expand Down
4 changes: 2 additions & 2 deletions test/java/src/test/java/ghostdriver/ElementQueryingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void scrollElementIntoView() {

@Test
public void getTextFromDifferentLocationsOfDOMTree() {
server.setGetHandler(new HttpRequestCallback() {
server.setHttpHandler("GET", new HttpRequestCallback() {
@Override
public void call(HttpServletRequest req, HttpServletResponse res) throws IOException {
res.getOutputStream().println("<html><body>" +
Expand Down Expand Up @@ -117,7 +117,7 @@ public void call(HttpServletRequest req, HttpServletResponse res) throws IOExcep

@Test(expected = InvalidElementStateException.class)
public void throwExceptionWhenInteractingWithInvisibleElement() {
server.setGetHandler(new HttpRequestCallback() {
server.setHttpHandler("GET", new HttpRequestCallback() {
@Override
public void call(HttpServletRequest req, HttpServletResponse res) throws IOException {
res.getOutputStream().println("<!DOCTYPE html>" +
Expand Down
43 changes: 43 additions & 0 deletions test/java/src/test/java/ghostdriver/FileUploadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,23 @@

package ghostdriver;

import ghostdriver.server.HttpRequestCallback;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
Expand All @@ -55,6 +64,40 @@ public void checkFileUploadCompletes() throws IOException {
writer.write(FILE_HTML);
writer.close();

server.setHttpHandler("POST", new HttpRequestCallback() {
@Override
public void call(HttpServletRequest req, HttpServletResponse res) throws IOException {
if (ServletFileUpload.isMultipartContent(req) && req.getPathInfo().endsWith("/upload")) {
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory(1024, new File(System.getProperty("java.io.tmpdir")));

// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);

// Parse the request
List<FileItem> items;
try {
items = upload.parseRequest(req);
} catch (FileUploadException fue) {
throw new IOException(fue);
}

res.setHeader("Content-Type", "text/html; charset=UTF-8");
InputStream is = items.get(0).getInputStream();
OutputStream os = res.getOutputStream();
IOUtils.copy(is, os);

os.write("<script>window.top.window.onUploadDone();</script>".getBytes());

IOUtils.closeQuietly(is);
IOUtils.closeQuietly(os);
return;
}

res.sendError(400);
}
});

// Upload the temp file
d.get(server.getBaseUrl() + "/common/upload.html");
d.findElement(By.id("upload")).sendKeys(testFile.getAbsolutePath());
Expand Down
14 changes: 7 additions & 7 deletions test/java/src/test/java/ghostdriver/FrameSwitchingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public void call(HttpServletRequest req, HttpServletResponse res) throws IOExcep
}
}

server.setGetHandler(new SpecialHttpRequestCallback());
server.setHttpHandler("GET", new SpecialHttpRequestCallback());

d.get(server.getBaseUrl() + "/common/frameset.html");
assertEquals(expectedTitle, d.getTitle());
Expand All @@ -252,7 +252,7 @@ public void call(HttpServletRequest req, HttpServletResponse res) throws IOExcep
@Test
public void shouldSwitchBetweenNestedFrames() {
// Define HTTP response for test
server.setGetHandler(new HttpRequestCallback() {
server.setHttpHandler("GET", new HttpRequestCallback() {
@Override
public void call(HttpServletRequest req, HttpServletResponse res) throws IOException {
String pathInfo = req.getPathInfo();
Expand Down Expand Up @@ -317,7 +317,7 @@ public void call(HttpServletRequest req, HttpServletResponse res) throws IOExcep
@Test
public void shouldSwitchBetweenNestedFramesPickedViaWebElement() {
// Define HTTP response for test
server.setGetHandler(new HttpRequestCallback() {
server.setHttpHandler("GET", new HttpRequestCallback() {
@Override
public void call(HttpServletRequest req, HttpServletResponse res) throws IOException {
String pathInfo = req.getPathInfo();
Expand Down Expand Up @@ -381,7 +381,7 @@ public void call(HttpServletRequest req, HttpServletResponse res) throws IOExcep

@Test
public void shouldBeAbleToSwitchToIFrameThatHasNoNameNorId() {
server.setGetHandler(new HttpRequestCallback() {
server.setHttpHandler("GET", new HttpRequestCallback() {
@Override
public void call(HttpServletRequest req, HttpServletResponse res) throws IOException {
res.getOutputStream().println("<html>" +
Expand All @@ -404,7 +404,7 @@ public void shouldTimeoutWhileChangingIframeSource() {
final String iFrameId = "iframeId";

// Define HTTP response for test
server.setGetHandler(new HttpRequestCallback() {
server.setHttpHandler("GET", new HttpRequestCallback() {
@Override
public void call(HttpServletRequest req, HttpServletResponse res) throws IOException {
String pathInfo = req.getPathInfo();
Expand All @@ -417,10 +417,10 @@ public void call(HttpServletRequest req, HttpServletResponse res) throws IOExcep
// main page
out.println("<html>\n" +
"<body>\n" +
" <iframe id='"+iFrameId+"'></iframe>\n" +
" <iframe id='" + iFrameId + "'></iframe>\n" +
" <script>\n" +
" setTimeout(function() {\n" +
" document.getElementById('"+iFrameId+"').src='iframe_content.html';\n" +
" document.getElementById('" + iFrameId + "').src='iframe_content.html';\n" +
" }, 2000);\n" +
" </script>\n" +
"</body>\n" +
Expand Down
2 changes: 1 addition & 1 deletion test/java/src/test/java/ghostdriver/MouseCommandsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public void clickAndHold() {
@Test
public void handleClickWhenOnClickInlineCodeFails() {
// Define HTTP response for test
server.setGetHandler(new HttpRequestCallback() {
server.setHttpHandler("GET", new HttpRequestCallback() {
@Override
public void call(HttpServletRequest req, HttpServletResponse res) throws IOException {
res.getOutputStream().println("<html>" +
Expand Down
2 changes: 1 addition & 1 deletion test/java/src/test/java/ghostdriver/WindowHandlesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void enumerateWindowHandle() {

@Test
public void openPopupAndGetCurrentUrl() throws InterruptedException {
server.setGetHandler(new HttpRequestCallback() {
server.setHttpHandler("GET", new HttpRequestCallback() {
@Override
public void call(HttpServletRequest req, HttpServletResponse res) throws IOException {
res.getOutputStream().println("<html>" +
Expand Down
19 changes: 13 additions & 6 deletions test/java/src/test/java/ghostdriver/server/CallbackHttpServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,28 @@
import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.ServletHolder;

import java.util.HashMap;
import java.util.Map;

import static java.lang.String.format;

public class CallbackHttpServer {

protected Server server;
protected final Map<String, HttpRequestCallback> httpReqCallbacks = new HashMap<String, HttpRequestCallback>();

public HttpRequestCallback getGetHandler() {
return getHandler;
public CallbackHttpServer() {
// Default HTTP GET request callback: returns files in "test/fixture"
setHttpHandler("GET", new GetFixtureHttpRequestCallback());
}

public void setGetHandler(HttpRequestCallback getHandler) {
this.getHandler = getHandler;
public HttpRequestCallback getHttpHandler(String httpMethod) {
return httpReqCallbacks.get(httpMethod.toUpperCase());
}

// Default HTTP GET Handler
protected HttpRequestCallback getHandler = new GetFixtureHttpRequestCallback();
public void setHttpHandler(String httpMethod, HttpRequestCallback getHandler) {
httpReqCallbacks.put(httpMethod.toUpperCase(), getHandler);
}

public void start() throws Exception {
server = new Server(0);
Expand Down
12 changes: 10 additions & 2 deletions test/java/src/test/java/ghostdriver/server/CallbackServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,18 @@ public class CallbackServlet extends HttpServlet {
}

protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
if (server.getGetHandler() != null) {
server.getGetHandler().call(req, res);
if (server.getHttpHandler("GET") != null) {
server.getHttpHandler("GET").call(req, res);
} else {
super.doGet(req, res);
}
}

protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
if (server.getHttpHandler("POST") != null) {
server.getHttpHandler("POST").call(req, res);
} else {
super.doPost(req, res);
}
}
}

0 comments on commit 6b25de5

Please sign in to comment.