View Javadoc
1   package org.kuali.student.lum.ui.selenium;
2   
3   import static org.junit.Assert.fail;
4   
5   import java.net.URL;
6   import java.util.List;
7   import java.util.concurrent.TimeUnit;
8   
9   import org.junit.After;
10  import org.junit.Before;
11  import org.junit.Test;
12  import org.openqa.selenium.By;
13  import org.openqa.selenium.Keys;
14  import org.openqa.selenium.NoSuchElementException;
15  import org.openqa.selenium.Platform;
16  import org.openqa.selenium.WebDriver;
17  import org.openqa.selenium.WebElement;
18  import org.openqa.selenium.firefox.FirefoxDriver;
19  import org.openqa.selenium.ie.InternetExplorerDriver;
20  import org.openqa.selenium.remote.DesiredCapabilities;
21  import org.openqa.selenium.remote.RemoteWebDriver;
22  
23  public class CreateCourseSeleniumTest {
24      private WebDriver driver;
25      //private StringBuffer verificationErrors = new StringBuffer();
26      private String browser;
27  
28      @Before
29      public void setUp() throws Exception {
30          String remoteTest = System.getProperty("selenium.remote");
31          String baseURL = System.getProperty("selenium.baseurl");
32          browser = System.getProperty("selenium.browser", "firefox");
33          if ("true".equals(remoteTest)) {
34              String version = System.getProperty("selenium.browser.version", "");
35              Platform platform = Platform.valueOf(System.getProperty("selenium.platform", "ANY"));
36              DesiredCapabilities capabilities = new DesiredCapabilities(browser, version, platform);
37              capabilities.setCapability("max-duration", "120"); // tests can't run more than 2 mins
38              //capabilities.setCapability("name", getClass().getName() + "." + name.getMethodName());
39              driver = new RemoteWebDriver(new URL(baseURL),
40                                           capabilities);
41              // longer timeouts for remote invocation
42              driver.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS);
43          } else {
44              if ("firefox".equals(browser)) {
45                  driver = new FirefoxDriver();
46              } else if ("ie".equals(browser)) {
47                  driver = new InternetExplorerDriver();
48              }
49              driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
50          }
51      }
52  
53      @Test
54      public void testCreateCourse() throws Exception {
55          driver.get("http://dev.ks.kuali.org/login.jsp");
56          //driver.get("http://localhost:8081/ks-with-rice-bundled-dev/login.jsp");
57          //Login Page
58          WebElement logonUserName = driver.findElement(By.id("j_username"));
59          typeValue(logonUserName, "admin");
60          WebElement logonPassword = driver.findElement(By.id("j_password"));
61          typeValue(logonPassword, "admin");
62          logonPassword.submit();
63          //driver.findElement(By.xpath("//input[@type='submit']")).click();
64          //Main Page
65          By selectAnAreaPanel = By.id("gwt-debug-Application-Header-Select-an-area--panel-0");
66          for (int second = 0;; second++) {
67              if (second >= 60)
68                  fail("timeout");
69              try {
70                  if (isElementPresent(selectAnAreaPanel))
71                      break;
72              } catch (Exception e) {}
73              Thread.sleep(1000);
74          }
75  
76          driver.findElement(selectAnAreaPanel).click();
77          driver.findElement(By.id("gwt-debug-Curriculum-Management-label")).click();
78          By createACourseLink = By.linkText("Create a Course");
79          for (int second = 0;; second++) {
80              if (second >= 60)
81                  fail("timeout");
82              try {
83                  if (isElementPresent(createACourseLink))
84                      break;
85              } catch (Exception e) {}
86              Thread.sleep(1000);
87          }
88  
89          driver.findElement(createACourseLink).click();
90          //Create a Course Popup
91          By useCurriculumReviewCheckbox = By.id("gwt-debug-Use-curriculum-review-process-input");
92          for (int second = 0;; second++) {
93              if (second >= 60)
94                  fail("timeout");
95              try {
96                  if (isElementPresent(useCurriculumReviewCheckbox))
97                      break;
98              } catch (Exception e) {}
99              Thread.sleep(1000);
100         }
101 
102         driver.findElement(useCurriculumReviewCheckbox).click();
103         driver.findElement(By.id("gwt-debug-Start-Proposal-anchor")).click();
104         //Start Proposal
105         //Course Info Section
106         By byProposalName = By.xpath("//input[@id='gwt-debug-proposal-name']");
107         for (int second = 0;; second++) {
108             if (second >= 60)
109                 fail("timeout");
110             try {
111                 if (isElementPresent(byProposalName))
112                     break;
113             } catch (Exception e) {}
114             Thread.sleep(1000);
115         }
116 
117         WebElement proposalName = driver.findElement(byProposalName);
118         typeValue(proposalName, "Selenium Tester");
119         WebElement courseTitle = driver.findElement(By.xpath("//input[@id='gwt-debug-courseTitle']"));
120         typeValue(courseTitle, "Selenium Tester");
121         WebElement transcriptTitle = driver.findElement(By.xpath("//input[@id='gwt-debug-transcriptTitle']"));
122         typeValue(transcriptTitle, "Selenium Transcript");
123         WebElement subjectArea = driver.findElement(By.xpath("//input[@id='gwt-debug-subjectArea']"));
124         subjectArea.click();
125         //subjectArea.sendKeys("CHEM", Keys.RIGHT);
126         typeValue(subjectArea, "CHEM", Keys.RIGHT);
127         // ERROR: Caught exception [ERROR: Unsupported command [keyDown]]
128         // ERROR: Caught exception [ERROR: Unsupported command [keyUp]]
129         for (int second = 0;; second++) {
130             if (second >= 60)
131                 fail("timeout");
132             try {
133                 if (isElementPresent(By.xpath("//td[@role='menuitem']")))
134                     break;
135             } catch (Exception e) {}
136             Thread.sleep(1000);
137         }
138 
139         driver.findElement(By.xpath("//td[@role='menuitem']")).click();
140         WebElement courseNumberSuffix = driver.findElement(By.xpath("//input[@id='gwt-debug-courseNumberSuffix']"));
141         typeValue(courseNumberSuffix, "111");
142         WebElement instructors = driver.findElement(By.xpath("//input[@id='gwt-debug-instructors']"));
143         typeValue(instructors, "Admin 1", Keys.RIGHT);
144         // ERROR: Caught exception [ERROR: Unsupported command [keyDown]]
145         // ERROR: Caught exception [ERROR: Unsupported command [keyUp]]
146         for (int second = 0;; second++) {
147             if (second >= 60)
148                 fail("timeout");
149             try {
150                 if (isElementPresent(By.xpath("//td[@role='menuitem']")))
151                     break;
152             } catch (Exception e) {}
153             Thread.sleep(1000);
154         }
155 
156         driver.findElement(By.xpath("//td[@role='menuitem']")).click();
157         //Always add 1 second delay before 'Add to list' buttons
158         Thread.sleep(1000);
159         driver.findElement(By.id("gwt-debug-instructors-Add-to-list-anchor")).click();
160         WebElement description = driver.findElement(By.xpath("//textarea[@id='gwt-debug-descr-plain']"));
161         typeValue(description, "Selenium Description");
162         WebElement rationale = driver.findElement(By.xpath("//textarea[@id='gwt-debug-proposal-rationale']"));
163         typeValue(rationale, "Selenium Rationale");
164         driver.findElement(By.id("gwt-debug-Save-and-Continue-anchor")).click();
165 
166         //Governance Section
167         for (int second = 0;; second++) {
168             if (second >= 60)
169                 fail("timeout");
170             try {
171                 if (isElementPresent(By.id("gwt-debug-All-input")))
172                     break;
173             } catch (Exception e) {}
174             Thread.sleep(1000);
175         }
176 
177         driver.findElement(By.id("gwt-debug-All-input")).click();
178         WebElement unitContentOwner = driver.findElement(By.xpath("//select[@id='gwt-debug-unitsContentOwner']"));
179         // ERROR: Caught exception [ERROR: Unsupported command [select]]
180         clickOptionInSelectList(unitContentOwner, "68");
181         //Always sleep before 'Add to list' buttons
182         Thread.sleep(1000);
183         driver.findElement(By.id("gwt-debug-unitsContentOwner-Add-to-list-anchor")).click();
184         driver.findElement(By.id("gwt-debug-Save-and-Continue-anchor")).click();
185 
186         //Course Logistics Section
187         for (int second = 0;; second++) {
188             if (second >= 60)
189                 fail("timeout");
190             try {
191                 if (isElementPresent(By.id("gwt-debug-Any-input")))
192                     break;
193             } catch (Exception e) {}
194             Thread.sleep(1000);
195         }
196 
197         driver.findElement(By.id("gwt-debug-Any-input")).click();
198         driver.findElement(By.id("gwt-debug-Completed-notation-input")).click();
199         driver.findElement(By.id("gwt-debug-Standard-final-Exam-input")).click();
200         WebElement outcome1Type = driver.findElement(By.xpath("//select[@id='gwt-debug-creditOptions-0-type']"));
201         clickOptionInSelectList(outcome1Type, "kuali.result.values.group.type.fixed");
202         // ERROR: Caught exception [ERROR: Unsupported command [select]]
203         WebElement outcome1CreditValue = driver.findElement(By
204                 .xpath("//input[@id='gwt-debug-creditOptions-0-fixedCreditValue']"));
205         typeValue(outcome1CreditValue, "5");
206         driver.findElement(By.id("gwt-debug-Save-and-Continue-anchor")).click();
207 
208         //Learning Objectives Section
209         for (int second = 0;; second++) {
210             if (second >= 60)
211                 fail("timeout");
212             try {
213                 if (isElementPresent(By.xpath("//textarea[@id='gwt-debug-courseSpecificLOs-0-loInfo-desc-plain']")))
214                     break;
215             } catch (Exception e) {}
216             Thread.sleep(1000);
217         }
218         WebElement learningObjective1 = driver.findElement(By
219                 .xpath("//textarea[@id='gwt-debug-courseSpecificLOs-0-loInfo-desc-plain']"));
220         typeValue(learningObjective1, "Selenium Learning Objectives");
221 
222         driver.findElement(By.id("gwt-debug-courseSpecificLOs-0-loInfo-desc-plain-Browse-for-categories")).click();
223         for (int second = 0;; second++) {
224             if (second >= 60)
225                 fail("timeout");
226             try {
227                 if (isElementPresent(By.id("gwt-debug-Communication-Skill-input")))
228                     break;
229             } catch (Exception e) {}
230             Thread.sleep(1000);
231         }
232 
233         driver.findElement(By.id("gwt-debug-Communication-Skill-input")).click();
234         driver.findElement(By.id("gwt-debug-Add-anchor")).click();
235         driver.findElement(By.id("gwt-debug-Save-and-Continue-anchor")).click();
236 
237         //Course Requisites Section
238         for (int second = 0;; second++) {
239             if (second >= 60)
240                 fail("timeout");
241             try {
242                 if (isElementPresent(By.id("gwt-debug-Add-Student-Eligibility---Prerequisite-anchor")))
243                     break;
244             } catch (Exception e) {}
245             Thread.sleep(1000);
246         }
247 
248         driver.findElement(By.id("gwt-debug-Add-Student-Eligibility---Prerequisite-anchor")).click();
249         for (int second = 0;; second++) {
250             if (second >= 60)
251                 fail("timeout");
252             try {
253                 if (isElementPresent(By.id("gwt-debug-Rule-Type")))
254                     break;
255             } catch (Exception e) {}
256             Thread.sleep(1000);
257         }
258 
259         WebElement ruleType = driver.findElement(By.id("gwt-debug-Rule-Type"));
260         clickOptionInSelectList(ruleType, "19");
261         // ERROR: Caught exception [ERROR: Unsupported command [select]]
262         for (int second = 0;; second++) {
263             if (second >= 60)
264                 fail("timeout");
265             try {
266                 if (isElementPresent(By
267                         .xpath("//input[@id='gwt-debug-kuali-reqComponent-field-type-value-positive-integer']")))
268                     break;
269             } catch (Exception e) {}
270             Thread.sleep(1000);
271         }
272         WebElement minCreditValue = driver.findElement(By
273                 .xpath("//input[@id='gwt-debug-kuali-reqComponent-field-type-value-positive-integer']"));
274         typeValue(minCreditValue, "10");
275         driver.findElement(By.id("gwt-debug-Add-anchor")).click();
276         //Give the 'Save' button a chance to become active
277         Thread.sleep(2000);//IE needs more time here than Firefox
278         driver.findElement(By.id("gwt-debug-Save-anchor")).click();
279         Thread.sleep(1000);
280         driver.findElement(By.id("gwt-debug-Save-and-Continue-anchor")).click();
281 
282         //Active Dates Section
283         for (int second = 0;; second++) {
284             if (second >= 60)
285                 fail("timeout");
286             try {
287                 if (isElementPresent(By.xpath("//select[@id='gwt-debug-startTerm']")))
288                     break;
289             } catch (Exception e) {}
290             Thread.sleep(1000);
291         }
292         WebElement startTerm = driver.findElement(By.xpath("//select[@id='gwt-debug-startTerm']"));
293         clickOptionInSelectList(startTerm, "kuali.atp.SP2010-2011");
294         //startTerm.click();
295         // ERROR: Caught exception [ERROR: Unsupported command [select]]
296         //driver.findElement(By.cssSelector("option[value=\"kuali.atp.SP2010-2011\"]")).click();
297         driver.findElement(By.id("gwt-debug-Save-and-Continue-anchor")).click();
298 
299         //Financials Section
300         for (int second = 0;; second++) {
301             if (second >= 60)
302                 fail("timeout");
303             try {
304                 if (isElementPresent(By.xpath("//textarea[@id='gwt-debug-feeJustification-plain']")))
305                     break;
306             } catch (Exception e) {}
307             Thread.sleep(1000);
308         }
309         WebElement justificationOfFees = driver.findElement(By
310                 .xpath("//textarea[@id='gwt-debug-feeJustification-plain']"));
311         typeValue(justificationOfFees, "Selenium Justification of Fees");
312         //These revenue and expenditure fields only work with Firefox.
313         //Maybe there's an issue in IE because these are multiplicity fields and thus some of these elements are dynamic?
314         if ("firefox".equals(browser)) {
315             driver.findElement(By.id("gwt-debug-revenues-Add-another-Organization-anchor")).click();
316             driver.findElement(By.id("gwt-debug-revenues-0-affiliatedOrgs-0-orgId-Advanced-Search-anchor")).click();
317             driver.findElement(By.id("gwt-debug-Search-anchor")).click();
318             for (int second = 0;; second++) {
319                 if (second >= 60)
320                     fail("timeout");
321                 try {
322                     if (isElementPresent(By.id("gwt-debug-Academics-Academic-Affairs--College--input")))
323                         break;
324                 } catch (Exception e) {}
325                 Thread.sleep(1000);
326             }
327 
328             driver.findElement(By.id("gwt-debug-Academics-Academic-Affairs--College--input")).click();
329             driver.findElement(By.id("gwt-debug-Search-anchor")).click();
330             for (int second = 0;; second++) {
331                 if (second >= 60)
332                     fail("timeout");
333                 try {
334                     if (isElementPresent(By.xpath("//input[@id='gwt-debug-revenues-0-affiliatedOrgs-0-percentage']")))
335                         break;
336                 } catch (Exception e) {}
337                 Thread.sleep(1000);
338             }
339             WebElement revenue1Percentage = driver.findElement(By
340                     .xpath("//input[@id='gwt-debug-revenues-0-affiliatedOrgs-0-percentage']"));
341             revenue1Percentage.click();
342             typeValue(revenue1Percentage, "100");
343             driver.findElement(By.id("gwt-debug-expenditure-affiliatedOrgs-Add-another-Organization-anchor")).click();
344             driver.findElement(By.id("gwt-debug-expenditure-affiliatedOrgs-0-orgId-Advanced-Search-anchor")).click();
345             driver.findElement(By.id("gwt-debug-Search-anchor")).click();
346             driver.findElement(By.id("gwt-debug-Admissions-Undergraduate-Admissions-input")).click();
347             driver.findElement(By.id("gwt-debug-Search-anchor")).click();
348             WebElement expenditure1Percentage = driver.findElement(By
349                     .xpath("//input[@id='gwt-debug-expenditure-affiliatedOrgs-0-percentage']"));
350             typeValue(expenditure1Percentage, "100");
351         }
352         driver.findElement(By.id("gwt-debug-Save-and-Continue-anchor")).click();
353 
354         //Authors & Collaborators Section
355         for (int second = 0;; second++) {
356             if (second >= 60)
357                 fail("timeout");
358             try {
359                 if (isElementPresent(By.id("gwt-debug-Name-Advanced-Search-anchor")))
360                     break;
361             } catch (Exception e) {}
362             Thread.sleep(1000);
363         }
364 
365         driver.findElement(By.id("gwt-debug-Name-Advanced-Search-anchor")).click();
366         driver.findElement(By.id("gwt-debug-Search-anchor")).click();
367         for (int second = 0;; second++) {
368             if (second >= 60)
369                 fail("timeout");
370             try {
371                 if (isElementPresent(By.id("gwt-debug-Admin-1--Test-1101-testadmin1-Admin-1--Test--testadmin1--input")))
372                     break;
373             } catch (Exception e) {}
374             Thread.sleep(1000);
375         }
376 
377         driver.findElement(By.id("gwt-debug-Admin-1--Test-1101-testadmin1-Admin-1--Test--testadmin1--input")).click();
378         driver.findElement(By.id("gwt-debug-Search-anchor")).click();
379         WebElement permission = driver.findElement(By.xpath("//select[@id='gwt-debug-Permission']"));
380         //permission.click();
381         clickOptionInSelectList(permission, "KS-SYS~Edit Document");
382         // ERROR: Caught exception [ERROR: Unsupported command [select]]
383         //driver.findElement(By.cssSelector("option[value=\"KS-SYS~Edit Document\"]")).click();
384         WebElement actionRequest = driver.findElement(By.xpath("//select[@id='gwt-debug-Action-Request']"));
385         //actionRequest.click();
386         clickOptionInSelectList(actionRequest, "F");
387         // ERROR: Caught exception [ERROR: Unsupported command [select]]
388         //driver.findElement(By.cssSelector("option[value=\"F\"]")).click();
389         driver.findElement(By.id("gwt-debug-Add-Collaborator-anchor")).click();
390         driver.findElement(By.id("gwt-debug-Save-and-Continue-anchor")).click();
391 
392         //Supporting Documents Section
393         for (int second = 0;; second++) {
394             if (second >= 60)
395                 fail("timeout");
396             try {
397                 if (isElementPresent(By.id("gwt-debug-Continue-anchor")))
398                     break;
399             } catch (Exception e) {}
400             Thread.sleep(1000);
401         }
402 
403         driver.findElement(By.id("gwt-debug-Continue-anchor")).click();
404 
405         //Finally the Review Proposal Section is displayed
406     }
407 
408     @After
409     public void tearDown() throws Exception {
410         driver.quit();
411 //        String verificationErrorString = verificationErrors.toString();
412 //        if (!"".equals(verificationErrorString)) {
413 //            fail(verificationErrorString);
414 //        }
415     }
416 
417     private boolean isElementPresent(By by) {
418         try {
419             driver.findElement(by);
420             return true;
421         } catch (NoSuchElementException e) {
422             return false;
423         }
424     }
425 
426     private void typeValue(final WebElement webElement, CharSequence... keysToSend) {
427         webElement.clear();
428         webElement.sendKeys(keysToSend);
429     }
430 
431     private void clickOptionInSelectList(final WebElement selectElement, String optionValue) {
432         List<WebElement> selectOptions = selectElement.findElements(By.tagName("option"));
433         for (WebElement option : selectOptions) {
434             if (optionValue != null) {
435                 //select element by option value attribute
436                 if (optionValue.equals(option.getAttribute("value"))) {
437                     option.click();
438                     break;
439                 }
440             }
441         }
442     }
443 }