Search in sources :

Example 71 with HttpServletResponse

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

Example 72 with HttpServletResponse

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"));
}
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 73 with HttpServletResponse

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"));
}
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 74 with HttpServletResponse

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"));
}
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 75 with HttpServletResponse

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

Aggregations

HttpServletResponse (javax.servlet.http.HttpServletResponse)1635 HttpServletRequest (javax.servlet.http.HttpServletRequest)1312 Test (org.junit.Test)705 IOException (java.io.IOException)576 ServletException (javax.servlet.ServletException)491 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)223 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)195 Request (org.eclipse.jetty.server.Request)186 HttpServlet (javax.servlet.http.HttpServlet)157 CountDownLatch (java.util.concurrent.CountDownLatch)156 FilterChain (javax.servlet.FilterChain)148 PrintWriter (java.io.PrintWriter)138 Test (org.testng.annotations.Test)127 HashMap (java.util.HashMap)106 ServletOutputStream (javax.servlet.ServletOutputStream)105 InterruptedIOException (java.io.InterruptedIOException)97 InputStream (java.io.InputStream)85 OutputStream (java.io.OutputStream)81 HttpSession (javax.servlet.http.HttpSession)75 ServletResponse (javax.servlet.ServletResponse)74