use of ghostdriver.server.HttpRequestCallback in project ghostdriver by detro.
the class ElementFindingTest method findElementViaXpathLocator.
@Test
public void findElementViaXpathLocator() {
// Define HTTP response for test
server.setHttpHandler("GET", new HttpRequestCallback() {
@Override
public void call(HttpServletRequest req, HttpServletResponse res) throws IOException {
ServletOutputStream out = res.getOutputStream();
out.println("<html><body>" + "<button class='login main btn'>Login Button</button>" + "</body></html>");
}
});
WebDriver d = getDriver();
d.get(server.getBaseUrl());
WebElement loginButton = d.findElement(By.xpath("//button[contains(@class, 'login')]"));
assertNotNull(loginButton);
assertTrue(loginButton.getText().toLowerCase().contains("login"));
assertEquals("button", loginButton.getTagName().toLowerCase());
}
use of ghostdriver.server.HttpRequestCallback in project ghostdriver by detro.
the class ElementQueryingTest method throwExceptionWhenInteractingWithInvisibleElement.
@Test(expected = InvalidElementStateException.class)
public void throwExceptionWhenInteractingWithInvisibleElement() {
server.setHttpHandler("GET", new HttpRequestCallback() {
@Override
public void call(HttpServletRequest req, HttpServletResponse res) throws IOException {
res.getOutputStream().println("<!DOCTYPE html>" + "<html>" + " <head>\n" + " <title>test</title>\n" + " </head>\n" + " <body>\n" + " <input type=\"text\" id=\"visible\">\n" + " <input type=\"text\" id=\"invisible\" style=\"display: none;\">\n" + " </body>" + "</html>");
}
});
WebDriver d = getDriver();
d.get(server.getBaseUrl());
WebElement visibleInput = d.findElement(By.id("visible"));
WebElement invisibleInput = d.findElement(By.id("invisible"));
String textToType = "text to type";
visibleInput.sendKeys(textToType);
assertEquals(textToType, visibleInput.getAttribute("value"));
invisibleInput.sendKeys(textToType);
}
use of ghostdriver.server.HttpRequestCallback in project ghostdriver by detro.
the class ElementQueryingTest method getTextFromDifferentLocationsOfDOMTree.
@Test
public void getTextFromDifferentLocationsOfDOMTree() {
server.setHttpHandler("GET", new HttpRequestCallback() {
@Override
public void call(HttpServletRequest req, HttpServletResponse res) throws IOException {
res.getOutputStream().println("<html><body>" + "<div class=\"item\">\n" + " <span class=\"item-title\">\n" + " <a href=\"#\">\n" + " <h1>The Title of The Item</h1>\n" + " </a>\n" + " </span>\n" + " <div>\n" + " (Loads of other stuff)\n" + " </div>\n" + "</div>" + "</body></html>");
}
});
WebDriver d = getDriver();
d.get(server.getBaseUrl());
assertEquals("The Title of The Item\n(Loads of other stuff)", d.findElement(By.className("item")).getText());
assertEquals("The Title of The Item", d.findElement(By.className("item")).findElement(By.tagName("h1")).getText());
assertEquals("The Title of The Item", d.findElement(By.className("item")).findElement(By.tagName("a")).getText());
assertEquals("The Title of The Item", d.findElement(By.className("item")).findElement(By.className("item-title")).getText());
}
use of ghostdriver.server.HttpRequestCallback in project ghostdriver by detro.
the class FileUploadTest method checkFileUploadCompletes.
@Test
public void checkFileUploadCompletes() throws IOException {
WebDriver d = getDriver();
// 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
d.get(server.getBaseUrl() + "/common/upload.html");
d.findElement(By.id("upload")).sendKeys(testFile.getAbsolutePath());
d.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(d, 10);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("upload_label")));
d.switchTo().frame("upload_target");
wait = new WebDriverWait(d, 5);
wait.until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//body"), LOREM_IPSUM_TEXT));
// Navigate after file upload to verify callbacks are properly released.
d.get("http://www.google.com/");
}
use of ghostdriver.server.HttpRequestCallback in project ghostdriver by detro.
the class FrameSwitchingTest method shouldSwitchBetweenNestedFrames.
@Test
public void shouldSwitchBetweenNestedFrames() {
// 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("two");
// Switch further down into frame "#three"
d.switchTo().frame("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"));
}
Aggregations