Code Search for Developers
 
 
  

HtmlUnitHelperTest.java from Jameleon at Krugle


Show HtmlUnitHelperTest.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.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import net.sf.jameleon.util.Configurator;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebWindow;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlHead;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;


public class HtmlUnitHelperTest extends TestCase implements HtmlUnitDelegate {

    private HtmlUnitHelper helper;
    private WebClient client;
    private HtmlForm workingForm;

    public HtmlUnitHelperTest( String name ) {
        super( name );
    }

    public static void main(String args[]) {
        junit.textui.TestRunner.run( suite() );
    }

    public static Test suite() {
        return new TestSuite( HtmlUnitHelperTest.class );
    }

    public void setUp() {
        helper = new HtmlUnitHelper(this);
        workingForm = null;
    }

    public void tearDown(){
        Configurator.clearInstance();
    }

    public void testClickElementWithXPath() throws Exception{
        navigate("file:./tst/html/actions.html");
        
        String xpath = "//a[text()='link test']";

        helper.clickElementWithXPath(xpath);
        assertTrue("The element was not clicked", helper.xPathMatches("//form[@id='emptyform']/input[@name='link_value' and @value='link was clicked']"));

        boolean exceptionThrown = false;
        try{
            helper.clickElementWithXPath("/html/head");
        }catch (RuntimeException re){
            exceptionThrown = true;
        }
        assertTrue("A head element can not be clicked on!", exceptionThrown);
    }

    public void testStore() throws Exception{
        HtmlPage page = (HtmlPage)getCurrentWebWindow().getEnclosedPage();
        String html = page.getWebResponse().getContentAsString();
        String pathToFile= "jameleon_test_results/unit/HtmlUnitHelperTest";
        File file = helper.store(pathToFile);
        File f = new File(pathToFile+".html");
        assertEquals("Files' paths", f.getAbsolutePath(), file.getAbsolutePath());
        FileInputStream fis = new FileInputStream(f);
        int length= fis.available();
        byte b[]= new byte[length];
        fis.read(b);
        String content = new String(b);
        assertEquals("Two files", html, content);
    }

    public void testGetDelegate(){
        assertEquals(this, helper.getDelegate());
    }

    public void testGetCurrentPage(){
        WebWindow window = getCurrentWebWindow();
        String expectedHtml = window.getEnclosedPage().getWebResponse().getContentAsString();
        String actualHtml = helper.getCurrentPage().getWebResponse().getContentAsString();
        assertEquals("html", expectedHtml, actualHtml);
    }

    public void testGetCurrentWebResponse(){
        WebWindow window = getCurrentWebWindow();
        String expectedHtml = window.getEnclosedPage().getWebResponse().getContentAsString();
        String actualHtml = helper.getCurrentPage().getWebResponse().getContentAsString();
        assertEquals("html", expectedHtml, actualHtml);
    }

    public void testGetCurrentPageConent(){
        WebWindow window = getCurrentWebWindow();
        String expectedHtml = window.getEnclosedPage().getWebResponse().getContentAsString();
        String actualHtml = helper.getCurrentPageContent();
        assertEquals("html", expectedHtml, actualHtml);
    }

    public void testGetHtmlElementByAttributeNameAndValue(){
        navigate("file:./tst/html/actions.html");
        HtmlForm form;
        boolean exceptionThrown = false;
        try{
            form = (HtmlForm)helper.getHtmlElementByAttributeNameAndValue(null, "id", "formfields");
        }catch(RuntimeException re){
            exceptionThrown = true;
        }
        assertTrue("An expected exception was not thrown", exceptionThrown);

        assertNull("expected nothing back", helper.getHtmlElementByAttributeNameAndValue("form", "name", "some_bad_form_name"));

        form = (HtmlForm) helper.getHtmlElementByAttributeNameAndValue("form", "id", "formfields");
        assertNotNull("username field should exist", form.getInputByName("username"));

        HtmlInput input = (HtmlInput) helper.getHtmlElementByAttributeNameAndValue("input", "name", "username");
        assertNotNull("username field should exist", input);
        assertEquals("username field should exist", "username", input.getNameAttribute());

    }

