Example usage for org.openqa.selenium By name

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

Introduction

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

Prototype

public static By name(String name) 

Source Link

Usage

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

License:Apache License

/**
 * @throws Exception if the test fails/*from   w ww. jav  a 2s  . c o m*/
 */
@Test
@Alerts({ "OnloadTest", "header -> content -> frameSet", "content\nClick for new frame content with onload",
        "header -> content -> frameSet -> onloadFrame", "onloadFrame\nNew content loaded..." })
@NotYetImplemented
public void windowLocationAssignOnload() throws Exception {
    final String html = "<html><head><title>OnloadTest</title></head>\n"
            + "<frameset rows='50,*' onLoad=\"top.header.addToFrameOrder('frameSet');\">\n"
            + "  <frame name='header' src='header.html'>\n" + "  <frame name='content' id='content' "
            + "src=\"javascript:window.location.assign('content.html')\">\n" + "</frameset>\n" + "</html>";

    final String headerFrame = "<html><head><title>headerFrame</title></head>\n"
            + "<script type='text/javascript'>\n" + "  function addToFrameOrder(frame) {\n"
            + "    var spacer = ' -> ';\n"
            + "    var frameOrder = document.getElementById('frameOrder').innerHTML;\n"
            + "    if (frameOrder == '') {spacer = '';}\n"
            + "    document.getElementById('frameOrder').innerHTML = frameOrder + spacer + frame;\n" + "  }\n"
            + "</script>\n" + "<body onload=\"addToFrameOrder('header');\">\n"
            + "  <div id='frameOrder'></div>\n" + "</body></html>";

    final String contentFrame = "<html><head><title>contentFrame</title></head>\n"
            + "<body onload=\"top.header.addToFrameOrder('content');\">\n" + "  <h3>content</h3>\n"
            + "  <a name='onloadFrameAnchor' href='onload.html' "
            + "target='content'>Click for new frame content with onload</a>\n" + "</body></html>";

    final String onloadFrame = "<html><head><title>onloadFrame</title></head>\n"
            + "<body onload=\"alert('Onload alert.');top.header.addToFrameOrder('onloadFrame');\">\n"
            + "  <script type='text/javascript'>\n" + "    alert('Body alert.');\n" + "  </script>\n"
            + "  <h3>onloadFrame</h3>\n" + "  <p id='newContent'>New content loaded...</p>\n"
            + "</body></html>";

    getMockWebConnection().setResponse(new URL(URL_FIRST, "header.html"), headerFrame);
    getMockWebConnection().setResponse(new URL(URL_FIRST, "content.html"), contentFrame);
    getMockWebConnection().setResponse(new URL(URL_FIRST, "onload.html"), onloadFrame);

    final WebDriver driver = loadPage2(html);
    // top frame
    assertEquals(getExpectedAlerts()[0], driver.getTitle());

    // header frame
    driver.switchTo().frame("header");
    assertEquals(getExpectedAlerts()[1], driver.findElement(By.id("frameOrder")).getText());

    // content frame
    driver.switchTo().defaultContent();
    driver.switchTo().frame("content");
    assertEquals(getExpectedAlerts()[2], driver.findElement(By.tagName("body")).getText());

    driver.findElement(By.name("onloadFrameAnchor")).click();
    final boolean ie = getBrowserVersion().isIE();
    verifyAlerts(driver, "Body alert.");
    if (!ie) {
        verifyAlerts(driver, "Onload alert.");
    }
    driver.switchTo().defaultContent();
    if (ie) {
        verifyAlerts(driver, "Onload alert.");
    }
    driver.switchTo().frame("header");
    assertEquals(getExpectedAlerts()[3], driver.findElement(By.id("frameOrder")).getText());

    driver.switchTo().defaultContent();
    driver.switchTo().frame("content");
    assertEquals(getExpectedAlerts()[4], driver.findElement(By.tagName("body")).getText());
}

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

License:Apache License

/**
 * @throws Exception if the test fails// www. j a  va  2  s.  c o m
 */
