Search in sources :

Example 11 with HttpRequestCallback

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

the class ElementFindingTest method findElementViaXpathLocator.

@Test
public void findElementViaXpathLocator() {
    // Define HTTP response for test
    server.setHttpHandler("GET", new HttpRequestCallback() {

        @Override
        public void call(HttpServletRequest req, HttpServletResponse res) throws IOException {
            ServletOutputStream out = res.getOutputStream();
            out.println("<html><body>" + "<button class='login main btn'>Login Button</button>" + "</body></html>");
        }
    });
    WebDriver d = getDriver();
    d.get(server.getBaseUrl());
    WebElement loginButton = d.findElement(By.xpath("//button[contains(@class, 'login')]"));
    assertNotNull(loginButton);
    assertTrue(loginButton.getText().toLowerCase().contains("login"));
    assertEquals("button", loginButton.getTagName().toLowerCase());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpRequestCallback(ghostdriver.server.HttpRequestCallback) ServletOutputStream(javax.servlet.ServletOutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Test(org.junit.Test)

Example 12 with HttpRequestCallback

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

the class ElementQueryingTest method throwExceptionWhenInteractingWithInvisibleElement.

@Test(expected = InvalidElementStateException.class)
public void throwExceptionWhenInteractingWithInvisibleElement() {
    server.setHttpHandler("GET", new HttpRequestCallback() {

        @Override
        public void call(HttpServletRequest req, HttpServletResponse res) throws IOException {
            res.getOutputStream().println("<!DOCTYPE html>" + "<html>" + "    <head>\n" + "        <title>test</title>\n" + "    </head>\n" + "    <body>\n" + "        <input type=\"text\" id=\"visible\">\n" + "        <input type=\"text\" id=\"invisible\" style=\"display: none;\">\n" + "    </body>" + "</html>");
        }
    });
    WebDriver d = getDriver();
    d.get(server.getBaseUrl());
    WebElement visibleInput = d.findElement(By.id("visible"));
    WebElement invisibleInput = d.findElement(By.id("invisible"));
    String textToType = "text to type";
    visibleInput.sendKeys(textToType);
    assertEquals(textToType, visibleInput.getAttribute("value"));
    invisibleInput.sendKeys(textToType);
}
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 13 with HttpRequestCallback

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

the class ElementQueryingTest method getTextFromDifferentLocationsOfDOMTree.

@Test
public void getTextFromDifferentLocationsOfDOMTree() {
    server.setHttpHandler("GET", new HttpRequestCallback() {

        @Override
        public void call(HttpServletRequest req, HttpServletResponse res) throws IOException {
            res.getOutputStream().println("<html><body>" + "<div class=\"item\">\n" + "    <span class=\"item-title\">\n" + "        <a href=\"#\">\n" + "             <h1>The Title of The Item</h1>\n" + "        </a>\n" + "     </span>\n" + "     <div>\n" + "         (Loads of other stuff)\n" + "     </div>\n" + "</div>" + "</body></html>");
        }
    });
    WebDriver d = getDriver();
    d.get(server.getBaseUrl());
    assertEquals("The Title of The Item\n(Loads of other stuff)", d.findElement(By.className("item")).getText());
    assertEquals("The Title of The Item", d.findElement(By.className("item")).findElement(By.tagName("h1")).getText());
    assertEquals("The Title of The Item", d.findElement(By.className("item")).findElement(By.tagName("a")).getText());
    assertEquals("The Title of The Item", d.findElement(By.className("item")).findElement(By.className("item-title")).getText());
}
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 14 with HttpRequestCallback

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

the class FileUploadTest method checkFileUploadCompletes.

@Test
public void checkFileUploadCompletes() 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();
    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
    d.get(server.getBaseUrl() + "/common/upload.html");
    d.findElement(By.id("upload")).sendKeys(testFile.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));
    // 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 15 with HttpRequestCallback

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

the class FrameSwitchingTest method shouldSwitchBetweenNestedFrames.

@Test
public void shouldSwitchBetweenNestedFrames() {
    // 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("two");
    // Switch further down into frame "#three"
    d.switchTo().frame("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)

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