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:com.gargoylesoftware.htmlunit.WebDriverTestCase.java

License:Apache License

/**
 * Release resources but DON'T close the browser if we are running with a real browser.
 * Note that HtmlUnitDriver is not cached by default, but that can be configured by {@link #isWebClientCached()}.
 *//*from   w  w w  .  j a v  a  2 s  . com*/
@After
@Override
public void releaseResources() {
    super.releaseResources();

    if (!isWebClientCached()) {
        if (webDriver_ != null) {
            webDriver_.quit();
        }
        assertTrue("There are still JS threads running after the test", getJavaScriptThreads().isEmpty());
    }

    if (useRealBrowser()) {
        synchronized (WEB_DRIVERS_REAL_BROWSERS) {
            final WebDriver driver = WEB_DRIVERS_REAL_BROWSERS.get(getBrowserVersion());
            if (driver != null) {
                try {
                    final String currentWindow = driver.getWindowHandle();

                    final Set<String> handles = driver.getWindowHandles();
                    // close all windows except the current one
                    handles.remove(currentWindow);

                    if (handles.size() > 0) {
                        for (final String handle : handles) {
                            try {
                                driver.switchTo().window(handle);
                                driver.close();
                            } catch (final NoSuchWindowException e) {
                                LOG.error("Error switching to browser window; quit browser.", e);
                                WEB_DRIVERS_REAL_BROWSERS.remove(getBrowserVersion());
                                WEB_DRIVERS_REAL_BROWSERS_USAGE_COUNT.remove(getBrowserVersion());
                                driver.quit();
                                return;
                            }
                        }

                        // we have to force WebDriver to treat the remaining window
                        // as the one we like to work with from now on
                        // looks like a web driver issue to me (version 2.47.2)
                        driver.switchTo().window(currentWindow);
                    }

                    driver.manage().deleteAllCookies();

                    // in the remaining window, load a blank page
                    driver.get("about:blank");
                } catch (final WebDriverException e) {
                    shutDownRealBrowsers();
                }
            }
        }
    }
}

From source file:com.github.igorsuhorukov.webdriver.example.Project.java

public Project(String url, WebDriver driver) {
    logger.info(String.format("Fetching %s", url));
    driver.get(url);
    WebElement main = driver.findElement(By.id("main"));
    name = main.findElement(By.tagName("h3")).getText();
    description = main.findElement(By.xpath("//h3/following-sibling::table/tbody/tr/td[1]")).getText();
    link = main.findElement(By.xpath("//td[text()='HomePage']/following-sibling::*")).getText();
    license = main.findElement(By.xpath("//td[text()='License']/following-sibling::*")).getText();
}

From source file:com.github.swt_release_fetcher.Main.java

License:Apache License