    public void testGetHtmlFormById(){
        navigate("file:./tst/html/actions.html");
        HtmlForm form;
        boolean exceptionThrown = false;
        try{
            form = helper.getHtmlFormById(null);
        }catch(RuntimeException re){
            exceptionThrown = true;
        }
        assertTrue("An expected exception was not thrown", exceptionThrown);

        form = helper.getHtmlFormById("formfields");
        assertNotNull("username field should exist", form.getInputByName("username"));

        assertNull("expected nothing back", helper.getHtmlFormById("some_bad_form_name"));
        
    }

    public void testGetHtmlFormByIndex(){
        navigate("file:./tst/html/forms.html");
        HtmlForm form = helper.getHtmlFormByIndex(1);
        assertNotNull("username field should exist", form.getInputByName("username"));
        form = helper.getHtmlFormByIndex(2);
        assertEquals("The name of the form", "fileform", form.getNameAttribute());
        assertNull("expected nothing back", helper.getHtmlFormByIndex(21));
    }

    public void testGetHtmlFormByName(){
        navigate("file:./tst/html/forms.html");
        HtmlForm form;
        boolean exceptionThrown = false;
        try{
            form = helper.getHtmlFormByName(null);
        }catch(RuntimeException re){
            exceptionThrown = true;
        }
        assertTrue("An expected exception was not thrown", exceptionThrown);

        form = helper.getHtmlFormByName("testform1");
        assertNotNull("username field should exist", form.getInputByName("username"));
        assertNull("expected nothing back", helper.getHtmlFormByName("some_bad_form_name"));
    }

    public void testGetHtmlFormByXPath(){
        navigate("file:./tst/html/forms.html");
        HtmlForm form;
        boolean exceptionThrown = false;
        try{
            form = helper.getHtmlFormByXPath(null);
        }catch(RuntimeException re){
            exceptionThrown = true;
        }
        assertTrue("An expected exception was not thrown", exceptionThrown);

        form = helper.getHtmlFormByXPath("//form[@name='testform1']");
        assertNotNull("username field should exist", form.getInputByName("username"));
        assertNull("expected nothing back", helper.getHtmlFormByXPath("//form[@name='somebadformname']"));
    }

    public void testGetHtmlElementByXPath(){
        helper.navigate("file:./tst/html/tables.html");
        HtmlElement element = helper.getHtmlElementByXPath("/html/head[title='tables page']");
        assertTrue("Expected HEAD element", element instanceof HtmlHead);
        element = helper.getHtmlElementByXPath("/html/head/title[text()='bad title']");
        assertNull("Should have gotten back no results", element);
        boolean exceptionThrown = false;
        try{
            element = helper.getHtmlElementByXPath("//title[text()='tables page'");
        }catch(RuntimeException re){
            exceptionThrown = true;
        }   
        assertTrue("An exception should have been thrown", exceptionThrown);
    }

    public void testGetHtmlInputByName(){
        navigate("file:./tst/html/forms.html");

        HtmlInput input = helper.getHtmlInputByName("username");
        assertEquals("attribute type", "text", input.getTypeAttribute());
        input = helper.getHtmlInputByName("password");
        assertEquals("attribute type", "password", input.getTypeAttribute());
        input = helper.getHtmlInputByName("checkbox1");
        assertEquals("attribute type", "checkbox", input.getTypeAttribute());

        workingForm = helper.getHtmlFormByIndex(1);

        input = helper.getHtmlInputByName("username");
        assertEquals("attribute type", "text", input.getTypeAttribute());
        input = helper.getHtmlInputByName("password");
        assertEquals("attribute type", "password", input.getTypeAttribute());
        input = helper.getHtmlInputByName("checkbox1");
        assertEquals("attribute type", "checkbox", input.getTypeAttribute());
    }

    public void testGetHtmlInputByNameAndValue(){
        navigate("file:./tst/html/forms.html");

        HtmlInput input = helper.getHtmlInputByNameAndValue("checkbox1", "cb_val1");
        assertEquals("attribute type", "checkbox", input.getTypeAttribute());
        assertEquals("attribute name", "checkbox1", input.getNameAttribute());

        workingForm = helper.getHtmlFormByIndex(1);

        input = helper.getHtmlInputByNameAndValue("checkbox1", "cb_val1");
        assertEquals("attribute type", "checkbox", input.getTypeAttribute());
        assertEquals("attribute name", "checkbox1", input.getNameAttribute());

        input = helper.getHtmlInputByNameAndValue("checkbox2", "cb_val3");
        assertEquals("attribute type", "checkbox", input.getTypeAttribute());
        assertEquals("attribute name", "checkbox2", input.getNameAttribute());
        assertEquals("attribute name", "cb_val3", input.getValueAttribute());
    }

