source: gs3-extensions/testing/trunk/src/src/org/greenstone/gsdl3/testing/GSTestingUtil.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: 5.0 KB
Line 
1package org.greenstone.gsdl3.testing;
2
3import java.util.List;
4
5import org.junit.Assert;
6import org.openqa.selenium.By;
7import org.openqa.selenium.NoSuchElementException;
8import org.openqa.selenium.WebDriver;
9import org.openqa.selenium.WebElement;
10import org.openqa.selenium.remote.RemoteWebDriver;
11import org.openqa.selenium.support.ui.ExpectedCondition;
12import org.openqa.selenium.support.ui.WebDriverWait;
13
14public class GSTestingUtil
15{
16 public static boolean isImageLoaded(WebDriver driver, WebElement image)
17 {
18 return (Boolean) ((RemoteWebDriver) driver).executeScript("return arguments[0].complete", image);
19 }
20
21 public static void loginAs(WebDriver driver, String username, String password)
22 {
23 //Click the login button
24 WebElement loginButton = findElementByXPath(driver, "//li[@id='userMenuButton']/a");
25 loginButton.click();
26
27 //Enter the username
28 WebElement usernameInput = findElementByXPath(driver, "//input[@name='username']");
29 usernameInput.sendKeys(username);
30
31 //Enter the password
32 WebElement passwordInput = findElementByXPath(driver, "//input[@name='password']");
33 passwordInput.sendKeys(password);
34
35 //Log in
36 loginButton = findElementByXPath(driver, "//input[@type = 'submit']");
37 loginButton.click();
38 }
39
40 public static void logout(WebDriver driver)
41 {
42 //Open up the user menu
43 WebElement menuButton = findElementByXPath(driver, "//li[@id='userMenuButton']/a");
44 menuButton.click();
45
46 //Wait for the menu to display
47 WebElement menu = findElementByXPath(driver, "//ul[@id='userMenu']");
48 waitForXPath(driver, "//ul[@id='userMenu']");
49 for (int i = 0; i < 30; i++)
50 {
51 if (menu.getAttribute("style").contains("display: block;"))
52 {
53 break;
54 }
55
56 //The menu hasn't displayed in 30 seconds
57 if (i == 29)
58 {
59 Assert.fail();
60 }
61
62 try
63 {
64 Thread.sleep(1000);
65 }
66 catch (Exception ex)
67 {
68 ex.printStackTrace();
69 }
70 }
71
72 WebElement logoutButton = menu.findElement(By.xpath("./a[2]"));
73 logoutButton.click();
74 }
75
76 public static String generateRandomString(int length)
77 {
78 StringBuffer stringToReturn = new StringBuffer("");
79 for (int i = 0; i < length; i++)
80 {
81 int num = (int) (Math.random() * 52);
82 if (num < 26)
83 {
84 char c = (char) ('a' + num);
85 stringToReturn.append(c);
86 }
87 else
88 {
89 char c = (char) ('A' + (num - 26));
90 stringToReturn.append(c);
91 }
92 }
93
94 return stringToReturn.toString();
95 }
96
97 public static void loadClassifierByName(WebDriver driver, String name)
98 {
99 WebElement link = findElementByXPath(driver, "//ul[@id='gs-nav']/li/a[text()='" + name + "']");
100 link.click();
101 }
102
103 public static void loadCollectionByName(WebDriver driver, String collection)
104 {
105 //WebElement demoColLinkElem = findElementByXPath(driver, "//div[contains(concat(' ', normalize-space(@class), ' '), 'collectionLinkText')]/a[descendant::text()='" + collection + "']");
106 //List<WebElement> demoColLinkElems = findElementsByXPath(driver, "//div[contains(concat(' ', normalize-space(@class), ' '), 'collectionLinkText')]/a[descendant::text()='" + collection + "']");
107 //demoColLinkElems.get(0).click();
108
109 WebElement demoColLinkElem = findElementByXPath(driver, "//div[contains(concat(' ', normalize-space(@class), ' '), 'collectionLinkText')][descendant::text()='" + collection + "']");
110 demoColLinkElem.click();
111 }
112
113 public static void performQuickSearch(WebDriver driver, String query, String index)
114 {
115 if (index != null)
116 {
117 WebElement requestedIndex = findElementByXPath(driver, "//div[@id='quicksearcharea']//select[@name='s1.index']//option[@value='" + index + "' or descendant::text()='" + index + "']");
118 requestedIndex.click();
119 }
120
121 WebElement quickSearchInput = findElementByXPath(driver, "//div[@id='quicksearcharea']//input[@name='s1.query']");
122 WebElement quickSearchSubmitButton = findElementByXPath(driver, "//input[@id='quickSearchSubmitButton']");
123
124 quickSearchInput.clear();
125 quickSearchInput.sendKeys(query);
126 quickSearchSubmitButton.click();
127 }
128
129 public static WebElement findElementByXPath(WebDriver driver, String xpath)
130 {
131 WebElement elem;
132 try
133 {
134 elem = driver.findElement(By.xpath(xpath));
135 return elem;
136 }
137 catch (NoSuchElementException ex)
138 {
139 return null;
140 }
141 }
142
143 public static List<WebElement> findElementsByXPath(WebDriver driver, String xpath)
144 {
145 List<WebElement> elems;
146 try
147 {
148 elems = driver.findElements(By.xpath(xpath));
149 return elems;
150 }
151 catch (NoSuchElementException ex)
152 {
153 return null;
154 }
155 }
156
157 public static void waitForXPath(WebDriver driver, String xpath)
158 {
159 final String path = xpath;
160 //Wait for the page to load
161 (new WebDriverWait(driver, 30)).until(new ExpectedCondition<Boolean>()
162 {
163 public Boolean apply(WebDriver d)
164 {
165 try
166 {
167 d.findElement(By.xpath(path));
168 return true;
169 }
170 catch (NoSuchElementException ex)
171 {
172 return false;
173 }
174 }
175 });
176 }
177}
Note: See TracBrowser for help on using the repository browser.