Example usage for org.openqa.selenium WebDriver getTitle

List of usage examples for org.openqa.selenium WebDriver getTitle

Introduction

In this page you can find the example usage for org.openqa.selenium WebDriver getTitle.

Prototype

String getTitle();

Source Link

Document

Get the title of the current page.

Usage

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

License:Apache License

/**
 * Test that a new page loads after history.pushState() is called.
 * @throws Exception if test fails// w  w  w. j a v  a2s  .c o m
 */
@Test
public void loadPageAfterPushState() throws Exception {
    final String html = "<html>\n" + "<head>\n" + "<title>page1</title>\n" + "<script>\n"
            + "  function pushState() {\n" + "    window.history.pushState({'key':'value'});\n" + "  }\n"
            + "</script>\n" + "</head>\n" + "<body onload='pushState()'>\n" + "</body></html>";
    final String html2 = "<html>\n" + "<head>\n" + "<title>page2</title>\n" + "</head>\n" + "<body>\n"
            + "</body></html>";

    final WebDriver driver = loadPage2(html);
    assertEquals("page1", driver.getTitle());

    loadPage2(html2, URL_SECOND);
    assertEquals("page2", driver.getTitle());
}

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

License:Apache License

/**
 * @throws Exception if the test fails/*from www .j av  a  2s. co m*/
 */
@Test
@Alerts("Second")
public void javaScriptAnchorClick() throws Exception {
    final String html = "<html><head><title>First</title><script>\n" + "function delegateClick() {\n"
            + "  try {\n" + "    document.getElementById(\"link1\").click();\n" + "  } catch(e) {}\n" + "}\n"
            + "</script></head><body>\n"
            + "<a id='link1' href='#' onclick='document.form1.submit()'>link 1</a>\n"
            + "<form name='form1' action='" + URL_SECOND + "' method='post'>\n"
            + "<input type=button id='button1' value='Test' onclick='delegateClick()'>\n"
            + "<input name='testText'>\n" + "</form>\n" + "</body></html>";

    final String secondHtml = "<html>\n" + "<head><title>Second</title></head>\n" + "</html>";

    getMockWebConnection().setResponse(URL_SECOND, secondHtml);

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

    assertEquals(getExpectedAlerts()[0], driver.getTitle());
}

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

License:Apache License

/**
 * Regression test for bug 743241.// w  ww.jav a2s.  c  o  m
 * @throws Exception if the test fails
 */
@Test
public void write_loadScript() throws Exception {
    final String html = "<html><head><title>First</title></head><body>\n"
            + "<script src='script.js'></script>\n" + "</form></body></html>";

    final String script = "document.write(\"<div id='div1'>hello</div>\");\n";
    getMockWebConnection().setDefaultResponse(script, JAVASCRIPT_MIME_TYPE);

    final WebDriver driver = loadPage2(html);
    assertEquals("First", driver.getTitle());

    assertEquals("hello", driver.findElement(By.id("div1")).getText());
}

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

License:Apache License

/**
 * @throws Exception if an error occurs/*www. ja va2s . c  om*/
 */
@Test
public void write_fromScriptAddedWithAppendChild_inline() throws Exception {
    final String html = "<html><head></head><body>\n" + "<div id='it'><script>\n" + "try {\n"
            + "  var s = document.createElement('script');\n"
            + "  var t = document.createTextNode(\"document.write('in inline script'); document.title = 'done';\");\n"
            + "  s.appendChild(t);\n" + "  document.body.appendChild(s);\n"
            + "} catch (e) { alert('exception'); }\n" + "</script></div></body></html>";
    final WebDriver driver = loadPageWithAlerts2(html);

    if (getExpectedAlerts().length == 0) {
        assertEquals("done", driver.getTitle());
        assertEquals("in inline script", driver.findElement(By.id("it")).getText());
    }
}

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

License:Apache License

/**
 * @throws Exception if an error occurs//from w w w  . j av a2 s  .  co  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());
}

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

License:Apache License

/**
 * @throws Exception if the test fails//from  ww w.j  a va2  s. c o m
 */
