001 /**
002 * Copyright 2004-2012 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.hr.time.test;
017
018 import java.io.File;
019 import java.io.FileOutputStream;
020 import java.io.StringReader;
021 import java.net.URL;
022 import java.util.ArrayList;
023 import java.util.Iterator;
024 import java.util.List;
025
026 import org.apache.log4j.Logger;
027 import org.kuali.hr.time.web.TkLoginFilter;
028 import org.kuali.rice.core.api.config.property.ConfigContext;
029
030 import com.gargoylesoftware.htmlunit.BrowserVersion;
031 import com.gargoylesoftware.htmlunit.WebClient;
032 import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
033 import com.gargoylesoftware.htmlunit.html.HtmlElement;
034 import com.gargoylesoftware.htmlunit.html.HtmlForm;
035 import com.gargoylesoftware.htmlunit.html.HtmlInput;
036 import com.gargoylesoftware.htmlunit.html.HtmlPage;
037 import org.kuali.rice.krad.util.GlobalVariables;
038
039 public class HtmlUnitUtil {
040
041 private static final Logger LOG = Logger.getLogger(HtmlUnitUtil.class);
042
043 /**
044 *
045 * @param url
046 * @return htmlpage without js enabled
047 * @throws Exception
048 */
049 public static HtmlPage gotoPageAndLogin(String url) throws Exception {
050 return gotoPageAndLogin(url, false);
051 }
052
053 /**
054 *
055 * @param url
056 * @param enableJavascript
057 * @return htmlpage with js enabled
058 * @throws Exception
059 */
060 public static HtmlPage gotoPageAndLogin(String url, boolean enableJavascript) throws Exception {
061 LOG.debug("URL: " + url);
062 final WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_7);
063 // this is required and needs to set to true, otherwise the values set by the onClick event won't be triggered, e.g. methodToCall
064 webClient.setJavaScriptEnabled(enableJavascript);
065 webClient.setTimeout(0);
066 return (HtmlPage) webClient.getPage(new URL(url));
067 }
068
069 public static boolean pageContainsText(HtmlPage page, String text) {
070 return page.asText().indexOf(text) >= 0;
071 }
072
073 public static HtmlPage clickInputContainingText(HtmlPage page, String...values) throws Exception {
074 page = (HtmlPage)getInputContainingText(page, values).click();
075 return page;
076 }
077
078 public static HtmlInput getInputContainingText(HtmlPage page, String... values) throws Exception {
079 List<HtmlForm> forms = page.getForms();
080 for (HtmlForm form : forms){
081 for(HtmlElement element : form.getHtmlElementDescendants()) {
082 if (element instanceof HtmlInput) {
083 if (elementContainsValues(element, values)) {
084 return (HtmlInput)element;
085 }
086 }
087 }
088 }
089 return null;
090 }
091
092
093 public static List<HtmlInput> getInputsContainingText(HtmlPage page, String... values) throws Exception {
094 List<HtmlInput> inputs = new ArrayList<HtmlInput>();
095 List<HtmlForm> forms = page.getForms();
096 for (HtmlForm form : forms){
097
098 for(HtmlElement element : form.getHtmlElementDescendants()) {
099 if (element instanceof HtmlInput) {
100 if (elementContainsValues(element, values)) {
101 inputs.add((HtmlInput)element);
102 }
103 }
104 }
105 }
106 return inputs;
107 }
108
109 protected static boolean elementContainsValues(HtmlElement element, String... values) {
110 for (String value : values) {
111 if (element.toString().indexOf(value) == -1) {
112 return false;
113 }
114 }
115 return true;
116 }
117
118 public static HtmlPage clickAnchorContainingText(HtmlPage page, String... values) throws Exception {
119 return (HtmlPage) getAnchorContainingText(page, values).click();
120 }
121
122 @SuppressWarnings("unchecked")
123 public static HtmlAnchor getAnchorContainingText(HtmlPage page, String... values) throws Exception {
124 for (Iterator iterator = page.getAnchors().iterator(); iterator.hasNext();) {
125 HtmlAnchor anchor = (HtmlAnchor) iterator.next();
126 if (elementContainsValues(anchor, values)) {
127 return anchor;
128 }
129 }
130 return null;
131 }
132
133 public static String getBaseURL() {
134 return ConfigContext.getCurrentContextConfig().getProperty("application.url");
135 }
136
137 public static String getContext() {
138 return "/" + ConfigContext.getCurrentContextConfig().getProperty("app.context.name");
139 }
140
141 public static String getTempDir() {
142 return ConfigContext.getCurrentContextConfig().getProperty("temp.dir");
143 }
144
145 public static Integer getPort() {
146 return new Integer(ConfigContext.getCurrentContextConfig().getProperty("kns.test.port"));
147 }
148
149 public static void createTempFile(HtmlPage page) throws Exception {
150 createTempFile(page, null);
151 }
152
153 public static void createTempFile(HtmlPage page, String name) throws Exception {
154 name = name == null ? "TestOutput" : name;
155 File temp = File.createTempFile(name, ".html", new File(HtmlUnitUtil.getTempDir()));
156 FileOutputStream fos = new FileOutputStream(temp);
157 String xml = page.asXml();
158 StringReader xmlReader = new StringReader(xml);
159 int i;
160 while ((i = xmlReader.read()) != -1) {
161 fos.write(i);
162 }
163 try {
164 fos.close();
165 } catch (Exception e) {
166 }
167 try {
168 xmlReader.close();
169 } catch (Exception e) {
170 }
171 }
172
173 public static HtmlInput getInputContainingText(HtmlForm form, String text) throws Exception {
174
175 for (HtmlElement element : form.getHtmlElementDescendants()) {
176 if (element instanceof HtmlInput) {
177 HtmlInput i = (HtmlInput) element;
178 if (element.toString().contains(text)) {
179 return i;
180 }
181 }
182
183 }
184 return null;
185 }
186
187 public static HtmlForm getDefaultForm(HtmlPage htmlPage) {
188 if (htmlPage.getForms().size() == 1) {
189 return (HtmlForm)htmlPage.getForms().get(0);
190 }
191 return (HtmlForm)htmlPage.getForms().get(1);
192 }
193
194 }