1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.testtools.selenium;
17
18 import org.apache.commons.io.FileUtils;
19 import org.junit.After;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.openqa.selenium.By;
23 import org.openqa.selenium.NoSuchWindowException;
24 import org.openqa.selenium.WebDriver;
25 import org.openqa.selenium.firefox.FirefoxDriver;
26 import org.openqa.selenium.firefox.FirefoxProfile;
27 import org.openqa.selenium.remote.DesiredCapabilities;
28
29 import java.io.File;
30 import java.net.MalformedURLException;
31 import java.util.concurrent.TimeUnit;
32
33
34
35
36 public class JenkinsJsonJobsResults {
37
38 WebDriver driver;
39 String jenkinsBase;
40 String[] jobs;
41 String outputDirectory;
42 boolean passed = false;
43
44 @Before
45 public void setUp() throws MalformedURLException, InterruptedException {
46 jenkinsBase = System.getProperty("jenkins.base.url", "http://ci.rice.kuali.org");
47 outputDirectory = System.getProperty("json.output.dir");
48 jobs = System.getProperty("jenkins.jobs", "rice-2.4-smoke-test").split("[,\\s]");
49
50 DesiredCapabilities capabilities = new DesiredCapabilities();
51 FirefoxProfile profile = new FirefoxProfile();
52 profile.setEnableNativeEvents(false);
53 capabilities.setCapability(FirefoxDriver.PROFILE, profile);
54
55 driver = new FirefoxDriver(capabilities);
56 driver.manage().timeouts().implicitlyWait(WebDriverUtils.configuredImplicityWait(), TimeUnit.SECONDS);
57 driver.get(jenkinsBase + "/login?form");
58
59
60 WebDriverUtils.waitFor(driver, WebDriverUtils.configuredImplicityWait(), By.id("username"),
61 this.getClass().toString());
62 driver.findElement(By.id("username")).sendKeys(System.getProperty("cas.username"));
63 driver.findElement(By.id("password")).sendKeys(System.getProperty("cas.password"));
64 driver.findElement(By.name("submit")).click();
65 Thread.sleep(1000);
66 if (driver.getPageSource().contains("The credentials you provided cannot be determined to be authentic.")) {
67 System.out.println("CAS Login Error");
68 System.exit(1);
69 }
70
71
72 WebDriverUtils.waitFor(driver, WebDriverUtils.configuredImplicityWait(), By.xpath("//span[contains(text(), 'Page generated')]"), this.getClass().toString());
73
74 passed = true;
75 }
76
77 @After
78 public void tearDown() {
79 closeAndQuitWebDriver();
80 }
81
82 private void closeAndQuitWebDriver() {
83 if (driver != null) {
84 if (WebDriverUtils.dontTearDownPropertyNotSet() && WebDriverUtils.dontTearDownOnFailure(passed)) {
85 try {
86 driver.close();
87 } catch (NoSuchWindowException nswe) {
88 System.out.println("NoSuchWindowException closing WebDriver " + nswe.getMessage());
89 } finally {
90 if (driver != null) {
91 driver.quit();
92 }
93 }
94 }
95 } else {
96 System.out.println("WebDriver is null for " + this.getClass().toString());
97 }
98 }
99
100 @Test
101 public void testGetJobResults() {
102 String jobJson = null;
103 String json = null;
104 String outputFile = null;
105 String url = null;
106 String job = null;
107
108 for (int i = 0, s = jobs.length; i < s; i++) {
109 try {
110 job = jobs[i];
111 url = jenkinsBase + "/job/" + job + "/api/json";
112
113 jobJson = retrieveJson(url);
114 jobJson = jobJson.substring(jobJson.indexOf("\"lastCompletedBuild\":{\"number\":") + 31, jobJson.length());
115 jobJson = jobJson.substring(0, jobJson.indexOf(","));
116
117 url = jenkinsBase + "/job/" + job + "/" + jobJson + "/testReport/api/json";
118 json = retrieveJson(url);
119
120 outputFile = job + "-" + jobJson + ".json";
121 if (outputDirectory != null) {
122 outputFile = outputDirectory + File.separatorChar + outputFile;
123 }
124
125 json = json.replaceAll("}],", "}],\n");
126 FileUtils.writeStringToFile(new File(outputFile), json);
127
128 } catch (Exception e) {
129 passed = false;
130 System.out.println("job: " + job + " url: " + url + " " + e.getMessage());
131 e.printStackTrace();
132 }
133 }
134 }
135
136 private String retrieveJson(String url) throws InterruptedException {
137 driver.get(url);
138 Thread.sleep(500);
139 int secondsWaited = 0;
140
141 while (!driver.getPageSource().contains("</pre></body></html>") && secondsWaited++ < 2 * WebDriverUtils.configuredImplicityWait()) {
142 Thread.sleep(1000);
143 }
144
145 String json = driver.getPageSource();
146 json = json.substring(json.indexOf("<pre>") + 5, json.indexOf("</pre></body></html>"));
147
148 return json;
149 }
150 }