Example usage for org.openqa.selenium By tagName

List of usage examples for org.openqa.selenium By tagName

Introduction

In this page you can find the example usage for org.openqa.selenium By tagName.

Prototype

public static By tagName(String tagName) 

Source Link

Usage

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

License:Apache License

/**
 * @throws Exception if the test fails/*from   www .  j  a  v a 2 s .com*/
 */
@Test
@Alerts("changed")
public void firingOnchange() throws Exception {
    final String html = "<html><body>\n" + "<form onchange='alert(\"changed\")'>\n"
            + "  <input type='file' id='file1'>\n" + "</form>\n" + "</body></html>";

    final WebDriver driver = loadPage2(html);
    final File tmpFile = File.createTempFile("htmlunit-test", ".txt");
    driver.findElement(By.id("file1")).sendKeys(tmpFile.getAbsolutePath());
    tmpFile.delete();
    driver.findElement(By.tagName("body")).click();

    verifyAlerts(driver, getExpectedAlerts());
}

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

License:Apache License

/**
 * @exception Exception If the test fails
 *//*w  w  w.j a  va 2  s.c  om*/
@Test
@BuggyWebDriver({ FF, IE })
public void select() throws Exception {
    final String html = "<html><head><title>foo</title></head><body>\n"
            + "<form id='form1'><select name='select1' multiple>\n"
            + "  <option value='option1'>Option1</option>\n" + "  <option value='option2'>Option2</option>\n"
            + "  <option value='option3' selected='selected'>Option3</option>\n"
            + "  <option value='option4'>Option4</option>\n" + "</select>\n"
            + "<input type='submit' name='button' value='foo'/>\n" + "</form></body></html>";

    final WebDriver driver = loadPage2(html);

    final List<WebElement> options = driver.findElements(By.tagName("option"));

    final Actions actions = new Actions(driver);
    final Action selectThreeOptions = actions.click(options.get(1)).click(options.get(2)).click(options.get(3))
            .build();

    selectThreeOptions.perform();

    assertFalse(options.get(0).isSelected());
    assertFalse(options.get(1).isSelected());
    assertFalse(options.get(2).isSelected());
    assertTrue(options.get(3).isSelected());
}

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

License:Apache License

/**
 * @exception Exception If the test fails
 *//*from   ww  w.j a  v  a 2  s  .co  m*/
@Test
@BuggyWebDriver(IE)
public void shiftClick() throws Exception {
    final String html = "<html><head><title>foo</title></head><body>\n"
            + "<form id='form1'><select name='select1' multiple>\n"
            + "  <option value='option1'>Option1</option>\n" + "  <option value='option2'>Option2</option>\n"
            + "  <option value='option3' selected='selected'>Option3</option>\n"
            + "  <option value='option4'>Option4</option>\n" + "</select>\n"
            + "<input type='submit' name='button' value='foo'/>\n" + "</form></body></html>";

    final WebDriver driver = loadPage2(html);

    final List<WebElement> options = driver.findElements(By.tagName("option"));

    final Actions actions = new Actions(driver);
    final Action selectThreeOptions = actions.click(options.get(1)).keyDown(Keys.SHIFT).click(options.get(3))
            .keyUp(Keys.SHIFT).build();

    selectThreeOptions.perform();

    assertFalse(options.get(0).isSelected());
    assertTrue(options.get(1).isSelected());
    assertTrue(options.get(2).isSelected());
    assertTrue(options.get(3).isSelected());
}

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

License:Apache License

/**
 * @exception Exception If the test fails
 *//*from  ww w.  j  a v a2  s  .c o  m*/
