Example usage for org.openqa.selenium WebDriver findElement

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

Introduction

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

Prototype

@Override
WebElement findElement(By by);

Source Link

Document

Find the first WebElement using the given method.

Usage

From source file:com.gargoylesoftware.htmlunit.html.HtmlAnchor2Test.java

License:Apache License

/**
 * @throws Exception if the test fails/*from   w ww .j a v a2  s.  c om*/
 */
@Test
public void clickAnchorName() throws Exception {
    final String html = "<html>\n" + "<head><title>foo</title></head>\n" + "<body>\n"
            + "  <a href='#clickedAnchor' id='a1'>link to foo1</a>\n" + "</body></html>";

    final MockWebConnection webConnection = getMockWebConnection();
    final WebDriver driver = loadPage2(html);

    assertEquals(1, webConnection.getRequestCount());

    driver.findElement(By.id("a1")).click();
    assertEquals(1, webConnection.getRequestCount()); // no second server hit
}

From source file:com.gargoylesoftware.htmlunit.html.HtmlAnchor2Test.java

License:Apache License

/**
 * @throws Exception if the test fails//from  w  w  w.j  ava  2  s  .  co m
 */
@Test
@Alerts({ "", "#anchor", "#!bang" })
public void dontReloadHashBang() throws Exception {
    final String html = "<html>\n" + "<head><title>foo</title></head>\n" + "<body>\n" + "  <a href='"
            + URL_FIRST + "test' id='a1'>link1</a>\n" + "  <a href='" + URL_FIRST
            + "test#anchor' id='a2'>link2</a>\n" + "  <a href='" + URL_FIRST + "test#!bang' id='a3'>link3</a>\n"
            + "  <script>\n" + "    alert(document.getElementById('a1').hash);\n"
            + "    alert(document.getElementById('a2').hash);\n"
            + "    alert(document.getElementById('a3').hash);\n" + "  </script>\n" + "</body></html>";

    final MockWebConnection webConnection = getMockWebConnection();
    webConnection.setDefaultResponse(html);

    final WebDriver driver = loadPageWithAlerts2(html);

    assertEquals(1, webConnection.getRequestCount());

    driver.findElement(By.id("a1")).click();
    assertEquals(2, webConnection.getRequestCount());

    driver.findElement(By.id("a2")).click();
    assertEquals(2, webConnection.getRequestCount());

    driver.findElement(By.id("a3")).click();
    assertEquals(2, webConnection.getRequestCount());
}

From source file:com.gargoylesoftware.htmlunit.html.HtmlAnchor2Test.java

License:Apache License

/**
 * Testcase for issue #1492.//from  w ww  .j a va2 s.c  om
 *
 * @throws Exception if the test fails
 */
@Test
@Alerts({ "#!board/WebDev", "#!article/WebDev/35", "#!article/WebDev/35" })
public void dontReloadHashBang2() throws Exception {
    final String html = "<html>\n" + "<head><title>foo</title></head>\n" + "<body>\n" + "  <a href='"
            + URL_FIRST + "test/#!board/WebDev' id='a1'>link1</a>\n" + "  <a href='" + URL_FIRST
            + "test/#!article/WebDev/35' id='a2'>link2</a>\n" + "  <a href='" + URL_FIRST
            + "test#!article/WebDev/35' id='a3'>link2</a>\n" + "  <script>\n"
            + "    alert(document.getElementById('a1').hash);\n"
            + "    alert(document.getElementById('a2').hash);\n"
            + "    alert(document.getElementById('a3').hash);\n" + "  </script>\n" + "</body></html>";

    final MockWebConnection webConnection = getMockWebConnection();
    webConnection.setDefaultResponse(html);

    final WebDriver driver = loadPageWithAlerts2(html);

    assertEquals(1, webConnection.getRequestCount());

    driver.findElement(By.id("a1")).click();
    assertEquals(2, webConnection.getRequestCount());

    driver.findElement(By.id("a2")).click();
    assertEquals(2, webConnection.getRequestCount());

    driver.findElement(By.id("a3")).click();
    assertEquals(3, webConnection.getRequestCount());
}

From source file:com.gargoylesoftware.htmlunit.html.HtmlAnchor2Test.java

License:Apache License

/**
 * FF behaves is different./*w ww .j av  a2s.  co  m*/
 * @throws Exception if an error occurs
 */
@Test
@Alerts(IE = "click href click doubleClick ", CHROME = "click href click href doubleClick ", FF = "click href click doubleClick href ")
@BuggyWebDriver({ FF, CHROME })
@NotYetImplemented({ FF, IE })
public void doubleClick() throws Exception {
    final String html = "<html>\n" + "<body>\n" + "  <a id='myAnchor' "
            + "href=\"javascript:document.getElementById('myTextarea').value+='href ';void(0);\" "
            + "onClick=\"document.getElementById('myTextarea').value+='click ';\" "
            + "onDblClick=\"document.getElementById('myTextarea').value+='doubleClick ';\">foo</a>\n"
            + "  <textarea id='myTextarea'></textarea>\n" + "</body></html>";

    final WebDriver driver = loadPage2(html);

    final Actions action = new Actions(driver);
    action.doubleClick(driver.findElement(By.id("myAnchor")));
    action.perform();

    assertEquals(getExpectedAlerts()[0], driver.findElement(By.id("myTextarea")).getAttribute("value"));
}

