use of ghostdriver.server.HttpRequestCallback in project ghostdriver by detro.
the class ElementJQueryEventsTest method shouldBeAbleToClickAndEventsBubbleUpUsingJquery.
@Test
public void shouldBeAbleToClickAndEventsBubbleUpUsingJquery() {
final String buttonId = "clickme";
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>");
}
});
WebDriver d = getDriver();
d.get(server.getBaseUrl());
// Click on the link inside the page
d.findElement(By.id(buttonId)).click();
// Check element was clicked as expected
assertTrue((Boolean) ((JavascriptExecutor) d).executeScript("return clicked;"));
}
use of ghostdriver.server.HttpRequestCallback in project ghostdriver by detro.
the class ElementMethodsTest method shouldHandleCasesWhereJavascriptCodeInitiatesPageLoadsThatFail.
@Test
public void shouldHandleCasesWhereJavascriptCodeInitiatesPageLoadsThatFail() throws InterruptedException {
final String crazyUrl = "http://abcdefghilmnopqrstuvz.zvutsr";
server.setHttpHandler("GET", new HttpRequestCallback() {
@Override
public void call(HttpServletRequest req, HttpServletResponse res) throws IOException {
res.getOutputStream().println("<script type=\"text/javascript\">\n" + " function myFunction() {\n" + " window.location.href = \"" + crazyUrl + "\";\n" + " }\n" + " </script>\n" + " <a onclick=\"javascript: myFunction();\">Click Here</a>");
}
});
WebDriver d = getDriver();
d.get(server.getBaseUrl());
// Click on the link to kickstart the javascript that will attempt to load a page that is supposed to fail
d.findElement(By.xpath("html/body/a")).click();
// The crazy URL should have not been loaded
assertTrue(!d.getCurrentUrl().equals(crazyUrl));
}
use of ghostdriver.server.HttpRequestCallback in project ghostdriver by detro.
the class DirectFileUploadTest method checkFileUploadCompletes.
@Test
public void checkFileUploadCompletes() throws IOException {
WebDriver d = getDriver();
if (!(d instanceof PhantomJSDriver)) {
// The command under test is only available when using PhantomJS
return;
}
PhantomJSDriver phantom = (PhantomJSDriver) d;
String buttonId = "upload";
// Create the test file for uploading
File testFile = File.createTempFile("webdriver", "tmp");
testFile.deleteOnExit();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(testFile.getAbsolutePath()), "utf-8"));
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
phantom.get(server.getBaseUrl() + "/common/upload.html");
phantom.executePhantomJS("var page = this; page.uploadFile('input#" + buttonId + "', '" + testFile.getAbsolutePath() + "');");
phantom.findElement(By.id("go")).submit();
// Uploading files across a network may take a while, even if they're really small.
// Wait for the loading label to disappear.
WebDriverWait wait = new WebDriverWait(phantom, 10);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("upload_label")));
phantom.switchTo().frame("upload_target");
wait = new WebDriverWait(phantom, 5);
wait.until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//body"), LOREM_IPSUM_TEXT));
// Navigate after file upload to verify callbacks are properly released.
phantom.get("http://www.google.com/");
}
use of ghostdriver.server.HttpRequestCallback in project ghostdriver by detro.
the class ElementFindingTest method findMultipleElements.
@Test
public void findMultipleElements() {
server.setHttpHandler("GET", new HttpRequestCallback() {
@Override
public void call(HttpServletRequest req, HttpServletResponse res) throws IOException {
res.getOutputStream().println("<div id=\"y-masthead\">" + "<input type=\"text\" name=\"t\" />" + "<input type=\"hidden\" name=\"h\" value=\"v\" />" + "<input type=\"button\" name=\"b\" value=\"button\" />" + "</div>");
}
});
WebDriver d = getDriver();
d.get(server.getBaseUrl());
assertEquals(3, d.findElements(By.tagName("input")).size());
}
use of ghostdriver.server.HttpRequestCallback in project ghostdriver by detro.
the class ElementFindingTest method findChildElement.
@Test
public void findChildElement() {
server.setHttpHandler("GET", new HttpRequestCallback() {
@Override
public void call(HttpServletRequest req, HttpServletResponse res) throws IOException {
res.getOutputStream().println("<div id=\"y-masthead\">" + "<input type=\"text\" name=\"t\" />" + "<input type=\"hidden\" name=\"h\" value=\"v\" />" + "</div>");
}
});
WebDriver d = getDriver();
d.get(server.getBaseUrl());
WebElement parent = d.findElement(By.id("y-masthead"));
assertNotNull(parent.findElement(By.name("t")));
}
Aggregations