/*
* Copyright (c) 2002-2008 Gargoyle Software Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gargoylesoftware.htmlunit.html;
import static org.junit.Assert.assertSame;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import org.w3c.dom.NodeList;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebTestCase;
/**
* Unit tests for {@link HtmlElement}.
*
* @version $Revision$
* @author Mike Bowler
* @author Denis N. Antonioli
* @author Daniel Gredler
* @author Ahmed Ashour
* @author Sudhan Moghe
*/
public class HtmlElementTest extends WebTestCase {
/**
* Test hasAttribute() on an element with the attribute.
* @throws Exception if the test fails
*/
@Test
public void testElementHasAttributeWith() throws Exception {
final String content = "
text";
final List collectedAlerts = new ArrayList();
final HtmlPage page = loadPage(content, collectedAlerts);
final HtmlElement node = page.getDocumentElement().getHtmlElementById("tag");
Assert.assertEquals("Element should have attribute", true, node.hasAttribute("id"));
}
/**
* Test hasAttribute() on an element without the attributes.
* @throws Exception if the test fails
*/
@Test
public void testElementHasAttributeNone() throws Exception {
final String content
= "text";
final List collectedAlerts = new ArrayList();
final HtmlPage page = loadPage(content, collectedAlerts);
final HtmlElement node = page.getDocumentElement().getHtmlElementById("tag");
Assert.assertEquals("Element should not have attribute", false, node.hasAttribute("foo"));
}
/**
* Test hasAttribute() on an element with the attribute.
* @throws Exception if the test fails
*/
@Test
public void testElementHasAttributeNSWith() throws Exception {
final String content
= "text";
final List collectedAlerts = new ArrayList();
final HtmlPage page = loadPage(content, collectedAlerts);
final HtmlElement node = page.getDocumentElement().getHtmlElementById("tag");
Assert.assertEquals("Element should have attribute", true, node.hasAttributeNS("http://foobar", "foo"));
}
/**
* Test hasAttribute() on an element without the attributes.
* @throws Exception if the test fails
*/
@Test
public void testElementHasAttributeNSNone() throws Exception {
final String content
= "text";
final List collectedAlerts = new ArrayList();
final HtmlPage page = loadPage(content, collectedAlerts);
final HtmlElement node = page.getDocumentElement().getHtmlElementById("tag");
Assert.assertEquals("Element should not have attribute", false, node.hasAttributeNS("http://foobar", "foo"));
}
/**
* Test getAttribute() on an element with the attribute.
* @throws Exception if the test fails
*/
@Test
public void testElementGetAttributeWith() throws Exception {
final String content
= "text";
final List collectedAlerts = new ArrayList();
final HtmlPage page = loadPage(content, collectedAlerts);
final HtmlElement node = page.getDocumentElement().getHtmlElementById("tag");
Assert.assertEquals("Element should have attribute", "tag", node.getAttribute("id"));
}
/**
* Test getAttribute() on an element without the attributes.
* @throws Exception if the test fails
*/
@Test
public void testElementGetAttributeNone() throws Exception {
final String content
= "text";
final List collectedAlerts = new ArrayList();
final HtmlPage page = loadPage(content, collectedAlerts);
final HtmlElement node = page.getDocumentElement().getHtmlElementById("tag");
Assert.assertEquals("Element should not have attribute", "", node.getAttribute("foo"));
}
/**
* Test getAttribute() on an element with the attribute.
* @throws Exception if the test fails
*/
@Test
public void testElementGetAttributeNSWith() throws Exception {
final String content
= "text";
final List collectedAlerts = new ArrayList();
final HtmlPage page = loadPage(content, collectedAlerts);
final HtmlElement node = page.getDocumentElement().getHtmlElementById("tag");
Assert.assertEquals("Element should have attribute", "bar", node.getAttributeNS("http://foobar", "foo"));
}
/**
* Test getAttribute() on an element without the attributes.
* @throws Exception if the test fails
*/
@Test
public void testElementGetAttributeNSNone() throws Exception {
final String content
= "text";
final List collectedAlerts = new ArrayList();
final HtmlPage page = loadPage(content, collectedAlerts);
final HtmlElement node = page.getDocumentElement().getHtmlElementById("tag");
Assert.assertEquals("Element should not have attribute", "", node.getAttributeNS("http://foobar", "foo"));
}
/**
* Test getNamespaceURI on an attribute that has a namespace.
* @throws Exception if the test fails
*/
@Test
public void testElementGetNamespaceURIWith() throws Exception {
final String content
= "text";
final List collectedAlerts = new ArrayList();
final HtmlPage page = loadPage(content, collectedAlerts);
final HtmlElement node = page.getDocumentElement().getHtmlElementById("tag");
for (final DomAttr attr : node.getAttributesCollection()) {
if (attr.getName().equals("ns:foo")) {
Assert.assertEquals("Element should have a namespace URI", "http://foobar", attr.getNamespaceURI());
return;
}
}
Assert.assertFalse("Attribute ns:foo not found.", true);
}
/**
* Test getNamespaceURI on an attribute that has a namespace.
* @throws Exception if the test fails
*/
@Test
public void testElementGetNamespaceURINone() throws Exception {
final String content
= "text";
final List collectedAlerts = new ArrayList();
final HtmlPage page = loadPage(content, collectedAlerts);
final HtmlElement node = page.getDocumentElement().getHtmlElementById("tag");
for (final DomAttr attr : node.getAttributesCollection()) {
if (attr.getName().equals("id")) {
Assert.assertEquals("Element should not have a namespace URI", null, attr.getNamespaceURI());
return;
}
}
Assert.assertFalse("Attribute ns:foo not found.", true);
}
/**
* Test getLocalName on an attribute that has a local name.
* @throws Exception if the test fails
*/
@Test
public void testElementGetLocalNameWith() throws Exception {
final String content
= "text";
final List collectedAlerts = new ArrayList();
final HtmlPage page = loadPage(content, collectedAlerts);
final HtmlElement node = page.getDocumentElement().getHtmlElementById("tag");
for (final DomAttr attr : node.getAttributesCollection()) {
if (attr.getName().equals("ns:foo")) {
Assert.assertEquals("Element should have a local name", "foo", attr.getLocalName());
return;
}
}
Assert.assertFalse("Attribute ns:foo not found.", true);
}
/**
* Test getLocalName on an attribute that has a local name.
* @throws Exception if the test fails
*/
@Test
public void testElementGetLocalNameNone() throws Exception {
final String content
= "text";
final List collectedAlerts = new ArrayList();
final HtmlPage page = loadPage(content, collectedAlerts);
final HtmlElement node = page.getDocumentElement().getHtmlElementById("tag");
for (final DomAttr attr : node.getAttributesCollection()) {
if (attr.getName().equals("id")) {
// This is not standard, but to change it now would break backwards compatibility.
Assert.assertEquals("Element should not have a local name", "id", attr.getLocalName());
return;
}
}
Assert.assertFalse("Attribute ns:foo not found.", true);
}
/**
* Test getPrefix on an attribute that has a prefix.
* @throws Exception if the test fails
*/
@Test
public void testElementGetPrefixWith() throws Exception {
final String content
= "text";
final List collectedAlerts = new ArrayList();
final HtmlPage page = loadPage(content, collectedAlerts);
final HtmlElement node = page.getDocumentElement().getHtmlElementById("tag");
for (final DomAttr attr : node.getAttributesCollection()) {
if (attr.getName().equals("ns:foo")) {
Assert.assertEquals("Element should have a prefix", "ns", attr.getPrefix());
return;
}
}
Assert.assertFalse("Attribute ns:foo not found.", true);
}
/**
* Test getPrefix on an attribute that has a prefix.
* @throws Exception if the test fails
*/
@Test
public void testElementGetPrefixNone() throws Exception {
final String content
= "text";
final List collectedAlerts = new ArrayList();
final HtmlPage page = loadPage(content, collectedAlerts);
final HtmlElement node = page.getDocumentElement().getHtmlElementById("tag");
for (final DomAttr attr : node.getAttributesCollection()) {
if (attr.getName().equals("id")) {
Assert.assertEquals("Element should not have a prefix", null, attr.getPrefix());
return;
}
}
Assert.assertFalse("Attribute ns:foo not found.", true);
}
/**
* Test setPrefix on an attribute that has a prefix.
* @throws Exception if the test fails
*/
@Test
public void testElementSetPrefix() throws Exception {
final String content
= "text";
final List collectedAlerts = new ArrayList();
final HtmlPage page = loadPage(content, collectedAlerts);
final HtmlElement node = page.getDocumentElement().getHtmlElementById("tag");
for (final DomAttr attr : node.getAttributesCollection()) {
if (attr.getName().equals("ns:foo")) {
attr.setPrefix("other");
Assert.assertEquals("Element should have a changed prefix", "other", attr.getPrefix());
Assert.assertEquals("setPrefix should change qualified name", "other:foo", attr.getName());
return;
}
}
Assert.assertFalse("Attribute ns:foo not found.", true);
}
/**
* Test setAttribute() on an element with the attribute.
* @throws Exception if the test fails
*/
@Test
public void testElementSetAttributeWith() throws Exception {
final String content
= "text";
final List collectedAlerts = new ArrayList();
final HtmlPage page = loadPage(content, collectedAlerts);
final HtmlElement node = page.getDocumentElement().getHtmlElementById("tag");
node.setAttribute("id", "other");
Assert.assertEquals("Element should have attribute", "other", node.getAttribute("id"));
}
/**
* Test setAttribute() on an element without the attributes.
* @throws Exception if the test fails
*/
@Test
public void testElementSetAttributeNone() throws Exception {
final String content
= "text";
final List collectedAlerts = new ArrayList();
final HtmlPage page = loadPage(content, collectedAlerts);
final HtmlElement node = page.getDocumentElement().getHtmlElementById("tag");
node.setAttribute("foo", "other");
Assert.assertEquals("Element should have attribute", "other", node.getAttribute("foo"));
}
/**
* Test setAttribute() on an element with the attribute.
* @throws Exception if the test fails
*/
@Test
public void testElementSetAttributeNSWith() throws Exception {
final String content
= "text";
final List collectedAlerts = new ArrayList();
final HtmlPage page = loadPage(content, collectedAlerts);
final HtmlElement node = page.getDocumentElement().getHtmlElementById("tag");
node.setAttributeNS("http://foobar", "ns:foo", "other");
Assert.assertEquals("Element should have attribute", "other", node.getAttributeNS("http://foobar", "foo"));
}
/**
* Test setAttribute() on an element without the attributes.
* @throws Exception if the test fails
*/
@Test
public void testElementSetAttributeNSNone() throws Exception {
final String content
= "text";
final List collectedAlerts = new ArrayList();
final HtmlPage page = loadPage(content, collectedAlerts);
final HtmlElement node = page.getDocumentElement().getHtmlElementById("tag");
node.setAttributeNS("http://foobar", "ns:foo", "other");
Assert.assertEquals("Element should not have attribute", "other", node.getAttributeNS("http://foobar", "foo"));
}
/**
* Test removeAttribute() on an element with the attribute.
* @throws Exception if the test fails
*/
@Test
public void testElementRemoveAttributeWith() throws Exception {
final String content
= "text";
final List collectedAlerts = new ArrayList();
final HtmlPage page = loadPage(content, collectedAlerts);
final HtmlElement node = page.getDocumentElement().getHtmlElementById("tag");
node.removeAttribute("id");
Assert.assertEquals("Element should not have removed attribute", "", node.getAttribute("id"));
}
/**
* Test removeAttribute() on an element without the attributes.
* @throws Exception if the test fails
*/
@Test
public void testElementRemoveAttributeNone() throws Exception {
final String content
= "text";
final List collectedAlerts = new ArrayList();
final HtmlPage page = loadPage(content, collectedAlerts);
final HtmlElement node = page.getDocumentElement().getHtmlElementById("tag");
node.removeAttribute("foo");
Assert.assertEquals("Element should not have attribute", "", node.getAttribute("foo"));
}
/**
* Test removeAttribute() on an element with the attribute.
* @throws Exception if the test fails
*/
@Test
public void testElementRemoveAttributeNSWith() throws Exception {
final String content
= "text";
final List collectedAlerts = new ArrayList();
final HtmlPage page = loadPage(content, collectedAlerts);
final HtmlElement node = page.getDocumentElement().getHtmlElementById("tag");
node.removeAttributeNS("http://foobar", "foo");
Assert.assertEquals("Element should not have removed attribute", "",
node.getAttributeNS("http://foobar", "foo"));
}
/**
* Test removeAttribute() on an element without the attributes.
* @throws Exception if the test fails
*/
@Test
public void testElementRemoveAttributeNSNone() throws Exception {
final String content
= "text";
final List collectedAlerts = new ArrayList();
final HtmlPage page = loadPage(content, collectedAlerts);
final HtmlElement node = page.getDocumentElement().getHtmlElementById("tag");
node.removeAttributeNS("http://foobar", "foo");
Assert.assertEquals("Element should not have attribute", "", node.getAttributeNS("http://foobar", "foo"));
}
/**
* Verifies that cloned node attributes have the same initial values, but changes can be made
* to the clone without affecting the original node, and that the id attribute is treated the
* same as all the other attributes. See bug 1707726.
* @throws Exception if an error occurs
*/
@Test
public void testClonedNodeAttributes() throws Exception {
final String html = "";
final List collectedAlerts = new ArrayList();
loadPage(html, collectedAlerts);
final String[] expectedAlerts = {"false", "true", "a", "a", "b", "b", "b", "c"};
assertEquals(expectedAlerts, collectedAlerts);
}
/**
* @throws Exception if the test fails
*/
@Test
public void testGetEnclosingForm() throws Exception {
final String htmlContent = ""
+ "foo\n"
+ "";
final HtmlPage page = loadPage(htmlContent);
final HtmlForm form = (HtmlForm) page.getHtmlElementById("form1");
final HtmlInput input = (HtmlInput) form.getHtmlElementById("foo");
assertSame(form, input.getEnclosingForm());
}
/**
* @throws Exception if the test fails
*/
@Test
public void testGetEnclosing() throws Exception {
final String htmlContent = ""
+ "foo\n"
+ "";
final HtmlPage page = loadPage(htmlContent);
final HtmlElement td1 = page.getHtmlElementById("td1");
assertEquals("tr1", td1.getEnclosingElement("tr").getId());
assertEquals("tr1", td1.getEnclosingElement("TR").getId());
assertEquals("table1", td1.getEnclosingElement("table").getId());
assertEquals("form1", td1.getEnclosingElement("form").getId());
final HtmlElement td2 = page.getHtmlElementById("td2");
assertEquals("tr2", td2.getEnclosingElement("tr").getId());
assertEquals("tr2", td2.getEnclosingElement("TR").getId());
assertEquals("table1", td2.getEnclosingElement("table").getId());
assertEquals("form1", td2.getEnclosingElement("form").getId());
}
/**
* @throws Exception if the test fails
*/
@Test
public void testAsText_WithComments() throws Exception {
final String htmlContent
= "foo\n"
+ "
foo
\n"
+ "";
final HtmlPage page = loadPage(htmlContent);
final HtmlElement element = page.getHtmlElementById("p1");
assertEquals("foo", element.asText());
}
/**
* Tests constants.
*/
@Test
public void testConstants() {
assertEquals("", HtmlElement.ATTRIBUTE_NOT_DEFINED);
assertEquals("", HtmlElement.ATTRIBUTE_VALUE_EMPTY);
assertTrue("Not the same object",
HtmlElement.ATTRIBUTE_NOT_DEFINED != HtmlElement.ATTRIBUTE_VALUE_EMPTY);
}
static class HtmlAttributeChangeListenerTestImpl implements HtmlAttributeChangeListener {
private final List collectedValues_ = new ArrayList();
@Test
public void attributeAdded(final HtmlAttributeChangeEvent event) {
collectedValues_.add("attributeAdded: " + event.getHtmlElement().getTagName() + ','
+ event.getName() + ',' + event.getValue());
}
@Test
public void attributeRemoved(final HtmlAttributeChangeEvent event) {
collectedValues_.add("attributeRemoved: " + event.getHtmlElement().getTagName() + ','
+ event.getName() + ',' + event.getValue());
}
@Test
public void attributeReplaced(final HtmlAttributeChangeEvent event) {
collectedValues_.add("attributeReplaced: " + event.getHtmlElement().getTagName() + ','
+ event.getName() + ',' + event.getValue());
}
List getCollectedValues() {
return collectedValues_;
}
}
/**
* @throws Exception if the test fails
*/
@Test
public void testHtmlAttributeChangeListener_AddAttribute() throws Exception {
final String htmlContent
= "foo\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "";
final String[] expectedValues =
{"attributeAdded: p,title,myTitle",
"attributeAdded: p,title,myTitle",
"attributeAdded: p,title,myTitle"};
final HtmlPage page = loadPage(htmlContent);
final HtmlBody body = (HtmlBody) page.getHtmlElementById("myBody");
final HtmlElement p1 = page.getHtmlElementById("p1");
final HtmlAttributeChangeListenerTestImpl listenerImpl = new HtmlAttributeChangeListenerTestImpl();
p1.addHtmlAttributeChangeListener(listenerImpl);
body.addHtmlAttributeChangeListener(listenerImpl);
page.addHtmlAttributeChangeListener(listenerImpl);
final HtmlButtonInput myButton = (HtmlButtonInput) page.getHtmlElementById("myButton");
myButton.click();
assertEquals(expectedValues, listenerImpl.getCollectedValues());
}
/**
* @throws Exception if the test fails
*/
@Test
public void testHtmlAttributeChangeListener_ReplaceAttribute() throws Exception {
final String htmlContent
= "foo\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "";
final String[] expectedValues =
{"attributeReplaced: p,title,myTitle",
"attributeReplaced: p,title,myTitle",
"attributeReplaced: p,title,myTitle"};
final HtmlPage page = loadPage(htmlContent);
final HtmlBody body = (HtmlBody) page.getHtmlElementById("myBody");
final HtmlElement p1 = page.getHtmlElementById("p1");
final HtmlAttributeChangeListenerTestImpl listenerImpl = new HtmlAttributeChangeListenerTestImpl();
page.addHtmlAttributeChangeListener(listenerImpl);
body.addHtmlAttributeChangeListener(listenerImpl);
p1.addHtmlAttributeChangeListener(listenerImpl);
final HtmlButtonInput myButton = (HtmlButtonInput) page.getHtmlElementById("myButton");
myButton.click();
assertEquals(expectedValues, listenerImpl.getCollectedValues());
assertEquals("myTitle" + 'a', p1.getAttributeValue("title"));
}
/**
* @throws Exception if the test fails
*/
@Test
public void testHtmlAttributeChangeListener_RemoveAttribute() throws Exception {
final String htmlContent
= "foo\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "";
final String[] expectedValues =
{"attributeRemoved: p,title,myTitle",
"attributeRemoved: p,title,myTitle",
"attributeRemoved: p,title,myTitle"};
final HtmlPage page = loadPage(htmlContent);
final HtmlBody body = (HtmlBody) page.getHtmlElementById("myBody");
final HtmlElement p1 = page.getHtmlElementById("p1");
final HtmlAttributeChangeListenerTestImpl listenerImpl = new HtmlAttributeChangeListenerTestImpl();
page.addHtmlAttributeChangeListener(listenerImpl);
body.addHtmlAttributeChangeListener(listenerImpl);
p1.addHtmlAttributeChangeListener(listenerImpl);
final HtmlButtonInput myButton = (HtmlButtonInput) page.getHtmlElementById("myButton");
myButton.click();
assertEquals(expectedValues, listenerImpl.getCollectedValues());
assertSame(HtmlElement.ATTRIBUTE_NOT_DEFINED, p1.getAttributeValue("title"));
}
/**
* @throws Exception if the test fails
*/
@Test
public void testHtmlAttributeChangeListener_RemoveListener() throws Exception {
final String htmlContent
= "foo\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "";
final String[] expectedValues = {"attributeReplaced: p,title,myTitle"};
final HtmlPage page = loadPage(htmlContent);
final HtmlElement p1 = page.getHtmlElementById("p1");
final HtmlAttributeChangeListenerTestImpl listenerImpl = new HtmlAttributeChangeListenerTestImpl();
p1.addHtmlAttributeChangeListener(listenerImpl);
final HtmlButtonInput myButton = (HtmlButtonInput) page.getHtmlElementById("myButton");
myButton.click();
p1.removeHtmlAttributeChangeListener(listenerImpl);
myButton.click();
assertEquals(expectedValues, listenerImpl.getCollectedValues());
assertEquals("myTitle" + 'a' + 'a', p1.getAttributeValue("title"));
}
/**
* @throws Exception if the test fails
*/
@Test
public void testMouseOver() throws Exception {
final String content = "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "";
final HtmlPage page = loadPage(content);
final HtmlBody body = (HtmlBody) page.getHtmlElementById("myBody");
body.mouseOver();
final HtmlTextArea textArea = (HtmlTextArea) page.getHtmlElementById("myTextarea");
assertEquals("mouseover-", textArea.getText());
}
/**
* @throws Exception if the test fails
*/
@Test
public void testMouseMove() throws Exception {
final String content = "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "";
final HtmlPage page = loadPage(content);
final HtmlBody body = (HtmlBody) page.getHtmlElementById("myBody");
body.mouseMove();
final HtmlTextArea textArea = (HtmlTextArea) page.getHtmlElementById("myTextarea");
assertEquals("mousemove-", textArea.getText());
}
/**
* @throws Exception if the test fails
*/
@Test
public void testMouseOut() throws Exception {
final String content = "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "";
final HtmlPage page = loadPage(content);
final HtmlBody body = (HtmlBody) page.getHtmlElementById("myBody");
body.mouseOut();
final HtmlTextArea textArea = (HtmlTextArea) page.getHtmlElementById("myTextarea");
assertEquals("mouseout-", textArea.getText());
}
/**
* @throws Exception if the test fails
*/
@Test
public void testMouseDown() throws Exception {
testMouseDown(BrowserVersion.FIREFOX_2, "mousedown-0");
testMouseDown(BrowserVersion.INTERNET_EXPLORER_6_0, "mousedown-1");
}
private void testMouseDown(final BrowserVersion browserVersion, final String expected) throws Exception {
final String content = "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "";
final List emptyList = Collections.emptyList();
final HtmlPage page = loadPage(browserVersion, content, emptyList);
final HtmlBody body = (HtmlBody) page.getHtmlElementById("myBody");
body.mouseDown();
final HtmlTextArea textArea = (HtmlTextArea) page.getHtmlElementById("myTextarea");
assertEquals(expected, textArea.getText());
}
/**
* @throws Exception if the test fails
*/
@Test
public void testMouseUp() throws Exception {
final String content = "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "";
final HtmlPage page = loadPage(content);
final HtmlBody body = (HtmlBody) page.getHtmlElementById("myBody");
body.mouseUp();
final HtmlTextArea textArea = (HtmlTextArea) page.getHtmlElementById("myTextarea");
assertEquals("mouseup-", textArea.getText());
}
/**
* @throws Exception if the test fails
*/
@Test
public void testRightClick() throws Exception {
testRightClick(BrowserVersion.INTERNET_EXPLORER_7_0, "mousedown-2-mouseup-2-contextmenu-0-");
testRightClick(BrowserVersion.FIREFOX_2, "mousedown-3-mouseup-3-contextmenu-3-");
}
private void testRightClick(final BrowserVersion browserVersion, final String expected)
throws Exception {
final String content = "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "
Hello
\n"
+ " \n"
+ "";
final HtmlPage page = loadPage(browserVersion, content, null);
final HtmlDivision div = (HtmlDivision) page.getHtmlElementById("myDiv");
div.rightClick();
final HtmlTextArea textArea = (HtmlTextArea) page.getHtmlElementById("myTextarea");
assertEquals(expected, textArea.getText());
}
/**
* Test the mouse down, then mouse up.
*
* @throws Exception if the test fails
*/
@Test
public void testMouse_Down_Up() throws Exception {
testMouse_Down_Up(BrowserVersion.INTERNET_EXPLORER_7_0, "mousedown-1-mouseup-1-");
testMouse_Down_Up(BrowserVersion.FIREFOX_2, "mousedown-1-mouseup-1-");
}
private void testMouse_Down_Up(final BrowserVersion browserVersion, final String expected)
throws Exception {
final String content = "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "
Hello
\n"
+ " \n"
+ "";
final HtmlPage page = loadPage(browserVersion, content, null);
final HtmlDivision div = (HtmlDivision) page.getHtmlElementById("myDiv");
div.mouseDown();
div.mouseUp();
final HtmlTextArea textArea = (HtmlTextArea) page.getHtmlElementById("myTextarea");
assertEquals(expected, textArea.getText());
}
/**
* @throws Exception if the test fails
*/
@Test
public void testAsXml_separateLineforEmptyElements() throws Exception {
final String content = "foo\n"
+ "
\n"
+ "";
final List collectedAlerts = new ArrayList();
final HtmlPage page = loadPage(content, collectedAlerts);
assertTrue(page.asXml().indexOf("/> ") == -1);
}
/**
* @throws Exception if an error occurs
*/
@Test
public void testGetElementsByTagName() throws Exception {
final String html
= "First\n"
+ "\n"
+ "\n"
+ "
a
b
c
\n"
+ "";
final HtmlPage page = loadPage(html);
final HtmlElement body = page.getBody();
final NodeList inputs = body.getElementsByTagName("input");
assertEquals(1, inputs.getLength());
assertEquals("button", inputs.item(0).getAttributes().getNamedItem("type").getNodeValue());
final NodeList divs = body.getElementsByTagName("div");
assertEquals(3, divs.getLength());
final HtmlDivision newDiv = new HtmlDivision(null, HtmlDivision.TAG_NAME, page, null);
body.appendChild(newDiv);
assertEquals(4, divs.getLength());
}
/**
* @throws Exception if an error occurs
*/
@Test
public void type() throws Exception {
final String html = "\n"
+ "\n"
+ " \n"
+ " \n"
+ "";
final String[] expectedAlerts = {"Hello Cruel World"};
final List collectedAlerts = new ArrayList();
final HtmlPage page = loadPage(BrowserVersion.getDefault(), html, collectedAlerts);
final HtmlTextInput input = (HtmlTextInput) page.getHtmlElementById("myInput");
input.type("Hello Cruel World");
assertEquals("Hello Cruel World", input.getValueAttribute());
((HtmlButtonInput) page.getHtmlElementById("myButton")).click();
assertEquals(expectedAlerts, collectedAlerts);
}
/**
* @throws Exception if an error occurs
*/
@Test
public void onpropertychange() throws Exception {
final String html = "\n"
+ "\n"
+ " \n"
+ "";
final String[] expectedAlerts = {"value"};
final List collectedAlerts = new ArrayList();
loadPage(BrowserVersion.INTERNET_EXPLORER_7_0, html, collectedAlerts);
assertEquals(expectedAlerts, collectedAlerts);
}
/**
* @throws Exception if the test fails
*/
@Test
public void typeOnFocus() throws Exception {
final String html
= "foo\n"
+ "\n"
+ "";
final String[] expectedAlerts = {"1"};
final List collectedAlerts = new ArrayList();
final HtmlPage page = loadPage(html, collectedAlerts);
((ClickableElement) page.getHtmlElementById("textfield1")).type('a');
assertEquals(expectedAlerts, collectedAlerts);
}
/**
* Test attribute.text and attribute.xml added for XmlElement attributes
* are undefined for HtmlElement.
* @throws Exception if the test fails
*/
@Test
public void testTextAndXmlUndefined() throws Exception {
final String html
= "foo\n"
+ " \n"
+ " \n"
+ "";
final List collectedAlerts = new ArrayList();
loadPage(BrowserVersion.INTERNET_EXPLORER_6_0, html, collectedAlerts);
final String[] expectedAlerts = {"type", "undefined", "undefined"};
assertEquals(expectedAlerts, collectedAlerts);
}
/**
* @throws Exception on test failure
*/
@Test
public void asText() throws Exception {
final String content = "\n"
+ "\n"
+ " test\n"
+ "\n"
+ "Welcome\n"
+ "
to the big world
\n"
+ "\n"
+ "";
final HtmlPage page = loadPage(content);
assertEquals("test Welcome", page.asText());
}
/**
* @throws Exception on test failure
*/
@Test
public void asTextOverridingVisibility() throws Exception {
final String content = "\n"
+ "\n"
+ " test\n"
+ "\n"
+ "Welcome\n"
+ "
hidden text\n"
+ "to the world\n"
+ "some more hidden text
\n"
+ "\n"
+ "";
final HtmlPage page = loadPage(content);
assertEquals("test Welcome to the world", page.asText());
}
/**
* @throws Exception on test failure
*/
@Test
public void asTextVisibilityCollapse() throws Exception {
final String content = "\n"
+ "\n"
+ " test\n"
+ "\n"
+ "Welcome\n"
+ "
hidden text\n"
+ "to the world\n"
+ "some more hidden text
\n"
+ "\n"
+ "";
final HtmlPage iePage = loadPage(BrowserVersion.INTERNET_EXPLORER_6_0, content, null);
assertEquals("test Welcome hidden text to the world some more hidden text", iePage.asText());
final HtmlPage ffPage = loadPage(BrowserVersion.FIREFOX_2, content, null);
assertEquals("test Welcome to the world", ffPage.asText());
}
}