View Javadoc

1   /**
2    * Copyright 2004-2014 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.missedpunch;
17  
18  import static org.junit.Assert.assertNotNull;
19  import static org.junit.Assert.assertTrue;
20  
21  import java.util.List;
22  
23  import org.apache.log4j.Logger;
24  import org.junit.After;
25  import org.junit.Assert;
26  import org.junit.Before;
27  import org.junit.Ignore;
28  import org.junit.Test;
29  import org.kuali.hr.KPMEWebTestCase;
30  import org.kuali.hr.util.HtmlUnitUtil;
31  import org.kuali.kpme.core.FunctionalTest;
32  import org.kuali.kpme.core.util.HrTestConstants;
33  import org.kuali.kpme.tklm.utils.TkTestConstants;
34  
35  import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
36  import com.gargoylesoftware.htmlunit.WebClient;
37  import com.gargoylesoftware.htmlunit.html.DomElement;
38  import com.gargoylesoftware.htmlunit.html.HtmlElement;
39  import com.gargoylesoftware.htmlunit.html.HtmlOption;
40  import com.gargoylesoftware.htmlunit.html.HtmlPage;
41  import com.gargoylesoftware.htmlunit.html.HtmlSelect;
42  import com.gargoylesoftware.htmlunit.html.HtmlTableDataCell;
43  import com.google.common.collect.Lists;
44  
45  @FunctionalTest
46  public class MissedPunchDocumentTest extends KPMEWebTestCase {
47      private static final Logger LOG = Logger.getLogger(MissedPunchDocumentTest.class);
48  
49  	@Before
50  	public void setUp() throws Exception {
51  		super.setUp();
52  	}
53  
54  	@After
55  	public void tearDown() throws Exception {
56  		super.tearDown();
57  	}
58  
59  	public void updateWebClient() {
60          WebClient webClient = getWebClient();
61  		webClient.setJavaScriptEnabled(true);
62  		webClient.setThrowExceptionOnScriptError(true);
63  		webClient.setAjaxController(new NicelyResynchronizingAjaxController());
64  		webClient.waitForBackgroundJavaScript(10000);
65  	}
66  
67  	@Test
68  	public void testLookup() throws Exception {
69  		HtmlPage lookupPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), HrTestConstants.Urls.MISSED_PUNCH_LOOKUP_URL);
70  		assertNotNull("lookup page is null", lookupPage);
71  
72  		assertTrue("lookup page should contain Timesheet Id field",lookupPage.asText().contains("Timesheet Document Id"));
73  		assertTrue("lookup page should contain Work Area",lookupPage.asText().contains("Work Area"));
74  		assertTrue("lookup page should contain Task",lookupPage.asText().contains("Task"));
75  		assertTrue("lookup page should contain Clock Action",lookupPage.asText().contains("Clock Action"));
76  		
77  		lookupPage = HtmlUnitUtil.clickInputContainingText(lookupPage, "search");
78  		assertNotNull("lookup result page is null", lookupPage);
79  
80  		//assertTrue("lookup page should contain 'UA Sub Obj'", lookupPage.asText().contains("UA Sub Obj"));
81  	}
82  	
83  	@Ignore
84  	@Test
85  	public void testMissedPunch() throws Exception {
86  
87  		String baseUrl = TkTestConstants.Urls.CLOCK_URL;
88  		HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), baseUrl);
89  		Assert.assertNotNull(page);
90  		Assert.assertTrue("Clock Page contains Missed Punch Button", page.asText()
91  				.contains("Missed Punch"));
92  		updateWebClient();
93  
94  		LOG.debug("Page is : " + page.asText());
95  
96  		// getting Assignment options
97  		HtmlSelect assOption = (HtmlSelect) page
98  				.getElementByName("selectedAssignment");
99  		Iterable<DomElement> it = assOption.getChildElements();
100 		List<DomElement> assList = Lists.newLinkedList(it);
101 		
102 		LOG.debug("Second ele is : "+assList.get(2));
103 
104 		// set Assignment value
105 		HtmlOption ho = null;
106 		if(assList.size() > 2) {
107 			ho = (HtmlOption) assList.get(2);
108 		} else {
109 			ho= (HtmlOption) assList.get(0);
110 		}
111 		LOG.debug("ho is : " + ho.asText());
112 		assOption.setSelectedAttribute(ho, true);
113 
114 				
115 		// get tdocid 
116 		HtmlTableDataCell htmlTable = (HtmlTableDataCell) page.getByXPath(
117 				"//tbody//tr//td").get(9);
118 		LOG.debug("htmlTable.getTextContent();"
119 				+ htmlTable.getTextContent());
120 
121 		String docId = htmlTable.getTextContent();
122 		
123 		// redirect to missed punch page
124 		HtmlPage mPunchPage = HtmlUnitUtil
125 				.gotoPageAndLogin(getWebClient(), getBaseURL()
126 						+ "/missedPunch?methodToCall=start&viewId=MissedPunch-SubmitView&missedPunch.timesheetDocumentId="
127 						+ docId);
128 		Assert.assertNotNull(mPunchPage);
129 		
130 		// clock action and assignment are drop down lists that are not readonly
131 		HtmlElement element = (HtmlElement)mPunchPage.getElementById("document.clockAction");
132 		Assert.assertNotNull(element);
133 		element = (HtmlElement)mPunchPage.getElementById("document.assignment");
134 		Assert.assertNotNull(element);
135 
136         LOG.debug("Page1 is : " + mPunchPage.asText());
137 
138 		// set Invalid action time
139 		HtmlUnitUtil.setFieldValue(mPunchPage, "document.documentHeader.documentDescription",
140 				"Missed Punch- test");
141 		HtmlUnitUtil.setFieldValue(mPunchPage, "document.clockAction", "CO");
142 		HtmlUnitUtil.setFieldValue(mPunchPage, "document.actionDate", "01/16/2012");
143 		HtmlUnitUtil.setFieldValue(mPunchPage, "document.actionTime", "5:5");
144 		HtmlUnitUtil.setFieldValue(mPunchPage, "document.assignment", "2_1234_2");
145 
146 		HtmlElement elementSubmit = mPunchPage
147 				.getElementByName("methodToCall.route");
148 		mPunchPage = elementSubmit.click();
149 
150         LOG.debug("After first click >>>>> page is : "
151 				+ mPunchPage.asText());
152 
153 		Assert.assertTrue("page text:\n" + mPunchPage.asText() + "\n does not contain:\n",
154 				mPunchPage.asText().contains("5:5 is not a valid time."));
155 
156 		// set Future time 
157 		HtmlUnitUtil.setFieldValue(mPunchPage, "document.actionDate", "01/20/2020");
158 		HtmlUnitUtil.setFieldValue(mPunchPage, "document.actionTime", "5:50 AM");
159 
160 		elementSubmit = mPunchPage.getElementByName("methodToCall.route");
161 		mPunchPage = elementSubmit.click();
162 
163 		Assert.assertTrue(
164 				"page text:\n" + mPunchPage.asText() + "\n does not contain:\n",
165 				mPunchPage.asText().contains(
166 						"Missed Punch Action Date cannot be a future date"));
167 
168         LOG.debug("After second click >>>>> page is : "
169 				+ mPunchPage.asText());
170 
171 		// before last clock in time
172 		HtmlUnitUtil.setFieldValue(mPunchPage, "document.clockAction", "CI");
173 		HtmlUnitUtil.setFieldValue(mPunchPage, "document.actionDate", "12/20/2009");
174 		HtmlUnitUtil.setFieldValue(mPunchPage, "document.actionTime", "5:50 AM");
175 //		HtmlUnitUtil.setFieldValue(mPunchPage, "document.assignment", "2_1234_2");
176 
177 		elementSubmit = mPunchPage.getElementByName("methodToCall.route");
178 
179 		mPunchPage = elementSubmit.click();
180         LOG.debug("After third click >>>>> page is : "
181 				+ mPunchPage.asText());
182 
183 		Assert.assertTrue(
184 				"page text:\n" + mPunchPage.asText() + "\n does not contain:\n",
185 				mPunchPage.asText()
186 						.contains(
187 								"Assignment is not effective as of date chosen."));
188 
189 	
190 		// set proper value
191 		HtmlUnitUtil.setFieldValue(mPunchPage, "document.actionDate", "01/19/2012");
192 		HtmlUnitUtil.setFieldValue(mPunchPage, "document.actionTime", "5:50 AM");
193 		HtmlUnitUtil.setFieldValue(mPunchPage, "document.clockAction", "CI");
194 //		HtmlUnitUtil.setFieldValue(mPunchPage, "document.assignment", "2_1234_2");
195 
196 		HtmlElement elementSubmit1 = mPunchPage.getElementByName("methodToCall.route");
197 		mPunchPage = elementSubmit1.click();
198 
199         LOG.debug("After forth click >>>>> page is : "
200 				+ mPunchPage.asText());
201 
202 		Assert.assertTrue("page text:\n" + mPunchPage.asText() + "\n does not contain:\n",
203 				mPunchPage.asText().contains("Document was successfully submitted."));
204 		
205 		// open another missed punch doc for clock out
206 		mPunchPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), getBaseURL()
207 				+ "/missedPunch.do?methodToCall=start&viewId=MissedPunch-SubmitView&missedPunch.timesheetDocumentId="
208 				+ docId);
209 		Assert.assertNotNull(mPunchPage);
210 		element = (HtmlElement)mPunchPage.getElementById("document.clockAction");
211 		Assert.assertNotNull(element);
212 		// element not found for assignment since it is a readonly field now
213 		element = (HtmlElement)mPunchPage.getElementById("document.assignment");
214 		Assert.assertNull(element);
215 		Assert.assertTrue("page text:does not contain: \n", 
216 				mPunchPage.asText().contains("Assignment: 	 work area description : $0.00 Rcd 2 TEST-DEPT description 2"));
217 		
218 	}
219 
220 }