    public void testNavigate(){
        helper.navigate("file:./tst/html/basic.html");
        HtmlPage page = (HtmlPage)helper.getCurrentPage();
        assertEquals("title", "basic html title", page.getTitleText());
    }

    public void testSetCheckBox1(){
        navigate("file:./tst/html/forms.html");
        String name = "checkbox1";
        helper.setCheckBox(name, true);
        assertTrue("checkbox not set", helper.xPathMatches("//form[1]/input[@name='"+name+"' and @checked]"));

        workingForm = helper.getHtmlFormByIndex(1);
        helper.setCheckBox(name, false);
        assertTrue("checkbox not set", helper.xPathMatches("//form[1]/input[@name='"+name+"' and not(@checked)]"));
    }

    public void testSetCheckBox2(){
        navigate("file:./tst/html/forms.html");
        String name = "checkbox2";
        String value = "cb_val2";
        helper.setCheckBox(name, value, true);
        assertTrue("checkbox not set", helper.xPathMatches("//form[1]/input[@name='"+name+"' and @value='"+value+"' and @checked]"));

        value = "cb_val3";
        workingForm = helper.getHtmlFormByIndex(1);
        helper.setCheckBox(name, value, false);
        assertTrue("checkbox not set", helper.xPathMatches("//form[1]/input[@name='"+name+"' and @value='"+value+"' and not(@checked)]"));
    }

    public void testSetHiddenField(){
        navigate("file:./tst/html/forms.html");
        String value = "some value";
        String value2 = "another value";
        String name = "hf_1";
        assertTrue("hidden field's default value", helper.xPathMatches("//form[1]/input[@name='"+name+"' and @value='hf value']"));
        helper.setHiddenField(name, value);
        assertTrue("hidden field not set", helper.xPathMatches("//form[1]/input[@name='"+name+"' and @value='"+value+"']"));

        workingForm = helper.getHtmlFormByIndex(1);
        helper.setHiddenField(name, value2);
        assertTrue("hidden field not set", helper.xPathMatches("//form[1]/input[@name='"+name+"' and @value='"+value2+"']"));
    }

    public void testSetRadioButton(){
        navigate("file:./tst/html/forms.html");
        String name = "radio1";
        String value = "rad1";
        helper.setRadioButton(name, value, true);
        assertTrue("radio button not set", helper.xPathMatches("//form[1]/input[@name='"+name+"' and @value='"+value+"' and @checked]"));

        value = "rad2";
        workingForm = helper.getHtmlFormByIndex(1);
        helper.setRadioButton(name, value, false);
        assertTrue("radio button not set", helper.xPathMatches("//form[1]/input[@name='"+name+"' and @value='"+value+"' and not(@checked)]"));
    }

    public void testSetSelectFieldByIndex(){
        navigate("file:./tst/html/forms.html");
        String name = "sf_single_values";
        int index = 1;
        helper.setSelectFieldByIndex(name, index, true);
        assertTrue("select field not set", helper.xPathMatches("//form[1]/select[@name='"+name+"']/option[@value='"+index+"' and @selected]"));

        workingForm = helper.getHtmlFormByIndex(1);
        helper.setSelectFieldByIndex(name, index, false);
        assertTrue("select was still set", helper.xPathMatches("//form[1]/select[@name='"+name+"']/option[@value='"+index+"' and not(@selected)]"));

        name = "sf_multiple_values";
        helper.setSelectFieldByIndex(name, index, true);
        assertTrue("select field not set", helper.xPathMatches("//form[1]/select[@name='"+name+"']/option[@value='"+index+"' and @selected]"));

        index = 2;
        helper.setSelectFieldByIndex(name, index, true);
        assertTrue("select field not set", helper.xPathMatches("//form[1]/select[@name='"+name+"']/option[@value='"+index+"' and @selected]"));
        assertTrue("select field should still be selected", helper.xPathMatches("//form[1]/select[@name='"+name+"']/option[@value='1' and @selected]"));
        assertTrue("select field should not be selected", helper.xPathMatches("//form[1]/select[@name='"+name+"']/option[@value='3' and not(@selected)]"));

        helper.setSelectFieldByIndex(name, 3, false);
        assertTrue("select field not set", helper.xPathMatches("//form[1]/select[@name='"+name+"']/option[@value='"+index+"' and @selected]"));
        assertTrue("select field should still be selected", helper.xPathMatches("//form[1]/select[@name='"+name+"']/option[@value='1' and @selected]"));
        assertTrue("select field should not be selected", helper.xPathMatches("//form[1]/select[@name='"+name+"']/option[@value='3' and not(@selected)]"));
    }

