Example usage for org.openqa.selenium WebDriver getCurrentUrl

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

Introduction

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

Prototype

String getCurrentUrl();

Source Link

Document

Get a string representing the current URL that the browser is looking at.

Usage

From source file:com.gwtplatform.carstore.cucumber.application.BasePage.java

License:Apache License

public void waitUntilPlaceIsLoaded(final String nameToken) {
    webDriverWait().until(new Function<WebDriver, Boolean>() {
        @Override//w w w .  ja v  a 2 s.  co  m
        public Boolean apply(WebDriver webDriver) {
            return webDriver.getCurrentUrl().contains("#" + nameToken);
        }
    });
}

From source file:com.hack23.cia.systemintegrationtest.UserPageVisit.java

License:Apache License

/**
 * Grab screenshot./*w ww . jav a2s.  com*/
 *
 * @param webDriver
 *            the web driver
 */
private static void grabScreenshot(final WebDriver webDriver) {
    final File scrFile = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE);
    try {
        screenShotNumber = screenShotNumber + 1;
        final String screenShotName = webDriver.getCurrentUrl().replace(systemTestTargetUrl, "")
                .replaceAll("#", "-").replaceAll("!", "-").replaceAll("/", "-").replaceAll("--", "-");
        FileUtils.copyFile(scrFile,
                new File("target/site/screenshots/Page" + screenShotName + "-" + screenShotNumber + ".png"));
    } catch (final IOException e) {
        e.printStackTrace();
    }
}

From source file:com.hotwire.selenium.desktop.us.partners.CarPartnerPageProvider.java

License:Open Source License

public static CarPartnersPage get(WebDriver webDriver) {

    // Wait while inner content will be loaded
    webDriver.manage().timeouts().pageLoadTimeout(PAGE_LOAD_TIMEOUT, TimeUnit.SECONDS);

    String url = webDriver.getCurrentUrl();
    LOGGER.info("PARTNER URL: " + url);

    if (checkUrl(url, "http://www.bookingbuddy.com")) {
        return new CarBookingBuddyPage(webDriver);
    }/* ww  w.  jav a2  s  .  c o  m*/
    if (checkUrl(url, "http://www.kayak.com")) {
        return new CarKayakPage(webDriver);
    }
    if (checkUrl(url, "http://www.carrentals.com")) {
        return new CarRentalsPage(webDriver);
    }
    if (checkUrl(url, "http://www.expedia.com")) {
        return new CarExpediaPage(webDriver);
    }
    if (checkUrl(url, "http://www.orbitz.com")) {
        return new CarOrbitzPage(webDriver);
    }

    throw new RuntimeException("Partner's page wasn't resolved");
}

From source file:com.hotwire.selenium.desktop.us.partners.HotelPartnerPageDetector.java

License:Open Source License

public static NAME detect(WebDriver webDriver) {

    // Wait while inner content will be loaded
    webDriver.manage().timeouts().pageLoadTimeout(PAGE_LOAD_TIMEOUT, TimeUnit.SECONDS);

    String url = webDriver.getCurrentUrl();
    LOGGER.info("PARTNER URL: " + url);

    for (NAME name : NAME.values()) {
        if (url.contains(name.pageName.toLowerCase())) {
            return name;
        }/*from w ww .  ja v  a2s  .co m*/
    }
    throw new RuntimeException("Could not recognize the url " + url);
}

From source file:com.ixxus.IxxusAbstractTest.java

/**
 * User Log out using logout URL Assumes User is logged in.
 *
 * @param driver WebDriver Instance/* w  w  w .j  a v  a  2 s. c  om*/
 */
public void logout(WebDriver driver) {
    if (driver != null) {
        try {
            if (driver.getCurrentUrl().contains(shareUrl.trim())) {
                shareUtil.logout(driver);
                if (logger.isTraceEnabled()) {
                    logger.trace("Logout");
                }
            }
        } catch (Exception e) {
            // Already logged out.
        }
    }
}

From source file:com.java.AppTestType_18_11_2015.java

