source: main/trunk/greenstone3/web/sites/localsite/collect/lucene-jdbm-demo/tests/src/gstests/TestClass.java@ 26114

Last change on this file since 26114 was 26114, checked in by sjm84, 12 years ago

Updating the tests so that more useful error messages are displayed

  • Property svn:executable set to *
File size: 14.5 KB
Line 
1package gstests;
2
3import java.util.List;
4
5import org.greenstone.gsdl3.testing.GSTestingUtil;
6import org.junit.AfterClass;
7import org.junit.Assert;
8import org.junit.Before;
9import org.junit.Test;
10import org.openqa.selenium.By;
11import org.openqa.selenium.WebDriver;
12import org.openqa.selenium.WebElement;
13import org.openqa.selenium.firefox.FirefoxDriver;
14
15public class TestClass
16{
17 //TODO: Do these dynamically
18 private static final int NUMBER_OF_CLASSIFIERS = 4;
19 private static final int NUMBER_OF_SEARCH_TYPES = 3;
20 private static final int NUMBER_OF_SEARCH_INDEXES = 5;
21
22 //TODO: Turn these into a list
23 private static final int TITLE_CLASSIFIER_SIZE = 11;
24 private static final int SUBJECT_CLASSIFIER_SIZE = 7;
25 private static final int ORGANISATIONS_CLASSIFIER_SIZE = 5;
26
27 private static final int HITS_PER_PAGE = 20;
28 private static final int SNAILS_RESULT_COUNT = 58;
29 private static final int SNAILS_OCCURENCE_COUNT = 398;
30
31 private static final String COLLECTION_NAME = "Demo Collection";
32
33 private static WebDriver _driver = new FirefoxDriver();
34
35 @Before
36 public void init()
37 {
38 _driver.get(System.getProperty("SERVERURL"));
39 }
40
41 @Test
42 /**
43 * Test the library home page
44 * 1. Test that the demo collection is there
45 */
46 public void testHomePage()
47 {
48 Assert.assertNotNull("The Demo Collection is not available", GSTestingUtil.findElementByXPath(_driver, "//div[@id='collectionLinks']/a[descendant::text()='Demo Collection']"));
49 }
50
51 @Test
52 public void testDemoCollectionHomePage()
53 {
54 GSTestingUtil.loadCollectionByName(_driver, COLLECTION_NAME);
55
56 //Check the title is correct
57 WebElement demoTitleElem = GSTestingUtil.findElementByXPath(_driver, "//div[@id='titlearea']/h2");
58 String title = demoTitleElem.getText();
59 Assert.assertTrue("The title is incorrect", title.equals("Demo Collection"));
60
61 //Check we have four browsing classifiers
62 List<WebElement> classifierLinks = GSTestingUtil.findElementsByXPath(_driver, "//ul[@id='gs-nav']/li");
63 Assert.assertEquals("There should be " + NUMBER_OF_CLASSIFIERS + " classifiers but there were " + classifierLinks.size(), classifierLinks.size(), NUMBER_OF_CLASSIFIERS);
64
65 //Check we have 3 search types
66 List<WebElement> searchTypes = GSTestingUtil.findElementsByXPath(_driver, "//div[@id='quicksearcharea']/ul/li");
67 Assert.assertEquals("There should be " + NUMBER_OF_SEARCH_TYPES + " search types but there were " + searchTypes.size(), searchTypes.size(), NUMBER_OF_SEARCH_TYPES);
68
69 //Check we have 5 search indexes
70 List<WebElement> searchIndexes = GSTestingUtil.findElementsByXPath(_driver, "//div[@id='quicksearcharea']/form/span[@class='textselect']/select/option");
71 Assert.assertEquals("There should be " + NUMBER_OF_SEARCH_INDEXES + " search indexes but there were " + searchIndexes.size(), searchIndexes.size(), NUMBER_OF_SEARCH_INDEXES);
72 }
73
74 @Test
75 public void testTitleClassifier()
76 {
77 GSTestingUtil.loadCollectionByName(_driver, COLLECTION_NAME);
78
79 //Load the title classifier
80 GSTestingUtil.loadClassifierByName(_driver, "titles");
81
82 //Check that we have 11 documents
83 List<WebElement> documents = GSTestingUtil.findElementsByXPath(_driver, "//table[@id='classifiernodelist']/tbody/tr");
84 Assert.assertEquals("There should be " + TITLE_CLASSIFIER_SIZE + " documents in the titles classifier but there were " + documents.size(), documents.size(), TITLE_CLASSIFIER_SIZE);
85 }
86
87 @Test
88 public void testSubjectsClassifier()
89 {
90 GSTestingUtil.loadCollectionByName(_driver, COLLECTION_NAME);
91
92 //Load the subject classifier
93 GSTestingUtil.loadClassifierByName(_driver, "subjects");
94
95 //Check that we have 7 subjects
96 List<WebElement> subjectElems = GSTestingUtil.findElementsByXPath(_driver, "//table[@id='classifiernodelist']/tbody/tr");
97 Assert.assertEquals("There should be " + SUBJECT_CLASSIFIER_SIZE + " documents in the subjects classifier but there were " + subjectElems.size(), subjectElems.size(), SUBJECT_CLASSIFIER_SIZE);
98
99 //Get all of the subject expand images
100 List<WebElement> expandImages = GSTestingUtil.findElementsByXPath(_driver, "//img[@src='interfaces/default/images/expand.png']");
101
102 //Open up a random subject
103 WebElement randomSubject = expandImages.get((int) (Math.random() * SUBJECT_CLASSIFIER_SIZE));
104 randomSubject.click();
105
106 //Make sure it opened correctly
107 String sectionNumber = randomSubject.getAttribute("id").substring(6);
108 GSTestingUtil.waitForXPath(_driver, "//table[@id='div" + sectionNumber + "']");
109 Assert.assertNotNull("The subjects classifier did not open correctly", GSTestingUtil.findElementByXPath(_driver, "//table[@id='div" + sectionNumber + "']"));
110 }
111
112 @Test
113 public void testOrganisationsClassifier()
114 {
115 GSTestingUtil.loadCollectionByName(_driver, COLLECTION_NAME);
116
117 //Load the organisation classifier
118 GSTestingUtil.loadClassifierByName(_driver, "organisations");
119
120 //Check that we have 5 organisations
121 List<WebElement> orgElems = GSTestingUtil.findElementsByXPath(_driver, "//table[@id='classifiernodelist']/tbody/tr");
122 Assert.assertEquals("There should be " + ORGANISATIONS_CLASSIFIER_SIZE + " documents in the organisations classifier but there were " + orgElems.size(), orgElems.size(), ORGANISATIONS_CLASSIFIER_SIZE);
123
124 //Get all of the subject expand images
125 List<WebElement> expandImages = GSTestingUtil.findElementsByXPath(_driver, "//img[@src='interfaces/default/images/expand.png']");
126
127 //Open up a random organisation
128 WebElement randomOrganisation = expandImages.get((int) (Math.random() * ORGANISATIONS_CLASSIFIER_SIZE));
129 randomOrganisation.click();
130
131 //Make sure it opened correctly
132 String sectionNumber = randomOrganisation.getAttribute("id").substring(6);
133 GSTestingUtil.waitForXPath(_driver, "//table[@id='div" + sectionNumber + "']");
134 Assert.assertNotNull("The organisations classifier did not open correctly", GSTestingUtil.findElementByXPath(_driver, "//table[@id='div" + sectionNumber + "']"));
135 }
136
137 @Test
138 public void testQuickSearch()
139 {
140 GSTestingUtil.loadCollectionByName(_driver, COLLECTION_NAME);
141
142 /*
143 * TEST A QUERY THAT SHOULD WORK
144 */
145
146 //Type "snails" into quick search area and submit
147 WebElement quickSearchInput = GSTestingUtil.findElementByXPath(_driver, "//div[@id='quicksearcharea']//input[@name='s1.query']");
148 quickSearchInput.sendKeys("snails");
149 WebElement quickSearchSubmitButton = GSTestingUtil.findElementByXPath(_driver, "//input[@id='quickSearchSubmitButton']");
150 quickSearchSubmitButton.click();
151
152 //Check the number of results on the page
153 List<WebElement> results = GSTestingUtil.findElementsByXPath(_driver, "//table[@id='resultsTable']/tbody/tr");
154 Assert.assertEquals("The number of results on the page should have been " + HITS_PER_PAGE + " but it was " + results.size(), results.size(), HITS_PER_PAGE);
155
156 //Check the term info has the correct values
157 WebElement termInfo = GSTestingUtil.findElementByXPath(_driver, "//p[@class='termList']/span[@class='termInfo']");
158 Assert.assertTrue("The term information was incorrect, it should have been \"" + "snails occurs " + SNAILS_OCCURENCE_COUNT + " times in " + SNAILS_RESULT_COUNT + " sections" + "\" but was \"" + termInfo.getText() + "\"", termInfo.getText().equals("snails occurs " + SNAILS_OCCURENCE_COUNT + " times in " + SNAILS_RESULT_COUNT + " sections"));
159
160 //Check the search results status bar
161 WebElement searchStatus = GSTestingUtil.findElementByXPath(_driver, "//td[@id='searchResultsStatusBar']");
162 Assert.assertTrue("The search status was incorrect, it should have been \"" + "Displaying 1 to " + HITS_PER_PAGE + " of " + SNAILS_RESULT_COUNT + " sections" + "\" but it was \"" + searchStatus.getText() + "\"", searchStatus.getText().equals("Displaying 1 to " + HITS_PER_PAGE + " of " + SNAILS_RESULT_COUNT + " sections"));
163
164 //Click the next button
165 WebElement nextButton = GSTestingUtil.findElementByXPath(_driver, "//td[@id='nextTD']/a");
166 nextButton.click();
167
168 //Check the search results status bar on the new page
169 searchStatus = GSTestingUtil.findElementByXPath(_driver, "//td[@id='searchResultsStatusBar']");
170 Assert.assertTrue("The search status was incorrect, it should have been \"" + "Displaying " + (HITS_PER_PAGE + 1) + " to " + (HITS_PER_PAGE * 2) + " of " + SNAILS_RESULT_COUNT + " sections" + "\" but it was \"" + searchStatus.getText() + "\"", searchStatus.getText().equals("Displaying " + (HITS_PER_PAGE + 1) + " to " + (HITS_PER_PAGE * 2) + " of " + SNAILS_RESULT_COUNT + " sections"));
171
172 //Click the previous button
173 WebElement prevButton = GSTestingUtil.findElementByXPath(_driver, "//td[@id='prevTD']/a");
174 prevButton.click();
175
176 /*
177 * TEST A RANDOM QUERY THAT SHOULD FAIL
178 */
179
180 //Generate a search that will fail
181 String randomSearchTerm = GSTestingUtil.generateRandomString(20);
182
183 quickSearchInput = GSTestingUtil.findElementByXPath(_driver, "//div[@id='quicksearcharea']//input[@name='s1.query']");
184 quickSearchInput.clear();
185 quickSearchInput.sendKeys(randomSearchTerm);
186 quickSearchSubmitButton = GSTestingUtil.findElementByXPath(_driver, "//input[@id='quickSearchSubmitButton']");
187 quickSearchSubmitButton.click();
188
189 //Make sure that no documents match
190 WebElement contentElem = GSTestingUtil.findElementByXPath(_driver, "//div[@id='gs_content']");
191 Assert.assertTrue("No results should have been found for \"" + randomSearchTerm, contentElem.getText().equals("No documents matched the query."));
192 }
193
194 @Test
195 public void testLoginAndAdmin()
196 {
197 GSTestingUtil.loginAs(_driver, "admin", "admin");
198
199 //Go to the admin page
200 WebElement adminPagesLink = GSTestingUtil.findElementByXPath(_driver, "//a[@href='library/admin/ListUsers']");
201 adminPagesLink.click();
202
203 //Make sure we are logged in correctly
204 WebElement userListTable = GSTestingUtil.findElementByXPath(_driver, "//table[@id='userListTable']");
205 Assert.assertNotNull("Administrator failed to log in", userListTable);
206
207 //Go to the add new user page
208 WebElement addNewUserButton = GSTestingUtil.findElementByXPath(_driver, "//a[@href='library/admin/AddUser']");
209 addNewUserButton.click();
210
211 //Get the form elements
212 WebElement usernameBox = GSTestingUtil.findElementByXPath(_driver, "//input[@name='s1.username']");
213 WebElement passwordBox = GSTestingUtil.findElementByXPath(_driver, "//input[@id='passwordOne']");
214 WebElement repasswordBox = GSTestingUtil.findElementByXPath(_driver, "//input[@id='passwordTwo']");
215 WebElement emailBox = GSTestingUtil.findElementByXPath(_driver, "//input[@name='s1.email']");
216 WebElement groupsBox = GSTestingUtil.findElementByXPath(_driver, "//input[@name='s1.groups']");
217 WebElement commentBox = GSTestingUtil.findElementByXPath(_driver, "//textarea[@name='s1.comment']");
218
219 //Generate a random user name of password
220 String randomUsername = GSTestingUtil.generateRandomString(8);
221 String randomPassword = GSTestingUtil.generateRandomString(8);
222
223 //Enter information into the form
224 usernameBox.sendKeys(randomUsername);
225 passwordBox.sendKeys(randomPassword);
226 repasswordBox.sendKeys(randomPassword);
227 emailBox.sendKeys(randomUsername + "@testusername.co.nz");
228 groupsBox.sendKeys("Test Group");
229 commentBox.sendKeys("A user added for testing purposes");
230
231 //Submit the form
232 WebElement submitButton = GSTestingUtil.findElementByXPath(_driver, "//input[@id='submitButton']");
233 submitButton.click();
234
235 //Check the new user information is correct
236 List<WebElement> userRows = GSTestingUtil.findElementsByXPath(_driver, "//table[@id='userListTable']/tbody/tr");
237 boolean found = false;
238 for (int i = 0; i < userRows.size(); i++)
239 {
240 List<WebElement> columns = userRows.get(i).findElements(By.tagName("td"));
241
242 if (columns.get(0).getText().equals(randomUsername))
243 {
244 found = true;
245 Assert.assertTrue("The new user enabled status was incorrect", columns.get(1).getText().equals("enabled"));
246 Assert.assertTrue("The new user group was incorrect", columns.get(2).getText().equals("TestGroup"));
247 Assert.assertTrue("The new user comment was incorrect", columns.get(3).getText().equals("A user added for testing purposes"));
248 Assert.assertTrue("The new user email was incorrect", columns.get(4).getText().equals(randomUsername + "@testusername.co.nz"));
249 }
250 }
251 Assert.assertTrue("The new user was not found", found);
252
253 //Log in as the new user
254 GSTestingUtil.logout(_driver);
255 GSTestingUtil.loginAs(_driver, randomUsername, randomPassword);
256
257 //Check the log in worked
258 WebElement menuButton = GSTestingUtil.findElementByXPath(_driver, "//li[@id='userMenuButton']/a");
259 Assert.assertTrue("The new user was not able to log in correctly", menuButton.getText().equals(randomUsername));
260
261 //Go to the home page
262 WebElement backToHomePageLink = GSTestingUtil.findElementByXPath(_driver, "//div[@id='breadcrumbs']/a[1]");
263 backToHomePageLink.click();
264
265 //Log in as admin
266 GSTestingUtil.logout(_driver);
267 GSTestingUtil.loginAs(_driver, "admin", "admin");
268
269 //Go to the list of users
270 addNewUserButton = GSTestingUtil.findElementByXPath(_driver, "//a[@href='library/admin/ListUsers']");
271 addNewUserButton.click();
272
273 //Delete the user
274 userRows = GSTestingUtil.findElementsByXPath(_driver, "//table[@id='userListTable']/tbody/tr");
275 for (int i = 0; i < userRows.size(); i++)
276 {
277 List<WebElement> columns = userRows.get(i).findElements(By.tagName("td"));
278
279 if (columns.get(0).getText().equals(randomUsername))
280 {
281 WebElement deleteLink = columns.get(6).findElement(By.xpath(".//input[@value='Delete']"));
282 deleteLink.click();
283 _driver.switchTo().alert().accept();
284
285 try
286 {
287 Thread.sleep(5000);
288 }
289 catch (InterruptedException e)
290 {
291 e.printStackTrace();
292 }
293 }
294 }
295
296 //Log out as admin
297 GSTestingUtil.logout(_driver);
298 }
299
300 @Test
301 public void testDocumentView()
302 {
303 GSTestingUtil.loadCollectionByName(_driver, COLLECTION_NAME);
304 GSTestingUtil.loadClassifierByName(_driver, "titles");
305
306 //Load a random document
307 List<WebElement> documents = GSTestingUtil.findElementsByXPath(_driver, "//table[@id='classifiernodelist']/tbody/tr");
308 WebElement randomRow = documents.get((int) (Math.random() * documents.size()));
309 WebElement link = randomRow.findElement(By.xpath(".//a"));
310 link.click();
311
312 //Check the cover image is loaded
313 WebElement coverImage = GSTestingUtil.findElementByXPath(_driver, "//div[@id='coverImage']/img");
314 Assert.assertTrue("The cover image of the document did not load correctly", GSTestingUtil.isImageLoaded(_driver, coverImage));
315 }
316
317 @AfterClass
318 public static void destroy()
319 {
320 _driver.quit();
321 }
322}
Note: See TracBrowser for help on using the repository browser.