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);
}
}
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);
}
}
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);
}
}
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/");
}
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);
}
Aggregations