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 an error occurs/*from   www.  java  2 s .  c o 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(new URL(getDefaultUrl(), "page2.html").toString(), driver.getCurrentUrl());
}

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

License:Apache License

/**
 * @throws Exception if an error occurs/*from   w w w.  j  ava  2  s  .c o  m*/
 */
@Test
@Alerts(DEFAULT = "page2.html", IE = "")
public void clickNestedInputImageElement() throws Exception {
    final String html = "<html>\n" + "<body>\n" + "  <a href='page2.html'>\n"
            + "    <input type='image' id='theInput' />\n" + "  </a>\n" + "</body></html>";

    getMockWebConnection().setDefaultResponse("");
    final WebDriver driver = loadPage2(html);
    final WebElement input = driver.findElement(By.id("theInput"));
    assertEquals("input", input.getTagName());
    input.click();
    assertEquals(new URL(getDefaultUrl(), getExpectedAlerts()[0]).toString(), driver.getCurrentUrl());
}

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

License:Apache License

/**
 * @throws Exception if an error occurs//  ww  w  . ja  v a 2s. c  o  m
 */
@Test
@Alerts(DEFAULT = "page2.html", IE = "")
public void clickNestedInputTextElement() throws Exception {
    final String html = "<html>\n" + "<body>\n" + "  <a href='page2.html'>\n"
            + "    <input type='text' id='theInput' />\n" + "  </a>\n" + "</body></html>";

    getMockWebConnection().setDefaultResponse("");
    final WebDriver driver = loadPage2(html);
    final WebElement input = driver.findElement(By.id("theInput"));
    assertEquals("input", input.getTagName());
    input.click();
    assertEquals(new URL(getDefaultUrl(), getExpectedAlerts()[0]).toString(), driver.getCurrentUrl());
}

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

License:Apache License

/**
 * @throws Exception if an error occurs//from ww  w.  ja v a  2s.  c  o m
 */
@Test
@Alerts(DEFAULT = "page2.html", IE = "")
public void clickNestedInputPasswordElement() throws Exception {
    final String html = "<html>\n" + "<body>\n" + "  <a href='page2.html'>\n"
            + "    <input type='password' id='theInput' />\n" + "  </a>\n" + "</body></html>";

    getMockWebConnection().setDefaultResponse("");
    final WebDriver driver = loadPage2(html);
    final WebElement input = driver.findElement(By.id("theInput"));
    assertEquals("input", input.getTagName());
    input.click();
    assertEquals(new URL(getDefaultUrl(), getExpectedAlerts()[0]).toString(), driver.getCurrentUrl());
}

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

License:Apache License

/**
 * @throws Exception if an error occurs/* ww w  . java2  s. com*/
 */
@Test
public void clickNestedOptionElement() throws Exception {
    final String html = "<html>\n" + "<body>\n" + "  <a href='page2.html'>\n" + "    <select>\n"
            + "      <option id='theOption'>test</option>\n" + "    </select>\n" + "  </a>\n"
            + "</body></html>";

    getMockWebConnection().setDefaultResponse("");
    final WebDriver driver = loadPage2(html);
    final WebElement option = driver.findElement(By.id("theOption"));
    assertEquals("option", option.getTagName());
    option.click();
    assertEquals(new URL(getDefaultUrl(), "page2.html").toString(), driver.getCurrentUrl());
}

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

License:Apache License

/**
 * @throws Exception if an error occurs/*  w  w w . j  av a  2s  .c  om*/
 */
@Test
@Alerts(DEFAULT = "page2.html", CHROME = "", IE = "")
public void clickNestedRadioElement() throws Exception {
    final String html = "<html>\n" + "<body>\n" + "  <a href='page2.html'>\n"
            + "    <input type='radio' id='theRadio' name='myRadio' value='Milk'>\n" + "  </a>\n"
            + "</body></html>";

    getMockWebConnection().setDefaultResponse("");
    final WebDriver driver = loadPage2(html);
    final WebElement radio = driver.findElement(By.id("theRadio"));
    assertEquals("input", radio.getTagName());
    radio.click();
    assertEquals(new URL(getDefaultUrl(), getExpectedAlerts()[0]).toString(), driver.getCurrentUrl());
}

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

License:Apache License

/**
 * @throws Exception if an error occurs//from w ww.j a  va2 s  .com
 */
@Test
@Alerts("page2.html")
public void clickNestedResetElement() throws Exception {
    final String html = "<html>\n" + "<body>\n" + "  <a href='page2.html'>\n"
            + "    <input type='reset' id='theInput' />\n" + "  </a>\n" + "</body></html>";

    getMockWebConnection().setDefaultResponse("");
    final WebDriver driver = loadPage2(html);
    final WebElement input = driver.findElement(By.id("theInput"));
    assertEquals("input", input.getTagName());
    input.click();
    assertEquals(new URL(getDefaultUrl(), getExpectedAlerts()[0]).toString(), driver.getCurrentUrl());
}

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

License:Apache License

/**
 * @throws Exception if an error occurs//from w ww. j  av  a 2 s.c  om
 */
@Test
@Alerts("page2.html")
public void clickNestedSubmitElement() throws Exception {
    final String html = "<html>\n" + "<body>\n" + "  <a href='page2.html'>\n"
            + "    <input type='submit' id='theInput' />\n" + "  </a>\n" + "</body></html>";

    getMockWebConnection().setDefaultResponse("");
    final WebDriver driver = loadPage2(html);
    final WebElement input = driver.findElement(By.id("theInput"));
    assertEquals("input", input.getTagName());
    input.click();
    assertEquals(new URL(getDefaultUrl(), getExpectedAlerts()[0]).toString(), driver.getCurrentUrl());
}

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

License:Apache License

/**
 * @throws Exception if an error occurs//w w  w . j  ava  2s. c o  m
 */
@Test
public void clickBlankTargetHashOnly() throws Exception {
    final String html = "<html>\n" + "<head><title>foo</title></head>\n" + "<body>\n"
            + "<a id='a' target='_blank' href='#'>Foo</a>\n" + "</body></html>\n";

    final WebDriver driver = loadPage2(html);
    assertEquals(1, driver.getWindowHandles().size());

    final WebElement tester = driver.findElement(By.id("a"));
    tester.click();
    assertEquals(2, driver.getWindowHandles().size());
}

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

License:Apache License

/**
 * @throws Exception if the test fails// w  w  w .ja va 2  s .  com
 */
@Test
public void click() throws Exception {
    final String html = "<html>\n" + "<head><title>foo</title></head>\n" + "<body>\n"
            + "<a href='http://www.foo1.com' id='a1'>link to foo1</a>\n" + "<a href='" + URL_SECOND
            + "' id='a2'>link to foo2</a>\n" + "</body></html>";

    final String secondContent = "<html><head><title>Second</title></head><body></body></html>";

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

    final WebDriver driver = loadPage2(html);
    assertEquals(1, webConnection.getRequestCount());

    // Test that the correct value is being passed back up to the server
    driver.findElement(By.id("a2")).click();

    assertEquals(URL_SECOND.toExternalForm(), driver.getCurrentUrl());
    assertSame("method", HttpMethod.GET, webConnection.getLastMethod());
    assertTrue(webConnection.getLastParameters().isEmpty());

    assertEquals(2, webConnection.getRequestCount());
}