Search in sources :

Example 81 with WebDriver

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;
}
Also used : WebDriver(org.openqa.selenium.WebDriver) Site(us.codecraft.webmagic.Site) Cookie(org.openqa.selenium.Cookie) PlainText(us.codecraft.webmagic.selector.PlainText) Html(us.codecraft.webmagic.selector.Html) Page(us.codecraft.webmagic.Page) WebElement(org.openqa.selenium.WebElement) Map(java.util.Map)

Example 82 with WebDriver

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;
    }
}
Also used : WebDriver(org.openqa.selenium.WebDriver) RemoteWebDriver(org.openqa.selenium.remote.RemoteWebDriver)

Example 83 with WebDriver

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();
}
Also used : WebDriver(org.openqa.selenium.WebDriver) RemoteWebDriver(org.openqa.selenium.remote.RemoteWebDriver) IOException(java.io.IOException)

Example 84 with WebDriver

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();
    }
}
Also used : WebDriver(org.openqa.selenium.WebDriver) WebElement(org.openqa.selenium.WebElement) HtmlUnitDriver(org.openqa.selenium.htmlunit.HtmlUnitDriver)

Example 85 with WebDriver

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));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) WebDriver(org.openqa.selenium.WebDriver) HttpRequestCallback(ghostdriver.server.HttpRequestCallback) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

WebDriver (org.openqa.selenium.WebDriver)167 WebElement (org.openqa.selenium.WebElement)87 Test (org.junit.Test)61 FirefoxDriver (org.openqa.selenium.firefox.FirefoxDriver)59 WebDriverWait (org.openqa.selenium.support.ui.WebDriverWait)40 Actions (org.openqa.selenium.interactions.Actions)25 IOException (java.io.IOException)12 Cookie (org.openqa.selenium.Cookie)11 List (java.util.List)10 File (java.io.File)9 HttpRequestCallback (ghostdriver.server.HttpRequestCallback)8 HttpServletRequest (javax.servlet.http.HttpServletRequest)8 HttpServletResponse (javax.servlet.http.HttpServletResponse)8 JavascriptExecutor (org.openqa.selenium.JavascriptExecutor)8 ChromeDriver (org.openqa.selenium.chrome.ChromeDriver)7 ExpectedCondition (org.openqa.selenium.support.ui.ExpectedCondition)7 By (org.openqa.selenium.By)5 Dimension (org.openqa.selenium.Dimension)5 Predicate (com.google.common.base.Predicate)4 TimeoutException (org.openqa.selenium.TimeoutException)4