· 9 years ago · Dec 28, 2016, 09:54 PM
1package testRocketChatPackage;
2
3import java.util.UUID;
4import java.util.concurrent.TimeUnit;
5
6import org.junit.After;
7import org.junit.AfterClass;
8import org.junit.Before;
9import org.junit.BeforeClass;
10import org.junit.Test;
11import org.openqa.selenium.By;
12import org.openqa.selenium.Dimension;
13import org.openqa.selenium.WebDriver;
14import org.openqa.selenium.WebElement;
15import org.openqa.selenium.chrome.ChromeDriver;
16import org.openqa.selenium.support.ui.ExpectedConditions;
17import org.openqa.selenium.support.ui.WebDriverWait;
18
19import org.junit.Assert;
20
21
22
23
24public class ChannelSuite {
25
26
27 public static WebDriver driver;
28
29 public static String Home = "http://localhost:3000/channel/general";
30 public static String loginUser = "test";
31 public static String loginPW = "test";
32
33 public static String InvalidChannelName1 = ("North Pole"); // Invalid case when name has spaces
34 public static String InvalidChannelName2 = ("North,Pole"); // Invalid case when name has a symbol
35 public static String InvalidChannelName3 = (" "); // Invalid case when name is not present
36 public static String ValidFriend = ("rocket.cat");
37
38 static String uniqueChannelName = "general";
39 static String uniqueFriendName = "Default_Friend";
40
41 private static By userName = By.id("emailOrUsername");
42 private static By password = By.id("pass");
43 private static By loginButton = By.cssSelector("button");
44
45 //private static By addRoomElement = By.className("add-room");
46 private static By addRoomElementActive = By.cssSelector("h3.add-room.active");
47
48 private static By channelCreate = By.id("channel-name");
49 //private static By channelCreate = By.cssSelector("input#channel-name.required");
50 private static By channelUsers = By.id("channel-members");
51 private static By buttonCreate = By.cssSelector("button.primary.save-channel");
52 private static By buttonCancel = By.cssSelector("button.cancel-channel");
53 private static By channelLocation = By.cssSelector("a.open-room");
54 private static By moreChannels = By.cssSelector("button.more.more-channels");
55 private static By allChannels = By.cssSelector("button.button.all");
56
57 private static By optionAll = By.cssSelector("select#sort.c-select");
58 private static By optionJoined = By.cssSelector("option.joined");
59 private static By optionSort = By.cssSelector("select#sort-channels.c-select");
60 private static By optionPrivacy = By.cssSelector("select#channel-type.c-select");
61
62 private static By channelSearch = By.cssSelector("input#channel-search.search");
63 private static By channelFound = By.cssSelector("a.channel-link");
64
65 private static By textFriend = By.cssSelector("textarea.input-message.autogrow-short");
66 private static By sendMessageButton = By.cssSelector("i.icon-paper-plane");
67
68 private static By leaveArrow = By.cssSelector("i.icon-logout.leave-room");
69 private static By PopUpWindow = By.cssSelector("div.sweet-alert.showSweetAlert.visible");
70 private static By PopUpYes = By.className("confirm");
71 private static By buttonOK = By.cssSelector("button.confirm");
72
73 private static By leftPanel = By.cssSelector("div.rooms-list");
74 private static By rightPanelOptions = By.cssSelector("div.tab-button");
75 private static By rightPanelClose = By.cssSelector("div.tab-button.active");
76 private static By trashButt = By.cssSelector("button.button.danger.delete");
77
78
79
80 private static boolean detectElement(By element) {
81
82 boolean Elementdetected = driver.findElements(element).size() > 0;
83 if (Elementdetected) {
84 return true;
85 }
86 else {
87 return false;
88 }
89 }
90
91 private static void waitForLeftPanelToLoadToClickCreateChannel() {
92
93 new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(addRoomElementActive));
94 WebElement dmElement = driver.findElement(addRoomElementActive);
95 dmElement.click();
96 }
97
98 private static void generateUniqueName(){
99
100 uniqueChannelName = UUID.randomUUID().toString().substring(0, 8);
101 uniqueFriendName = UUID.randomUUID().toString().substring(0, 8);
102 }
103
104 private static void createChannelWithUser(String channelName, String friendName) {
105
106 waitForLeftPanelToLoadToClickCreateChannel();
107
108 if ((channelName == "unique") && (friendName == "unique")) {
109 generateUniqueName();
110 }
111 else if (channelName == "unique") {
112 generateUniqueName();
113 uniqueFriendName = friendName;
114 }
115 else if (friendName == "unique") {
116 generateUniqueName();
117 uniqueChannelName = channelName;
118 }
119 else {
120 uniqueChannelName = channelName;
121 uniqueFriendName = friendName;
122 }
123
124 driver.findElement(channelCreate).sendKeys(uniqueChannelName);
125 driver.findElement(channelUsers).sendKeys(uniqueFriendName);
126 driver.findElement(buttonCreate).click();
127
128 }
129
130 private static void creatingValidChannels(String validChannelName, String friendName) {
131
132 createChannelWithUser("unique", ValidFriend);
133 System.out.println(uniqueChannelName + " has been created.");
134 }
135
136 private static void creatingInvalidChannels(String invalidChannelName, String friendName) {
137
138 createChannelWithUser(invalidChannelName, friendName);
139 invalidChannelClickCancel(invalidChannelName);
140 System.out.println(invalidChannelName + " is not a valid channel name.");
141 }
142
143 private static void typeChannelNameWhenSearchChannelPanelIsOpened(String channelName) throws Exception {
144
145 new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(channelSearch));
146 driver.findElement(channelSearch).sendKeys(channelName);
147 Thread.sleep(1500);
148 driver.findElement(channelFound).click();
149
150 }
151
152 private static void searchChannelsUsingMoreChannels(String channelName) throws Exception {
153
154 waitForLeftPanelToLoadToClickCreateChannel();
155 Thread.sleep(2000);
156
157 driver.findElement(moreChannels).click();
158 //Thread.sleep(2000);
159
160 typeChannelNameWhenSearchChannelPanelIsOpened(channelName);
161
162 }
163
164 private static void searchChannelsUsingAllChannels(String channelName) throws Exception {
165
166 waitForLeftPanelToLoadToClickCreateChannel();
167 driver.findElement(addRoomElementActive).click();
168
169 System.out.println("Search 1");
170
171 //new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(allChannels));
172 Thread.sleep(2000);
173 driver.findElement(allChannels).click();
174
175 System.out.println("Search 2");
176
177 typeChannelNameWhenSearchChannelPanelIsOpened(channelName);
178
179 }
180
181 private static void searchChannelsUsingLeftPanelListOfChannels(String channelName) {
182
183 waitForLeftPanelToLoadToClickCreateChannel();
184 int size = driver.findElements(channelLocation).size();
185 //System.out.print(size);
186 int i;
187
188 for (i = 0; i < size; i++) {
189 WebElement channelArrayElement = driver.findElements(channelLocation).get(i);
190 String channelAttribute = channelArrayElement.getAttribute("title");
191 if (channelAttribute == channelName) {
192 channelArrayElement.click();
193 break;
194 }
195 }
196 }
197
198 private static void invalidChannelClickCancel(String channelName) {
199
200 new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(buttonCancel));
201 driver.findElement(buttonCancel).click();
202 System.out.println(channelName + " is not a valid channel name.");
203
204 }
205
206 private static void clicksAchannelToMakeItActiveIfNot() {
207
208 if (!(detectElement(addRoomElementActive))) {
209 WebElement firstChannel = driver.findElement(channelLocation);
210 firstChannel.click();
211 }
212 }
213
214 public void LeaveOrDeleteChannel() throws Exception {
215
216 //Leave Channel - Must NOT be the OWNER of the channel
217// WebElement channelLeaveIcon = driver.findElements(leaveArrow).get(0);
218// channelLeaveIcon.click();
219//
220// driver.findElement(PopUpYes).click();
221//
222//
223 //Window will pop up if you are the OWNER of the channel
224// if (detectElement(PopUpWindow)) {
225//
226// driver.findElement(buttonOK).click();
227//
228// new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(rightPanelOptions));
229
230
231 //Delete Channel - Must be the OWNER to delete a channel
232 WebElement infoIconButt = driver.findElements(rightPanelOptions).get(1);
233 infoIconButt.click();
234 Thread.sleep(1000);
235
236 driver.findElement(trashButt).click();
237 Thread.sleep(1500);
238
239 driver.findElement(PopUpYes).click();
240 Thread.sleep(2500);
241
242 driver.findElement(rightPanelClose).click();
243 System.out.print("Channel has been deleted.");
244// }
245 }
246
247/*
248 * TEST Table of Contents:
249 *
250 *
251 * CREATE CHANNEL (CHANNEL NAME / FRIEND NAME):
252 * ** A Valid Channel name consists of ONLY letters, numbers, hyphens, and underscores**
253 *
254 * 1. Valid / Valid
255 * 2. Valid / Invalid
256 * 3. Invalid1 / Valid
257 * 4. Invalid1 / Invalid
258 * 5. Invalid2 / Valid
259 * 6. Invalid2 / Invalid
260 * 7. Invalid3 / Valid
261 * 8. Invalid3 / Invalid
262 * 9. Duplicated Channel (Invalid)
263 *
264 * ***NOTE: Invalid USER will STILL create the channel if the channel name is valid.***
265 *
266 *
267 * Channel Search and Messaging
268 *
269 * 10. Method 1
270 * 11. Method 2 // @TODO - CANNOT DETECT "All Channels" BUTTON
271 * 12. Method 3
272 *
273 * Other
274 *
275 * 13. Creating Different Channel Types // @TODO Not Yet Implemented
276 *
277 */
278
279
280 @BeforeClass
281 public static void setupTest() throws Exception{
282
283 // Sets webdriver to Chrome
284 System.setProperty(
285 "webdriver.chrome.driver",
286 "/home/osboxes/Documents/Selenium Library/chromedriver.exe"
287 );
288
289 driver = new ChromeDriver();
290 driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
291
292 // Logging In
293 driver.get(Home);
294 //driver.manage().window().maximize();
295 driver.findElement(userName).sendKeys(loginUser);
296 driver.findElement(password).sendKeys(loginPW);
297 driver.findElement(loginButton).click();
298 //Thread.sleep(500);
299 }
300
301 @Before
302 public void navLogin() {
303
304 //driver.get(Home);
305 clicksAchannelToMakeItActiveIfNot();
306
307 }
308
309 @Test // 1
310 public void createValidChannelWithValidFriend() throws Exception {
311
312 creatingValidChannels("unique", ValidFriend);
313 LeaveOrDeleteChannel();
314
315 }
316
317 @Test // 2
318 public void createValidChannelWithInvalidFriend() throws Exception {
319
320 creatingValidChannels("unique", "unique");
321 LeaveOrDeleteChannel();
322
323 }
324
325 @Test // 3
326 public void createInvalidChannel1WithValidFriend() {
327
328 creatingInvalidChannels(InvalidChannelName1, ValidFriend);
329
330 }
331
332 @Test // 4
333 public void createInvalidChannel1WithInvalidFriend() {
334
335 creatingInvalidChannels(InvalidChannelName1, "unique");
336
337 }
338
339 @Test // 5
340 public void createInvalidChannel2WithValidFriend() {
341
342 creatingInvalidChannels(InvalidChannelName2, ValidFriend);
343
344 }
345
346 @Test // 6
347 public void createInvalidChannel2WithInvalidFriend() {
348
349 creatingInvalidChannels(InvalidChannelName2, "unique");
350
351 }
352
353 @Test // 7
354 public void createInvalidChannel3WithValidFriend() {
355
356 creatingInvalidChannels(InvalidChannelName3, ValidFriend);
357
358 }
359
360 @Test // 8
361 public void createInvalidChannel3WithInvalidFriend() {
362
363 creatingInvalidChannels(InvalidChannelName3, "unique");
364
365 }
366
367 @Test // 9
368 public void createDuplicateChannelFail() throws Exception {
369
370 creatingValidChannels("unique", ValidFriend);
371
372 new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(addRoomElementActive));
373 WebElement dmElement = driver.findElement(addRoomElementActive);
374 Thread.sleep(1000);
375 dmElement.click();
376
377 creatingInvalidChannels(uniqueChannelName, ValidFriend);
378// new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(channelCreate));
379// driver.findElement(channelCreate).sendKeys(uniqueChannelName);
380// driver.findElement(channelUsers).sendKeys(ValidFriend);
381// driver.findElement(buttonCreate).click();
382//
383// invalidChannelClickCancel(uniqueChannelName);
384 System.out.println("The channel " + uniqueChannelName + " already exists.");
385
386 LeaveOrDeleteChannel();
387
388 }
389
390 @Test // 10
391 public void channelSearchAndMessagingMethod1() throws Exception {
392
393 creatingValidChannels("unique", ValidFriend);
394
395 //Search Method 1
396 clicksAchannelToMakeItActiveIfNot();
397 searchChannelsUsingMoreChannels(uniqueChannelName);
398
399 new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(textFriend));
400
401 driver.findElement(textFriend).sendKeys("Hello " + uniqueChannelName + "!");
402 driver.findElement(sendMessageButton).click();
403
404 LeaveOrDeleteChannel();
405
406 }
407
408 //@TODO @Test // 11
409 public void channelSearchAndMessagingMethod2() throws Exception {
410
411 creatingValidChannels("unique", ValidFriend);
412
413 //Search Method 2
414 clicksAchannelToMakeItActiveIfNot();
415 searchChannelsUsingAllChannels(uniqueChannelName); //UNABLE TO DETECT THE "All Channels" BUTTON
416
417 //System.out.println("Test Passed");
418
419 new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(textFriend));
420
421 //System.out.println("Test 2 Passed");
422
423 driver.findElement(textFriend).sendKeys("Hello " + uniqueChannelName + " !");
424 driver.findElement(sendMessageButton).click();
425
426 LeaveOrDeleteChannel();
427
428 }
429
430 @Test // 12
431 public void channelSearchAndMessagingMethod3() throws Exception {
432
433 creatingValidChannels("unique", ValidFriend);
434
435 //Search Method 3
436 clicksAchannelToMakeItActiveIfNot();
437 searchChannelsUsingLeftPanelListOfChannels(uniqueChannelName);
438
439 driver.findElement(textFriend).sendKeys("Hello " + uniqueChannelName + " !");
440 driver.findElement(sendMessageButton).click();
441
442 LeaveOrDeleteChannel();
443
444 }
445
446 //@TODO @Test // 13
447 public void creatingDifferentChannelTypes() throws Exception {
448
449 clicksAchannelToMakeItActiveIfNot();
450 Thread.sleep(2000);
451 waitForLeftPanelToLoadToClickCreateChannel();
452
453 //driver.findElement(optionAll).click();
454 //driver.findElement(optionSort).click();
455 //driver.findElement(optionSort).click();
456 //driver.findElement(optionPrivacy).click();
457
458 //new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(rightPanelOptions));
459
460 LeaveOrDeleteChannel();
461
462 }
463
464 @AfterClass
465 public static void endTesting(){
466 driver.close();
467 driver.quit();
468 }
469
470}