@Test
@BuggyWebDriver({ FF, IE })
public void controlClick() throws Exception {
    final String html = "<html><head><title>foo</title></head><body>\n"
            + "<form id='form1'><select name='select1' multiple>\n"
            + "  <option value='option1'>Option1</option>\n" + "  <option value='option2'>Option2</option>\n"
            + "  <option value='option3' selected='selected'>Option3</option>\n"
            + "  <option value='option4'>Option4</option>\n" + "</select>\n"
            + "<input type='submit' name='button' value='foo'/>\n" + "</form></body></html>";

    final WebDriver driver = loadPage2(html);

    final List<WebElement> options = driver.findElements(By.tagName("option"));

    final Actions actions = new Actions(driver);
    final Action selectThreeOptions = actions.click(options.get(1)).keyDown(Keys.CONTROL).click(options.get(3))
            .keyUp(Keys.CONTROL).build();

    selectThreeOptions.perform();

    assertFalse(options.get(0).isSelected());
    assertTrue(options.get(1).isSelected());
    assertFalse(options.get(2).isSelected());
    assertTrue(options.get(3).isSelected());
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.Document2Test.java

License:Apache License

/**
 * Regression test for issue 1568./*  w  ww  .  j a v a2s . co m*/
 * @throws Exception if the test fails
 */
@Test
@Alerts(DEFAULT = { "[object HTMLBodyElement]", "http://localhost:12345/#",
        "http://localhost:12345/#" }, IE11 = { "null", "http://localhost:12345/#",
                "http://localhost:12345/#" }, IE8 = { "[object]", "http://localhost:12345/#",
                        "http://localhost:12345/#" }, CHROME = { "[object HTMLBodyElement]",
                                "[object HTMLBodyElement]", "[object HTMLBodyElement]" })
public void activeElement_iframe() throws Exception {
    final String html = "<html>\n" + "<head></head>\n" + "<body>\n"

            + "  <a id='insert' " + "onclick=\"insertText("
            + "'<html><head></head><body>first frame text</body></html>');\" href=\"#\">"
            + "insert text to frame</a>\n" + "  <a id= 'update' " + "onclick=\"insertText("
            + "'<html><head></head><body>another frame text</body></html>');\" href=\"#\">"
            + "change frame text again</a><br>\n"
            + "  <iframe id='innerFrame' name='innerFrame' src='frame1.html'></iframe>\n"

            + "  <script>\n" + "    alert(document.activeElement);\n"

            + "    function insertText(text) {\n" + "      with (innerFrame.document) {\n" + "        open();\n"
            + "        writeln(text);\n" + "        close();\n" + "      }\n"
            + "      alert(document.activeElement);\n" + "    }\n" + "  </script>\n" + "</body>\n" + "</html>";

    getMockWebConnection().setResponse(new URL("http://example.com/frame1.html"), "");

    final WebDriver driver = loadPage2(html);

    driver.findElement(By.id("insert")).click();
    driver.switchTo().frame(driver.findElement(By.id("innerFrame")));
    assertEquals("first frame text", driver.findElement(By.tagName("body")).getText());

    driver.switchTo().defaultContent();
    driver.findElement(By.id("update")).click();
    driver.switchTo().frame(driver.findElement(By.id("innerFrame")));
    assertEquals("another frame text", driver.findElement(By.tagName("body")).getText());

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

From source file:com.gargoylesoftware.htmlunit.javascript.host.dom.Document2Test.java

License:Apache License

/**
 * Regression test for issue 1568./*from   ww  w .j ava2 s.  co  m*/
 * @throws Exception if the test fails
 */
@Test
@Alerts(DEFAULT = { "[object HTMLBodyElement]", "http://localhost:12345/#", "http://localhost:12345/#" }, IE = {
        "null", "http://localhost:12345/#", "http://localhost:12345/#" })
@NotYetImplemented(IE)
public void activeElement_iframe() throws Exception {
    final String html = "<html>\n" + "<head></head>\n" + "<body>\n"

            + "  <a id='insert' " + "onclick=\"insertText("
            + "'<html><head></head><body>first frame text</body></html>');\" href=\"#\">\n"
            + "insert text to frame</a>\n" + "  <a id= 'update' " + "onclick=\"insertText("
            + "'<html><head></head><body>another frame text</body></html>');\" href=\"#\">\n"
            + "change frame text again</a><br>\n"
            + "  <iframe id='innerFrame' name='innerFrame' src='frame1.html'></iframe>\n"

            + "  <script>\n" + "    alert(document.activeElement);\n"

            + "    function insertText(text) {\n" + "      with (innerFrame.document) {\n" + "        open();\n"
            + "        writeln(text);\n" + "        close();\n" + "      }\n"
            + "      alert(document.activeElement);\n" + "    }\n" + "  </script>\n" + "</body>\n" + "</html>";

    getMockWebConnection().setResponse(new URL("http://example.com/frame1.html"), "");

    final WebDriver driver = loadPage2(html);
    verifyAlerts(driver, getExpectedAlerts()[0]);

    driver.findElement(By.id("insert")).click();
    verifyAlerts(driver, getExpectedAlerts()[1]);

    driver.switchTo().frame(driver.findElement(By.id("innerFrame")));
    assertEquals("first frame text", driver.findElement(By.tagName("body")).getText());

    driver.switchTo().defaultContent();
    driver.findElement(By.id("update")).click();
    verifyAlerts(driver, getExpectedAlerts()[2]);

    driver.switchTo().frame(driver.findElement(By.id("innerFrame")));
    assertEquals("another frame text", driver.findElement(By.tagName("body")).getText());
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.event.EventTest.java

License:Apache License

/**
 * Regression test for bug//from  w w  w.  java 2 s .  c o  m
 * <a href="http://sourceforge.net/p/htmlunit/bugs/898/">898</a>.
 * Name resolution doesn't work the same in inline handlers than in "normal" JS code!
 * @throws Exception if the test fails
 */
@Test
@Alerts({ "form1 -> custom", "form2 -> [object HTMLFormElement]", "form1: [object HTMLFormElement]",
        "form2: [object HTMLFormElement]", "form1 -> custom", "form2 -> [object HTMLFormElement]" })
public void nameResolution() throws Exception {
    final String html = "<html><head><script>\n" + "var form1 = 'custom';\n" + "function testFunc() {\n"
            + "  alert('form1 -> ' + form1);\n" + "  alert('form2 -> ' + form2);\n" + "}\n"
            + "</script></head>\n" + "<body onload='testFunc()'>\n" + "<form name='form1'></form>\n"
            + "<form name='form2'></form>\n"
            + "<button onclick=\"alert('form1: ' + form1); alert('form2: ' + form2); testFunc()\">click me</button>\n"
            + "</body></html>";

    final String[] alerts = getExpectedAlerts();
    int i = 0;

    final WebDriver driver = loadPage2(html);
    verifyAlerts(driver, alerts[i++], alerts[i++]);

    driver.findElement(By.tagName("button")).click();
    verifyAlerts(driver, alerts[i++], alerts[i++], alerts[i++], alerts[i++]);
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.EventTest.java

License:Apache License

/**
 * Regression test for bug/*from  ww  w.  j ava 2s .c  om*/
 * <a href="http://sourceforge.net/tracker/?func=detail&aid=2851920&group_id=47038&atid=448266">2851920</a>.
 * Name resolution doesn't work the same in inline handlers than in "normal" JS code!
 * @throws Exception if the test fails
 */
@Test
@NotYetImplemented
@Alerts(FF = { "form1 -> custom", "form2 -> [object HTMLFormElement]", "form1: [object HTMLFormElement]",
        "form2: [object HTMLFormElement]", "form1 -> custom", "form2 -> [object HTMLFormElement]" }, IE = {
                "form1 -> custom", "form2 -> [object]", "form1: [object]", "form2: [object]", "form1 -> custom",
                "form2 -> [object]" })
public void nameResolution() throws Exception {
    final String html = "<html><head><script>\n" + "var form1 = 'custom';\n" + "function testFunc() {\n"
            + " alert('form1 -> ' + form1);\n" + " alert('form2 -> ' + form2);\n" + "}\n" + "</script></head>\n"
            + "<body onload='testFunc()'>\n" + "<form name='form1'></form>\n" + "<form name='form2'></form>\n"
            + "<button onclick=\"alert('form1: ' + form1); alert('form2: ' + form2); testFunc()\">click me</button>\n"
            + "</body></html>";

    final WebDriver driver = loadPage2(html);
    driver.findElement(By.tagName("button")).click();

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

From source file:com.gargoylesoftware.htmlunit.javascript.host.html.HTMLAnchorElement2Test.java

License:Apache License

/**
 * Regression test for https://sourceforge.net/tracker/?func=detail&atid=448266&aid=1689798&group_id=47038.
 * In href, "this" should be the window and not the link.
 * @throws Exception if the test fails/*ww  w .j  a  v  a 2s.  c o  m*/
 */
@Test
@Alerts("true")
public void thisInJavascriptHref() throws Exception {
    final String html = "<html>\n" + "<body>\n" + "  <a href='javascript:alert(this === window)'>link 1</a>\n"
            + "</body></html>";

    final WebDriver driver = loadPage2(html);
    driver.findElement(By.tagName("a")).click();
    assertEquals(1, getMockWebConnection().getRequestCount());
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocumentWrite2Test.java

License:Apache License

/**
 * @throws Exception if an error occurs// ww  w  .j av  a2s .  c  o  m
 */
@Test
public void write_fromScriptAddedWithAppendChild_external() throws Exception {
    final String html = "<html><head></head><body>\n" + "<div id='it'>here</div><script>\n"
            + "  var s = document.createElement('script');\n" + "  s.src = 'foo.js';\n"
            + "  document.body.appendChild(s);\n" + "</script></body></html>";

    final String js = "document.write('from external script');\n" + "document.title = 'done';";

    getMockWebConnection().setDefaultResponse(js, JAVASCRIPT_MIME_TYPE);
    final WebDriver driver = loadPage2(html);

    assertEquals("done", driver.getTitle());
    assertEquals("here", driver.findElement(By.id("it")).getText());
    assertEquals("here", driver.findElement(By.tagName("body")).getText());
}