From source file:com.gargoylesoftware.htmlunit.html.HtmlAnchorTest.java

License:Apache License

/**
 * @throws Exception if an error occurs/*from  w  ww.  j a va  2 s  .c o  m*/
 */
@Test
@Alerts({ "hi", "%28%29" })
public void href_js_escaping() throws Exception {
    final String html = "<html><head><script>\n" + "  function sayHello(text) {\n" + "    alert(text);\n"
            + "  }\n" + "</script></head>\n" + "<body>\n"
            + "  <a id='myAnchor' href=\"javascript:sayHello%28'hi'%29\">My Link</a>\n"
            + "  <input id='myButton' type=button onclick=\"javascript:sayHello('%28%29')\" value='My Button'>\n"
            + "</body></html>";

    final WebDriver driver = loadPage2(html);
    driver.findElement(By.id("myAnchor")).click();
    verifyAlerts(driver, getExpectedAlerts()[0]);
    driver.findElement(By.id("myButton")).click();
    verifyAlerts(driver, getExpectedAlerts()[1]);
}

From source file:com.gargoylesoftware.htmlunit.html.HtmlAnchorTest.java

License:Apache License

/**
 * @throws Exception if an error occurs//w w w . jav  a 2s .com
 */
@Test
@Alerts({ "(*%a", "%28%A" })
public void href_js_escaping2() throws Exception {
    final String html = "<html><head><script>\n" + "  function sayHello(text) {\n" + "    alert(text);\n"
            + "  }\n" + "</script></head>\n" + "<body>\n"
            + "  <a id='myAnchor' href=\"javascript:sayHello%28'%28%2a%a'%29\">My Link</a>\n"
            + "  <input id='myButton' type=button onclick=\"javascript:sayHello('%28%A')\" value='My Button'>\n"
            + "</body></html>";

    final WebDriver driver = loadPage2(html);
    driver.findElement(By.id("myAnchor")).click();
    verifyAlerts(driver, getExpectedAlerts()[0]);
    driver.findElement(By.id("myButton")).click();
    verifyAlerts(driver, getExpectedAlerts()[1]);
}

From source file:com.gargoylesoftware.htmlunit.html.HtmlAnchorTest.java

License:Apache License

/**
 * @throws Exception if an error occurs/*w ww.  j a  va2s.c o  m*/
 */
@Test
public void clickNestedElement() throws Exception {
    final String html = "<html>\n" + "<body>\n" + "  <a href='page2.html'>\n"
            + "    <span id='theSpan'>My Link</span>\n" + "  </a>\n" + "</body></html>";

    getMockWebConnection().setDefaultResponse("");
    final WebDriver driver = loadPage2(html);
    final WebElement span = driver.findElement(By.id("theSpan"));
    assertEquals("span", span.getTagName());
    span.click();
    assertEquals(URL_FIRST + "page2.html", driver.getCurrentUrl());
}

From source file:com.gargoylesoftware.htmlunit.html.HtmlAnchorTest.java

License:Apache License

/**
 * @throws Exception if an error occurs//from   w  w  w .  j  a  v  a2 s .  c  om
 */
@Test
public void clickNestedButtonElement() throws Exception {
    final String html = "<html>\n" + "<body>\n" + "  <a href='page2.html'>\n"
            + "    <button id='theButton'></button>\n" + "  </a>\n" + "</body></html>";

    getMockWebConnection().setDefaultResponse("");
    final WebDriver driver = loadPage2(html);
    final WebElement button = driver.findElement(By.id("theButton"));
    assertEquals("button", button.getTagName());
    button.click();
    assertEquals(URL_FIRST + "page2.html", driver.getCurrentUrl());
}

From source file:com.gargoylesoftware.htmlunit.html.HtmlAnchorTest.java

License:Apache License

/**
 * @throws Exception if an error occurs//w ww.  j av  a 2s  .  c o  m
 */
@Test
@Alerts(DEFAULT = "", FF = "page2.html")
public void clickNestedCheckboxElement() throws Exception {
    final String html = "<html>\n" + "<body>\n" + "  <a href='page2.html'>\n"
            + "    <input type='checkbox' id='theCheckbox' name='myCheckbox' value='Milk'>\n" + "  </a>\n"
            + "</body></html>";

    getMockWebConnection().setDefaultResponse("");
    final WebDriver driver = loadPage2(html);
    final WebElement checkbox = driver.findElement(By.id("theCheckbox"));
    assertEquals("input", checkbox.getTagName());
    checkbox.click();
    assertEquals(URL_FIRST + getExpectedAlerts()[0], driver.getCurrentUrl());
}

From source file:com.gargoylesoftware.htmlunit.html.HtmlAnchorTest.java

License:Apache License

/**
 * @throws Exception if an error occurs// w  ww  .  ja  v a  2 s .  co m
 */
@Test
public void clickNestedImageElement() throws Exception {
    final String html = "<html>\n" + "<body>\n" + "  <a href='page2.html'>\n"
            + "    <img id='theImage' src='test.png' />\n" + "  </a>\n" + "</body></html>";

    getMockWebConnection().setDefaultResponse("");
    final WebDriver driver = loadPage2(html);
    final WebElement img = driver.findElement(By.id("theImage"));
    assertEquals("img", img.getTagName());
    img.click();
    assertEquals(URL_FIRST + "page2.html", driver.getCurrentUrl());
}