1 | package org.greenstone.gsdl3.testing;
|
---|
2 |
|
---|
3 | import java.util.List;
|
---|
4 |
|
---|
5 | import org.junit.Assert;
|
---|
6 | import org.openqa.selenium.By;
|
---|
7 | import org.openqa.selenium.NoSuchElementException;
|
---|
8 | import org.openqa.selenium.WebDriver;
|
---|
9 | import org.openqa.selenium.WebElement;
|
---|
10 | import org.openqa.selenium.remote.RemoteWebDriver;
|
---|
11 | import org.openqa.selenium.support.ui.ExpectedCondition;
|
---|
12 | import org.openqa.selenium.support.ui.WebDriverWait;
|
---|
13 |
|
---|
14 | public 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[@id='collectionLinks']/a[descendant::text()='" + collection + "']");
|
---|
106 | demoColLinkElem.click();
|
---|
107 | }
|
---|
108 |
|
---|
109 | public static void performQuickSearch(WebDriver driver, String query, String index)
|
---|
110 | {
|
---|
111 | if (index != null)
|
---|
112 | {
|
---|
113 | WebElement requestedIndex = findElementByXPath(driver, "//div[@id='quicksearcharea']//select[@name='s1.index']//option[@value='" + index + "' or descendant::text()='" + index + "']");
|
---|
114 | requestedIndex.click();
|
---|
115 | }
|
---|
116 |
|
---|
117 | WebElement quickSearchInput = findElementByXPath(driver, "//div[@id='quicksearcharea']//input[@name='s1.query']");
|
---|
118 | WebElement quickSearchSubmitButton = findElementByXPath(driver, "//input[@id='quickSearchSubmitButton']");
|
---|
119 |
|
---|
120 | quickSearchInput.clear();
|
---|
121 | quickSearchInput.sendKeys(query);
|
---|
122 | quickSearchSubmitButton.click();
|
---|
123 | }
|
---|
124 |
|
---|
125 | public static WebElement findElementByXPath(WebDriver driver, String xpath)
|
---|
126 | {
|
---|
127 | WebElement elem;
|
---|
128 | try
|
---|
129 | {
|
---|
130 | elem = driver.findElement(By.xpath(xpath));
|
---|
131 | return elem;
|
---|
132 | }
|
---|
133 | catch (NoSuchElementException ex)
|
---|
134 | {
|
---|
135 | return null;
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | public static List<WebElement> findElementsByXPath(WebDriver driver, String xpath)
|
---|
140 | {
|
---|
141 | List<WebElement> elems;
|
---|
142 | try
|
---|
143 | {
|
---|
144 | elems = driver.findElements(By.xpath(xpath));
|
---|
145 | return elems;
|
---|
146 | }
|
---|
147 | catch (NoSuchElementException ex)
|
---|
148 | {
|
---|
149 | return null;
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | public static void waitForXPath(WebDriver driver, String xpath)
|
---|
154 | {
|
---|
155 | final String path = xpath;
|
---|
156 | //Wait for the page to load
|
---|
157 | (new WebDriverWait(driver, 30)).until(new ExpectedCondition<Boolean>()
|
---|
158 | {
|
---|
159 | public Boolean apply(WebDriver d)
|
---|
160 | {
|
---|
161 | try
|
---|
162 | {
|
---|
163 | d.findElement(By.xpath(path));
|
---|
164 | return true;
|
---|
165 | }
|
---|
166 | catch (NoSuchElementException ex)
|
---|
167 | {
|
---|
168 | return false;
|
---|
169 | }
|
---|
170 | }
|
---|
171 | });
|
---|
172 | }
|
---|
173 | }
|
---|