Search in sources :

Example 21 with HttpRequestCallback

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

the class AuthBasicTest method canSendKeysAndClickOnPageWithCSP.

// we should be able to interact with pages that have content security policies
// @Ignore
@Test
public void canSendKeysAndClickOnPageWithCSP() {
    server.setHttpHandler("GET", new HttpRequestCallback() {

        @Override
        public void call(HttpServletRequest req, HttpServletResponse res) throws IOException {
            res.addHeader("Content-Security-Policy", "default-src 'self'; script-src 'self';");
            res.getOutputStream().println("<html>\n" + "<head>\n" + "</head>\n" + "<body>\n" + "<input id='username' />\n" + "</body>\n" + "</html>");
        }
    });
    // Get Driver Instance
    WebDriver d = getDriver();
    d.get(server.getBaseUrl());
    WebElement element = d.findElement(By.id("username"));
    element.sendKeys("jesg");
    element.click();
    try {
        ((JavascriptExecutor) d).executeScript("1+1");
        fail("we should not be able to eval javascript on csp page");
    } catch (WebDriverException e) {
    }
}
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) WebElement(org.openqa.selenium.WebElement) WebDriverException(org.openqa.selenium.WebDriverException) 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