@Test
@Alerts({ "OnloadTest", "header -> content -> frameSet", "content\nClick for new frame content with onload",
        "header -> content -> frameSet -> onloadFrame", "onloadFrame\nNew content loaded..." })
@NotYetImplemented
public void windowLocationSetOnload() throws Exception {
    final String html = "<html><head><title>OnloadTest</title></head>\n"
            + "<frameset rows='50,*' onLoad=\"top.header.addToFrameOrder('frameSet');\">\n"
            + "  <frame name='header' src='header.html'>\n" + "  <frame name='content' id='content' "
            + "src=\"javascript:window.location='content.html'\">\n" + "</frameset>\n" + "</html>";

    final String headerFrame = "<html><head><title>headerFrame</title></head>\n"
            + "<script type='text/javascript'>\n" + "  function addToFrameOrder(frame) {\n"
            + "    var spacer = ' -> ';\n"
            + "    var frameOrder = document.getElementById('frameOrder').innerHTML;\n"
            + "    if (frameOrder == '') {spacer = '';}\n"
            + "    document.getElementById('frameOrder').innerHTML = frameOrder + spacer + frame;\n" + "  }\n"
            + "</script>\n" + "<body onload=\"addToFrameOrder('header');\">\n"
            + "  <div id='frameOrder'></div>\n" + "</body></html>";

    final String contentFrame = "<html><head><title>contentFrame</title></head>\n"
            + "<body onload=\"top.header.addToFrameOrder('content');\">\n" + "  <h3>content</h3>\n"
            + "  <a name='onloadFrameAnchor' href='onload.html' "
            + "target='content'>Click for new frame content with onload</a>\n" + "</body></html>";

    final String onloadFrame = "<html><head><title>onloadFrame</title></head>\n"
            + "<body onload=\"alert('Onload alert.');top.header.addToFrameOrder('onloadFrame');\">\n"
            + "  <script type='text/javascript'>\n" + "    alert('Body alert.');\n" + "  </script>\n"
            + "  <h3>onloadFrame</h3>\n" + "  <p id='newContent'>New content loaded...</p>\n"
            + "</body></html>";

    getMockWebConnection().setResponse(new URL(URL_FIRST, "header.html"), headerFrame);
    getMockWebConnection().setResponse(new URL(URL_FIRST, "content.html"), contentFrame);
    getMockWebConnection().setResponse(new URL(URL_FIRST, "onload.html"), onloadFrame);

    final WebDriver driver = loadPage2(html);
    // top frame
    assertEquals(getExpectedAlerts()[0], driver.getTitle());

    // header frame
    driver.switchTo().frame("header");
    assertEquals(getExpectedAlerts()[1], driver.findElement(By.id("frameOrder")).getText());

    // content frame
    driver.switchTo().defaultContent();
    driver.switchTo().frame("content");
    assertEquals(getExpectedAlerts()[2], driver.findElement(By.tagName("body")).getText());

    if (StringUtils.isNotEmpty(getExpectedAlerts()[2])) {
        driver.findElement(By.name("onloadFrameAnchor")).click();
        driver.switchTo().defaultContent();
        driver.switchTo().frame("header");
        assertEquals(getExpectedAlerts()[3], driver.findElement(By.id("frameOrder")).getText());

        driver.switchTo().defaultContent();
        driver.switchTo().frame("content");
        assertEquals(getExpectedAlerts()[4], driver.findElement(By.tagName("body")).getText());
    }
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*from w w  w .j a v a2  s.  co m*/
 */
@Test
@Alerts({ "false", "true", "false", "true", "false", "true" })
public void disabledAttribute() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html><head><title>foo</title><script>\n"
            + "function test() {\n" + "  alert(document.form1.button1.disabled);\n"
            + "  alert(document.form1.button2.disabled);\n" + "  alert(document.form1.button3.disabled);\n"
            + "  document.form1.button1.disabled = true;\n" + "  document.form1.button2.disabled = false;\n"
            + "  document.form1.button3.disabled = true;\n" + "  alert(document.form1.button1.disabled);\n"
            + "  alert(document.form1.button2.disabled);\n" + "  alert(document.form1.button3.disabled);\n"
            + "}\n" + "</script></head><body>\n" + "<p>hello world</p>\n" + "<form name='form1'>\n"
            + "  <input type='submit' name='button1' value='1'/>\n"
            + "  <input type='submit' name='button2' value='2' disabled/>\n"
            + "  <input type='submit' name='button3' value='3'/>\n" + "</form>\n"
            + "<a href='javascript:test()' id='clickme'>click me</a>\n" + "</body></html>";

    final WebDriver driver = loadPage2(html);

    final WebElement button1 = driver.findElement(By.name("button1"));
    final WebElement button2 = driver.findElement(By.name("button2"));
    final WebElement button3 = driver.findElement(By.name("button3"));
    assertTrue(button1.isEnabled());
    assertFalse(button2.isEnabled());
    assertTrue(button3.isEnabled());

    driver.findElement(By.id("clickme")).click();
    verifyAlerts(driver, getExpectedAlerts());
    assertFalse(button1.isEnabled());
    assertTrue(button2.isEnabled());
    assertFalse(button3.isEnabled());
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*w  ww  .  ja va2 s.  c o m*/
 */
@Test
@Alerts("true")
public void thisDotFormInOnClick() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html>\n" + "<body>\n" + "<form name='form1'>\n"
            + "<input type='submit' id='clickMe' onClick=\"this.form.target='_blank'; return false;\">\n"
            + "</form>\n" + "<script>\n" + "alert(document.forms[0].target == '');\n" + "</script>\n"
            + "</body></html>";

    final WebDriver driver = loadPageWithAlerts2(html);

    // HtmlUnitDriver is buggy, it returns null here
    // assertEquals("", driver.findElement(By.name("form1")).getAttribute("target"));

    driver.findElement(By.id("clickMe")).click();

    assertEquals("_blank", driver.findElement(By.name("form1")).getAttribute("target"));
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*from   w ww  . ja  v  a 2  s .c  om*/
 */
@Test
@Alerts("foo")
public void onChange() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html><head><title>foo</title>\n"
            + "</head><body>\n" + "<p>hello world</p>\n" + "<form name='form1'>\n"
            + "  <input type='text' name='text1' onchange='alert(this.value)'>\n"
            + "<input name='myButton' type='button' onclick='document.form1.text1.value=\"from button\"'>\n"
            + "</form>\n" + "</body></html>";

    final WebDriver driver = loadPage2(html);

    final WebElement textinput = driver.findElement(By.name("text1"));
    textinput.sendKeys("foo");
    final WebElement button = driver.findElement(By.name("myButton"));
    button.click();
    verifyAlerts(driver, getExpectedAlerts());
    Thread.sleep(10);
    assertEquals("from button", textinput.getAttribute("value"));
}

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

