use of org.openqa.selenium.WebDriver in project webmagic by code4craft.
the class SeleniumDownloader method download.
@Override
public Page download(Request request, Task task) {
checkInit();
WebDriver webDriver;
try {
webDriver = webDriverPool.get();
} catch (InterruptedException e) {
logger.warn("interrupted", e);
return null;
}
logger.info("downloading page " + request.getUrl());
webDriver.get(request.getUrl());
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
WebDriver.Options manage = webDriver.manage();
Site site = task.getSite();
if (site.getCookies() != null) {
for (Map.Entry<String, String> cookieEntry : site.getCookies().entrySet()) {
Cookie cookie = new Cookie(cookieEntry.getKey(), cookieEntry.getValue());
manage.addCookie(cookie);
}
}
/*
* TODO You can add mouse event or other processes
*
* @author: bob.li.0718@gmail.com
*/
WebElement webElement = webDriver.findElement(By.xpath("/html"));
String content = webElement.getAttribute("outerHTML");
Page page = new Page();
page.setRawText(content);
page.setHtml(new Html(UrlUtils.fixAllRelativeHrefs(content, request.getUrl())));
page.setUrl(new PlainText(request.getUrl()));
page.setRequest(request);
webDriverPool.returnToPool(webDriver);
return page;
}
use of org.openqa.selenium.WebDriver in project webmagic by code4craft.
the class WebDriverPool method closeAll.
public void closeAll() {
boolean b = stat.compareAndSet(STAT_RUNNING, STAT_CLODED);
if (!b) {
throw new IllegalStateException("Already closed!");
}
for (WebDriver webDriver : webDriverList) {
logger.info("Quit webDriver" + webDriver);
webDriver.quit();
webDriver = null;
}
}
use of org.openqa.selenium.WebDriver in project webmagic by code4craft.
the class WebDriverPool method get.
/**
*
* @return
* @throws InterruptedException
*/
public WebDriver get() throws InterruptedException {
checkRunning();
WebDriver poll = innerQueue.poll();
if (poll != null) {
return poll;
}
if (webDriverList.size() < capacity) {
synchronized (webDriverList) {
if (webDriverList.size() < capacity) {
// add new WebDriver instance into pool
try {
configure();
innerQueue.add(mDriver);
webDriverList.add(mDriver);
} catch (IOException e) {
e.printStackTrace();
}
// ChromeDriver e = new ChromeDriver();
// WebDriver e = getWebDriver();
// innerQueue.add(e);
// webDriverList.add(e);
}
}
}
return innerQueue.take();
}
use of org.openqa.selenium.WebDriver in project cucumber-jvm by cucumber.
the class RentACarSupport method createCars.
public void createCars(int availableCars) {
WebDriver driver = new HtmlUnitDriver();
try {
driver.get("http://localhost:9878/rentit/create");
WebElement numberOfCarsToCreate = driver.findElement(By.id("numberOfCars"));
numberOfCarsToCreate.clear();
numberOfCarsToCreate.sendKeys("" + availableCars);
WebElement createButton = driver.findElement(By.id("createButton"));
createButton.click();
} finally {
driver.close();
}
}
use of org.openqa.selenium.WebDriver in project ghostdriver by detro.
the class ElementMethodsTest method shouldHandleCasesWhereJavascriptCodeInitiatesPageLoadsThatFail.
@Test
public void shouldHandleCasesWhereJavascriptCodeInitiatesPageLoadsThatFail() throws InterruptedException {
final String crazyUrl = "http://abcdefghilmnopqrstuvz.zvutsr";
server.setHttpHandler("GET", new HttpRequestCallback() {
@Override
public void call(HttpServletRequest req, HttpServletResponse res) throws IOException {
res.getOutputStream().println("<script type=\"text/javascript\">\n" + " function myFunction() {\n" + " window.location.href = \"" + crazyUrl + "\";\n" + " }\n" + " </script>\n" + " <a onclick=\"javascript: myFunction();\">Click Here</a>");
}
});
WebDriver d = getDriver();
d.get(server.getBaseUrl());
// Click on the link to kickstart the javascript that will attempt to load a page that is supposed to fail
d.findElement(By.xpath("html/body/a")).click();
// The crazy URL should have not been loaded
assertTrue(!d.getCurrentUrl().equals(crazyUrl));
}
Aggregations