public void SIGNOUT(WebDriver driver) {
    driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
    try {/*from  www  .j a  va2 s  .co m*/
        String url = driver.getCurrentUrl();
        String url2 = url.toUpperCase();
        //according to new to changes the following is not needed
        /*if(url2.contains("LISTINGID"))
        {
           driver.findElement(By.id("uname")).click();
           Thread.sleep(2000);
                   
        }*/

        driver.findElement(By.linkText("Sign Out")).click();
        if (driver.findElement(By.linkText("Sign In")).isDisplayed())
            resultDetails.setFlag(true);

    } catch (Exception e) {
        e.printStackTrace();
        resultDetails.setFlag(false);
    }
}

From source file:com.java.AppTestType_18_11_2015.java

public void VERIFYTHIS(WebDriver driver, String fieldText, String value) {
    //Used to verify the text in any specific Loctor or Message
    //<XPH or LNK or IDI or TXT><Locator> or MSG in fieldText 
    //Text or Message to be verified in Value
    try {/*from  w  w  w.  j a va  2  s  .  c  o  m*/
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.MILLISECONDS);
        String f = fieldText;
        String val = value;
        String locatorType = f.substring(0, 3);
        String locatorValue = f.substring(3, f.length());
        if (locatorType.equalsIgnoreCase("XPH")) {
            System.out.println(driver.findElement(By.xpath(locatorValue)).getText());
            if (driver.findElement(By.xpath(locatorValue)).getText().equalsIgnoreCase(val))
                resultDetails.setFlag(true);
        } else if (locatorType.equalsIgnoreCase("LNK")) {

            if (driver.findElement(By.linkText(locatorValue)).getText().equalsIgnoreCase(val))
                resultDetails.setFlag(true);

        } else if (locatorType.equalsIgnoreCase("IDI") || locatorType.equalsIgnoreCase("TXT")) {

            if (driver.findElement(By.id(locatorValue)).getText().equalsIgnoreCase(val))
                resultDetails.setFlag(true);

        } else if (locatorType.equalsIgnoreCase("MSG")) {

            int j = 0;
            for (int i = 0; i < 20; i++) {
                if (driver.findElement(By.xpath("//*[contains(text(),'" + val + "')]")).isDisplayed()) {
                    j = 1;
                    break;
                }
                Thread.sleep(1000);

            }
            if (j == 1)
                resultDetails.setFlag(true);

        }

        else if (locatorType.equalsIgnoreCase("TTL")) {
            int j = 0;
            for (int i = 0; i < 20; i++) {
                if (driver.getTitle().equalsIgnoreCase(val)) {
                    j = 1;
                    break;
                }
                Thread.sleep(1000);

            }
            if (j == 1)
                resultDetails.setFlag(true);

        } else if (locatorType.equalsIgnoreCase("URL")) {
            int j = 0;
            for (int i = 0; i < 20; i++) {
                if (driver.getCurrentUrl().equalsIgnoreCase(val)) {
                    j = 1;
                    break;
                }
                Thread.sleep(1000);

            }
            if (j == 1)
                resultDetails.setFlag(true);

        }
    }

    catch (Exception e) {
        e.printStackTrace();
        resultDetails.setErrorMessage(
                fieldText + " Text is not matching with that of the locator (or) the locator not found");
        resultDetails.setFlag(false);
    }

}

From source file:com.liferay.faces.test.selenium.browser.internal.BrowserDriverImpl.java

License:Open Source License

@Override
public String getCurrentWindowUrl() {

    WebDriver webDriver = getWebDriver();

    return webDriver.getCurrentUrl();
}

From source file:com.mgmtp.jfunk.web.JFunkWebDriverEventListener.java

License:Apache License

/**
 * Saves the currently displayed browser window. The page title is used for the filename -
 * preceded by some identifying information (thread, counter). Pages of the same type are
 * collected inside the same subdirectory. The subdirectory uses
 * {@link SaveOutput#getIdentifier()} for its name. If an alert is present, saving is not
 * supported and thus skipped./*www.  ja v a 2 s .  c  o  m*/
 *
 * @param action
 *            the event which triggered to save the page. Will be included in the filename.
 * @param triggeredBy
 *            the object which triggered the event (e.g. a button or a link)
 */