public static void main(String[] args) throws Exception {

    for (String arg : args) {
        if (arg.equals("--deploy")) {
            deployArtifacts = true;/*from  w  w  w.jav a 2 s.com*/
        }
        if (arg.equals("--help")) {
            showHelp();
        }
        if (arg.equals("--debug")) {
            debug = true;
        }
        if (arg.equals("--silent")) {
            silentMode = true;
        }
    }

    // the mirror we use for all following downloads
    String mirrorUrl = "";

    // lightweight headless browser
    WebDriver driver = new HtmlUnitDriver();

    // determine if the website has changed since our last visit
    // stop if no change was detected
    // Ignore this check if we just want to deploy
    if (!deployArtifacts) {
        SwtWebsite sw = new SwtWebsite();

        try {
            if (!sw.hasChanged(driver, WEBSITE_URL)) {
                // exit if no change was detected
                printSilent("SWT website hasn't changed since our last visit. Stopping here.");
                driver.quit();
                System.exit(0);
            } else {
                // proceed if the site has changed
                System.out
                        .println("Page change detected! You may want to run the script in deploy mode again.");
            }
        } catch (IOException ioe) {
            System.out.println(ioe.getMessage());
        }
    }

    // get SWT's main site
    printDebug("Parsing eclipse.org/swt to find a mirror");
    driver.get(WEBSITE_URL);
    printDebug(WEBSITE_URL);

    // find the stable release branch link and hit it
    final List<WebElement> elements = driver.findElements(By.linkText("Linux"));
    final String deeplink = elements.get(0).getAttribute("href");
    printDebug("deeplink: " + deeplink);
    driver.get(deeplink);

    // get the direct download link from the next page
    final WebElement directDownloadLink = driver.findElement(By.linkText("Direct link to file"));
    printDebug("direct download link: " + directDownloadLink.getAttribute("href"));

    // the direct link again redirects, here is our final download link!
    driver.get(directDownloadLink.getAttribute("href"));
    final String finalDownloadLink = driver.getCurrentUrl();
    printDebug("final download link: " + finalDownloadLink);

    // Close the browser
    driver.quit();

    // extract the mirror URL for all following downloads
    String[] foo = finalDownloadLink.split("\\/", 0);
    final String filename = foo[foo.length - 1];
    mirrorUrl = (String) finalDownloadLink.subSequence(0, finalDownloadLink.length() - filename.length());
    // debug output
    printDebug("full download url: " + finalDownloadLink);
    printDebug("mirror url: " + mirrorUrl);

    // determine current release name
    String[] releaseName = filename.split("-gtk-linux-x86.zip");
    String versionName = releaseName[0].split("-")[1];
    System.out.println("current swt version: " + versionName);

    // TODO move to properties file
    PackageInfo[] packages = {
            // Win32
            new PackageInfo("win32-win32-x86.zip", "org.eclipse.swt.win32.win32.x86"),
            new PackageInfo("win32-win32-x86_64.zip", "org.eclipse.swt.win32.win32.x86_64"),
            // Linux
            new PackageInfo("gtk-linux-ppc.zip", "org.eclipse.swt.gtk.linux.ppc"),
            new PackageInfo("gtk-linux-ppc64.zip", "org.eclipse.swt.gtk.linux.ppc64"),
            new PackageInfo("gtk-linux-x86.zip", "org.eclipse.swt.gtk.linux.x86"),
            new PackageInfo("gtk-linux-x86_64.zip", "org.eclipse.swt.gtk.linux.x86_64"),
            new PackageInfo("gtk-linux-s390.zip", "org.eclipse.swt.gtk.linux.s390"),
            new PackageInfo("gtk-linux-s390x.zip", "org.eclipse.swt.gtk.linux.s390x"),
            // OSX
            new PackageInfo("cocoa-macosx.zip", "org.eclipse.swt.cocoa.macosx"),
            new PackageInfo("cocoa-macosx-x86_64.zip", "org.eclipse.swt.cocoa.macosx.x86_64"),
            // Additional platforms
            new PackageInfo("gtk-aix-ppc.zip", "org.eclipse.swt.gtk.aix.ppc"),
            new PackageInfo("gtk-aix-ppc64.zip", "org.eclipse.swt.gtk.aix.ppc64"),
            new PackageInfo("gtk-hpux-ia64.zip", "org.eclipse.swt.gtk.hpux.ia64"),
            new PackageInfo("gtk-solaris-sparc.zip", "org.eclipse.swt.gtk.solaris.sparc"),
            new PackageInfo("gtk-solaris-x86.zip", "org.eclipse.swt.gtk.solaris.x86") };

    File downloadDir = new File("downloads");
    if (!downloadDir.exists()) {
        downloadDir.mkdirs();
    }

    for (PackageInfo pkg : packages) {
        final String zipFileName = releaseName[0] + "-" + pkg.zipName;
        final URL downloadUrl = new URL(mirrorUrl + zipFileName);
        final URL checksumUrl = new URL(mirrorUrl + "checksum/" + zipFileName + ".md5");

        System.out.print("* Downloading " + pkg.zipName + " ... ");
        Artifact artifact = new Artifact(new File(downloadDir, zipFileName), versionName, pkg.artifactId);
        artifact.downloadAndValidate(downloadUrl, checksumUrl);

        if (deployArtifacts) {
            artifact.deploy();
        }
    }
}

From source file:com.github.swt_release_fetcher.SwtWebsite.java

License:Apache License

/**
 * Determines if the website has changed since our last visit. Uses a file
 * called ''pageSource'' to persist the site
 *
 * @param driver//ww  w  . j a v a  2  s  .c o m
 * @param websiteUrl URL to SWT's project site
 * @return true if the site has changed, false if not. Also returns true on
 * the first run
 * @throws IOException If the file ''pageSource'' coudln't be read or
 * written
 */
public boolean hasChanged(WebDriver driver, String websiteUrl) throws IOException {

    driver.get(websiteUrl);
    String pageSoruce = driver.getPageSource();
    String persistedPageSource = "";
    File f = new File("pageSource");
    // create a new file to persist page source
    if (!f.exists()) {
        try {
            f.createNewFile();
        } catch (IOException ieo) {
            throw new IOException("Unable to create file " + f.getAbsolutePath(), ieo);
        }
        // if it was already there, this is not the first run of swt-release-fetcher
        // read in the file content
    } else {
        try {
            persistedPageSource = FileUtils.readFileToString(f, "utf-8");
        } catch (IOException ieo) {
            throw new IOException("Unable to read file " + f.getAbsolutePath(), ieo);
        }
    }

    // check if the page has changed
    if (persistedPageSource.equals(pageSoruce)) {
        return false;
        // NOTE: If this is the first run of swt-release-fethcer the file 
        // will be empty and thus filled with content here
    } else {
        try {
            FileUtils.writeStringToFile(f, pageSoruce);
            return true;
        } catch (IOException ieo) {
            throw new IOException("Unable to write to file " + f.getAbsolutePath(), ieo);
        }

    }
}