@Test
@Alerts(DEFAULT = { "body1", "setActive not available" }, IE = { "body1", "text1", "[object HTMLButtonElement]",
        "text2", "[object Window]", "onfocus text2" })
@BuggyWebDriver(IE)
// alert conflicts with focus/blur
@NotYetImplemented
public void setActiveAndFocus() throws Exception {
    final String firstHtml = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html>\n" + "<head>\n"
            + "  <title>First</title>\n" + "  <script>var win2;</script>\n" + "</head>\n"
            + "<body id='body1' onload='alert(document.activeElement.id)'>\n" + "<form name='form1'>\n"
            + "  <input id='text1' onfocus='alert(\"onfocus text1\"); win2.focus();'>\n"
            + "  <button id='button1' onClick='win2=window.open(\"" + URL_SECOND
            + "\", \"second\");'>Click me</a>\n" + "</form>\n" + "</body></html>";

    final String secondHtml = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html>\n" + "<head>\n"
            + "  <title>Second</title>\n" + "</head>\n" + "<body id='body2'>\n"
            + "  <input id='text2' onfocus='alert(\"onfocus text2\")'>\n"
            + "  <button id='button2' onClick='doTest();'>Click me</a>\n" + "  <script>\n"
            + "    function doTest() {\n" + "      var elem = opener.document.getElementById('text1');\n"
            + "      alert(opener.document.activeElement.id);\n"
            + "      if (!elem.setActive) { alert('setActive not available'); return; }\n"
            + "      elem.setActive();\n" + "      alert(opener.document.activeElement.id);\n"
            + "      alert(document.activeElement);\n" + "      document.getElementById('text2').setActive();\n"
            + "      alert(document.activeElement.id);\n" + "      alert(opener);\n" + "      opener.focus();\n"
            + "    }\n" + "  </script>\n" + "</body></html>";
    getMockWebConnection().setResponse(URL_SECOND, secondHtml);

    final WebDriver driver = loadPage2(firstHtml);
    verifyAlerts(driver, "body1");
    assertEquals("First", driver.getTitle());

    driver.findElement(By.id("button1")).click();
    driver.switchTo().window("second");
    verifyAlerts(driver, "body1");
    assertEquals("Second", driver.getTitle());

    driver.findElement(By.id("button2")).click();
    verifyAlerts(driver, getExpectedAlerts());
}

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

License:Apache License

/**
 * @throws Exception on test failure/*from www.ja  v  a  2 s  . c om*/
 */
@Test
@Alerts({ "a", "a", "undefined", "null" })
public void getAttribute() throws Exception {
    final String html = "<html>\n" + "<head>\n" + "  <title>test</title>\n" + "  <script>\n"
            + "  function doTest() {\n" + "    var myNode = document.getElementById('myNode');\n"
            + "    alert(myNode.title);\n" + "    alert(myNode.getAttribute('title'));\n"
            + "    alert(myNode.Title);\n" + "    alert(myNode.getAttribute('class'));\n" + "  }\n"
            + "  </script>\n" + "</head>\n" + "<body onload='doTest()'>\n" + "<p id='myNode' title='a'>\n"
            + "</p>\n" + "</body>\n" + "</html>";

    final WebDriver webDriver = loadPageWithAlerts2(html);
    assertEquals("test", webDriver.getTitle());
}

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

License:Apache License

/**
 * @throws Exception on test failure//w  w  w.jav  a 2 s.c  o  m
 */
@Test
@Alerts({ "a", "b", "undefined", "foo" })
public void setAttribute() throws Exception {
    final String html = "<html>\n" + "<head>\n" + "  <title>test</title>\n" + "  <script>\n"
            + "  function doTest() {\n" + "    var myNode = document.getElementById('myNode');\n"
            + "    alert(myNode.title);\n" + "    myNode.setAttribute('title', 'b');\n"
            + "    alert(myNode.title);\n" + "    alert(myNode.Title);\n" + "    myNode.Title = 'foo';\n"
            + "    alert(myNode.Title);\n" + "  }\n" + "  </script>\n" + "</head>\n"
            + "<body onload='doTest()'>\n" + "<p id='myNode' title='a'>\n" + "</p>\n" + "</body>\n" + "</html>";

    final WebDriver webDriver = loadPageWithAlerts2(html);
    assertEquals("test", webDriver.getTitle());
}

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

