001    package org.kuali.student.lum.ui.selenium;
002    
003    import static org.junit.Assert.fail;
004    
005    import java.net.URL;
006    import java.util.List;
007    import java.util.concurrent.TimeUnit;
008    
009    import org.junit.After;
010    import org.junit.Before;
011    import org.junit.Test;
012    import org.openqa.selenium.By;
013    import org.openqa.selenium.Keys;
014    import org.openqa.selenium.NoSuchElementException;
015    import org.openqa.selenium.Platform;
016    import org.openqa.selenium.WebDriver;
017    import org.openqa.selenium.WebElement;
018    import org.openqa.selenium.firefox.FirefoxDriver;
019    import org.openqa.selenium.ie.InternetExplorerDriver;
020    import org.openqa.selenium.remote.DesiredCapabilities;
021    import org.openqa.selenium.remote.RemoteWebDriver;
022    
023    public class CreateCourseSeleniumTest {
024        private WebDriver driver;
025        //private StringBuffer verificationErrors = new StringBuffer();
026        private String browser;
027    
028        @Before
029        public void setUp() throws Exception {
030            String remoteTest = System.getProperty("selenium.remote");
031            String baseURL = System.getProperty("selenium.baseurl");
032            browser = System.getProperty("selenium.browser", "firefox");
033            if ("true".equals(remoteTest)) {
034                String version = System.getProperty("selenium.browser.version", "");
035                Platform platform = Platform.valueOf(System.getProperty("selenium.platform", "ANY"));
036                DesiredCapabilities capabilities = new DesiredCapabilities(browser, version, platform);
037                capabilities.setCapability("max-duration", "120"); // tests can't run more than 2 mins
038                //capabilities.setCapability("name", getClass().getName() + "." + name.getMethodName());
039                driver = new RemoteWebDriver(new URL(baseURL),
040                                             capabilities);
041                // longer timeouts for remote invocation
042                driver.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS);
043            } else {
044                if ("firefox".equals(browser)) {
045                    driver = new FirefoxDriver();
046                } else if ("ie".equals(browser)) {
047                    driver = new InternetExplorerDriver();
048                }
049                driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
050            }
051        }
052    
053        @Test
054        public void testCreateCourse() throws Exception {
055            driver.get("http://dev.ks.kuali.org/login.jsp");
056            //driver.get("http://localhost:8081/ks-embedded-dev/login.jsp");
057            //Login Page
058            WebElement logonUserName = driver.findElement(By.id("j_username"));
059            typeValue(logonUserName, "admin");
060            WebElement logonPassword = driver.findElement(By.id("j_password"));
061            typeValue(logonPassword, "admin");
062            logonPassword.submit();
063            //driver.findElement(By.xpath("//input[@type='submit']")).click();
064            //Main Page
065            By selectAnAreaPanel = By.id("gwt-debug-Application-Header-Select-an-area--panel-0");
066            for (int second = 0;; second++) {
067                if (second >= 60)
068                    fail("timeout");
069                try {
070                    if (isElementPresent(selectAnAreaPanel))
071                        break;
072                } catch (Exception e) {}
073                Thread.sleep(1000);
074            }
075    
076            driver.findElement(selectAnAreaPanel).click();
077            driver.findElement(By.id("gwt-debug-Curriculum-Management-label")).click();
078            By createACourseLink = By.linkText("Create a Course");
079            for (int second = 0;; second++) {
080                if (second >= 60)
081                    fail("timeout");
082                try {
083                    if (isElementPresent(createACourseLink))
084                        break;
085                } catch (Exception e) {}
086                Thread.sleep(1000);
087            }
088    
089            driver.findElement(createACourseLink).click();
090            //Create a Course Popup
091            By useCurriculumReviewCheckbox = By.id("gwt-debug-Use-curriculum-review-process-input");
092            for (int second = 0;; second++) {
093                if (second >= 60)
094                    fail("timeout");
095                try {
096                    if (isElementPresent(useCurriculumReviewCheckbox))
097                        break;
098                } catch (Exception e) {}
099                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.resultComponentType.credit.degree.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    }