Example usage for org.openqa.selenium.remote RemoteWebDriver getCurrentUrl

List of usage examples for org.openqa.selenium.remote RemoteWebDriver getCurrentUrl

Introduction

In this page you can find the example usage for org.openqa.selenium.remote RemoteWebDriver getCurrentUrl.

Prototype

@Override
    public String getCurrentUrl() 

Source Link

Usage

From source file:com.screenslicer.core.util.Util.java

License:Open Source License

public static WebElement toElement(RemoteWebDriver driver, HtmlNode htmlNode, Element body)
        throws ActionFailed {
    if (body == null) {
        body = Util.openElement(driver, null, null, null);
    }/*from   w  ww  . j av  a2s  . co  m*/
    if (!CommonUtil.isEmpty(htmlNode.id)) {
        WebElement element = toElement(driver, body.getElementById(htmlNode.id));
        if (element != null) {
            return element;
        }
    }
    List<Elements> selected = new ArrayList<Elements>();
    if (!CommonUtil.isEmpty(htmlNode.tagName)) {
        selected.add(body.getElementsByTag(htmlNode.tagName));
    } else if (!CommonUtil.isEmpty(htmlNode.href)) {
        selected.add(body.getElementsByTag("a"));
    }
    if (!CommonUtil.isEmpty(htmlNode.name)) {
        selected.add(body.getElementsByAttributeValue("name", htmlNode.name));
    }
    if (!CommonUtil.isEmpty(htmlNode.type)) {
        selected.add(body.getElementsByAttributeValue("type", htmlNode.type));
    }
    if (!CommonUtil.isEmpty(htmlNode.value)) {
        selected.add(body.getElementsByAttributeValue("value", htmlNode.value));
    }
    if (!CommonUtil.isEmpty(htmlNode.title)) {
        selected.add(body.getElementsByAttributeValue("title", htmlNode.title));
    }
    if (htmlNode.classes != null && htmlNode.classes.length > 0) {
        Map<Element, Integer> found = new HashMap<Element, Integer>();
        for (int i = 0; i < htmlNode.classes.length; i++) {
            Elements elements = body.getElementsByClass(htmlNode.classes[i]);
            for (Element element : elements) {
                if (!found.containsKey(element)) {
                    found.put(element, 0);
                }
                found.put(element, found.get(element) + 1);
            }
        }
        Elements elements = new Elements();
        for (int i = htmlNode.classes.length; i > 0; i--) {
            for (Map.Entry<Element, Integer> entry : found.entrySet()) {
                if (entry.getValue() == i) {
                    elements.add(entry.getKey());
                }
            }
            if (!elements.isEmpty()) {
                break;
            }
        }
        selected.add(elements);
    }
    if (!CommonUtil.isEmpty(htmlNode.href)) {
        Elements hrefs = body.getElementsByAttribute("href");
        Elements toAdd = new Elements();
        String currentUrl = driver.getCurrentUrl();
        String hrefGiven = htmlNode.href;
        for (Element href : hrefs) {
            String hrefFound = href.attr("href");
            if (hrefGiven.equalsIgnoreCase(hrefFound)) {
                toAdd.add(href);
            } else {
                String uriGiven = Util.toCanonicalUri(currentUrl, hrefGiven);
                String uriFound = Util.toCanonicalUri(currentUrl, hrefFound);
                if (uriGiven.equalsIgnoreCase(uriFound)) {
                    toAdd.add(href);
                }
            }
        }
        selected.add(toAdd);
    }
    if (!CommonUtil.isEmpty(htmlNode.innerText)) {
        selected.add(body.getElementsMatchingText(Pattern.quote(htmlNode.innerText)));
    }
    if (htmlNode.multiple != null) {
        selected.add(body.getElementsByAttribute("multiple"));
    }
    Map<Element, Integer> votes = new HashMap<Element, Integer>();
    for (Elements elements : selected) {
        for (Element element : elements) {
            if (!Util.isHidden(element)) {
                if (!votes.containsKey(element)) {
                    votes.put(element, 0);
                }
                votes.put(element, votes.get(element) + 1);
            }
        }
    }
    int maxVote = 0;
    Element maxElement = null;
    for (Map.Entry<Element, Integer> entry : votes.entrySet()) {
        if (entry.getValue() > maxVote) {
            maxVote = entry.getValue();
            maxElement = entry.getKey();
        }
    }
    return toElement(driver, maxElement);
}

