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

License:Apache License

/**
 * @throws Exception if the test fails//from  w  w  w. j a  va2 s. co m
 */
@Test
@Alerts("1")
public void testOutsideForm() throws Exception {
    final String html = "<html><head></head>\n" + "<body>\n"
            + "<input id='myInput' type='image' src='test.png' onclick='alert(1)'>\n" + "</body></html>";
    final WebDriver webDriver = loadPage2(html);
    webDriver.findElement(By.id("myInput")).click();

    verifyAlerts(webDriver, getExpectedAlerts());
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*from  ww  w  .ja  v  a2s  . 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.HtmlImageInputTest.java

License:Apache License

/**
 * @throws Exception if the test fails/* ww  w. jav  a 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.HtmlImageInputTest.java

License:Apache License

/**
 * @throws Exception if the test fails/*from   ww w . j a  v a2s .c  o m*/
 */
@Test
@Alerts("1")
public void outsideForm() throws Exception {
    final String html = "<html><head></head>\n" + "<body>\n"
            + "<input id='myInput' type='image' src='test.png' onclick='alert(1)'>\n" + "</body></html>";
    final WebDriver webDriver = loadPage2(html);
    webDriver.findElement(By.id("myInput")).click();

    verifyAlerts(webDriver, getExpectedAlerts());
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*  w  w w . ja v a2 s.c o  m*/
 */
@Test
@Alerts("[object HTMLIFrameElement]")
public void simpleScriptable() throws Exception {
    final String html = "<html><head>\n" + "<script>\n" + "  function test() {\n"
            + "    alert(document.getElementById('myId'));\n" + "  }\n" + "</script>\n"
            + "</head><body onload='test()'>\n" + "  <iframe id='myId'>\n" + "</body></html>";

    final WebDriver webDriver = loadPageWithAlerts2(html);

    if (webDriver instanceof HtmlUnitDriver) {
        final HtmlElement element = toHtmlElement(webDriver.findElement(By.id("myId")));
        assertTrue(HtmlInlineFrame.class.isInstance(element));
    }
}

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

License:Apache License

/**
 * Test, the right frame is used for a target, even if some frames
 * have the same name.// w  w w  . j a va 2s  .  co m
 *
 * @throws Exception if the test fails
 */
@Test
public void targetResolution() throws Exception {
    final String framesContent = "<html><head><title>Top Page</title></head>\n"
            + "<body><div id='content'>Body of top frame</div>\n"
            + "  <iframe src='left.html' id='id-left' name='left'></iframe>\n"
            + "  <iframe src='right.html' id='id-right' name='right'></iframe>\n" + "</body>\n" + "</html>";

    final String rightFrame = "<html><head><title>Right Frame</title></head>\n"
            + "<body><div id='content'>Body of right frame</div></body>\n" + "</html>";

    final String leftFrame = "<html><head><title>Left Frame</title></head>\n" + "<body>\n"
            + "  <div id='content'>Body of left frame</div>\n"
            + "  <a id='link' name='link' href='new_inner.html' target='right'>Click link</a>\n"
            + "  <iframe id='id-inner' name='right' width='100' height='100' src='inner.html'></iframe>\n"
            + "</body>\n" + "</html>";

    final String innerFrame = "<html><head><title>Inner Frame</title></head>\n"
            + "<body><div id='content'>Body of inner frame</div></body>\n" + "</html>";

    final String newInnerFrame = "<html><head><title>New inner Frame</title></head>\n"
            + "<body><div id='content'>Body of new inner frame</div></body>\n" + "</html>";

    final String baseUrl = URL_FIRST.toString();

    final URL leftFrameUrl = new URL(baseUrl + "left.html");
    final URL rightFrameUrl = new URL(baseUrl + "right.html");
    final URL innerFrameURL = new URL(baseUrl + "inner.html");
    final URL newInnerFrameURL = new URL(baseUrl + "new_inner.html");

    final MockWebConnection webConnection = getMockWebConnection();
    webConnection.setResponse(leftFrameUrl, leftFrame);
    webConnection.setResponse(rightFrameUrl, rightFrame);
    webConnection.setResponse(innerFrameURL, innerFrame);
    webConnection.setResponse(newInnerFrameURL, newInnerFrame);

    final WebDriver driver = loadPage2(framesContent);

    // top frame
    assertEquals("Top Page", driver.getTitle());
    assertEquals("Body of top frame", driver.findElement(By.id("content")).getText());

    // left frame
    driver.switchTo().frame("id-left");
    assertEquals("Body of left frame", driver.findElement(By.id("content")).getText());
    // inner frame
    driver.switchTo().frame("id-inner");
    assertEquals("Body of inner frame", driver.findElement(By.id("content")).getText());
    // right frame
    driver.switchTo().defaultContent();
    driver.switchTo().frame("id-right");
    assertEquals("Body of right frame", driver.findElement(By.id("content")).getText());

    // clicking on a link which contains a target 'right'. But this target frame is defined two times.
    driver.switchTo().defaultContent();
    driver.switchTo().frame("id-left");
    driver.findElement(By.id("link")).click();

    // left frame
    driver.switchTo().defaultContent();
    driver.switchTo().frame("id-left");
    assertEquals("Body of left frame", driver.findElement(By.id("content")).getText());
    // inner frame
    driver.switchTo().frame("id-inner");
    assertEquals("Body of new inner frame", driver.findElement(By.id("content")).getText());
    // right frame
    driver.switchTo().defaultContent();
    driver.switchTo().frame("id-right");
    assertEquals("Body of right frame", driver.findElement(By.id("content")).getText());
}

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

License:Apache License

/**
 * Looks like url's with the about schema are always behave
 * like 'about:blank'./*  www .  j av a 2  s .  com*/
 *
 * @throws Exception if the test fails
 */
@Test
@Alerts(DEFAULT = "about:blank", CHROME = "about://unsupported", IE = "exception")
@NotYetImplemented({ CHROME, IE })
public void aboutSrc() throws Exception {
    final String html = "<html><head>\n" + "<script>\n" + "  function test() {\n"
            + "    var frame = document.getElementById('tstFrame');\n" + "    try {"
            + "      alert(frame.contentWindow.location.href);\n" + "    } catch(e) { alert('exception'); }\n"
            + "  }\n" + "</script>\n" + "</head>\n" + "<body>\n"
            + "  <iframe id='tstFrame' src='about://unsupported'></iframe>\n"
            + "  <button id='test' onclick='test()'>Test</button>\n" + "</body></html>";

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

    assertEquals(getExpectedAlerts(), getCollectedAlerts(driver));
}

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

License:Apache License

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

    final WebDriver webDriver = loadPageWithAlerts2(html);

    if (webDriver instanceof HtmlUnitDriver) {
        final HtmlElement element = toHtmlElement(webDriver.findElement(By.id("myId")));
        assertTrue(HtmlInlineFrame.class.isInstance(element));
    }
}

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

License:Apache License

private void testClickEventSequence(final String input, final boolean onClickRetFalse) throws Exception {
    final String events = " onmousedown=\"log('mousedown')\" onmouseup=\"log('mouseup')\" "
            + "onfocus=\"log('onfocus')\" onchange=\"log('onchange')\" " + "onclick=\"log('onclick')\"";
    String tag = StringUtils.replaceOnce(input, ">", events + ">");
    if (onClickRetFalse) {
        tag = StringUtils.replaceOnce(tag, "onclick')", "onclick');return false;");
    }/*from w w  w .j av  a 2  s  .  com*/

    final String html = "<html><head>\n" + "<script>\n" + "  function log(x) {\n"
            + "    document.getElementById('log_').value += x + '; ';\n" + "  }\n" + "</script>\n"

            + "</head>\n<body>\n" + "<form>\n" + "  <textarea id='log_' rows='4' cols='50'></textarea>\n" + tag
            + "\n" + "  <input type='text' id='next'>\n" + "</form>\n"

            + "</body></html>";

    final List<String> alerts = new LinkedList<>();

    final WebDriver driver = loadPage2(html);
    final WebElement log = driver.findElement(By.id("log_"));

    driver.findElement(By.id(TEST_ID)).click();
    alerts.add(log.getAttribute("value").trim());

    log.clear();
    driver.findElement(By.id("next")).click();

    alerts.add(log.getAttribute("value").trim());
    assertEquals(getExpectedAlerts(), alerts);
}

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

License:Apache License

/**
 * @throws Exception if an error occurs/* w  w w.  j a  v a2  s.co  m*/
 */
@Test
@Alerts({ "labelclick", "chboxclick" })
public void clickFor() throws Exception {
    final String html = "<html>\n" + "<body>\n"
            + "  <label id='label1' for='checkbox1' onclick='alert(\"labelclick\")'>Toggle checkbox</label>\n"
            + "  <input type='checkbox' id='checkbox1' onclick='alert(\"chboxclick\")'>\n" + "</body></html>";
    final WebDriver driver = loadPage2(html);
    driver.findElement(By.id("label1")).click();
    verifyAlerts(driver, getExpectedAlerts());
}