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.gargoylesoftware.htmlunit.javascript.host.Window2Test.java

License:Apache License

/**
 * @throws Exception if the test fails//from  w w  w  .j a v  a  2 s . c  om
 */
@Test
public void setDocumentLocation() throws Exception {
    final String firstContent = "<html>\n" + "<head><title>First</title></head>\n" + "<body>\n"
            + "<form name='form1'>\n" + "  <a id='link' onClick='document.location=\"" + URL_SECOND
            + "\";'>Click me</a>\n" + "</form>\n" + "</body></html>";
    final String secondContent = "<html><head><title>Second</title></head><body></body></html>";

    getMockWebConnection().setResponse(URL_SECOND, secondContent);

    final WebDriver driver = loadPage2(firstContent);
    assertEquals("First", driver.getTitle());
    assertEquals(1, driver.getWindowHandles().size());

    driver.findElement(By.id("link")).click();
    assertEquals("Second", driver.getTitle());

    assertEquals(1, driver.getWindowHandles().size());
    assertEquals(new String[] { "", "second/" }, getMockWebConnection().getRequestedUrls(URL_FIRST));
    assertEquals(URL_SECOND.toString(), driver.getCurrentUrl());
}

From source file:com.gargoylesoftware.htmlunit.WebClient3Test.java

License:Apache License

/**
 * Regression test for issue 3193004.//from w  w  w.  j a v a  2  s .com
 * Ensure that the click returns once the target page has been loaded into the target window.
 * @throws Exception if an error occurs
 */
@Test
public void clickReturnsWhenThePageHasBeenCompleteLoaded() throws Exception {
    final String firstContent = "<html><head>\n" + "<script>window.setInterval(\'',1);</script></head>\n"
            + "<body><a href='" + URL_SECOND + "'>to second</a></body></html>";
    final String secondContent = "<html><body></body></html>";

    final MockWebConnection webConnection = getMockWebConnection();
    webConnection.setResponse(URL_SECOND, secondContent);

    for (int i = 1; i < 100; i++) {
        final WebDriver webDriver = loadPage2(firstContent);
        webDriver.findElement(By.tagName("a")).click();
        assertEquals("Run " + i, URL_SECOND.toExternalForm(), webDriver.getCurrentUrl());
    }
}

From source file:com.gargoylesoftware.htmlunit.WebClient6Test.java

License:Apache License

private void redirectGet(final int code, final HttpMethod httpMethod, final String redirectUrl)
        throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_
            + "<html><body><a href='redirect.html'>redirect</a></body></html>";
    final int reqCount = getMockWebConnection().getRequestCount();

    final URL url = new URL(URL_FIRST, "page2.html");
    getMockWebConnection().setResponse(url, html);

    final List<NameValuePair> headers = new ArrayList<>();
    headers.add(new NameValuePair("Location", redirectUrl));
    getMockWebConnection().setDefaultResponse("", code, "Found", null, headers);

    expandExpectedAlertsVariables(URL_FIRST);
    final WebDriver driver = loadPage2(html);
    driver.findElement(By.tagName("a")).click();

    final String[] expected = getExpectedAlerts();

    assertEquals(reqCount + Integer.parseInt(expected[1]), getMockWebConnection().getRequestCount());
    assertEquals(httpMethod, getMockWebConnection().getLastWebRequest().getHttpMethod());
    // assertEquals(getExpectedAlerts()[0], getMockWebConnection().getLastWebRequest().getUrl().toString());
    assertEquals(expected[0], driver.getCurrentUrl());
}

From source file:com.gargoylesoftware.htmlunit.WebClient6Test.java

License:Apache License

private void redirectPost(final int code, final HttpMethod httpMethod, final String redirectUrl,
        final boolean resendParams) throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_
            + "<html><body><form action='redirect.html' method='POST'>\n"
            + "  <input type='hidden' name='param1' value='paramValue'>\n"
            + "  <input type='submit' id='postBtn' value='Submit'>\n" + "</form></body></html>";
    final int reqCount = getMockWebConnection().getRequestCount();

    final URL url = new URL(URL_FIRST, "page2.html");
    getMockWebConnection().setResponse(url, html);

    final List<NameValuePair> headers = new ArrayList<>();
    headers.add(new NameValuePair("Location", redirectUrl));
    getMockWebConnection().setDefaultResponse("", code, "Found", null, headers);

    expandExpectedAlertsVariables(URL_FIRST);
    final WebDriver driver = loadPage2(html);
    driver.findElement(By.id("postBtn")).click();

    final String[] expected = getExpectedAlerts();

    assertEquals(reqCount + Integer.parseInt(expected[1]), getMockWebConnection().getRequestCount());
    assertEquals(httpMethod, getMockWebConnection().getLastWebRequest().getHttpMethod());

    if (resendParams) {
        assertTrue(getMockWebConnection().getLastWebRequest().getRequestParameters().size() > 0);

        final NameValuePair param = getMockWebConnection().getLastWebRequest().getRequestParameters().get(0);
        if ("param1".equals(param.getName())) {
            assertEquals("paramValue", param.getValue());
        } else if ("test".equals(param.getName())) {
            assertEquals("foo", param.getValue());
        } else if (!param.getName().startsWith("ignore")) {
            Assert.fail("unexpected param '" + param.getName() + "'");
        }/*from   w  w w  .  j a  va  2  s  .co m*/
    } else {
        assertEquals(0, getMockWebConnection().getLastWebRequest().getRequestParameters().size());
    }

    // assertEquals(getExpectedAlerts()[0], getMockWebConnection().getLastWebRequest().getUrl().toString());
    assertEquals(expected[0], driver.getCurrentUrl());
}

