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.HtmlHeadTest.java

License:Apache License

/**
 * @throws Exception if the test fails/* w ww.  j  a v  a  2s .com*/
 */
@Test
@Alerts("[object HTMLHeadElement]")
public void simpleScriptable() throws Exception {
    final String html = "<html><head id='myId'><script>\n" + "  function test() {\n"
            + "    alert(document.getElementById('myId'));\n" + "  }\n" + "</script>\n"
            + "</head><body onload='test()'>\n" + "</body></html>";

    final WebDriver driver = loadPageWithAlerts2(html);
    if (driver instanceof HtmlUnitDriver) {
        final HtmlElement element = toHtmlElement(driver.findElement(By.id("myId")));
        assertTrue(element instanceof HtmlHead);
    }
}

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

License:Apache License

/**
 * Verifies that a asText() returns an empty string.
 * @throws Exception if the test fails//from   www. j  a v  a2  s  .  c  om
 */
@Test
public void asText() throws Exception {
    final String htmlContent = "<html><head><title>foo</title></head><body>\n" + "<form id='form1'>\n"
            + "  <input type='hidden' name='foo' id='foo' value='bla'>\n" + "</form></body></html>";

    final WebDriver driver = loadPage2(htmlContent);

    final WebElement input = driver.findElement(By.id("foo"));
    assertEquals("", input.getText());
}

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

License:Apache License

/**
 * @throws Exception if an error occurs//w  ww  . j a va 2 s.co  m
 */
@Test
public void setValueOnChange() throws Exception {
    final String html = "<html>\n" + "<head></head>\n" + "<body>\n"
            + "  <input type='hidden' id='h' value='Hello world'"
            + " onChange='alert(\"foo\");alert(event.type);'>\n" + "  <button id='b'>some button</button>\n"
            + "  <button id='set' onclick='document.getElementById(\"h\").value=\"HtmlUnit\"'>setValue</button>\n"
            + "</body></html>";

    final WebDriver driver = loadPage2(html);
    driver.findElement(By.id("set")).click();

    assertEquals(Collections.emptyList(), getCollectedAlerts(driver));

    // trigger lost focus
    driver.findElement(By.id("b")).click();
    assertEquals(Collections.emptyList(), getCollectedAlerts(driver));
}

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

License:Apache License

/**
 * @throws Exception if an error occurs//from w w w. j  a  va 2s  . c  o  m
 */
@Test
public void setDefaultValueOnChange() throws Exception {
    final String html = "<html>\n" + "<head></head>\n" + "<body>\n"
            + "  <input type='hidden' id='h' value='Hello world'"
            + " onChange='alert(\"foo\");alert(event.type);'>\n" + "  <button id='b'>some button</button>\n"
            + "  <button id='set' onclick='document.getElementById(\"h\").defaultValue=\"HtmlUnit\"'>"
            + "setValue</button>\n" + "</body></html>";

    final WebDriver driver = loadPage2(html);
    driver.findElement(By.id("set")).click();

    assertEquals(Collections.emptyList(), getCollectedAlerts(driver));

    // trigger lost focus
    driver.findElement(By.id("b")).click();
    assertEquals(Collections.emptyList(), getCollectedAlerts(driver));
}

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

License:Apache License

private void isDisplayed(final String src) throws Exception {
    try (InputStream is = getClass().getClassLoader().getResourceAsStream("testfiles/tiny-jpg.img")) {
        final byte[] directBytes = IOUtils.toByteArray(is);
        final URL urlImage = new URL(URL_FIRST, "img.jpg");
        final List<NameValuePair> emptyList = Collections.emptyList();
        getMockWebConnection().setResponse(urlImage, directBytes, 200, "ok", "image/jpg", emptyList);
    }/*from   w  w  w  .j  a  va  2s  .co m*/

    final String html = "<html><head><title>Page A</title></head>\n" + "<body>\n" + "  <img id='myImg' " + src
            + " >\n" + "</body></html>";

    final WebDriver driver = loadPage2(html);

    final boolean displayed = driver.findElement(By.id("myImg")).isDisplayed();
    assertEquals(Boolean.parseBoolean(getExpectedAlerts()[0]), displayed);
}

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

