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("http://localhost:8487/greenstone3/library");
|
---|
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(GSTestingUtil.findElementByXPath(_driver, "//div[@id='collectionLinks']/a[@title='Demonstration collection containing a few books from the Humanitarian and Development Libraries. Uses Lucene and JDBM.']"));
|
---|
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(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(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(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(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(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(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(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(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(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(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(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(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(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 | quickSearchInput = GSTestingUtil.findElementByXPath(_driver, "//div[@id='quicksearcharea']//input[@name='s1.query']");
|
---|
182 | quickSearchInput.clear();
|
---|
183 | quickSearchInput.sendKeys(GSTestingUtil.generateRandomString(20));
|
---|
184 | quickSearchSubmitButton = GSTestingUtil.findElementByXPath(_driver, "//input[@id='quickSearchSubmitButton']");
|
---|
185 | quickSearchSubmitButton.click();
|
---|
186 |
|
---|
187 | //Make sure that no documents match
|
---|
188 | WebElement contentElem = GSTestingUtil.findElementByXPath(_driver, "//div[@id='gs_content']");
|
---|
189 | Assert.assertTrue(contentElem.getText().equals("No documents matched the query."));
|
---|
190 | }
|
---|
191 |
|
---|
192 | @Test
|
---|
193 | public void testLoginAndAdmin()
|
---|
194 | {
|
---|
195 | GSTestingUtil.loginAs(_driver, "admin", "admin");
|
---|
196 |
|
---|
197 | //Go to the admin page
|
---|
198 | WebElement adminPagesLink = GSTestingUtil.findElementByXPath(_driver, "//a[@href='library/admin/ListUsers']");
|
---|
199 | adminPagesLink.click();
|
---|
200 |
|
---|
201 | //Make sure we are logged in correctly
|
---|
202 | WebElement userListTable = GSTestingUtil.findElementByXPath(_driver, "//table[@id='userListTable']");
|
---|
203 | Assert.assertNotNull(userListTable);
|
---|
204 |
|
---|
205 | //Go to the add new user page
|
---|
206 | WebElement addNewUserButton = GSTestingUtil.findElementByXPath(_driver, "//a[@href='library/admin/AddUser']");
|
---|
207 | addNewUserButton.click();
|
---|
208 |
|
---|
209 | //Get the form elements
|
---|
210 | WebElement usernameBox = GSTestingUtil.findElementByXPath(_driver, "//input[@name='s1.username']");
|
---|
211 | WebElement passwordBox = GSTestingUtil.findElementByXPath(_driver, "//input[@id='passwordOne']");
|
---|
212 | WebElement repasswordBox = GSTestingUtil.findElementByXPath(_driver, "//input[@id='passwordTwo']");
|
---|
213 | WebElement emailBox = GSTestingUtil.findElementByXPath(_driver, "//input[@name='s1.email']");
|
---|
214 | WebElement groupsBox = GSTestingUtil.findElementByXPath(_driver, "//input[@name='s1.groups']");
|
---|
215 | WebElement commentBox = GSTestingUtil.findElementByXPath(_driver, "//textarea[@name='s1.comment']");
|
---|
216 |
|
---|
217 | //Generate a random user name of password
|
---|
218 | String randomUsername = GSTestingUtil.generateRandomString(8);
|
---|
219 | String randomPassword = GSTestingUtil.generateRandomString(8);
|
---|
220 |
|
---|
221 | //Enter information into the form
|
---|
222 | usernameBox.sendKeys(randomUsername);
|
---|
223 | passwordBox.sendKeys(randomPassword);
|
---|
224 | repasswordBox.sendKeys(randomPassword);
|
---|
225 | emailBox.sendKeys(randomUsername + "@testusername.co.nz");
|
---|
226 | groupsBox.sendKeys("Test Group");
|
---|
227 | commentBox.sendKeys("A user added for testing purposes");
|
---|
228 |
|
---|
229 | //Submit the form
|
---|
230 | WebElement submitButton = GSTestingUtil.findElementByXPath(_driver, "//input[@id='submitButton']");
|
---|
231 | submitButton.click();
|
---|
232 |
|
---|
233 | //Check the new user information is correct
|
---|
234 | List<WebElement> userRows = GSTestingUtil.findElementsByXPath(_driver, "//table[@id='userListTable']/tbody/tr");
|
---|
235 | boolean found = false;
|
---|
236 | for (int i = 0; i < userRows.size(); i++)
|
---|
237 | {
|
---|
238 | List<WebElement> columns = userRows.get(i).findElements(By.tagName("td"));
|
---|
239 |
|
---|
240 | if (columns.get(0).getText().equals(randomUsername))
|
---|
241 | {
|
---|
242 | found = true;
|
---|
243 | Assert.assertTrue(columns.get(1).getText().equals("enabled"));
|
---|
244 | Assert.assertTrue(columns.get(2).getText().equals("TestGroup"));
|
---|
245 | Assert.assertTrue(columns.get(3).getText().equals("A user added for testing purposes"));
|
---|
246 | Assert.assertTrue(columns.get(4).getText().equals(randomUsername + "@testusername.co.nz"));
|
---|
247 | }
|
---|
248 | }
|
---|
249 | Assert.assertTrue(found);
|
---|
250 |
|
---|
251 | //Log in as the new user
|
---|
252 | GSTestingUtil.logout(_driver);
|
---|
253 | GSTestingUtil.loginAs(_driver, randomUsername, randomPassword);
|
---|
254 |
|
---|
255 | //Check the log in worked
|
---|
256 | WebElement menuButton = GSTestingUtil.findElementByXPath(_driver, "//li[@id='userMenuButton']/a");
|
---|
257 | Assert.assertTrue(menuButton.getText().equals(randomUsername));
|
---|
258 |
|
---|
259 | //Go to the home page
|
---|
260 | WebElement backToHomePageLink = GSTestingUtil.findElementByXPath(_driver, "//div[@id='breadcrumbs']/a[1]");
|
---|
261 | backToHomePageLink.click();
|
---|
262 |
|
---|
263 | //Log in as admin
|
---|
264 | GSTestingUtil.logout(_driver);
|
---|
265 | GSTestingUtil.loginAs(_driver, "admin", "admin");
|
---|
266 |
|
---|
267 | //Go to the list of users
|
---|
268 | addNewUserButton = GSTestingUtil.findElementByXPath(_driver, "//a[@href='library/admin/ListUsers']");
|
---|
269 | addNewUserButton.click();
|
---|
270 |
|
---|
271 | //Delete the user
|
---|
272 | userRows = GSTestingUtil.findElementsByXPath(_driver, "//table[@id='userListTable']/tbody/tr");
|
---|
273 | for (int i = 0; i < userRows.size(); i++)
|
---|
274 | {
|
---|
275 | List<WebElement> columns = userRows.get(i).findElements(By.tagName("td"));
|
---|
276 |
|
---|
277 | if (columns.get(0).getText().equals(randomUsername))
|
---|
278 | {
|
---|
279 | WebElement deleteLink = columns.get(6).findElement(By.xpath(".//input[@value='Delete']"));
|
---|
280 | deleteLink.click();
|
---|
281 | _driver.switchTo().alert().accept();
|
---|
282 |
|
---|
283 | try
|
---|
284 | {
|
---|
285 | Thread.sleep(5000);
|
---|
286 | }
|
---|
287 | catch (InterruptedException e)
|
---|
288 | {
|
---|
289 | e.printStackTrace();
|
---|
290 | }
|
---|
291 | }
|
---|
292 | }
|
---|
293 |
|
---|
294 | //Log out as admin
|
---|
295 | GSTestingUtil.logout(_driver);
|
---|
296 | }
|
---|
297 |
|
---|
298 | @Test
|
---|
299 | public void testDocumentView()
|
---|
300 | {
|
---|
301 | GSTestingUtil.loadCollectionByName(_driver, COLLECTION_NAME);
|
---|
302 | GSTestingUtil.loadClassifierByName(_driver, "titles");
|
---|
303 |
|
---|
304 | //Load a random document
|
---|
305 | List<WebElement> documents = GSTestingUtil.findElementsByXPath(_driver, "//table[@id='classifiernodelist']/tbody/tr");
|
---|
306 | WebElement randomRow = documents.get((int) (Math.random() * documents.size()));
|
---|
307 | WebElement link = randomRow.findElement(By.xpath(".//a"));
|
---|
308 | link.click();
|
---|
309 |
|
---|
310 | //Check the cover image is loaded
|
---|
311 | WebElement coverImage = GSTestingUtil.findElementByXPath(_driver, "//div[@id='coverImage']/img");
|
---|
312 | Assert.assertTrue(GSTestingUtil.isImageLoaded(_driver, coverImage));
|
---|
313 | }
|
---|
314 |
|
---|
315 | @AfterClass
|
---|
316 | public static void destroy()
|
---|
317 | {
|
---|
318 | _driver.quit();
|
---|
319 | }
|
---|
320 | } |
---|