Search in sources :

Example 1 with GetFixtureHttpRequestCallback

use of ghostdriver.server.GetFixtureHttpRequestCallback 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)

Aggregations

GetFixtureHttpRequestCallback (ghostdriver.server.GetFixtureHttpRequestCallback)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Test (org.junit.Test)1 WebDriverWait (org.openqa.selenium.support.ui.WebDriverWait)1