View Javadoc

1   /**
2    * Copyright 2004-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.hr.time.test;
17  
18  import java.io.File;
19  import java.io.FileOutputStream;
20  import java.io.StringReader;
21  import java.net.URL;
22  import java.util.ArrayList;
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import org.apache.log4j.Logger;
27  import org.kuali.hr.time.web.TkLoginFilter;
28  import org.kuali.rice.core.api.config.property.ConfigContext;
29  
30  import com.gargoylesoftware.htmlunit.BrowserVersion;
31  import com.gargoylesoftware.htmlunit.WebClient;
32  import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
33  import com.gargoylesoftware.htmlunit.html.HtmlElement;
34  import com.gargoylesoftware.htmlunit.html.HtmlForm;
35  import com.gargoylesoftware.htmlunit.html.HtmlInput;
36  import com.gargoylesoftware.htmlunit.html.HtmlPage;
37  import org.kuali.rice.krad.util.GlobalVariables;
38  
39  public class HtmlUnitUtil {
40  
41      private static final Logger LOG = Logger.getLogger(HtmlUnitUtil.class);
42  
43      /**
44       * 
45       * @param url
46       * @return htmlpage without js enabled
47       * @throws Exception
48       */
49      public static HtmlPage gotoPageAndLogin(String url) throws Exception {
50      	return gotoPageAndLogin(url, false);
51      }
52      
53      /**
54       * 
55       * @param url
56       * @param enableJavascript
57       * @return htmlpage with js enabled
58       * @throws Exception
59       */
60      public static HtmlPage gotoPageAndLogin(String url, boolean enableJavascript) throws Exception {
61      	LOG.debug("URL: " + url);
62      	final WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_7);
63      	// this is required and needs to set to true, otherwise the values set by the onClick event won't be triggered, e.g. methodToCall
64      	webClient.setJavaScriptEnabled(enableJavascript);
65      	webClient.setTimeout(0);
66      	return (HtmlPage) webClient.getPage(new URL(url));
67      }
68  
69      public static boolean pageContainsText(HtmlPage page, String text) {
70  	return page.asText().indexOf(text) >= 0;
71      }
72  
73  	public static HtmlPage clickInputContainingText(HtmlPage page, String...values) throws Exception {
74  		page = (HtmlPage)getInputContainingText(page, values).click();
75  		return page;
76  	}
77  
78      public static HtmlInput getInputContainingText(HtmlPage page, String... values) throws Exception {
79  		List<HtmlForm> forms = page.getForms();
80  		for (HtmlForm form : forms){
81  			for(HtmlElement element : form.getHtmlElementDescendants()) {
82  				if (element instanceof HtmlInput) {
83  					if (elementContainsValues(element, values)) {
84  						return (HtmlInput)element;
85  					}
86  				}
87  			}
88  		}
89  		return null;
90  	}
91  
92  
93      public static List<HtmlInput> getInputsContainingText(HtmlPage page, String... values) throws Exception {
94  		List<HtmlInput> inputs = new ArrayList<HtmlInput>();
95  		List<HtmlForm> forms = page.getForms();
96  		for (HtmlForm form : forms){
97  
98  			for(HtmlElement element : form.getHtmlElementDescendants()) {
99  				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 }