Package org.openqa.selenium.phantomjs

Examples of org.openqa.selenium.phantomjs.PhantomJSDriver


        } else if (driver.equals(DRIVER_FIREFOX)) {
            mDriver = new FirefoxDriver(sCaps);
        } else if (driver.equals(DRIVER_CHROME)) {
            mDriver = new ChromeDriver(sCaps);
        } else if (driver.equals(DRIVER_PHANTOMJS)) {
            mDriver = new PhantomJSDriver(sCaps);
        }
    }
View Full Code Here


            // Skip this test if not using PhantomJS.
            // The command under test is only available when using PhantomJS
            return;
        }

        PhantomJSDriver phantom = (PhantomJSDriver)d;

        // Do we get results back?
        Object result = phantom.executePhantomJS("return 1 + 1");
        assertEquals(new Long(2), (Long)result);

        // Can we read arguments?
        result = phantom.executePhantomJS("return arguments[0] + arguments[0]", new Long(1));
        assertEquals(new Long(2), (Long)result);

        // Can we override some browser JavaScript functions in the page context?
        result = phantom.executePhantomJS("var page = this;" +
           "page.onInitialized = function () { " +
                "page.evaluate(function () { " +
                    "Math.random = function() { return 42 / 100 } " +
                "})" +
            "}");

        phantom.get("http://ariya.github.com/js/random/");

        WebElement numbers = phantom.findElement(By.id("numbers"));
        boolean foundAtLeastOne = false;
        for(String number : numbers.getText().split(" ")) {
            foundAtLeastOne = true;
            assertEquals("42", number);
        }
View Full Code Here

        if (!(d instanceof PhantomJSDriver)) {
            // Skip this test if not using PhantomJS.
            // 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/");
    }
View Full Code Here

TOP

Related Classes of org.openqa.selenium.phantomjs.PhantomJSDriver

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.