001 /*
002 * Created on Dec 12, 2004
003 *
004 * Copyright 2004 Chris Nelson
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
009 * Unless required by applicable law or agreed to in writing,
010 * software distributed under the License is distributed on an "AS IS" BASIS,
011 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 * See the License for the specific language governing permissions and limitations under the License.
013 */
014 package org.trails.test.functional;
015
016 import com.gargoylesoftware.htmlunit.ElementNotFoundException;
017 import com.gargoylesoftware.htmlunit.WebClient;
018 import com.gargoylesoftware.htmlunit.html.*;
019 import com.gargoylesoftware.htmlunit.html.xpath.HtmlUnitXPath;
020 import junit.framework.TestCase;
021 import org.jaxen.JaxenException;
022
023 import java.io.IOException;
024 import java.net.URL;
025 import java.util.Properties;
026
027 /**
028 * @author fus8882
029 * <p/>
030 * TODO To change the template for this generated type comment go to
031 * Window - Preferences - Java - Code Style - Code Templates
032 */
033 public class FunctionalTest extends TestCase
034 {
035
036 WebClient webClient;
037 protected HtmlPage startPage;
038
039 public void setUp() throws Exception
040 {
041 Properties testProperties = new Properties();
042 testProperties.load(this.getClass().getResourceAsStream("/functionaltest.properties"));
043 webClient = new WebClient();
044 setUpWebClient(webClient);
045 startPage = (HtmlPage) webClient.getPage(new URL(testProperties.getProperty("test.url")));
046 }
047
048 protected HtmlForm getFirstForm(HtmlPage page)
049 {
050 return (HtmlForm) page.getForms().get(0);
051 }
052
053 protected HtmlPage clickButton(HtmlForm form, String buttonValue) throws IOException
054 {
055 ClickableElement button = null;
056 try
057 {
058 button = ((HtmlSubmitInput) form.getInputByValue(buttonValue));
059 } catch (ElementNotFoundException e)
060 {
061 button = (HtmlButton) form.getButtonByName(buttonValue);
062 }
063 return (HtmlPage) button.click();
064 }
065
066 protected HtmlPage clickButton(HtmlPage page, String buttonValue) throws IOException
067 {
068 return clickButton((HtmlForm) page.getForms().get(0), buttonValue);
069 }
070
071 protected HtmlPage clickLinkOnPage(HtmlPage page, String linkText) throws IOException
072 {
073 return (HtmlPage) page.getFirstAnchorByText(linkText).click();
074 }
075
076 protected HtmlDivision getErrorDiv(HtmlPage page) throws JaxenException
077 {
078 return (HtmlDivision) new HtmlUnitXPath("//div[@class='error']").selectSingleNode(page);
079 }
080
081 protected String getId(String idField, HtmlPage savedCategoryPage) throws JaxenException
082 {
083 HtmlListItem span = (HtmlListItem) new HtmlUnitXPath("//li[contains(., '" + idField + "')]").selectSingleNode(savedCategoryPage);
084 return span.asText().replaceAll(idField, "").trim();
085 }
086
087 protected void assertXPathPresent(HtmlPage page, String xpath) throws Exception
088 {
089 assertNotNull(new HtmlUnitXPath(xpath).selectSingleNode(page));
090
091 }
092
093 protected void assertXPathNotPresent(HtmlPage page, String xpath) throws Exception
094 {
095 assertNull(new HtmlUnitXPath(xpath).selectSingleNode(page));
096 }
097
098 protected HtmlTextArea getTextAreaByName(HtmlPage page, String name) throws JaxenException
099 {
100 HtmlTextArea textArea = (HtmlTextArea)
101 new HtmlUnitXPath("//textarea/preceding-sibling::label[contains(text(), '" + name + "')]/following-sibling::textarea").selectSingleNode(page);
102 return textArea;
103 }
104
105 protected HtmlInput getInputByName(HtmlPage page, String name) throws JaxenException
106 {
107 return (HtmlInput)
108 new HtmlUnitXPath("//input/preceding-sibling::label[contains(text(), '" + name + "')]/following-sibling::input").selectSingleNode(page);
109 }
110
111 protected HtmlSelect getSelectByName(HtmlPage page, String name) throws JaxenException
112 {
113 return (HtmlSelect)
114 new HtmlUnitXPath("//select/preceding-sibling::label[contains(text(), '" + name + "')]/following-sibling::select").selectSingleNode(page);
115 }
116
117 /**
118 * Hook method which is called during setup phase, before the first request.
119 * It allows subclasses to modify the webClient, such as for disabling javascript
120 *
121 * @param webClient
122 */
123 protected void setUpWebClient(WebClient webClient)
124 {
125 /**
126 * momentarily disabling javascript for all the functional test cases
127 * until we add the dojo toolkit
128 */
129 webClient.setJavaScriptEnabled(false);
130 }
131
132 }