    public void testSetSelectFieldByOptionText(){
        navigate("file:./tst/html/forms.html");
        String name = "sf_single_values";
        String value = "1";
        String optionText = "one";
        helper.setSelectFieldByOptionText(name, optionText, true);
        assertTrue("select field not set", helper.xPathMatches("//form[1]/select[@name='"+name+"']/option[@value='"+value+"' and @selected]"));

        workingForm = helper.getHtmlFormByIndex(1);
        helper.setSelectFieldByOptionText(name, optionText, false);
        assertTrue("select was still set", helper.xPathMatches("//form[1]/select[@name='"+name+"']/option[@value='"+value+"' and not(@selected)]"));

        name = "sf_multiple_values";
        helper.setSelectFieldByOptionText(name, optionText, true);
        assertTrue("select field not set", helper.xPathMatches("//form[1]/select[@name='"+name+"']/option[@value='"+value+"' and @selected]"));

        optionText = "two";
        value = "2";
        helper.setSelectFieldByOptionText(name, optionText, true);
        assertTrue("select field not set", helper.xPathMatches("//form[1]/select[@name='"+name+"']/option[@value='"+value+"' and @selected]"));
        assertTrue("select field should still be selected", helper.xPathMatches("//form[1]/select[@name='"+name+"']/option[@value='1' and @selected]"));
        assertTrue("select field should not be selected", helper.xPathMatches("//form[1]/select[@name='"+name+"']/option[@value='3' and not(@selected)]"));

        optionText = "three";
        helper.setSelectFieldByOptionText(name, optionText , false);
        assertTrue("select field not set", helper.xPathMatches("//form[1]/select[@name='"+name+"']/option[@value='"+value+"' and @selected]"));
        assertTrue("select field should still be selected", helper.xPathMatches("//form[1]/select[@name='"+name+"']/option[@value='1' and @selected]"));
        assertTrue("select field should not be selected", helper.xPathMatches("//form[1]/select[@name='"+name+"']/option[@value='3' and not(@selected)]"));
    }

    public void testSetSelectFieldByValue(){
        navigate("file:./tst/html/forms.html");
        String name = "sf_single_values";
        String value = "1";
        helper.setSelectFieldByValue(name, value, true);
        assertTrue("select field not set", helper.xPathMatches("//form[1]/select[@name='"+name+"']/option[@value='"+value+"' and @selected]"));

        value = "1";
        workingForm = helper.getHtmlFormByIndex(1);
        helper.setSelectFieldByValue(name, value, false);
        assertTrue("select was still set", helper.xPathMatches("//form[1]/select[@name='"+name+"']/option[@value='"+value+"' and not(@selected)]"));

        name = "sf_multiple_values";
        value = "1";
        helper.setSelectFieldByValue(name, value, true);
        assertTrue("select field not set", helper.xPathMatches("//form[1]/select[@name='"+name+"']/option[@value='"+value+"' and @selected]"));

        name = "sf_multiple_values";
        value = "2";
        helper.setSelectFieldByValue(name, value, true);
        assertTrue("select field not set", helper.xPathMatches("//form[1]/select[@name='"+name+"']/option[@value='"+value+"' and @selected]"));
        assertTrue("select field should still be selected", helper.xPathMatches("//form[1]/select[@name='"+name+"']/option[@value='1' and @selected]"));
        assertTrue("select field should not be selected", helper.xPathMatches("//form[1]/select[@name='"+name+"']/option[@value='3' and not(@selected)]"));

        helper.setSelectFieldByValue(name, "3", false);
        assertTrue("select field not set", helper.xPathMatches("//form[1]/select[@name='"+name+"']/option[@value='"+value+"' and @selected]"));
        assertTrue("select field should still be selected", helper.xPathMatches("//form[1]/select[@name='"+name+"']/option[@value='1' and @selected]"));
        assertTrue("select field should not be selected", helper.xPathMatches("//form[1]/select[@name='"+name+"']/option[@value='3' and not(@selected)]"));
    }

