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.tynamo.test.functional;
015    
016    import com.gargoylesoftware.htmlunit.ElementNotFoundException;
017    import com.gargoylesoftware.htmlunit.WebClient;
018    import com.gargoylesoftware.htmlunit.html.*;
019    import static org.testng.Assert.*;
020    
021    import java.io.IOException;
022    import java.net.URL;
023    import java.util.Properties;
024    
025    public class FunctionalTest
026    {
027    
028            WebClient webClient;
029            protected HtmlPage startPage;
030    
031            public void setUp() throws Exception
032            {
033                    Properties testProperties = new Properties();
034                    testProperties.load(this.getClass().getResourceAsStream("/functionaltest.properties"));
035                    webClient = new WebClient();
036                    setUpWebClient(webClient);
037                    startPage = (HtmlPage) webClient.getPage(new URL(testProperties.getProperty("test.url")));
038            }
039    
040            protected HtmlForm getFirstForm(HtmlPage page)
041            {
042                    return (HtmlForm) page.getForms().get(0);
043            }
044    
045            protected HtmlPage clickButton(HtmlForm form, String buttonValue) throws IOException
046            {
047                    ClickableElement button = null;
048                    try
049                    {
050                            button = ((HtmlSubmitInput) form.getInputByValue(buttonValue));
051                    } catch (ElementNotFoundException e)
052                    {
053                            button = (HtmlButton) form.getButtonByName(buttonValue);
054                    }
055                    return (HtmlPage) button.click();
056            }
057    
058            protected HtmlPage clickButton(HtmlPage page, String buttonValue) throws IOException
059            {
060                    return clickButton((HtmlForm) page.getForms().get(0), buttonValue);
061            }
062    
063            protected HtmlPage clickLinkOnPage(HtmlPage page, String linkText) throws IOException
064            {
065                    return (HtmlPage) page.getFirstAnchorByText(linkText).click();
066            }
067    
068    //      protected HtmlDivision getErrorDiv(HtmlPage page) throws JaxenException
069    //      {
070    //              return (HtmlDivision) new HtmlUnitXPath("//div[@class='error']").selectSingleNode(page);
071    //      }
072    //
073    //      protected String getId(String idField, HtmlPage savedCategoryPage) throws JaxenException
074    //      {
075    //              HtmlListItem span = (HtmlListItem) new HtmlUnitXPath("//li[contains(., '" + idField + "')]").selectSingleNode(savedCategoryPage);
076    //              return span.asText().replaceAll(idField, "").trim();
077    //      }
078    
079            protected void assertXPathPresent(HtmlPage page, String xpath) throws Exception
080            {
081                    assertNotNull(page.getByXPath(xpath).get(0));
082    
083            }
084    
085    //      protected void assertXPathNotPresent(HtmlPage page, String xpath) throws Exception
086    //      {
087    //              assertNull(new HtmlUnitXPath(xpath).selectSingleNode(page));
088    //      }
089    //
090    //      protected HtmlTextArea getTextAreaByName(HtmlPage page, String name) throws JaxenException
091    //      {
092    //              HtmlTextArea textArea = (HtmlTextArea)
093    //                              new HtmlUnitXPath("//textarea/preceding-sibling::label[contains(text(), '" + name + "')]/following-sibling::textarea").selectSingleNode(page);
094    //              return textArea;
095    //      }
096    //
097    //      protected HtmlInput getInputByName(HtmlPage page, String name) throws JaxenException
098    //      {
099    //              return (HtmlInput)
100    //                              new HtmlUnitXPath("//input/preceding-sibling::label[contains(text(), '" + name + "')]/following-sibling::input").selectSingleNode(page);
101    //      }
102    //
103    //      protected HtmlSelect getSelectByName(HtmlPage page, String name) throws JaxenException
104    //      {
105    //              return (HtmlSelect)
106    //                              new HtmlUnitXPath("//select/preceding-sibling::label[contains(text(),  '" + name + "')]/following-sibling::select").selectSingleNode(page);
107    //      }
108    
109            /**
110             * Hook method which is called during setup phase, before the first request.
111             * It allows subclasses to modify the webClient, such as for disabling javascript
112             *
113             * @param webClient
114             */
115            protected void setUpWebClient(WebClient webClient)
116            {
117                    /**
118                     * momentarily disabling javascript for all the functional test cases
119                     * until we add the dojo toolkit
120                     */
121                    webClient.setJavaScriptEnabled(false);
122            }
123    }