License:Apache License

/**
 * @throws Exception if the test fails//from w w  w . j  av a 2 s  .  c  om
 */
@Test
@Alerts(DEFAULT = "URL?button.x=0&button.y=0", CHROME = "URL?button.x=9&button.y=7&button=foo", IE = "URL?button.x=14&button.y=15")
@NotYetImplemented({ CHROME, IE })
public void click_NoPosition() throws Exception {
    final String html = "<html><head><title>foo</title></head><body>\n" + "<form id='form1'>\n"
            + "<input type='image' name='aButton' value='foo'/>\n"
            + "<input type='image' name='button' value='foo'/>\n"
            + "<input type='image' name='anotherButton' value='foo'/>\n" + "</form></body></html>";
    final WebDriver webDriver = loadPage2(html);
    webDriver.findElement(By.name("button")).click();

    expandExpectedAlertsVariables(URL_FIRST);
    assertEquals(getExpectedAlerts()[0], webDriver.getCurrentUrl());
}

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

License:Apache License

/**
 * @throws Exception if the test fails// w w w .  ja va  2 s  .c o  m
 */
@Test
@Alerts(DEFAULT = "URL?button.x=0&button.y=0", CHROME = "URL?button.x=22&button.y=7", IE = "URL?button.x=14&button.y=15")
@NotYetImplemented({ CHROME, IE })
public void click_NoPosition_NoValue() throws Exception {
    final String html = "<html><head><title>foo</title></head><body>\n" + "<form id='form1'>\n"
            + "<input type='image' name='button'>\n" + "</form></body></html>";
    final WebDriver webDriver = loadPage2(html);
    webDriver.findElement(By.name("button")).click();

    expandExpectedAlertsVariables(URL_FIRST);
    assertEquals(getExpectedAlerts()[0], webDriver.getCurrentUrl());
}

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

License:Apache License

/**
 * @throws Exception if the test fails//from  www. j  av  a  2 s.  c o  m
 */
@Test
@Alerts("URL?imageInput.x=0&imageInput.y=0")
public void javascriptClick() throws Exception {
    final String html = "<html><head><title>foo</title>\n" + "</head><body>\n" + "<form>\n"
            + "  <input type='image' name='imageInput'>\n"
            + "  <input type='button' id='submit' value='submit' "
            + "onclick='document.getElementsByName(\"imageInput\")[0].click()'>\n" + "</form>\n"
            + "</body></html>";

    final WebDriver webDriver = loadPage2(html);
    webDriver.findElement(By.id("submit")).click();

    expandExpectedAlertsVariables(URL_FIRST);
    assertEquals(getExpectedAlerts()[0], webDriver.getCurrentUrl());
}

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

License:Apache License

/**
 * Test for bug: http://sourceforge.net/p/htmlunit/bugs/646/.
 * @throws Exception if an error occurs//from  w  ww .ja v a  2 s .com
 */
@Test
@Alerts("1")
public void clickFiresOnMouseDown() throws Exception {
    final String html = "<html><body><input type='image' src='x.png' id='i' onmousedown='alert(1)'></body></html>";

    final WebDriver webDriver = loadPage2(html);
    webDriver.findElement(By.id("i")).click();

    verifyAlerts(webDriver, getExpectedAlerts());
}

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

License:Apache License

/**
 * Test for bug: http://sourceforge.net/p/htmlunit/bugs/646/.
 * @throws Exception if an error occurs// w w w .ja  v  a 2  s .c  o  m
 */
@Test
@Alerts("1")
public void clickFiresOnMouseUp() throws Exception {
    final String html = "<html><body><input type='image' src='x.png' id='i' onmouseup='alert(1)'></body></html>";
    final WebDriver webDriver = loadPage2(html);
    webDriver.findElement(By.id("i")).click();

    verifyAlerts(webDriver, getExpectedAlerts());
}