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