License:Apache License

/**
 * @throws Exception if the test fails//from ww  w  . j a va2  s.co  m
 */
@Test
@Alerts("foo")
public void onChangeSetByJavaScript() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html><head><title>foo</title>\n"
            + "</head><body>\n" + "<p>hello world</p>\n" + "<form name='form1'>\n"
            + "  <input type='text' name='text1' id='text1'>\n"
            + "<input name='myButton' type='button' onclick='document.form1.text1.value=\"from button\"'>\n"
            + "</form>\n" + "<script>\n"
            + "document.getElementById('text1').onchange = function(event) { alert(this.value) };\n"
            + "</script>\n" + "</body></html>";

    final WebDriver driver = loadPage2(html);

    final WebElement textinput = driver.findElement(By.name("text1"));
    textinput.sendKeys("foo");
    final WebElement button = driver.findElement(By.name("myButton"));
    button.click();
    verifyAlerts(driver, getExpectedAlerts());
    Thread.sleep(10);
    assertEquals("from button", textinput.getAttribute("value"));
}

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

License:Apache License

/**
 * Test that changing type doesn't throw.
 * Test must be extended when setting type really does something.
 * @throws Exception if the test fails/*  w w w  .ja  va 2 s .  co  m*/
 */
@Test
@Alerts({ "radio", "hidden", "image" })
public void changeType() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html><head><title>First</title><script>\n"
            + "function doTest() {\n" + "  var input = document.myForm.myRadio;\n" + "  alert(input.type);\n"

            + "  try {\n" + "    input.type = 'hidden';\n" + "  } catch(e) { alert('error');}\n"
            + "  alert(input.type);\n"

            + "  try {\n" + "    input.setAttribute('type', 'image');\n" + "  } catch(e) { alert('error');}\n"
            + "  alert(input.type);\n" + "}\n</script></head>\n" + "<body onload='doTest()'>\n"
            + "  <form name='myForm' action='foo'>\n" + "    <input type='radio' name='myRadio'/>\n"
            + "  </form>\n" + "</body></html>";

    final WebDriver driver = loadPageWithAlerts2(html);

    if (driver instanceof HtmlUnitDriver) {
        final WebElement myRadio = driver.findElement(By.name("myRadio"));
        assertTrue(toHtmlElement(myRadio) instanceof HtmlImageInput);
    }
}

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