    public void testSetSelectFieldByXPath(){
        navigate("file:./tst/html/forms.html");
        String xpath = "//form[1]/select[@name='sf_single_values']/option[1]";
        String name= "sf_single_values";
        helper.setSelectFieldByXPath(xpath, true);
        assertTrue("select field not set", helper.xPathMatches("//form[1]/select[@name='"+name+"']/option[@value='1' and @selected]"));

        xpath = "//form[1]/select[@name='sf_single_values']/option[@value='1']";
        helper.setSelectFieldByXPath(xpath, false);
        assertTrue("select was still set", helper.xPathMatches("//form[1]/select[@name='"+name+"']/option[@value='1' and not(@selected)]"));

        name = "sf_multiple_values";
        xpath = "//form[1]/select[@name='sf_multiple_values']/option[@value='1']";
        helper.setSelectFieldByXPath(xpath, true);
        assertTrue("select field not set", helper.xPathMatches("//form[1]/select[@name='"+name+"']/option[@value='1' and @selected]"));

        xpath = "//form[1]/select[@name='sf_multiple_values']/option[text()='two']";
        helper.setSelectFieldByXPath(xpath, true);
        assertTrue("select field not set", helper.xPathMatches("//form[1]/select[@name='"+name+"']/option[@value='2' and @selected]"));
        assertTrue("select field should still be selected", helper.xPathMatches("//form[1]/select[@name='"+name+"']/option[@value='1' and @selected]"));
        assertTrue("select field should not be selected", helper.xPathMatches("//form[1]/select[@name='"+name+"']/option[@value='3' and not(@selected)]"));

        xpath = "//form[1]/select[@name='sf_multiple_values']/option[text()='three']";
        helper.setSelectFieldByXPath(xpath, false);
        assertTrue("select field not set", helper.xPathMatches("//form[1]/select[@name='"+name+"']/option[@value='2' and @selected]"));
        assertTrue("select field should still be selected", helper.xPathMatches("//form[1]/select[@name='"+name+"']/option[@value='1' and @selected]"));
        assertTrue("select field should not be selected", helper.xPathMatches("//form[1]/select[@name='"+name+"']/option[@value='3' and not(@selected)]"));
    }

    public void testSetTextArea(){
        navigate("file:./tst/html/forms.html");
        String value = "some value";
        String value2 = "another value";
        String name = "ta_1";
        helper.setTextArea(name, value);
        assertTrue("text area not set", helper.xPathMatches("//form[1]/textarea[@name='"+name+"' and text()='"+value+"']"));

        workingForm = helper.getHtmlFormByIndex(1);
        helper.setTextArea(name, value2);
        assertTrue("text field not set", helper.xPathMatches("//form[1]/textarea[@name='"+name+"' and text()='"+value2+"']"));
    }

    public void testSetTextField(){
        navigate("file:./tst/html/forms.html");
        String value = "some value";
        String value2 = "another value";
        String name = "username";
        helper.setTextField(name, value);
        assertTrue("text field not set", helper.xPathMatches("//form[1]/input[@name='"+name+"' and @value='"+value+"']"));

        workingForm = helper.getHtmlFormByIndex(1);
        helper.setTextField(name, value2);
        assertTrue("text field not set", helper.xPathMatches("//form[1]/input[@name='"+name+"' and @value='"+value2+"']"));
    }

    public void testSetPasswordField(){
        navigate("file:./tst/html/forms.html");
        String value = "some value";
        String value2 = "another value";
        String name = "password";
        helper.setPasswordField(name, value);
        assertTrue("password field not set", helper.xPathMatches("//form[1]/input[@name='"+name+"' and @value='"+value+"']"));
        workingForm = helper.getHtmlFormByIndex(1);
        helper.setPasswordField(name, value2);
        assertTrue("password field not set", helper.xPathMatches("//form[1]/input[@name='"+name+"' and @value='"+value2+"']"));
    }