protected void savePage(final WebDriver driver, final String action, final String triggeredBy) {
    try {
        // this updates the driver's window handles, so a subsequent call to
        // getWindowHandle() fails if the window no longer exists
        driver.getWindowHandles();
        driver.getWindowHandle();
    } catch (NoSuchWindowException ex) {
        // Window is already closed. Saving the page could cause problems, e. g.
        // ChromeDriver ould hang.
        return;
    }
    File moduleArchiveDir = moduleArchiveDirProvider.get();
    if (moduleArchiveDir == null) {
        return;
    }

    if (config.getBoolean(JFunkConstants.ARCHIVE_DO_NOT_SAVE_WHEN_ALERT, false)) {
        try {
            // Saving the page does not work if an alert is present
            driver.switchTo().alert();
            log.trace("Cannot save page. Alert is present.");
            return;
        } catch (NoAlertPresentException ex) {
            // ignore
        } catch (UnsupportedOperationException ex) {
            // ignore
            // HtmlUnit does not support alerts
        } catch (Exception ex) {
            // ignore
        }
    }

    for (SaveOutput saveOutput : SaveOutput.values()) {
        boolean saveSwitch = saveOutputMap.get(saveOutput);
        if (!saveSwitch) {
            // Saving is disabled by property
            continue;
        }

        File f = null;
        try {
            f = dumpFileCreatorProvider.get().createDumpFile(
                    new File(moduleArchiveDir, saveOutput.getIdentifier()), saveOutput.getExtension(),
                    driver.getCurrentUrl(), action);

            if (f == null) {
                return;
            }

            switch (saveOutput) {
            case HTML:
                StringBuilder html = new StringBuilder();
                html.append("<!-- Requested URL: ");
                html.append(driver.getCurrentUrl());
                html.append(" -->");
                html.append(IOUtils.LINE_SEPARATOR);
                html.append(driver.getPageSource());
                writeStringToFile(f, html.toString(), "UTF-8");
                copyFile(f, new File(moduleArchiveDir, JFunkConstants.LASTPAGE_HTML));
                log.trace("Saving page: filename={}, action={}, trigger={}, response={}", f.getName(), action,
                        triggeredBy, driver.getCurrentUrl());
                break;
            case PNG:
                if (driver instanceof TakesScreenshot) {
                    File tmpFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
                    if (tmpFile != null) {
                        copyFile(tmpFile, f);
                        log.trace("Saving page: filename={}, action={}, trigger={}, response={}", f.getName(),
                                action, triggeredBy, driver.getCurrentUrl());
                        deleteQuietly(tmpFile);
                    }
                }
                break;
            case HTML_VALIDATION:
                /*
                 * JFunkWebDriver.getPageSource() doesn't return the complete page source
                 * e.g. DOCTYPE is missing. Therefore we are using a more complicated way to
                 * retrieve the "real" page source. However, this only works when using
                 * HtmlUnitDriver.
                 */
                if (WebDriverUtils.isHtmlUnitDriver(driver)) {
                    String content = ((HtmlPage) WebDriverUtils.getHtmlUnitDriverWebClient(driver)
                            .getCurrentWindow().getEnclosedPage()).getWebResponse().getContentAsString();
                    writeStringToFile(f, content, "UTF-8");
                    HtmlValidatorUtil.validateHtml(f.getParentFile(), config, f);
                }
                break;
            default:
                throw new IllegalStateException("unknown enum constant");
            }
        } catch (Exception ex) {
            log.error("Could not save file: {}. {}", f, ex.getMessage());
            return;
        }
    }
}

From source file:com.opera.core.systems.NavigationTest.java

License:Apache License

@Test
// TODO(andreastt): Should be made local
public void testHttpRedirect() {
    final String fetchedUrl = "http://t/core/bts/javascript/CORE-26410/003-2.php";
    driver.navigate().to(fetchedUrl);/*from   w  w  w  .j  a v a  2s .c  om*/

    // Wait for redirect
    Wait<WebDriver> wait = new WebDriverWait(driver, OperaIntervals.PAGE_LOAD_TIMEOUT.getValue());
    wait.until(new ExpectedCondition<Object>() {
        public Boolean apply(WebDriver driver) {
            return !driver.getCurrentUrl().equals(fetchedUrl);
        }
    });

    assertEquals("http://t/core/bts/javascript/CORE-26410/001-3.php", driver.getCurrentUrl());
}