License:Apache License

/**
 * Caution: with IE if you get a node with some lowercase letters, the node will be retrieved
 * and will get as name the value passed as attribute to getAttributeNode.
 * The consequence for IE: x.getAttributeNode("Foo").nodeName != x.getAttributeNode("foo").nodeName
 * @throws Exception on test failure//ww  w.j av  a 2 s .com
 */
@Test
@Alerts(DEFAULT = { "null", "expando=undefined", "firstChild=null", "lastChild=null", "name=custom_attribute",
        "nextSibling=null", "nodeName=custom_attribute", "nodeType=2", "nodeValue=bleh",
        "(ownerDocument==document)=true", "parentNode=null", "previousSibling=null", "specified=true",
        "value=bleh" }, IE = { "null", "expando=true", "firstChild=[object Text]", "lastChild=[object Text]",
                "name=custom_attribute", "nextSibling=null", "nodeName=custom_attribute", "nodeType=2",
                "nodeValue=bleh", "(ownerDocument==document)=true", "parentNode=null", "previousSibling=null",
                "specified=true", "value=bleh" })
public void getAttributeNode() throws Exception {
    final String html = "<html>\n" + "<head>\n" + "  <title>test</title>\n" + "  <script>\n"
            + "    function test() {\n" + "      var div = document.getElementById('div2');\n"
            + "      alert(div.getAttributeNode('notExisting'));\n"
            + "      var customAtt = div.getAttributeNode('custom_attribute');\n"
            + "      alertAttributeProperties(customAtt);\n" + "    }\n"
            + "    function alertAttributeProperties(att) {\n" + "      alert('expando=' + att.expando);\n"
            + "      alert('firstChild=' + att.firstChild);\n" + "      alert('lastChild=' + att.lastChild);\n"
            + "      alert('name=' + att.name);\n" + "      alert('nextSibling=' + att.nextSibling);\n"
            + "      alert('nodeName=' + att.nodeName);\n" + "      alert('nodeType=' + att.nodeType);\n"
            + "      alert('nodeValue=' + att.nodeValue);\n"
            + "      alert('(ownerDocument==document)=' + (att.ownerDocument==document));\n"
            + "      alert('parentNode=' + att.parentNode);\n"
            + "      alert('previousSibling=' + att.previousSibling);\n"
            + "      alert('specified=' + att.specified);\n" + "      alert('value=' + att.value);\n"
            + "    }\n" + "  </script>\n" + "</head>\n" + "<body onload='test()'>\n"
            + "  <div id='div1'></div>\n" + "  <div id='div2' name='blah' custom_attribute='bleh'></div>\n"
            + "  <div id='div3'></div>\n" + "</body>\n" + "</html>";

    final WebDriver webDriver = loadPageWithAlerts2(html);
    assertEquals("test", webDriver.getTitle());
}

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

License:Apache License

/**
 * @throws Exception on test failure//  w w  w.  j a  v a  2 s  . co  m
 */
@Test
@Alerts({ "left", "left", "right", "right" })
public void setAttributeNode() throws Exception {
    final String html = "<html>\n" + "<head>\n" + "  <title>test</title>\n" + "  <script>\n"
            + "    function test() {\n" + "      // Get the old alignment.\n"
            + "      var div1 = document.getElementById('div1');\n"
            + "      var a1 = div1.getAttributeNode('align');\n" + "      alert(a1.value);\n"
            + "      // Set the new alignment.\n" + "      var a2 = document.createAttribute('align');\n"
            + "      a2.value = 'right';\n" + "      a1 = div1.setAttributeNode(a2);\n"
            + "      alert(a1.value);\n" + "      alert(div1.getAttributeNode('align').value);\n"
            + "      alert(div1.getAttribute('align'));\n" + "    }\n" + "  </script>\n" + "</head>\n"
            + "<body onload='test()'>\n" + "  <div id='div1' align='left'></div>\n" + "</body>\n" + "</html>";
    final WebDriver webDriver = loadPageWithAlerts2(html);
    assertEquals("test", webDriver.getTitle());
}