1 | package gstests;
|
---|
2 |
|
---|
3 | import java.util.List;
|
---|
4 |
|
---|
5 | import org.greenstone.gsdl3.testing.GSTestingUtil;
|
---|
6 | import org.junit.AfterClass;
|
---|
7 | import org.junit.Assert;
|
---|
8 | import org.junit.Before;
|
---|
9 | import org.junit.Test;
|
---|
10 | import org.openqa.selenium.By;
|
---|
11 | import org.openqa.selenium.WebDriver;
|
---|
12 | import org.openqa.selenium.WebElement;
|
---|
13 | import org.openqa.selenium.firefox.FirefoxDriver;
|
---|
14 |
|
---|
15 | public 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 | GSTestingUtil.performQuickSearch(_driver, "snails", null);
|
---|
148 |
|
---|
149 | //Check the number of results on the page
|
---|
150 | List<WebElement> results = GSTestingUtil.findElementsByXPath(_driver, "//table[@id='resultsTable']/tbody/tr");
|
---|
151 | 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);
|
---|
152 |
|
---|
153 | //Check the term info has the correct values
|
---|
154 | WebElement termInfo = GSTestingUtil.findElementByXPath(_driver, "//p[@class='termList']/span[@class='termInfo']");
|
---|
155 | 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"));
|
---|
156 |
|
---|
157 | //Check the search results status bar
|
---|
158 | WebElement searchStatus = GSTestingUtil.findElementByXPath(_driver, "//td[@id='searchResultsStatusBar']");
|
---|
159 | 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"));
|
---|
160 |
|
---|
161 | //Click the next button
|
---|
162 | WebElement nextButton = GSTestingUtil.findElementByXPath(_driver, "//td[@id='nextTD']/a");
|
---|
163 | nextButton.click();
|
---|
164 |
|
---|
165 | //Check the search results status bar on the new page
|
---|
166 | searchStatus = GSTestingUtil.findElementByXPath(_driver, "//td[@id='searchResultsStatusBar']");
|
---|
167 | 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"));
|
---|
168 |
|
---|
169 | //Click the previous button
|
---|
170 | WebElement prevButton = GSTestingUtil.findElementByXPath(_driver, "//td[@id='prevTD']/a");
|
---|
171 | prevButton.click();
|
---|
172 |
|
---|
173 | /*
|
---|
174 | * TEST A RANDOM QUERY THAT SHOULD FAIL
|
---|
175 | */
|
---|
176 |
|
---|
177 | //Generate a search that will fail
|
---|
178 | String randomSearchTerm = GSTestingUtil.generateRandomString(20);
|
---|
179 |
|
---|
180 | GSTestingUtil.performQuickSearch(_driver, randomSearchTerm, null);
|
---|
181 |
|
---|
182 | //Make sure that no documents match
|
---|
183 | WebElement contentElem = GSTestingUtil.findElementByXPath(_driver, "//div[@id='gs_content']");
|
---|
184 | Assert.assertTrue("No results should have been found for \"" + randomSearchTerm, contentElem.getText().equals("No documents matched the query."));
|
---|
185 | }
|
---|
186 |
|
---|
187 | @Test
|
---|
188 | public void testLoginAndAdmin()
|
---|
189 | {
|
---|
190 | GSTestingUtil.loginAs(_driver, "admin", "admin");
|
---|
191 |
|
---|
192 | //Go to the admin page
|
---|
193 | WebElement adminPagesLink = GSTestingUtil.findElementByXPath(_driver, "//a[@href='library/admin/ListUsers']");
|
---|
194 | adminPagesLink.click();
|
---|
195 |
|
---|
196 | //Make sure we are logged in correctly
|
---|
197 | WebElement userListTable = GSTestingUtil.findElementByXPath(_driver, "//table[@id='userListTable']");
|
---|
198 | Assert.assertNotNull("Administrator failed to log in", userListTable);
|
---|
199 |
|
---|
200 | //Go to the add new user page
|
---|
201 | WebElement addNewUserButton = GSTestingUtil.findElementByXPath(_driver, "//a[@href='library/admin/AddUser']");
|
---|
202 | addNewUserButton.click();
|
---|
203 |
|
---|
204 | //Get the form elements
|
---|
205 | WebElement usernameBox = GSTestingUtil.findElementByXPath(_driver, "//input[@name='s1.username']");
|
---|
206 | WebElement passwordBox = GSTestingUtil.findElementByXPath(_driver, "//input[@id='passwordOne']");
|
---|
207 | WebElement repasswordBox = GSTestingUtil.findElementByXPath(_driver, "//input[@id='passwordTwo']");
|
---|
208 | WebElement emailBox = GSTestingUtil.findElementByXPath(_driver, "//input[@name='s1.email']");
|
---|
209 | WebElement groupsBox = GSTestingUtil.findElementByXPath(_driver, "//input[@name='s1.groups']");
|
---|
210 | WebElement commentBox = GSTestingUtil.findElementByXPath(_driver, "//textarea[@name='s1.comment']");
|
---|
211 |
|
---|
212 | //Generate a random user name of password
|
---|
213 | String randomUsername = GSTestingUtil.generateRandomString(8);
|
---|
214 | String randomPassword = GSTestingUtil.generateRandomString(8);
|
---|
215 |
|
---|
216 | //Enter information into the form
|
---|
217 | usernameBox.sendKeys(randomUsername);
|
---|
218 | passwordBox.sendKeys(randomPassword);
|
---|
219 | repasswordBox.sendKeys(randomPassword);
|
---|
220 | emailBox.sendKeys(randomUsername + "@testusername.co.nz");
|
---|
221 | groupsBox.sendKeys("Test Group");
|
---|
222 | commentBox.sendKeys("A user added for testing purposes");
|
---|
223 |
|
---|
224 | //Submit the form
|
---|
225 | WebElement submitButton = GSTestingUtil.findElementByXPath(_driver, "//input[@id='submitButton']");
|
---|
226 | submitButton.click();
|
---|
227 |
|
---|
228 | //Check the new user information is correct
|
---|
229 | List<WebElement> userRows = GSTestingUtil.findElementsByXPath(_driver, "//table[@id='userListTable']/tbody/tr");
|
---|
230 | boolean found = false;
|
---|
231 | for (int i = 0; i < userRows.size(); i++)
|
---|
232 | {
|
---|
233 | List<WebElement> columns = userRows.get(i).findElements(By.tagName("td"));
|
---|
234 |
|
---|
235 | if (columns.get(0).getText().equals(randomUsername))
|
---|
236 | {
|
---|
237 | found = true;
|
---|
238 | Assert.assertTrue("The new user enabled status was incorrect", columns.get(1).getText().equals("enabled"));
|
---|
239 | Assert.assertTrue("The new user group was incorrect", columns.get(2).getText().equals("TestGroup"));
|
---|
240 | Assert.assertTrue("The new user comment was incorrect", columns.get(3).getText().equals("A user added for testing purposes"));
|
---|
241 | Assert.assertTrue("The new user email was incorrect", columns.get(4).getText().equals(randomUsername + "@testusername.co.nz"));
|
---|
242 | }
|
---|
243 | }
|
---|
244 | Assert.assertTrue("The new user was not found", found);
|
---|
245 |
|
---|
246 | //Log in as the new user
|
---|
247 | GSTestingUtil.logout(_driver);
|
---|
248 | GSTestingUtil.loginAs(_driver, randomUsername, randomPassword);
|
---|
249 |
|
---|
250 | //Check the log in worked
|
---|
251 | WebElement menuButton = GSTestingUtil.findElementByXPath(_driver, "//li[@id='userMenuButton']/a");
|
---|
252 | Assert.assertTrue("The new user was not able to log in correctly", menuButton.getText().equals(randomUsername));
|
---|
253 |
|
---|
254 | //Go to the home page
|
---|
255 | WebElement backToHomePageLink = GSTestingUtil.findElementByXPath(_driver, "//div[@id='breadcrumbs']/a[1]");
|
---|
256 | backToHomePageLink.click();
|
---|
257 |
|
---|
258 | //Log in as admin
|
---|
259 | GSTestingUtil.logout(_driver);
|
---|
260 | GSTestingUtil.loginAs(_driver, "admin", "admin");
|
---|
261 |
|
---|
262 | //Go to the list of users
|
---|
263 | addNewUserButton = GSTestingUtil.findElementByXPath(_driver, "//a[@href='library/admin/ListUsers']");
|
---|
264 | addNewUserButton.click();
|
---|
265 |
|
---|
266 | //Delete the user
|
---|
267 | userRows = GSTestingUtil.findElementsByXPath(_driver, "//table[@id='userListTable']/tbody/tr");
|
---|
268 | for (int i = 0; i < userRows.size(); i++)
|
---|
269 | {
|
---|
270 | List<WebElement> columns = userRows.get(i).findElements(By.tagName("td"));
|
---|
271 |
|
---|
272 | if (columns.get(0).getText().equals(randomUsername))
|
---|
273 | {
|
---|
274 | WebElement deleteLink = columns.get(6).findElement(By.xpath(".//input[@value='Delete']"));
|
---|
275 | deleteLink.click();
|
---|
276 | _driver.switchTo().alert().accept();
|
---|
277 |
|
---|
278 | try
|
---|
279 | {
|
---|
280 | Thread.sleep(5000);
|
---|
281 | }
|
---|
282 | catch (InterruptedException e)
|
---|
283 | {
|
---|
284 | e.printStackTrace();
|
---|
285 | }
|
---|
286 | }
|
---|
287 | }
|
---|
288 |
|
---|
289 | //Log out as admin
|
---|
290 | GSTestingUtil.logout(_driver);
|
---|
291 | }
|
---|
292 |
|
---|
293 | @Test
|
---|
294 | public void testDocumentView()
|
---|
295 | {
|
---|
296 | GSTestingUtil.loadCollectionByName(_driver, COLLECTION_NAME);
|
---|
297 | GSTestingUtil.loadClassifierByName(_driver, "titles");
|
---|
298 |
|
---|
299 | //Load a random document
|
---|
300 | List<WebElement> documents = GSTestingUtil.findElementsByXPath(_driver, "//table[@id='classifiernodelist']/tbody/tr");
|
---|
301 | WebElement randomRow = documents.get((int) (Math.random() * documents.size()));
|
---|
302 | WebElement link = randomRow.findElement(By.xpath(".//a"));
|
---|
303 | link.click();
|
---|
304 |
|
---|
305 | //Check the cover image is loaded
|
---|
306 | WebElement coverImage = GSTestingUtil.findElementByXPath(_driver, "//div[@id='coverImage']/img");
|
---|
307 | Assert.assertTrue("The cover image of the document did not load correctly", GSTestingUtil.isImageLoaded(_driver, coverImage));
|
---|
308 | }
|
---|
309 |
|
---|
310 | @AfterClass
|
---|
311 | public static void destroy()
|
---|
312 | {
|
---|
313 | _driver.quit();
|
---|
314 | }
|
---|
315 | } |
---|