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

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

Changes necessary to get Sam's basic Selenium tests of the lucene-jdbm-demo collection working again: 1. The change to jquery.min.js is that the first hash character now used to be an at-sign which was deprecated and issued warnings. 2. build.xml now points the Selenium testing java code to the geckodriver, required to run newer firefox which no longer comes with a built-in driver for Selenium. 3. Needed to make changes to the testing code, since XPaths in the XML used to find some elements have changed and also the number of documents in the first page of the Titles classifier, and exact wording of certain search results.

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