From source file:com.watchrabbit.crawler.driver.factory.FirefoxWebDriverFactoryIT.java

License:Apache License

@Test
public void shouldGetDriver() {
    RemoteWebDriver driver = firefoxFactory.produceDriver();
    driver.get("http://google.com");

    assertThat(driver.getCurrentUrl()).contains("google");
    firefoxFactory.returnWebDriver(driver);
}

From source file:com.watchrabbit.crawler.driver.service.LoaderServiceImpl.java

License:Apache License

private void waitUsing(LoadingStrategy strategy, RemoteWebDriver driver) {
    WebDriverWait wait = new WebDriverWait(driver, strategy.getWaitTime());
    try {//ww w  .java 2 s . co  m
        wait.until(strategy.hasFinishedProcessing());
    } catch (TimeoutException ex) {
        LOGGER.info("Timed out on {}", driver.getCurrentUrl());
    }
}

From source file:com.watchrabbit.scanner.attacker.verify.BasicXSSVerificationStrategy.java

License:Apache License

@Override
public Vulnerability verify(RemoteWebDriver driver, long loadMilisec) {
    WebDriverWait wait = new WebDriverWait(driver, 1);
    try {/* ww w  .j a  va2s  .  c om*/
        wait.until((WebDriver predicatedDriver) -> false);
    } catch (TimeoutException ex) {
        LOGGER.info("Timed out on {}", driver.getCurrentUrl());
    }
    String result = (String) driver.executeScript("return window.w$");

    LOGGER.debug("XSS Attack result is {}", result);
    if (StringUtils.isNotBlank(result)) {
        return Vulnerability.EXISTS;
    } else {
        return Vulnerability.NONE;
    }
}

From source file:com.watchrabbit.scanner.supervisor.service.AttackerServiceImpl.java

License:Apache License

@Override
public AttackResult performAttack(String originalAdress, RemoteWebDriver driver, AttackData data) {
    data.getForm().getFields().stream().filter(field -> field.getField().isDisplayed())
            .filter(field -> field.getField().isEnabled()).forEach(this::fill);
    data.getFields().forEach(this::fillAttacked);

    Stopwatch stopwatch = Stopwatch.createStarted(() -> {
        data.getForm().getSendButton().submit();
        loaderService.waitFor(driver);/*from   w w  w  .  j  ava2 s .  co  m*/
    });
    Vulnerability vulnerability = data.getVerificationStrategy().verify(driver,
            stopwatch.getExecutionTime(TimeUnit.MILLISECONDS));

    boolean sent = isFormSent(originalAdress, driver.getCurrentUrl());
    return new AttackResult.Builder().withAttackData(data).withResultAddress(driver.getCurrentUrl())
            .withVulnerability(vulnerability).withFormSent(sent).build();
}

From source file:org.asqatasun.websnapshot.service.SnapshotCreatorImpl.java

License:Open Source License

/**
 *
 * @param driver/*from ww w  .  j  a v a2 s.com*/
 * @param url
 */
private String loadPage(RemoteWebDriver driver, String url) {
    try {
        driver.get(url);
        driver.executeScript("if (getComputedStyle(document.body, null).backgroundColor === 'rgba(0, 0, 0, 0)'"
                + "|| getComputedStyle(document.body, null).backgroundColor === 'transparent') {"
                + "document.body.style.backgroundColor = 'white';" + "}");
    } catch (Exception e) {
        Logger.getLogger(this.getClass()).debug("Cannot load this url : " + url);
        return SnapshotCreationResponse.ERROR;
    }
    if (!url.equals(driver.getCurrentUrl())) {
        String urlAvailibility = UrlUtils.checkURLAvailable(driver.getCurrentUrl());
        if (!urlAvailibility.equals(UrlUtils.URL_AVAILABLE)) {
            return urlAvailibility;
        }
    }
    return SnapshotCreationResponse.SUCCESS;
}