License:Apache License

/**
 * Test that field delegates submit to the containing form.
 * @throws Exception if the test fails/* w  w  w.  j av a2 s. c o  m*/
 */
@Test
public void onChangeCallsFormSubmit() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html><head>\n" + "</head>\n" + "<body>\n"
            + "  <form name='test' action='foo'>\n" + "    <input name='field1' onchange='submit()'>\n"
            + "    <img src='unknown.gif'>\n" + "  </form>\n" + "</body></html>";

    getMockWebConnection().setDefaultResponse("<html><title>page 2</title><body></body></html>");

    final WebDriver driver = loadPage2(html);
    driver.findElement(By.name("field1")).sendKeys("bla");
    driver.findElement(By.tagName("img")).click();
    assertEquals("page 2", driver.getTitle());
}

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

License:Apache License

/**
 * @throws Exception if the test fails//w  w  w .  j  a v a 2 s.  c om
 */
@Test
@Alerts("foo")
public void onChange() throws Exception {
    final String html = "<html>\n" + "<head><title>foo</title></head>\n" + "<body>\n" + "  <p>hello world</p>\n"
            + "  <form name='form1'>\n"
            + "    <textarea name='textarea1' onchange='alert(this.value)'></textarea>\n"
            + "    <input name='myButton' type='button' onclick='document.form1.textarea1.value=\"from button\"'>\n"
            + "  </form>\n" + "</body></html>";

    final WebDriver driver = loadPage2(html);

    final WebElement textarea = driver.findElement(By.name("textarea1"));
    textarea.sendKeys("foo");
    driver.findElement(By.name("myButton")).click();

    verifyAlerts(driver, getExpectedAlerts());
}

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

License:Apache License

/**
 * @throws Exception if the test fails//  ww w .j a  v  a  2s.com
 */
@Test
@NotYetImplemented
public void createObjectURL() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html>\n" + "<head><title>foo</title>\n"
            + "<script>\n" + "function test() {\n" + "  if (document.testForm.fileupload.files) {\n"
            + "    var files = document.testForm.fileupload.files;\n"

            + "    var url = window.URL.createObjectURL(files[0]);\n" + "    alert(url);\n"
            + "    window.URL.revokeObjectURL(url);\n" + "  }\n" + "}\n" + "</script>\n" + "</head>\n"
            + "<body>\n" + "  <form name='testForm'>\n"
            + "    <input type='file' id='fileupload' name='fileupload'>\n" + "  </form>\n"
            + "  <button id='testBtn' onclick='test()'>Tester</button>\n" + "</body>\n" + "</html>";

    final WebDriver driver = loadPage2(html);

    final File tstFile = File.createTempFile("HtmlUnitUploadTest", ".txt");
    try {
        FileUtils.writeStringToFile(tstFile, "Hello HtmlUnit", ISO_8859_1);

        final String path = tstFile.getCanonicalPath();
        driver.findElement(By.name("fileupload")).sendKeys(path);

        driver.findElement(By.id("testBtn")).click();

        final String url = getCollectedAlerts(driver).get(0);
        Assert.assertTrue(url, url.startsWith("blob:"));
    } finally {
        FileUtils.deleteQuietly(tstFile);
    }
}