Search in sources :

Example 1 with PhantomJSDriver

use of org.openqa.selenium.phantomjs.PhantomJSDriver in project ghostdriver by detro.

the class BaseTest method prepareDriver.

@Before
public void prepareDriver() throws Exception {
    // Which driver to use? (default "phantomjs")
    String driver = sConfig.getProperty("driver", DRIVER_PHANTOMJS);
    // Start appropriate Driver
    if (isUrl(driver)) {
        sCaps.setBrowserName("phantomjs");
        mDriver = new RemoteWebDriver(new URL(driver), sCaps);
    } 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);
    }
}
Also used : PhantomJSDriver(org.openqa.selenium.phantomjs.PhantomJSDriver) FirefoxDriver(org.openqa.selenium.firefox.FirefoxDriver) RemoteWebDriver(org.openqa.selenium.remote.RemoteWebDriver) ChromeDriver(org.openqa.selenium.chrome.ChromeDriver) URL(java.net.URL) Before(org.junit.Before)

Example 2 with PhantomJSDriver

use of org.openqa.selenium.phantomjs.PhantomJSDriver in project ats-framework by Axway.

the class RealHtmlElement method pressEnterKey.

/**
     * Simulate Enter key
     */
@Override
@PublicAtsApi
public void pressEnterKey() {
    new RealHtmlElementState(this).waitToBecomeExisting();
    WebElement element = RealHtmlElementLocator.findElement(this);
    if (webDriver instanceof PhantomJSDriver) {
        element.sendKeys(Keys.ENTER);
    } else {
        element.sendKeys(Keys.RETURN);
    }
}
Also used : RealHtmlElementState(com.axway.ats.uiengine.utilities.realbrowser.html.RealHtmlElementState) PhantomJSDriver(org.openqa.selenium.phantomjs.PhantomJSDriver) WebElement(org.openqa.selenium.WebElement) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 3 with PhantomJSDriver

use of org.openqa.selenium.phantomjs.PhantomJSDriver in project webmagic by code4craft.

the class WebDriverPool method configure.

/**
	 * Configure the GhostDriver, and initialize a WebDriver instance. This part
	 * of code comes from GhostDriver.
	 * https://github.com/detro/ghostdriver/tree/master/test/java/src/test/java/ghostdriver
	 * 
	 * @author bob.li.0718@gmail.com
	 * @throws IOException
	 */
public void configure() throws IOException {
    // Read config file
    sConfig = new Properties();
    String configFile = DEFAULT_CONFIG_FILE;
    if (System.getProperty("selenuim_config") != null) {
        configFile = System.getProperty("selenuim_config");
    }
    sConfig.load(new FileReader(configFile));
    // Prepare capabilities
    sCaps = new DesiredCapabilities();
    sCaps.setJavascriptEnabled(true);
    sCaps.setCapability("takesScreenshot", false);
    String driver = sConfig.getProperty("driver", DRIVER_PHANTOMJS);
    // Fetch PhantomJS-specific configuration parameters
    if (driver.equals(DRIVER_PHANTOMJS)) {
        // "phantomjs_exec_path"
        if (sConfig.getProperty("phantomjs_exec_path") != null) {
            sCaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, sConfig.getProperty("phantomjs_exec_path"));
        } else {
            throw new IOException(String.format("Property '%s' not set!", PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY));
        }
        // "phantomjs_driver_path"
        if (sConfig.getProperty("phantomjs_driver_path") != null) {
            System.out.println("Test will use an external GhostDriver");
            sCaps.setCapability(PhantomJSDriverService.PHANTOMJS_GHOSTDRIVER_PATH_PROPERTY, sConfig.getProperty("phantomjs_driver_path"));
        } else {
            System.out.println("Test will use PhantomJS internal GhostDriver");
        }
    }
    // Disable "web-security", enable all possible "ssl-protocols" and
    // "ignore-ssl-errors" for PhantomJSDriver
    // sCaps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, new
    // String[] {
    // "--web-security=false",
    // "--ssl-protocol=any",
    // "--ignore-ssl-errors=true"
    // });
    ArrayList<String> cliArgsCap = new ArrayList<String>();
    cliArgsCap.add("--web-security=false");
    cliArgsCap.add("--ssl-protocol=any");
    cliArgsCap.add("--ignore-ssl-errors=true");
    sCaps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cliArgsCap);
    // Control LogLevel for GhostDriver, via CLI arguments
    sCaps.setCapability(PhantomJSDriverService.PHANTOMJS_GHOSTDRIVER_CLI_ARGS, new String[] { "--logLevel=" + (sConfig.getProperty("phantomjs_driver_loglevel") != null ? sConfig.getProperty("phantomjs_driver_loglevel") : "INFO") });
    // Start appropriate Driver
    if (isUrl(driver)) {
        sCaps.setBrowserName("phantomjs");
        mDriver = new RemoteWebDriver(new URL(driver), sCaps);
    } 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);
    }
}
Also used : PhantomJSDriver(org.openqa.selenium.phantomjs.PhantomJSDriver) FirefoxDriver(org.openqa.selenium.firefox.FirefoxDriver) RemoteWebDriver(org.openqa.selenium.remote.RemoteWebDriver) DesiredCapabilities(org.openqa.selenium.remote.DesiredCapabilities) ArrayList(java.util.ArrayList) FileReader(java.io.FileReader) IOException(java.io.IOException) Properties(java.util.Properties) ChromeDriver(org.openqa.selenium.chrome.ChromeDriver) URL(java.net.URL)

Example 4 with PhantomJSDriver

use of org.openqa.selenium.phantomjs.PhantomJSDriver in project ghostdriver by detro.

the class DirectFileUploadTest method checkFileUploadCompletes.

@Test
public void checkFileUploadCompletes() throws IOException {
    WebDriver d = getDriver();
    if (!(d instanceof PhantomJSDriver)) {
        // 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/");
}
Also used : WebDriver(org.openqa.selenium.WebDriver) PhantomJSDriver(org.openqa.selenium.phantomjs.PhantomJSDriver) 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 5 with PhantomJSDriver

use of org.openqa.selenium.phantomjs.PhantomJSDriver in project ghostdriver by detro.

the class PhantomJSCommandTest method executePhantomJS.

@Test
public void executePhantomJS() {
    WebDriver d = getDriver();
    if (!(d instanceof PhantomJSDriver)) {
        // 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);
    }
    assert (foundAtLeastOne);
}
Also used : WebDriver(org.openqa.selenium.WebDriver) PhantomJSDriver(org.openqa.selenium.phantomjs.PhantomJSDriver) WebElement(org.openqa.selenium.WebElement) Test(org.junit.Test)

Aggregations

PhantomJSDriver (org.openqa.selenium.phantomjs.PhantomJSDriver)9 DesiredCapabilities (org.openqa.selenium.remote.DesiredCapabilities)4 WebDriver (org.openqa.selenium.WebDriver)3 WebElement (org.openqa.selenium.WebElement)3 IOException (java.io.IOException)2 URL (java.net.URL)2 Test (org.junit.Test)2 JavascriptExecutor (org.openqa.selenium.JavascriptExecutor)2 PublicAtsApi (com.axway.ats.common.PublicAtsApi)1 VerificationException (com.axway.ats.uiengine.exceptions.VerificationException)1 RealHtmlElementState (com.axway.ats.uiengine.utilities.realbrowser.html.RealHtmlElementState)1 HttpRequestCallback (ghostdriver.server.HttpRequestCallback)1 Rectangle (java.awt.Rectangle)1 BufferedImage (java.awt.image.BufferedImage)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileReader (java.io.FileReader)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1