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

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

Introduction

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

Prototype

@Override
    public WebElement findElementByTagName(String using) 

Source Link

Usage

From source file:com.screenslicer.core.scrape.QueryCommon.java

License:Open Source License

public static boolean doAuth(RemoteWebDriver driver, Credentials credentials) throws ActionFailed {
    if (credentials == null || CommonUtil.isEmpty(credentials.username)
            || CommonUtil.isEmpty(credentials.password)) {
        return false;
    }/*w w  w  . ja  va2  s . co m*/
    try {
        List<WebElement> inputs = driver.findElementsByTagName("input");
        String html = CommonUtil.strip(driver.findElementByTagName("body").getAttribute("outerHTML"), true);
        List<WebElement> usernames = new ArrayList<WebElement>();
        List<WebElement> passwords = new ArrayList<WebElement>();
        List<String> usernamesHtml = new ArrayList<String>();
        List<String> passwordsHtml = new ArrayList<String>();
        for (WebElement input : inputs) {
            String type = input.getAttribute("type");
            if ("text".equalsIgnoreCase(type)) {
                usernames.add(input);
                String controlHtml = input.getAttribute("outerHTML");
                controlHtml = CommonUtil.strip(controlHtml, true);
                usernamesHtml.add(controlHtml);
            } else if ("password".equalsIgnoreCase(type)) {
                passwords.add(input);
                String controlHtml = input.getAttribute("outerHTML");
                controlHtml = CommonUtil.strip(controlHtml, true);
                passwordsHtml.add(controlHtml);
            }
        }
        class Login {
            final WebElement username;
            final WebElement password;
            final int index;

            Login(WebElement username, WebElement password, int index) {
                this.username = username;
                this.password = password;
                this.index = index;
            }
        }
        ;
        List<Login> logins = new ArrayList<Login>();
        for (int curPassword = 0; curPassword < passwords.size(); curPassword++) {
            int passwordIndex = html.indexOf(passwordsHtml.get(curPassword));
            int minDist = Integer.MAX_VALUE;
            int indexOfMin = -1;
            WebElement minUsername = null;
            WebElement minPassword = passwords.get(curPassword);
            for (int curUsername = 0; curUsername < usernames.size(); curUsername++) {
                int usernameIndex = html.indexOf(usernamesHtml.get(curUsername));
                if (usernameIndex < passwordIndex && passwordIndex - usernameIndex < minDist
                        && usernameIndex > -1 && passwordIndex > -1) {
                    minDist = passwordIndex - usernameIndex;
                    minUsername = usernames.get(curUsername);
                    indexOfMin = (usernameIndex + passwordIndex) / 2;
                }
            }
            logins.add(new Login(minUsername, minPassword, indexOfMin));
        }
        if (!logins.isEmpty()) {
            Login closestLogin = logins.get(0);
            if (logins.size() > 1) {
                //TODO translate
                Pattern hints = Pattern.compile(
                        "(?:log(?:ged)?\\s?-?in)|(?:sign(?:ed)?\\s?\\-?in)|(?:remember\\s?me)|(?:tabindex\\s?=\\s?[^0-9]?[12][^0-9])",
                        Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CHARACTER_CLASS);
                Matcher matcher = hints.matcher(html);
                int closest = Integer.MAX_VALUE;
                while (matcher.find()) {
                    int start = matcher.start();
                    for (Login login : logins) {
                        int dist = Math.abs(login.index - start);
                        if (dist < closest) {
                            closest = dist;
                            closestLogin = login;
                        }
                    }
                }
            }
            QueryCommon.typeText(driver, closestLogin.username, credentials.username, true, false);
            QueryCommon.typeText(driver, closestLogin.password, credentials.password, false, true);
            return true;
        }
    } catch (Throwable t) {
        Log.exception(t);
        throw new ActionFailed(t);
    }
    throw new ActionFailed("Could not sign in");
}

From source file:com.screenslicer.core.scrape.QueryKeyword.java

License:Open Source License

