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 com.gargoylesoftware.htmlunit.WebClient;
19  import com.gargoylesoftware.htmlunit.html.*;
20  import org.apache.log4j.Logger;
21  import org.kuali.rice.core.api.config.property.ConfigContext;
22  
23  import java.io.File;
24  import java.io.FileOutputStream;
25  import java.io.StringReader;
26  import java.net.URL;
27  import java.util.ArrayList;
28  import java.util.Iterator;
29  import java.util.List;
30  
31  public class HtmlUnitUtil {
32      private static final Logger LOG = Logger.getLogger(HtmlUnitUtil.class);
33  
34      /**
35       * 
36       * @param url
37       * @return htmlpage without js enabled
38       * @throws Exception
39       */
40      public static HtmlPage gotoPageAndLogin(WebClient webClient, String url) throws Exception {
41      	return gotoPageAndLogin(webClient, url, false);
42      }
43      
44      /**
45       * 
46       * @param url
47       * @param enableJavascript
48       * @return htmlpage with js enabled
49       * @throws Exception
50       */
51      public static HtmlPage gotoPageAndLogin(WebClient webClient, String url, boolean enableJavascript) throws Exception {
52      	LOG.debug("URL: " + url);
53  
54      	// this is required and needs to set to true, otherwise the values set by the onClick event won't be triggered, e.g. methodToCall
55          webClient.setJavaScriptEnabled(enableJavascript);
56      	return (HtmlPage) webClient.getPage(new URL(url));
57      }
58  
59      public static boolean pageContainsText(HtmlPage page, String text) {
60  	return page.asText().indexOf(text) >= 0;
61      }
62  
63  	public static HtmlPage clickInputContainingText(HtmlPage page, String...values) throws Exception {
64  		page = (HtmlPage)getInputContainingText(page, values).click();
65  		return page;
66  	}
67  
68      public static HtmlInput getInputContainingText(HtmlPage page, String... values) throws Exception {
69  		List<HtmlForm> forms = page.getForms();
70  		for (HtmlForm form : forms){
71  			for(HtmlElement element : form.getHtmlElementDescendants()) {
72  				if (element instanceof HtmlInput) {
73  					if (elementContainsValues(element, values)) {
74  						return (HtmlInput)element;
75  					}
76  				}
77  			}
78  		}
79  		return null;
80  	}
81  
82  
83      public static List<HtmlInput> getInputsContainingText(HtmlPage page, String... values) throws Exception {
84  		List<HtmlInput> inputs = new ArrayList<HtmlInput>();
85  		List<HtmlForm> forms = page.getForms();
86  		for (HtmlForm form : forms){
87  
88  			for(HtmlElement element : form.getHtmlElementDescendants()) {
89  				if (element instanceof HtmlInput) {
90  					if (elementContainsValues(element, values)) {
91  						inputs.add((HtmlInput)element);
92  					}
93  				}
94  			}
95  		}
96  		return inputs;
97  	}
98  
99  	protected static boolean elementContainsValues(HtmlElement element, String... values) {
100 		for (String value : values) {
101 			if (element.toString().indexOf(value) == -1) {
102 				return false;
103 			}
104         }
105 		return true;
106 	}
107 
108 	public static HtmlPage clickAnchorContainingText(HtmlPage page, String... values) throws Exception {
109 		return (HtmlPage) getAnchorContainingText(page, values).click();
110 	}
111 
112 	@SuppressWarnings("unchecked")
113 	public static HtmlAnchor getAnchorContainingText(HtmlPage page, String... values) throws Exception {
114 		for (Iterator iterator = page.getAnchors().iterator(); iterator.hasNext();) {
115 			HtmlAnchor anchor = (HtmlAnchor) iterator.next();
116 			if (elementContainsValues(anchor, values)) {
117 				return anchor;
118 			}
119 		}
120 		return null;
121 	}
122 
123     public static String getBaseURL() {
124 	    return ConfigContext.getCurrentContextConfig().getProperty("application.url");
125     }
126     
127     public static String getContext() {
128     	return "/" + ConfigContext.getCurrentContextConfig().getProperty("app.context.name");
129     }
130 
131     public static String getTempDir() {
132 	return ConfigContext.getCurrentContextConfig().getProperty("temp.dir");
133     }
134 
135     public static Integer getPort() {
136 	return new Integer(ConfigContext.getCurrentContextConfig().getProperty("kns.test.port"));
137     }
138 
139     public static void createTempFile(HtmlPage page) throws Exception {
140 	createTempFile(page, null);
141     }
142 
143     public static void createTempFile(HtmlPage page, String name) throws Exception {
144 	name = name == null ? "TestOutput" : name;
145 	File temp = File.createTempFile(name, ".html", new File(HtmlUnitUtil.getTempDir()));
146 	FileOutputStream fos = new FileOutputStream(temp);
147 	String xml = page.asXml();
148 	StringReader xmlReader = new StringReader(xml);
149 	int i;
150 	while ((i = xmlReader.read()) != -1) {
151 	    fos.write(i);
152 	}
153 	try {
154 	    fos.close();
155 	} catch (Exception e) {
156 	}
157 	try {
158 	    xmlReader.close();
159 	} catch (Exception e) {
160 	}
161     }
162 
163     public static HtmlInput getInputContainingText(HtmlForm form, String text) throws Exception {
164 
165 		for (HtmlElement element : form.getHtmlElementDescendants()) {
166 			if (element instanceof HtmlInput) {
167 				HtmlInput i = (HtmlInput) element;
168 				if (element.toString().contains(text)) {
169 					return i;
170 				}
171 			}
172 
173 		}
174 		return null;
175     }
176     
177 	public static HtmlForm getDefaultForm(HtmlPage htmlPage) {
178 		if (htmlPage.getForms().size() == 1) {
179 			return (HtmlForm)htmlPage.getForms().get(0);
180 		}
181 		return (HtmlForm)htmlPage.getForms().get(1);
182 	}
183 
184 }