HtmlUnitFunctionTagTest.java from Jameleon at Krugle
Show HtmlUnitFunctionTagTest.java syntax highlighted
/*
Jameleon HtmlUnit plug-in - A plug-in that uses HtmlUnit to drive web sites
Copyright (C) 2006 Christian W. Hargraves (engrean@hotmail.com)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111AssertLevel.NO_FUNCTION07 USA
*/
package net.sf.jameleon.plugin.htmlunit;
import junit.framework.AssertionFailedError;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import net.sf.jameleon.MockTestCaseTag;
import net.sf.jameleon.plugin.htmlunit.util.MockHtmlUnitHelper;
import net.sf.jameleon.util.AssertLevel;
import net.sf.jameleon.util.Configurator;
import net.sf.jameleon.util.StateStorer;
import org.apache.commons.jelly.JellyContext;
import org.apache.commons.jelly.JellyTagException;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
public class HtmlUnitFunctionTagTest extends TestCase {
protected MockHtmlUnitFunctionTag functionTag;
protected MockHtmlUnitSessionTag sessionTag;
public HtmlUnitFunctionTagTest( String name ) {
super( name );
}
public static void main(String args[]) {
junit.textui.TestRunner.run( suite() );
}
public static Test suite() {
return new TestSuite( HtmlUnitFunctionTagTest.class );
}
public void setUp() throws JellyTagException {
functionTag = new MockHtmlUnitFunctionTag();
functionTag.setContext(new JellyContext());
sessionTag = new MockHtmlUnitSessionTag();
sessionTag.setContext(functionTag.getContext());
sessionTag.setParent(new MockTestCaseTag());
functionTag.setParent(sessionTag);
}
public void tearDown(){
Configurator.clearInstance();
}
public void testAssertXPathMatches(){
functionTag.setupEnvironment();
functionTag.navigate("file:./tst/html/tables.html");
String xpath = "//head[title='tables page']";
functionTag.assertXPathMatches(xpath);
functionTag.assertXPathMatches(xpath, AssertLevel.NO_LEVEL);
functionTag.assertXPathMatches("some message",xpath);
functionTag.assertXPathMatches("some message",xpath, AssertLevel.NO_LEVEL);
assertEquals("HtmlUnitDelegate", functionTag.sessionTag, functionTag.helper.getDelegate());
}
public void testClickElementWithXPath() throws Exception{
MockHtmlUnitHelper helper = setMockHelper();
String xpath = "//head[title='tables page']";
functionTag.clickElementWithXPath(xpath);
assertEquals("Xpath sent to helper", xpath, helper.clickElementWithXPathXPath);
}
public void testGetHtmlElementByXPath(){
MockHtmlUnitHelper helper = setMockHelper();
String xpath = "//head[title='tables page']";
functionTag.getHtmlElementByXPath(xpath);
assertEquals("Xpath sent to helper", xpath, helper.getHtmlElementByXPathXPath);
}
public void testGetHtmlElementByAttributeNameAndValue(){
MockHtmlUnitHelper helper = setMockHelper();
String ele = "test";
String attrName = "foo";
String attrValue = "bar";
helper.getHtmlElementByAttributeNameAndValue(ele, attrName, attrValue);
assertEquals("element name", ele, helper.getHtmlElementByAttributeNameAndValueTagname);
assertEquals("attribute name", attrName, helper.getHtmlElementByAttributeNameAndValueAttributeName);
assertEquals("attribute value", attrValue, helper.getHtmlElementByAttributeNameAndValueAttributeValue);
}
public void testGetHtmlForm(){
functionTag.setupEnvironment();
functionTag.navigate("file:./tst/html/forms.html");
//By index
HtmlForm form = functionTag.getHtmlForm("1");
assertEquals("form name", "testform1", form.getNameAttribute());
//By name
form = functionTag.getHtmlForm("testform1");
assertNotNull("username field should exist", form.getInputByName("username"));
//By id
form = functionTag.getHtmlForm("file_form_id");
assertEquals("The name of the form", "fileform", form.getNameAttribute());
//By xpath
form = functionTag.getHtmlForm("//form[2]");
assertNotNull("form with xpath should not be null", form);
assertEquals("The name of the form", "fileform", form.getNameAttribute());
}
public void testGetHtmlFormById(){
MockHtmlUnitHelper helper = setMockHelper();
String value = "test";
functionTag.getHtmlFormById(value);
assertEquals("id", value, helper.getHtmlFormByIdAttributeValue);
}
public void testGetHtmlFormByIndex(){
MockHtmlUnitHelper helper = setMockHelper();
int index = 1;
functionTag.getHtmlFormByIndex(index);
assertEquals("index", index, helper.getHtmlFormByIndex);
}
public void testGetHtmlFormByName(){
MockHtmlUnitHelper helper = setMockHelper();
String value = "test";
functionTag.getHtmlFormByName(value);
assertEquals("id", value, helper.getHtmlFormByNameAttributeValue);
}
public void testGetHtmlFormByXPath(){
MockHtmlUnitHelper helper = setMockHelper();
String xpath = "//form[1]";
functionTag.getHtmlFormByXPath(xpath);
assertEquals("form name", xpath, helper.getHtmlFormByXPath);
}
public void testGetWorkingForm(){
functionTag.setupEnvironment();
functionTag.navigate("file:./tst/html/forms.html");
HtmlForm form = functionTag.getHtmlFormByIndex(1);
functionTag.workingForm = form;
assertEquals("working form", form, functionTag.workingForm);
}
public void testSetInputElement(){
MockHtmlUnitHelper helper = setMockHelper();
functionTag.setHtmlInputValue(null, "foovalue", "bartype");
assertEquals("value", "foovalue", helper.setHtmlInputValueValue);
assertEquals("type", "bartype", helper.setHtmlInputValueType);
}
public void testSetWorkingFormById(){
functionTag.setupEnvironment();
functionTag.navigate("file:./tst/html/forms.html");
functionTag.setWorkingFormById("test_form_1");
assertEquals("working form", "testform1", functionTag.workingForm.getNameAttribute());
}
public void testSetWorkingFormByIndex(){
functionTag.setupEnvironment();
functionTag.navigate("file:./tst/html/forms.html");
functionTag.setWorkingFormByIndex(1);
assertEquals("working form", "testform1", functionTag.workingForm.getNameAttribute());
}
public void testSetWorkingFormByName(){
functionTag.setupEnvironment();
functionTag.navigate("file:./tst/html/forms.html");
functionTag.setWorkingFormByName("testform1");
assertEquals("working form", "test_form_1", functionTag.workingForm.getIdAttribute());
}
public void testSetWorkingFormByXPath(){
functionTag.setupEnvironment();
functionTag.navigate("file:./tst/html/forms.html");
functionTag.setWorkingFormByXPath("//form[@name='testform1']");
assertEquals("working form", "test_form_1", functionTag.workingForm.getIdAttribute());
}
public void testSetWorkingForm(){
functionTag.setupEnvironment();
functionTag.navigate("file:./tst/html/forms.html");
functionTag.setWorkingForm("test_form_1");
assertEquals("working form", "testform1", functionTag.workingForm.getNameAttribute());
functionTag.setWorkingForm("1");
assertEquals("working form", "testform1", functionTag.workingForm.getNameAttribute());
functionTag.setWorkingForm("testform1");
assertEquals("working form", "test_form_1", functionTag.workingForm.getIdAttribute());
functionTag.setWorkingForm("//form[@name='testform1']");
assertEquals("working form", "test_form_1", functionTag.workingForm.getIdAttribute());
}
public void testSetInputElementByXPath(){
MockHtmlUnitHelper helper = setMockHelper();
String xpath = "//form[@name='testform1']/input[@type='text' and @name='username']";
String value = "un";
String type = "text";
functionTag.setHtmlInputValueByXPath(xpath, value, type);
assertEquals("value", value, helper.setHtmlInputValueByXPathValue);
assertEquals("type", type, helper.setHtmlInputValueByXPathType);
assertEquals("xpath", xpath, helper.setHtmlInputValueByXPathXPath);
}
public void testSetCheckBox1(){
MockHtmlUnitHelper helper = setMockHelper();
String fieldName = "un";
functionTag.setCheckBox(fieldName, false);
assertEquals("checkBox", fieldName, helper.setCheckBoxName);
}
public void testSetCheckBox2(){
MockHtmlUnitHelper helper = setMockHelper();
String fieldName = "un";
String fieldValue = "va";
functionTag.setCheckBox(fieldName, fieldValue, false);
assertEquals("checkBox name", fieldName, helper.setCheckBoxName);
assertEquals("checkBox value", fieldValue, helper.setCheckBoxValue);
}
public void testSetRadioButton(){
MockHtmlUnitHelper helper = setMockHelper();
String fieldName = "un";
String fieldValue = "va";
functionTag.setRadioButton(fieldName, fieldValue, false);
assertEquals("radio button name", fieldName, helper.setRadioButtonName);
assertEquals("radio button value", fieldValue, helper.setRadioButtonValue);
}
public void testSetFileField(){
MockHtmlUnitHelper helper = setMockHelper();
String fieldName = "un";
functionTag.setFileField(fieldName, null);
assertEquals("file field", fieldName, helper.setFileField);
}
public void testSetHiddenField(){
MockHtmlUnitHelper helper = setMockHelper();
String fieldName = "un";
functionTag.setHiddenField(fieldName, null);
assertEquals("hidden field", fieldName, helper.setHiddenField);
}
public void testSetPasswordField(){
MockHtmlUnitHelper helper = setMockHelper();
String fieldName = "un";
functionTag.setPasswordField(fieldName, null);
assertEquals("password field", fieldName, helper.setPasswordField);
}
public void testSetSelectFieldByIndex(){
MockHtmlUnitHelper helper = setMockHelper();
String fieldName = "un";
int index = 5;
functionTag.setSelectFieldByIndex(fieldName, index, true);
assertEquals("select field name", fieldName, helper.setSelectFieldByIndexName);
assertEquals("select field index #", index, helper.setSelectFieldByIndex);
}
public void testSetSelectFieldByOptionText(){
MockHtmlUnitHelper helper = setMockHelper();
String fieldName = "un";
String optionText = "va";
functionTag.setSelectFieldByOptionText(fieldName, optionText, true);
assertEquals("select field name", fieldName, helper.setSelectFieldByOptionTextName);
assertEquals("select field option text", optionText, helper.setSelectFieldByOptionText);
}
public void testSetSelectFieldByValue(){
MockHtmlUnitHelper helper = setMockHelper();
String fieldName = "un";
String valueAttribute = "va";
functionTag.setSelectFieldByValue(fieldName, valueAttribute, true);
assertEquals("select field name", fieldName, helper.setSelectFieldByValueName);
assertEquals("select field value attribute", valueAttribute, helper.setSelectFieldByValueAttribute);
}
public void testSetSelectFieldByXPath(){
MockHtmlUnitHelper helper = setMockHelper();
String xpath = "un";
functionTag.setSelectFieldByXPath(xpath, true);
assertEquals("select option xpath", xpath, helper.setSelectFieldByXPath);
}
public void testSetTextArea(){
MockHtmlUnitHelper helper = setMockHelper();
String fieldName = "un";
functionTag.setTextArea(fieldName, null);
assertEquals("text area field", fieldName, helper.setTextArea);
}
public void testSetTextField(){
MockHtmlUnitHelper helper = setMockHelper();
String fieldName = "un";
functionTag.setTextField(fieldName, null);
assertEquals("text field", fieldName, helper.setTextField);
}
public void testSetupEnvironment(){
assertNull("session tag before setup", functionTag.sessionTag);
assertNull("session htmlHelper", functionTag.helper);
functionTag.setupEnvironment();
assertNotNull("session tag after setup", functionTag.sessionTag);
assertEquals("HtmlUnitDelegate", functionTag.sessionTag, functionTag.helper.getDelegate());
}
public void testStore() throws Exception{
sessionTag.baseUrl = "file:";
sessionTag.beginAt = "./tst/html/sessionTag.html";
sessionTag.setUpSession();
sessionTag.startApplication();
functionTag.setupEnvironment();
MockHtmlUnitHelper helper = setMockHelper();
StateStorer.getInstance().eventOccured(StateStorer.ON_ERROR_EVENT);
assertNotNull("pathToFile", functionTag.pathToFileToStore);
assertNotNull("pathToFile", helper.storeFileName);
assertEquals("paths", functionTag.pathToFileToStore, helper.storeFileName);
}
public void testNavigate(){
MockHtmlUnitHelper helper = setMockHelper();
functionTag.navigate("/some/url");
assertEquals("url", "/some/url", helper.navigateUrl);
}
public void testAssertTitleEquals(){
functionTag.setupEnvironment();
functionTag.navigate("file:./tst/html/basic.html");
functionTag.assertTitleEquals("basic html title");
functionTag.assertTitleEquals("basic html title", AssertLevel.NO_LEVEL);
functionTag.assertTitleEquals("check title", "basic html title");
functionTag.assertTitleEquals("check title", "basic html title", AssertLevel.NO_LEVEL);
boolean assertFailed = false;
try{
functionTag.assertTitleEquals("basic html");
}catch (AssertionFailedError afe){
assertFailed = true;
}
assertTrue("The title was wrong, but it still passed", assertFailed);
}
public void testAssertTextPresent(){
functionTag.setupEnvironment();
functionTag.navigate("file:./tst/html/basic.html");
functionTag.assertTextPresent("some text");
functionTag.assertTextPresent("some text", AssertLevel.NO_LEVEL);
functionTag.assertTextPresent("err msg", "some text");
functionTag.assertTextPresent("err msg", "some text", AssertLevel.NO_LEVEL);
}
private MockHtmlUnitHelper setMockHelper(){
MockHtmlUnitHelper helper = new MockHtmlUnitHelper(sessionTag);
functionTag.helper = helper;
return helper;
}
}
See more files for this project here