private static void handleIframe(RemoteWebDriver driver) throws ActionFailed {
    List<WebElement> iframes = null;
    try {/*from w ww  .  j  a va  2 s .  co m*/
        iframes = driver.findElementsByTagName("iframe");
    } catch (Throwable t) {
        throw new ActionFailed(t);
    }
    try {
        for (WebElement iframe : iframes) {
            try {
                Dimension dim = iframe.getSize();
                if (iframe.isDisplayed() && (dim.getHeight() * dim.getWidth()) > MIN_IFRAME_AREA) {
                    String src = iframe.getAttribute("src");
                    if (!CommonUtil.isEmpty(src) && src.indexOf("://") > -1 && src.indexOf("?") > -1) {
                        String origHandle = null;
                        String origUrl = null;
                        String newHandle = null;
                        try {
                            origHandle = driver.getWindowHandle();
                            origUrl = driver.getCurrentUrl();
                            newHandle = Util.newWindow(driver);
                        } catch (Throwable t) {
                            Log.exception(t);
                            throw new ActionFailed(t);
                        }
                        boolean undo = false;
                        try {
                            Util.get(driver, src, true);
                            driver.executeScript(
                                    "document.getElementsByTagName('html')[0].style.overflow='scroll';");
                            Util.driverSleepShort();
                            if (driver.findElementByTagName("body").getText().length() < MIN_SOURCE_DIFF) {
                                undo = true;
                            }
                        } catch (Throwable t) {
                            Log.exception(t);
                            undo = true;
                        }
                        try {
                            if (undo) {
                                if (origHandle.equals(newHandle)) {
                                    if (!driver.getCurrentUrl().equals(origUrl)) {
                                        try {
                                            driver.navigate().back();
                                        } catch (Throwable t) {
                                            Log.exception(t);
                                        }
                                    }
                                    if (!driver.getCurrentUrl().equals(origUrl)) {
                                        driver.get(origUrl);
                                    }
                                } else {
                                    Util.cleanUpNewWindows(driver, origHandle);
                                }
                            } else {
                                Util.cleanUpNewWindows(driver, newHandle);
                                break;
                            }
                        } catch (Throwable t) {
                            Log.exception(t);
                            throw new ActionFailed(t);
                        }
                    }
                }
            } catch (Throwable t) {
                Log.exception(t);
                continue;
            }
        }
    } catch (Throwable t) {
        Log.exception(t);
        throw new ActionFailed(t);
    }
}

From source file:org.openqa.runner.Commands.java

License:Apache License

public static void assertTextPresent(RemoteWebDriver remoteWebDriver, State state, Map<String, String> params) {
    String allText = remoteWebDriver.findElementByTagName("body").getText();
    if (!allText.contains(params.get("text")))
        state.setAborted();//  w  w  w  .j  a  v a2s. co  m
}

From source file:org.openqa.runner.Commands.java

License:Apache License

public static void verifyTextPresent(RemoteWebDriver remoteWebDriver, State state, Map<String, String> params) {
    String allText = remoteWebDriver.findElementByTagName("body").getText();
    if (!allText.contains(params.get("text")))
        state.setFailed();//from  ww w  . jav  a2s . co  m
}

From source file:org.tanaguru.rules.generator.json.TanaguruJsTestCaseExtractor.java

License:Open Source License

/**
 * @param args the command line arguments
 * @throws java.net.MalformedURLException
 *//*from w w  w.j a va  2s .c  o m*/
public static void main(String[] args) throws MalformedURLException, IOException, InterruptedException {
    //        DesiredCapabilities caps = new DesiredCapabilities();
    //        caps.setJavascriptEnabled(true);
    //        WebDriver driver = new PhantomJSDriver(caps);
    //        FirefoxDriver driver = 
    //                new FirefoxDriver(
    //                        new FirefoxBinary(new File(PATH_TO_FF)), 
    //                        new FirefoxProfile());

    DesiredCapabilities ff = DesiredCapabilities.firefox();
    //        DesiredCapabilities chrome = DesiredCapabilities.chrome();
    RemoteWebDriver driver = null;
    try {
        driver = new RemoteWebDriver(new URL(HUB_URL), ff);
        //            driver = new RemoteWebDriver(new URL("http://172.17.0.23:4444/wd/hub"), chrome);
        String script = IOUtils.toString(
                TanaguruJsTestCaseExtractor.class.getClassLoader().getResourceAsStream(JS_SCRIPT_NAME));

        for (Element el : Jsoup.parse(new URL(URL_LIST), 5000).select("a")) {
            String url = el.attr("abs:href");
            String resourceName = StringUtils.remove(el.attr("href"), ".html");
            if (StringUtils.contains(url, "html")) {
                driver.get(url);
                driver.findElementByTagName("body").sendKeys(Keys.TAB);
                try {
                    FileUtils.write(new File(PATH_TO_WRITE + resourceName + ".json"),
                            new JSONArray(driver.executeScript(script).toString()).toString(4));
                } catch (JSONException ex) {
                }
            }
        }
    } finally {
        if (driver != null) {
            driver.quit();
        }
    }
}