From source file:com.google.caja.plugin.BrowserTestCase.java

License:Apache License

protected String runBrowserTest(String label, boolean isKnownFailure, String name, String... params)
        throws Exception {
    debugHook();//  ww w. j av  a  2  s .co  m
    String result = "";
    boolean passed = false;
    try {
        localServer.start("127.0.0.1", serverPort);

        String url = testUrl(name);
        if (params != null && 0 < params.length) {
            url += "?" + Joiner.on("&").join(params);
        }
        Echo.echo("- Running " + url);

        try {
            WebDriver driver = wdh.begin();
            driver.get(url);
            result = driveBrowser(driver);
            passed = true;
        } finally {
            captureResults(label, passed);
            wdh.end(passed || isKnownFailure);
        }
    } catch (Exception e) {
        Echo.rethrow(e);
    } finally {
        localServer.stop();
    }
    return result;
}

From source file:com.google.caja.plugin.DomitaTest.java

License:Apache License

void exerciseFirefox(String pageName) {
    //System.setProperty("webdriver.firefox.bin", "/usr/bin/firefox");
    WebDriver driver = new FirefoxDriver();

    driver.get("http://localhost:8000/" + "ant-lib/com/google/caja/plugin/" + pageName);

    int clickRounds = 0;
    List<WebElement> clickingList = null;
    for (; clickRounds < clickRoundLimit; clickRounds++) {
        clickingList = driver.findElements(By.xpath("//*[contains(@class,'clickme')]/*"));
        if (clickingList.size() == 0) {
            break;
        }//from  w  w  w .jav  a 2s .c  om
        for (WebElement e : clickingList) {
            e.click();
        }
    }
    assertTrue("Too many click rounds. " + "Remaining elements = " + renderElements(clickingList),
            clickRounds < clickRoundLimit);

    int waitRounds = 0;
    List<WebElement> waitingList = null;
    for (; waitRounds < waitRoundLimit; waitRounds++) {
        waitingList = driver.findElements(By.xpath("//*[contains(@class,'waiting')]"));
        if (waitingList.size() == 0) {
            break;
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
    }
    assertTrue("Too many wait rounds. " + "Remaining elements = " + renderElements(waitingList),
            waitRounds < waitRoundLimit);

    // check the title of the document
    String title = driver.getTitle();
    assertTrue("The title shows " + title, title.contains("all tests passed"));

    driver.quit();
}

From source file:com.google.testing.web.WebTestTest.java

License:Apache License

@Test
public void newWebDriverSession() {
    WebTest wt = new WebTest();
    WebDriver driver = wt.newWebDriverSession();
    driver.get(wt.HTTPAddress().resolve("/healthz").toString());
    driver.quit();//from   w ww .j  av  a 2s  . co m
}

From source file:com.hp.test.framework.Reporting.screenshot.java

public static void generatescreenshot(String html_path, String Screenshot_file_path)
        throws MalformedURLException, FileNotFoundException, IOException {
    try {/*w ww.j a va2s .  c o  m*/
        WebDriver driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get("file:///" + html_path);//+C:/Users/yanamalp/Desktop/DS_Latest/DSIntegrationTest/ATU%20Reports/Results/Run_3/CurrentRun.html");
        new Select(driver.findElement(By.id("tcFilter"))).selectByVisibleText("Skipped Test Cases");
        File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        // Now you can do whatever you need to do with it, for example copy somewhere
        FileUtils.copyFile(scrFile, new File(Screenshot_file_path));
        driver.quit();
    } catch (Exception e) {
        log.error("Error in getting Screen shot for the Last run" + e.getMessage());
    }
}

From source file:com.huangyunkun.jviff.runner.OpenRunner.java

License:Apache License

@Override
public void run(final WebDriver webDriver, final URL host, final Step step) {
    try {// w  w  w .  ja va2 s  . c o m
        URL url = new URL(host, step.getContent());
        webDriver.get(url.toString());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.ibm.watson.app.qaclassifier.selenium.drivers.SkipWelcomeScreenRule.java

License:Open Source License

@Override
protected void before() throws Throwable {
    WebDriver driver = driverRef.get();
    driver.get(env.getAppUrl());
    if (config.skipWelcomeScreen()) {
        if (didSkipWelcomeScreen.add(driver)) {
            skipWelcomeScreen();/*from  w w  w . j  ava2  s . c o  m*/
        }
        CommonFunctions.waitForApp(driver);
    }
}