From source file:com.gargoylesoftware.htmlunit.WebClient6Test.java

License:Apache License

/**
 * Regression test for bug 2822048: a 302 redirect without Location header.
 * @throws Exception if an error occurs//from w w  w  .  j a va2 s  .co m
 */
// TODO [IE]ERRORPAGE real IE displays his own error page (res://ieframe.dll/dnserror.htm#<url>)
@Test
public void redirect302WithoutLocation() throws Exception {
    final String html = "<html><body><a href='page2'>to redirect</a></body></html>";
    getMockWebConnection().setDefaultResponse("", 302, "Found", null);

    final WebDriver driver = loadPage2(html);
    driver.findElement(By.tagName("a")).click();
    assertEquals(URL_FIRST + "page2", driver.getCurrentUrl());
}

From source file:com.gargoylesoftware.htmlunit.WebClient6Test.java

License:Apache License

/**
 * Regression test for bug 3017719: a 302 redirect should change the page url.
 * @throws Exception if an error occurs//  ww w  . ja va2s  .c  o m
 */
@Test
public void redirect302ChangePageUrl() throws Exception {
    final String html = "<html><body><a href='redirect.html'>redirect</a></body></html>";

    final URL url = new URL(URL_FIRST, "page2.html");
    getMockWebConnection().setResponse(url, html);

    final List<NameValuePair> headers = new ArrayList<>();
    headers.add(new NameValuePair("Location", "/page2.html"));
    getMockWebConnection().setDefaultResponse("", 302, "Found", null, headers);

    final WebDriver driver = loadPage2(html);
    driver.findElement(By.tagName("a")).click();
    assertEquals(url.toString(), driver.getCurrentUrl());
}

From source file:com.gargoylesoftware.htmlunit.WebClient6Test.java

License:Apache License

/**
 * Regression test for bug 3035155./*from w  ww  .j av a  2s  . c  om*/
 * Bug was fixes in HttpClient 4.1.
 * @throws Exception if an error occurs
 */
@Test
public void redirect302UrlsInQuery() throws Exception {
    final String html = "<html><body><a href='redirect.html'>redirect</a></body></html>";

    final URL url = new URL(URL_FIRST, "page2.html");
    getMockWebConnection().setResponse(url, html);

    final List<NameValuePair> headers = new ArrayList<>();
    headers.add(new NameValuePair("Location", "/page2.html?param=http%3A//somwhere.org"));
    getMockWebConnection().setDefaultResponse("", 302, "Found", null, headers);

    final WebDriver driver = loadPage2(html);
    driver.findElement(By.tagName("a")).click();
    assertEquals(url.toString() + "?param=http%3A//somwhere.org", driver.getCurrentUrl());
}

From source file:com.giri.target.svr.cmd.ISCommandProcessor.java

License:Open Source License

private String resolve(String string, WebDriver selenium) {
    String output = string;//from w w w.j a  va 2 s  .c o m
    if (string == "title") {
        output = selenium.getTitle();
    } else if (string == "url") {
        output = selenium.getCurrentUrl();
    }
    return output;
}

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 ww  .  j a  v a  2 s. c om
        }
        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.google.appengine.tck.env.appspot.AppspotLoginHandler.java

License:Open Source License

public void login(WebDriver driver, LoginContext context) {
    try {//from   w  w w .  jav a2 s .  co m
        WebElement email = driver.findElement(By.id("Email"));
        if (email.getAttribute("readonly") == null) {
            email.clear();
            email.sendKeys(context.getEmail());
        }

        WebElement password = driver.findElement(By.id("Passwd"));
        password.sendKeys(context.getPassword());

        driver.findElement(By.name("signIn")).click();
    } catch (NoSuchElementException e) {
        throw new IllegalStateException(
                String.format("URL[%s]\n\n%s", driver.getCurrentUrl(), driver.getPageSource()), e);
    }
}