Example usage for org.openqa.selenium WebDriver get

List of usage examples for org.openqa.selenium WebDriver get

Introduction

In this page you can find the example usage for org.openqa.selenium WebDriver get.

Prototype

void get(String url);

Source Link

Document

Load a new web page in the current browser window.

Usage

From source file:edu.umd.cs.guitar.ripper.WebApplicationTest.java

License:Open Source License

public void testConnectHtmlUnit() {
    WebDriver driver = new HtmlUnitDriver();
    String URL = "http://www.google.com/analytics/";
    driver.get(URL);
    System.out.println("DONE");
}

From source file:edu.umd.cs.guitar.simple.HtmlUnitDriverTest.java

License:Open Source License

@Test
public void test() {
    WebDriver driver;
    driver = new HtmlUnitDriver();
    ((HtmlUnitDriver) driver).setJavascriptEnabled(true);

    driver.get("about:blank");
    ((JavascriptExecutor) driver).executeScript(injectAnchorTag(XYZ_TEST, "about:blank"));
    System.out.println(driver.getWindowHandles().size());

    // Click on the anchor element
    WebElement anchor;//  w ww.  j  a  va  2 s. c  o m
    anchor = driver.findElement(By.id(XYZ_TEST));
    anchor.click();

    System.out.println(driver.getWindowHandles().size());
}

From source file:edu.usc.cs.ir.htmlunit.handler.HtmlUnitWebDriver.java

License:Apache License

public static WebDriver getDriverForPage(String url) {
    long pageLoadTimout = 3;
    enableJavascript = true;//from   w ww .  ja  v  a2s. com
    enableCss = false;
    javascriptTimeout = 13500;
    int redirects = 4;
    enableRedirect = redirects <= 0 ? false : true;
    maxRedirects = redirects;

    WebDriver driver = null;

    try {
        driver = new HtmlUnitWebDriver();
        driver.manage().timeouts().pageLoadTimeout(pageLoadTimout, TimeUnit.SECONDS);
        driver.get(url);
    } catch (Exception e) {
        if (e instanceof TimeoutException) {
            System.out.println("HtmlUnit WebDriver: Timeout Exception: Capturing whatever loaded so far...");
            return driver;
        }
        cleanUpDriver(driver);
        throw new RuntimeException(e);
    }

    return driver;
}

From source file:edu.usc.cs.ir.selenium.handler.Arguntrader.java

License:Apache License

