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

License:Apache License

/**
 * @throws Exception if an error occurs//w ww  . j  a va  2 s  .com
 */
@Test
public void isDisplayed() throws Exception {
    final String html = "<html>\n" + "<head>\n" + "  <title>foo</title>\n"
            + "  <style type='text/css' id='s'>\n" + "    img { border: 0px }\n" + "  </style>\n" + "</head>\n"
            + "<body>\n" + "</body>\n" + "</html>";

    final WebDriver driver = loadPageWithAlerts2(html);
    final boolean displayed = driver.findElement(By.id("s")).isDisplayed();
    assertFalse(displayed);
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*  ww w. ja v  a 2  s  . c  o m*/
 */
@Test
public void submit() throws Exception {
    final String html = "<html><head><title>foo</title></head><body>\n" + "<form id='form1' method='post'>\n"
            + "<input type='submit' name='aButton' value='foo'/>\n"
            + "<input type='suBMit' name='button' value='foo'/>\n"
            + "<input type='submit' name='anotherButton' value='foo'/>\n" + "</form></body></html>";

    final WebDriver wd = loadPageWithAlerts2(html);

    final WebElement button = wd.findElement(By.name("button"));
    button.click();

    assertEquals("foo", wd.getTitle());

    assertEquals(Collections.singletonList(new NameValuePair("button", "foo")),
            getMockWebConnection().getLastParameters());
}

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

License:Apache License

/**
 * @throws Exception if the test fails// w w  w.  j  a v a  2  s.c o  m
 */
@Test
@Alerts({ "foo", "bar" })
public void click_onClick() throws Exception {
    final String html = "<html><head><title>foo</title></head><body>\n"
            + "<form id='form1' onSubmit='alert(\"bar\"); return false;'>\n"
            + "  <input type='submit' name='button' value='foo' onClick='alert(\"foo\")'/>\n"
            + "</form></body></html>";

    final WebDriver webDriver = loadPage2(html);

    final WebElement button = webDriver.findElement(By.name("button"));
    button.click();

    verifyAlerts(webDriver, getExpectedAlerts());
}

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

License:Apache License

/**
 * @throws Exception if the test fails//from   w  w  w  . j a  v  a  2s  .  c om
 */
@Test
public void click_onClick_JavascriptReturnsTrue() throws Exception {
    final String html = "<html><head><title>First</title></head><body>\n"
            + "<form name='form1' method='get' action='foo.html'>\n"
            + "<input name='button' type='submit' value='PushMe' id='button1'"
            + "onclick='return true'/></form>\n" + "</body></html>";
    final String secondHtml = "<html><head><title>Second</title></head><body></body></html>";

    getMockWebConnection().setResponse(new URL(URL_FIRST, "foo.html"), secondHtml);

    final WebDriver wd = loadPageWithAlerts2(html);

    final WebElement button = wd.findElement(By.id("button1"));
    button.click();

    assertEquals("Second", wd.getTitle());
}

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

License:Apache License

/**
 * @throws Exception if the test fails// w  w  w.  j ava 2 s.c  o m
 */
@Test
@Alerts("1")
public void outsideForm() throws Exception {
    final String html = "<html><head></head>\n" + "<body>\n"
            + "<input id='myInput' type='submit' onclick='alert(1)'>\n" + "</body></html>";

    final WebDriver webDriver = loadPage2(html);
    final WebElement input = webDriver.findElement(By.id("myInput"));
    input.click();

    verifyAlerts(webDriver, getExpectedAlerts());
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*w w  w .  ja v  a  2  s  .  co  m*/
 */
@Test
@Alerts("2")
public void doubleSubmission() throws Exception {
    final String html = "<html>\n" + "<head>\n" + "  <script type='text/javascript'>\n"
            + "    function submitForm() {\n"
            + "      document.deliveryChannelForm.submitBtn.disabled = true;\n"
            + "      document.deliveryChannelForm.submit();\n" + "    }\n" + "  </script>\n" + "</head>\n"
            + "<body>\n" + "  <form action='test' name='deliveryChannelForm'>\n"
            + "    <input name='submitBtn' type='submit' value='Save' title='Save' onclick='submitForm();'>\n"
            + "  </form>\n" + "</body>\n" + "</html>";

    getMockWebConnection().setDefaultResponse("");
    final WebDriver webDriver = loadPage2(html);
    final WebElement input = webDriver.findElement(By.name("submitBtn"));
    input.click();

    assertEquals(Integer.parseInt(getExpectedAlerts()[0]), getMockWebConnection().getRequestCount());
}

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

License:Apache License

/**
 * @throws Exception if the test fails/* w ww .  j a v  a2  s. c  o m*/
 */
@Test
public void doubleSubmissionWithRedirect() throws Exception {
    final String html = "<html><body>\n" + "<script>\n" + "function submitForm(btn) {\n"
            + "  btn.form.submit();\n" + "  btn.disabled = true;\n" + "}\n" + "</script>\n"
            + "<form method='post' action='test'>\n" + "  <input name='text' type='text'>\n"
            + "  <input name='btn' type='submit' onclick='submitForm(this);'>\n" + "</form>\n"
            + "</body></html>";

    final MockWebConnection mockWebConnection = getMockWebConnection();

    final List<NameValuePair> redirectHeaders = Arrays.asList(new NameValuePair("Location", "/nextPage"));
    mockWebConnection.setResponse(new URL(URL_FIRST, "test"), "", 302, "Found", null, redirectHeaders);

    mockWebConnection.setResponse(new URL(URL_FIRST, "nextPage"),
            "<html><head><title>next page</title></head></html>");

    final WebDriver wd = loadPageWithAlerts2(html);
    final WebElement input = wd.findElement(By.name("btn"));
    input.click();

    assertEquals("next page", wd.getTitle());
    assertEquals(3, mockWebConnection.getRequestCount());
}

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

License:Apache License

/**
 * @throws Exception if the test fails/* w  w  w. j  a v a2  s.  co  m*/
 */
@Test
public void typing() throws Exception {
    final String htmlContent = "<html><head><title>foo</title></head><body>\n" + "<form id='form1'>\n"
            + "  <input type='tel' id='foo'>\n" + "</form></body></html>";

    final WebDriver driver = loadPage2(htmlContent);

    final WebElement input = driver.findElement(By.id("foo"));
    input.sendKeys("hello");
    assertEquals("hello", input.getAttribute("value"));
}

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

License:Apache License

/**
 * @throws Exception if the test fails//  w  w  w .  java2 s. co  m
 */
@Test
@Alerts(" foo \n bar\n test\n a <p>html snippet</p>")
public void asText() throws Exception {
    final String html = "<html><head><title>foo</title></head><body>\n" + "<form id='form1'>\n"
            + "<textarea id='textArea1'> foo \n bar\r\n test\r a " + "<p>html snippet</p>\n" + "</textarea>\n"
            + "</form></body></html>";
    final WebDriver driver = loadPage2(html);
    final WebElement textArea = driver.findElement(By.id("textArea1"));
    assertEquals(getExpectedAlerts()[0], textArea.getText());
}

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

License:Apache License

/**
 * @throws Exception if the test fails//from  w ww.  j  a  v  a 2 s . c om
 */
@Test
@Alerts("")
public void asTextAndVisibility() throws Exception {
    final String html = "<html><head><title>foo</title></head><body>\n" + "<form id='form1'>\n"
            + "<textarea id='textArea1' style='visibility:hidden'> foo \n bar " + "</textarea>\n"
            + "</form></body></html>";
    final WebDriver driver = loadPage2(html);
    final WebElement textArea = driver.findElement(By.id("textArea1"));
    assertEquals(getExpectedAlerts()[0], textArea.getText());
}