Example usage for org.openqa.selenium By tagName

List of usage examples for org.openqa.selenium By tagName

Introduction

In this page you can find the example usage for org.openqa.selenium By tagName.

Prototype

public static By tagName(String tagName) 

Source Link

Usage

From source file:com.gargoylesoftware.htmlunit.javascript.host.LocationTest.java

License:Apache License

/**
 * @throws Exception if the test fails/* www  . j  a v a 2s  .c o m*/
 */
@Test
public void onlick_set_location() throws Exception {
    final String html = "<html><head></head>\n" + "<body>\n"
            + "<a href='foo2.html' onclick='document.location = \"foo3.html\"'>click me</a>\n"
            + "</body></html>";

    getMockWebConnection().setDefaultResponse("");
    final WebDriver driver = loadPageWithAlerts2(html);
    driver.findElement(By.tagName("a")).click();

    final String[] expectedRequests = { "", "foo3.html", "foo2.html" };
    assertEquals(expectedRequests, getMockWebConnection().getRequestedUrls(getDefaultUrl()));

    assertEquals(new URL(getDefaultUrl(), "foo2.html").toString(), driver.getCurrentUrl());
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.MessageChannelTest.java

License:Apache License

/**
 * @throws Exception if an error occurs/*  www .  ja v a2  s.  c  o m*/
 */
@Test
@Alerts("Message back from the IFrame, Hello from the main page!")
public void test() throws Exception {
    final String html = "<html><body>\n" + "<p>My body</p>\n" + "<iframe src='" + URL_SECOND
            + "' width='480' height='320'></iframe>\n" + "</body>\n" + "<script>\n"
            + "  if (window.MessageChannel) {\n" + "    var channel = new MessageChannel();\n"
            + "    var para = document.querySelector('p');\n"
            + "    var ifr = document.querySelector('iframe');\n" + "    var otherWindow = ifr.contentWindow;\n"
            + "    function iframeLoaded() {\n"
            + "      otherWindow.postMessage('Hello from the main page!', '*', [channel.port2]);\n" + "    }\n"
            + "    ifr.addEventListener('load', iframeLoaded, false);\n" + "    function handleMessage(e) {\n"
            + "      para.innerHTML = e.data;\n" + "    }\n" + "    channel.port1.onmessage = handleMessage;\n"
            + "  }\n" + "</script></html>";

    final String html2 = "<html><body>\n" + "  <p>iFrame body</p>\n" + "</body>\n" + "<script>\n"
            + "  if (window.MessageChannel) {\n" + "    var para = document.querySelector('p');\n"
            + "    onmessage = function(e) {\n" + "      para.innerHTML = e.data;\n"
            + "      e.ports[0].postMessage('Message back from the IFrame');\n" + "    }\n" + "  }\n"
            + "</script></html>";

    getMockWebConnection().setResponse(URL_SECOND, html2);
    final WebDriver driver = loadPage2(html);
    final List<String> actual = new ArrayList<>();
    actual.add(driver.findElement(By.tagName("p")).getText());
    driver.switchTo().frame(0);
    actual.add(driver.findElement(By.tagName("p")).getText());
    assertEquals(getExpectedAlerts(), actual);
}

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

License:Apache License

/**
 * Ensure that response stream can be read more than one time.
 * @throws Exception if an error occurs//from  ww  w  .  ja v  a 2 s .c  om
 */
@Test
public void readStreamTwice() throws Exception {
    final String html = "<html>\n" + "<body>\n" + "<iframe src='binaryFile.bin'></iframe>\n"
            + "<iframe src='foo.html'></iframe>\n" + "</body></html>";

    final MockWebConnection mockConnection = getMockWebConnection();
    final byte[] binaryContent = new byte[4818];
    final Random random = new Random();
    for (int i = 0; i < binaryContent.length; i++) {
        binaryContent[i] = (byte) (random.nextInt(Byte.MAX_VALUE));
    }
    mockConnection.setDefaultResponse(binaryContent, 200, "OK", "application/octet-stream");
    final URL urlFoo = new URL(URL_FIRST, "foo.html");
    mockConnection.setResponse(urlFoo, "<html></html>");

    final WebDriver driver = loadPage2(html);
    final WebElement iframe1 = driver.findElement(By.tagName("iframe"));
    if (driver instanceof HtmlUnitDriver) {
        final HtmlInlineFrame htmlUnitIFrame1 = (HtmlInlineFrame) toHtmlElement(iframe1);
        final WebResponse iframeWebResponse = htmlUnitIFrame1.getEnclosedPage().getWebResponse();
        byte[] receivedBytes = IOUtils.toByteArray(iframeWebResponse.getContentAsStream());
        receivedBytes = IOUtils.toByteArray(iframeWebResponse.getContentAsStream());
        assertArrayEquals(binaryContent, receivedBytes);
    }
}

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

License:Apache License

/**
 * Regression test for issue 3193004./*from www . j a v  a  2  s . c  o m*/
 * 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

/**
 * Regression test for bug 2822048: a 302 redirect without Location header.
 * @throws Exception if an error occurs//from   w ww .  jav  a2 s .  c o 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/*from w  ww .  java2s  .  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./*  w  w w .j av  a 2 s. c  o  m*/
 * 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.ggasoftware.jdiuitest.web.selenium.elements.pageobjects.annotations.WebAnnotationsUtil.java

License:Open Source License

public static By getFindByLocator(FindBy locator) {
    if (locator == null)
        return null;
    if (!"".equals(locator.id()))
        return By.id(locator.id());
    if (!"".equals(locator.className()))
        return By.className(locator.className());
    if (!"".equals(locator.xpath()))
        return By.xpath(locator.xpath());
    if (!"".equals(locator.css()))
        return By.cssSelector(locator.css());
    if (!"".equals(locator.linkText()))
        return By.linkText(locator.linkText());
    if (!"".equals(locator.name()))
        return By.name(locator.name());
    if (!"".equals(locator.partialLinkText()))
        return By.partialLinkText(locator.partialLinkText());
    if (!"".equals(locator.tagName()))
        return By.tagName(locator.tagName());
    return null;/*  w ww . j a  v a  2  s .  com*/
}

From source file:com.gigaspaces.webuitf.datagrid.configuration.ConfigurationGrid.java

License:Open Source License

protected String getValue(WebElement webElement) {

    WebElement valueColumnElement = webElement.findElement(By.className("x-grid3-col-configValueCol"));
    WebElement spanElement = valueColumnElement.findElement(By.tagName("span"));
    return spanElement.getText();
}