Search in sources :

Example 6 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 7 with HttpRequestCallback

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));
}
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)

Example 8 with HttpRequestCallback

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/");
}
Also used : WebDriver(org.openqa.selenium.WebDriver) PhantomJSDriver(org.openqa.selenium.phantomjs.PhantomJSDriver) 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 9 with HttpRequestCallback

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());
}
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 10 with HttpRequestCallback

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

Aggregations

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