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

Last change on this file since 32672 was 32672, checked in by ak19, 5 years ago

Tidying up after previous commit

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