use of javax.servlet.http.HttpServletResponse in project ghostdriver by detro.
the class FrameSwitchingTest method shouldSwitchBackToMainFrameIfLinkInFrameCausesTopFrameReload.
@Test
public void shouldSwitchBackToMainFrameIfLinkInFrameCausesTopFrameReload() throws Exception {
WebDriver d = getDriver();
String expectedTitle = "Unique title";
class SpecialHttpRequestCallback extends GetFixtureHttpRequestCallback {
@Override
public void call(HttpServletRequest req, HttpServletResponse res) throws IOException {
if (req.getPathInfo().matches("^.*page/\\d+$")) {
int lastIndex = req.getPathInfo().lastIndexOf('/');
String pageNumber = (lastIndex == -1 ? "Unknown" : req.getPathInfo().substring(lastIndex + 1));
String resBody = String.format("<html><head><title>Page%s</title></head>" + "<body>Page number <span id=\"pageNumber\">%s</span>" + "<p><a href=\"../xhtmlTest.html\" target=\"_top\">top</a>" + "</body></html>", pageNumber, pageNumber);
res.getOutputStream().println(resBody);
} else {
super.call(req, res);
}
}
}
server.setHttpHandler("GET", new SpecialHttpRequestCallback());
d.get(server.getBaseUrl() + "/common/frameset.html");
assertEquals(expectedTitle, d.getTitle());
d.switchTo().frame(0);
d.findElement(By.linkText("top")).click();
// Wait for new content to load in the frame.
expectedTitle = "XHTML Test Page";
WebDriverWait wait = new WebDriverWait(d, 10);
wait.until(ExpectedConditions.titleIs(expectedTitle));
assertEquals(expectedTitle, d.getTitle());
WebElement element = d.findElement(By.id("amazing"));
assertNotNull(element);
}
use of javax.servlet.http.HttpServletResponse in project ghostdriver by detro.
the class ElementMethodsTest method shouldNotHandleCasesWhenAsyncJavascriptInitiatesAPageLoadFarInTheFuture.
@Test
public void shouldNotHandleCasesWhenAsyncJavascriptInitiatesAPageLoadFarInTheFuture() {
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" + " setTimeout(function() {\n" + " window.location.href = 'http://www.google.com';\n" + " }, 5000);\n" + " }\n" + " </script>\n" + " <a onclick=\"javascript: myFunction();\">Click Here</a>");
}
});
WebDriver d = getDriver();
d.get(server.getBaseUrl());
// Initiate timer that will finish with loading Google in the window
d.findElement(By.xpath("html/body/a")).click();
// "google.com" hasn't loaded yet at this stage
assertFalse(d.getTitle().toLowerCase().contains("google"));
}
use of javax.servlet.http.HttpServletResponse in project ghostdriver by detro.
the class ElementMethodsTest method shouldWaitForOnClickCallbackToFinishBeforeContinuing.
@Test
public void shouldWaitForOnClickCallbackToFinishBeforeContinuing() {
server.setHttpHandler("GET", new HttpRequestCallback() {
@Override
public void call(HttpServletRequest req, HttpServletResponse res) throws IOException {
res.getOutputStream().println("<script type=\"text/javascript\">\n" + " function sleep(milliseconds) {\n" + " var start = new Date().getTime();\n" + " for (;;) {\n" + " if ((new Date().getTime() - start) > milliseconds){\n" + " break;\n" + " }\n" + " }\n" + " } \n" + " function myFunction() {\n" + " sleep(1000)\n" + " window.location.href = 'http://www.google.com';\n" + " }\n" + " </script>\n" + " <a onclick=\"javascript: myFunction();\">Click Here</a>");
}
});
WebDriver d = getDriver();
d.get(server.getBaseUrl());
d.findElement(By.xpath("html/body/a")).click();
assertTrue(d.getTitle().toLowerCase().contains("google"));
}
use of javax.servlet.http.HttpServletResponse in project ghostdriver by detro.
the class FrameSwitchingTest method shouldSwitchBetweenNestedFramesPickedViaWebElement.
@Test
public void shouldSwitchBetweenNestedFramesPickedViaWebElement() {
// Define HTTP response for test
server.setHttpHandler("GET", new HttpRequestCallback() {
@Override
public void call(HttpServletRequest req, HttpServletResponse res) throws IOException {
String pathInfo = req.getPathInfo();
ServletOutputStream out = res.getOutputStream();
// @see https://github.com/watir/watirspec/tree/master/html/nested_frame.html
if (pathInfo.endsWith("nested_frame_1.html")) {
// nested frame 1
out.println("frame 1");
} else if (pathInfo.endsWith("nested_frame_2.html")) {
// nested frame 2
out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\">\n" + "<html>\n" + " <body>\n" + " <iframe id=\"three\" src=\"nested_frame_3.html\"></iframe>\n" + " </body>\n" + "</html>");
} else if (pathInfo.endsWith("nested_frame_3.html")) {
// nested frame 3, nested inside frame 2
out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\">\n" + "<html>\n" + " <body>\n" + " <a id=\"four\" href=\"definition_lists.html\" target=\"_top\">this link should consume the page</a>\n" + " </body>\n" + "</html>");
} else if (pathInfo.endsWith("definition_lists.html")) {
// definition lists
out.println("<html>\n" + " <head>\n" + " <title>definition_lists</title>\n" + " </head>\n" + "</html>");
} else {
// main page
out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\">\n" + "<html>\n" + " <frameset cols=\"20%, 80%\">\n" + " <frame id=\"one\" src=\"nested_frame_1.html\">\n" + " <frame id=\"two\" src=\"nested_frame_2.html\">\n" + " </frameset>\n" + "</html>");
}
}
});
// Launch Driver against the above defined server
WebDriver d = getDriver();
d.get(server.getBaseUrl());
// Switch to frame "#two"
d.switchTo().frame(d.findElement(By.id("two")));
// Switch further down into frame "#three"
d.switchTo().frame(d.findElement(By.id("three")));
// Click on the link in frame "#three"
d.findElement(By.id("four")).click();
// Expect page to have loaded and title to be set correctly
new WebDriverWait(d, 5).until(ExpectedConditions.titleIs("definition_lists"));
}
use of javax.servlet.http.HttpServletResponse 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;"));
}
Aggregations