public static void main(String[] args) {
    Arguntrader glocktalk = new Arguntrader();
    FirefoxProfile profile = new FirefoxProfile();
    profile.setPreference(FirefoxProfile.ALLOWED_HOSTS_PREFERENCE, "localhost");
    profile.setPreference("dom.ipc.plugins.enabled.libflashplayer.so", "false");
    profile.setPreference("permissions.default.stylesheet", 1);
    profile.setPreference("permissions.default.image", 1);

    FirefoxBinary binary = new FirefoxBinary();
    binary.setTimeout(TimeUnit.SECONDS.toMillis(180));

    WebDriver driver = new FirefoxDriver(binary, profile);
    driver.manage().timeouts().pageLoadTimeout(10000, TimeUnit.MILLISECONDS);

    try {/*from   w w w.  java 2 s  .co  m*/
        driver.get("http://arguntrader.com/viewforum.php?f=8");
        System.out.println(new String(glocktalk.processDriver(driver).getBytes("UTF-8")));
    } catch (Exception e) {
        if (e instanceof TimeoutException) {
            System.out.println("Timeout Exception");

            try {
                System.out.println(new String(glocktalk.processDriver(driver).getBytes("UTF-8")));
            } catch (UnsupportedEncodingException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

        }
        e.printStackTrace();
    } finally {
        if (driver != null) {
            driver.close();
            driver.quit();
        }
    }
}

From source file:edu.usc.cs.ir.selenium.handler.Glocktalk.java

License:Apache License

public String processDriver(WebDriver driver) {
    StringBuffer buffer = new StringBuffer();

    // Extract content - getText doesn't return any links
    String content = driver.findElement(By.tagName("body")).getText();
    buffer.append(content).append("\n");

    // Extract threads from a forum through all of its navigation
    if (driver.getCurrentUrl().startsWith("http://www.glocktalk.com/forum")) {
        // Extract the last page number
        List<WebElement> pages = driver.findElements(By.xpath("//div[@class='PageNav']//nav//a[@class='']"));
        buffer.append(getThreadsFromForum(driver)).append("\n");

        if (pages != null && !pages.isEmpty()) {

            WebElement currentPage = driver
                    .findElement(By.xpath("//div[@class='PageNav']//nav//a[@class='currentPage ']"));
            Integer currentPageNo = Integer.parseInt(currentPage.getText());
            if (currentPageNo % PAGE_FETCH_LIMIT == 1) {
                Integer lastPage = Integer.parseInt(pages.get(pages.size() - 1).getText());
                String currentUrl = driver.getCurrentUrl().substring(0,
                        driver.getCurrentUrl().lastIndexOf("/") + 1);
                int i = currentPageNo + 1;
                for (; i < (currentPageNo + PAGE_FETCH_LIMIT) && i <= lastPage; i++) {
                    try {
                        driver.get(currentUrl + "page-" + i);
                    } catch (TimeoutException e) {
                        System.out.println("Timeout Exception for page = " + i);
                    } finally {
                        buffer.append(getThreadsFromForum(driver)).append("\n");
                    }//from  www .ja  v  a2s. com
                }

                if (i <= lastPage)
                    buffer.append("<a href=\"").append(currentUrl + "page-" + i).append("\" />").append("\n");
            }
        }
    }

    // Extract all links from a thread
    else if (driver.getCurrentUrl().startsWith("http://www.glocktalk.com/threads")) {

        // Extract the last page number
        List<WebElement> pages = driver.findElements(By.xpath("//div[@class='PageNav']//nav//a[@class='']"));
        buffer.append(getPostsFromThread(driver)).append("\n");

        if (pages != null && !pages.isEmpty()) {

            WebElement currentPage = driver
                    .findElement(By.xpath("//div[@class='PageNav']//nav//a[@class='currentPage ']"));
            Integer currentPageNo = Integer.parseInt(currentPage.getText());
            if (currentPageNo % PAGE_FETCH_LIMIT == 1) {
                Integer lastPage = Integer.parseInt(pages.get(pages.size() - 1).getText());
                String currentUrl = driver.getCurrentUrl().substring(0,
                        driver.getCurrentUrl().lastIndexOf("/") + 1);
                int i = currentPageNo + 1;
                for (; i < (currentPageNo + PAGE_FETCH_LIMIT) && i <= lastPage; i++) {
                    try {
                        driver.get(currentUrl + "page-" + i);
                    } catch (TimeoutException e) {
                        System.out.println("Timeout Exception for page = " + i);
                    } finally {
                        buffer.append(getPostsFromThread(driver)).append("\n");
                    }
                }

                if (i <= lastPage)
                    buffer.append("<a href=\"").append(currentUrl + "page-" + i).append("\" />").append("\n");
            }
        }

    }

    // Extract User Images
    else if (driver.getCurrentUrl().startsWith("http://www.glocktalk.com/members")) {
        List<WebElement> srcLinks = driver.findElements(By.xpath("//div[@class='profilePage']//*[@src]"));
        Set<String> images = new HashSet<String>();
        for (WebElement link : srcLinks) {
            String linkValue = null;
            if (!link.getTagName().equals("img"))
                continue;
            linkValue = link.getAttribute("src");
            System.out.println(linkValue);
            images.add("<img src=\"" + linkValue + "\" />");
        }
        buffer.append(images);
    }

    // Extract Media - Images
    else if (driver.getCurrentUrl().startsWith("http://www.glocktalk.com/media")) {

        // Extract the last page number
        List<WebElement> pages = driver.findElements(By.xpath("//div[@class='PageNav']//nav//a[@class='']"));
        buffer.append(getImagesFromMedia(driver)).append("\n");

        if (pages != null && !pages.isEmpty()) {

            WebElement currentPage = driver
                    .findElement(By.xpath("//div[@class='PageNav']//nav//a[@class='currentPage ']"));
            Integer currentPageNo = Integer.parseInt(currentPage.getText());
            if (currentPageNo % PAGE_FETCH_LIMIT == 1) {
                Integer lastPage = Integer.parseInt(pages.get(pages.size() - 1).getText());
                String currentUrl = driver.getCurrentUrl().substring(0,
                        driver.getCurrentUrl().lastIndexOf("/"));
                int i = currentPageNo + 1;
                for (; i < (currentPageNo + PAGE_FETCH_LIMIT) && i <= lastPage; i++) {
                    try {
                        driver.get(currentUrl + "?page=" + i);
                    } catch (TimeoutException e) {
                        System.out.println("Timeout Exception for page = " + i);
                    } finally {
                        buffer.append(getImagesFromMedia(driver)).append("\n");
                    }
                }

                if (i <= lastPage)
                    buffer.append("<a href=\"").append(currentUrl + "?page=" + i).append("\" />").append("\n");
            }
        }
    }

    //System.out.println();
    return buffer.toString();
}

From source file:edu.usc.cs.ir.selenium.handler.Glocktalk.java

License:Apache License

public static void main(String[] args) {
    Glocktalk glocktalk = new Glocktalk();
    FirefoxProfile profile = new FirefoxProfile();
    profile.setPreference(FirefoxProfile.ALLOWED_HOSTS_PREFERENCE, "localhost");
    profile.setPreference("dom.ipc.plugins.enabled.libflashplayer.so", "false");
    profile.setPreference("permissions.default.stylesheet", 1);
    profile.setPreference("permissions.default.image", 1);

    FirefoxBinary binary = new FirefoxBinary();
    binary.setTimeout(TimeUnit.SECONDS.toMillis(180));

    WebDriver driver = new FirefoxDriver(binary, profile);
    driver.manage().timeouts().pageLoadTimeout(10000, TimeUnit.MILLISECONDS);

    try {/*w  w  w  .  java 2 s .com*/
        driver.get("http://www.glocktalk.com/media?page=6");
        System.out.println(new String(glocktalk.processDriver(driver).getBytes("UTF-8")));
    } catch (Exception e) {
        if (e instanceof TimeoutException) {
            System.out.println("Timeout Exception");

            try {
                System.out.println(new String(glocktalk.processDriver(driver).getBytes("UTF-8")));
            } catch (UnsupportedEncodingException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

        }
        //e.printStackTrace();
    } finally {
        if (driver != null) {
            driver.close();
            driver.quit();
        }
    }
}

From source file:edu.usc.cs.ir.selenium.handler.GlocktalkBasic.java

License:Apache License

public static void main(String[] args) {
    GlocktalkBasic glocktalk = new GlocktalkBasic();
    WebDriver driver = new FirefoxDriver();
    try {//from  www .j av  a  2  s .c  o  m
        driver.get("http://www.glocktalk.com/forum/general-firearms-forum.82/");
        System.out.println(new String(glocktalk.processDriver(driver).getBytes("UTF-8")));
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (driver != null) {
            driver.close();
            driver.quit();
        }
    }
}

From source file:endtoend.browser.util.JsOnOffTestUtils.java

License:Apache License

public static void assertJavaScriptIsOn(WebDriver driver) {
    driver.get(classNameToTestFileUrl(JsOnOffTestUtils.class));
    assertThat(driver.findElements(By.tagName("div")), hasSize(1 + 3));
}

From source file:endtoend.browser.util.JsOnOffTestUtils.java

License:Apache License

public static void assertJavaScriptIsOff(WebDriver driver) {
    driver.get(classNameToTestFileUrl(JsOnOffTestUtils.class));
    assertThat(driver.findElements(By.tagName("div")), hasSize(1));
}

From source file:es.udc.tfg_es.clubtriatlon.test.util.SeleniumMethods.java

License:Open Source License

private static final WebDriver auntenticate(String email, String password) {

    WebDriver driver = new FirefoxDriver();
    driver.get("http://localhost:9090/triatlon/");

    driver.findElement(By.linkText("Autenticarse")).click();
    driver.getCurrentUrl(); //Update the Url
    driver.findElement(By.name("email")).sendKeys(email);
    driver.findElement(By.name("password")).sendKeys(password);
    driver.findElement(By.id("loginForm")).findElement(By.id("loginButton")).click();

    return driver;
}