001 /** 002 * Copyright 2004-2013 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 com.gargoylesoftware.htmlunit.WebClient; 019 import com.gargoylesoftware.htmlunit.html.*; 020 import org.apache.log4j.Logger; 021 import org.kuali.rice.core.api.config.property.ConfigContext; 022 023 import java.io.File; 024 import java.io.FileOutputStream; 025 import java.io.StringReader; 026 import java.net.URL; 027 import java.util.ArrayList; 028 import java.util.Iterator; 029 import java.util.List; 030 031 public class HtmlUnitUtil { 032 private static final Logger LOG = Logger.getLogger(HtmlUnitUtil.class); 033 034 /** 035 * 036 * @param url 037 * @return htmlpage without js enabled 038 * @throws Exception 039 */ 040 public static HtmlPage gotoPageAndLogin(WebClient webClient, String url) throws Exception { 041 return gotoPageAndLogin(webClient, url, false); 042 } 043 044 /** 045 * 046 * @param url 047 * @param enableJavascript 048 * @return htmlpage with js enabled 049 * @throws Exception 050 */ 051 public static HtmlPage gotoPageAndLogin(WebClient webClient, String url, boolean enableJavascript) throws Exception { 052 LOG.debug("URL: " + url); 053 054 // this is required and needs to set to true, otherwise the values set by the onClick event won't be triggered, e.g. methodToCall 055 webClient.setJavaScriptEnabled(enableJavascript); 056 return (HtmlPage) webClient.getPage(new URL(url)); 057 } 058 059 public static boolean pageContainsText(HtmlPage page, String text) { 060 return page.asText().indexOf(text) >= 0; 061 } 062 063 public static HtmlPage clickInputContainingText(HtmlPage page, String...values) throws Exception { 064 page = (HtmlPage)getInputContainingText(page, values).click(); 065 return page; 066 } 067 068 public static HtmlInput getInputContainingText(HtmlPage page, String... values) throws Exception { 069 List<HtmlForm> forms = page.getForms(); 070 for (HtmlForm form : forms){ 071 for(HtmlElement element : form.getHtmlElementDescendants()) { 072 if (element instanceof HtmlInput) { 073 if (elementContainsValues(element, values)) { 074 return (HtmlInput)element; 075 } 076 } 077 } 078 } 079 return null; 080 } 081 082 083 public static List<HtmlInput> getInputsContainingText(HtmlPage page, String... values) throws Exception { 084 List<HtmlInput> inputs = new ArrayList<HtmlInput>(); 085 List<HtmlForm> forms = page.getForms(); 086 for (HtmlForm form : forms){ 087 088 for(HtmlElement element : form.getHtmlElementDescendants()) { 089 if (element instanceof HtmlInput) { 090 if (elementContainsValues(element, values)) { 091 inputs.add((HtmlInput)element); 092 } 093 } 094 } 095 } 096 return inputs; 097 } 098 099 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 }