Search in sources :

Example 1 with HttpRequestCallback

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;"));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) WebDriver(org.openqa.selenium.WebDriver) JavascriptExecutor(org.openqa.selenium.JavascriptExecutor) HttpRequestCallback(ghostdriver.server.HttpRequestCallback) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Test(org.junit.Test)

Example 2 with HttpRequestCallback

use of ghostdriver.server.HttpRequestCallback 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"));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpRequestCallback(ghostdriver.server.HttpRequestCallback) GetFixtureHttpRequestCallback(ghostdriver.server.GetFixtureHttpRequestCallback) ServletOutputStream(javax.servlet.ServletOutputStream) WebDriverWait(org.openqa.selenium.support.ui.WebDriverWait) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Test(org.junit.Test)

Example 3 with HttpRequestCallback

use of ghostdriver.server.HttpRequestCallback in project ghostdriver by detro.

the class FileUploadTest method checkMultipleFileUploadCompletes.

@Test
public void checkMultipleFileUploadCompletes() 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();
    // Create the test file for uploading
    File testFile2 = File.createTempFile("webdriver", "tmp");
    testFile2.deleteOnExit();
    writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(testFile2.getAbsolutePath()), "utf-8"));
    writer.write(FILE_HTML2);
    writer.close();
    server.setHttpHandler("POST", new HttpRequestCallback() {

        @Override
        public void call(HttpServletRequest req, HttpServletResponse res) throws IOException {
            if (ServletFileUpload.isMultipartContent(req) && req.getPathInfo().endsWith("/uploadmult")) {
                // 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);
                is = items.get(1).getInputStream();
                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-multiple.html");
    d.findElement(By.id("upload")).sendKeys(testFile.getAbsolutePath() + "\n" + testFile2.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));
    wait.until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//body"), "Hello"));
    // Navigate after file upload to verify callbacks are properly released.
    d.get("http://www.google.com/");
}
Also used : WebDriver(org.openqa.selenium.WebDriver) HttpRequestCallback(ghostdriver.server.HttpRequestCallback) HttpServletResponse(javax.servlet.http.HttpServletResponse) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) WebDriverWait(org.openqa.selenium.support.ui.WebDriverWait) List(java.util.List) FileUploadException(org.apache.commons.fileupload.FileUploadException) Test(org.junit.Test)

Example 4 with HttpRequestCallback

use of ghostdriver.server.HttpRequestCallback in project ghostdriver by detro.

the class ElementFindingTest method findChildElements.

@Test
public void findChildElements() {
    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"));
    List<WebElement> children = parent.findElements(By.tagName("input"));
    assertEquals(2, children.size());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpRequestCallback(ghostdriver.server.HttpRequestCallback) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Test(org.junit.Test)

Example 5 with HttpRequestCallback

use of ghostdriver.server.HttpRequestCallback 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"));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) WebDriver(org.openqa.selenium.WebDriver) HttpRequestCallback(ghostdriver.server.HttpRequestCallback) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

HttpRequestCallback (ghostdriver.server.HttpRequestCallback)21 HttpServletRequest (javax.servlet.http.HttpServletRequest)21 HttpServletResponse (javax.servlet.http.HttpServletResponse)21 Test (org.junit.Test)21 IOException (java.io.IOException)18 WebDriver (org.openqa.selenium.WebDriver)11 WebDriverWait (org.openqa.selenium.support.ui.WebDriverWait)6 GetFixtureHttpRequestCallback (ghostdriver.server.GetFixtureHttpRequestCallback)4 ServletOutputStream (javax.servlet.ServletOutputStream)4 List (java.util.List)3 FileUploadException (org.apache.commons.fileupload.FileUploadException)3 DiskFileItemFactory (org.apache.commons.fileupload.disk.DiskFileItemFactory)3 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)3 JavascriptExecutor (org.openqa.selenium.JavascriptExecutor)2 WebElement (org.openqa.selenium.WebElement)2 WebDriverException (org.openqa.selenium.WebDriverException)1 PhantomJSDriver (org.openqa.selenium.phantomjs.PhantomJSDriver)1