    public void testSetFileField(){
        navigate("file:./tst/html/forms.html");
        String value = "some value";
        String value2 = "another value";
        String name = "some_file";
        helper.setFileField(name, value);
        assertTrue("file field not set", helper.xPathMatches("//form[2]/input[@name='"+name+"' and @value='"+value+"']"));
        workingForm = helper.getHtmlFormByIndex(2);
        helper.setFileField(name, value2);
        assertTrue("file field not set", helper.xPathMatches("//form[2]/input[@name='"+name+"' and @value='"+value2+"']"));
    }

    public void testSetHtmlInputValue(){
        navigate("file:./tst/html/forms.html");
        String xpath = "//form[@name='testform1']/input[@type='text' and @name='username']";
        String value = "un";
        HtmlInput input = (HtmlInput)helper.getHtmlElementByXPath(xpath);
        helper.setHtmlInputValue(input, value, "text");
        assertEquals("username field", value, input.getValueAttribute());
        assertTrue("user field value not set", helper.xPathMatches("//form[@name='testform1']/input[@type='text' and @name='username' and @value='un']"));
        Exception e = null;
        try{
            helper.setHtmlInputValue(input, value, "password");
        }catch(RuntimeException re){
            e = re;
        }
        assertNotNull("And exception should have been thrown", e);
        assertTrue("An error message about the type not matching should be part of the error message",
                   e.getMessage().indexOf("was not expected 'password'") > -1);

        e = null;
        try{
            helper.setHtmlInputValue(null, value, "password");
        }catch(RuntimeException re){
            e = re;
        }
        assertNotNull("And exception should have been thrown", e);
        assertTrue("An error message about the input field being null should be part of the error message",
                   e.getMessage().indexOf("input field was null") > -1);
    }

    public void testSetHtmlInputByXPath(){
        navigate("file:./tst/html/forms.html");
        String xpath = "//form[@name='testform1']/input[@type='text' and @name='username']";
        String value = "un";
        
        helper.setHtmlInputValueByXPath(xpath, value, "text");
        assertTrue("field value not set", helper.xPathMatches("//form[@name='testform1']/input[@type='text' and @name='username' and @value='un']"));
        xpath = "//form[@name='testform1']/input[@type='text' and @name='badfieldname']";
        boolean exceptionThrown = false;
        try{
            helper.setHtmlInputValueByXPath(xpath, value, "text");
        }catch(RuntimeException re){
            exceptionThrown = true;
        }
        assertTrue("An exception should have been thrown", exceptionThrown);
    }

    public void testXPathMatches(){
        helper.navigate("file:./tst/html/tables.html");
        assertTrue("title of the page 1", helper.xPathMatches("/html/head[title='tables page']"));
        assertTrue("title of the page 2", helper.xPathMatches("//title[text()='tables page']"));
        assertFalse("XPath w/o a match", helper.xPathMatches("/html/head/title[text()='bad title']"));
        boolean exceptionThrown = false;
        try{
            assertTrue("invalid Xpath", helper.xPathMatches("//title[text()='tables page'"));
        }catch(RuntimeException re){
            exceptionThrown = true;
        }   
        assertTrue("An exception should have been thrown", exceptionThrown);
    }


//Helper methods and interface methods for testing
    /**
     * Gets a handle on the current web window.
     * 
     * @return WebWindow
     */
    public WebWindow getCurrentWebWindow(){
        if (client == null) {
            navigate();
        }
        return client.getCurrentWindow();
    }

    public WebClient getWebClient(){
        if (client == null) {
            navigate();
        }
        return client;
    }

    public HtmlForm getWorkingForm(){
        return workingForm;
    }

    public void navigate(){
        navigate("file:./tst/html/sessionTag.html");
    }

    public void navigate(String urlS){
        client = new WebClient();
        try{
            URL url = new URL(urlS);
            client.getPage(url);
        }catch(MalformedURLException mfe){
            fail("bad url "+mfe.getMessage());
        }catch (IOException ioe){
            fail("IOException "+ioe.getMessage());
        }
    }
}




See more files for this project here

Jameleon

Jameleon is a data-driven automated testing tool that is easily extensible via plug-ins. Features of applications are automated in Java and tied together independently in XML, creating self-documenting automated test cases.

Project homepage: http://sourceforge.net/projects/jameleon
Programming language(s): Java,XML
License: other

  HtmlUnitHelperTest.java
  MockHtmlUnitHelper.java