Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package testRocketChatPackage;
- import java.util.UUID;
- import java.util.concurrent.TimeUnit;
- import org.junit.After;
- import org.junit.AfterClass;
- import org.junit.Before;
- import org.junit.BeforeClass;
- import org.junit.Test;
- import org.openqa.selenium.By;
- import org.openqa.selenium.Dimension;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.WebElement;
- import org.openqa.selenium.chrome.ChromeDriver;
- import org.openqa.selenium.support.ui.ExpectedConditions;
- import org.openqa.selenium.support.ui.WebDriverWait;
- import org.junit.Assert;
- public class ChannelSuite {
- public static WebDriver driver;
- public static String Home = "http://localhost:3000/channel/general";
- public static String loginUser = "test";
- public static String loginPW = "test";
- public static String InvalidChannelName1 = ("North Pole"); // Invalid case when name has spaces
- public static String InvalidChannelName2 = ("North,Pole"); // Invalid case when name has a symbol
- public static String InvalidChannelName3 = (" "); // Invalid case when name is not present
- public static String ValidFriend = ("rocket.cat");
- static String uniqueChannelName = "general";
- static String uniqueFriendName = "Default_Friend";
- private static By userName = By.id("emailOrUsername");
- private static By password = By.id("pass");
- private static By loginButton = By.cssSelector("button");
- //private static By addRoomElement = By.className("add-room");
- private static By addRoomElementActive = By.cssSelector("h3.add-room.active");
- private static By channelCreate = By.id("channel-name");
- //private static By channelCreate = By.cssSelector("input#channel-name.required");
- private static By channelUsers = By.id("channel-members");
- private static By buttonCreate = By.cssSelector("button.primary.save-channel");
- private static By buttonCancel = By.cssSelector("button.cancel-channel");
- private static By channelLocation = By.cssSelector("a.open-room");
- private static By moreChannels = By.cssSelector("button.more.more-channels");
- private static By allChannels = By.cssSelector("button.button.all");
- private static By optionAll = By.cssSelector("select#sort.c-select");
- private static By optionJoined = By.cssSelector("option.joined");
- private static By optionSort = By.cssSelector("select#sort-channels.c-select");
- private static By optionPrivacy = By.cssSelector("select#channel-type.c-select");
- private static By channelSearch = By.cssSelector("input#channel-search.search");
- private static By channelFound = By.cssSelector("a.channel-link");
- private static By textFriend = By.cssSelector("textarea.input-message.autogrow-short");
- private static By sendMessageButton = By.cssSelector("i.icon-paper-plane");
- private static By leaveArrow = By.cssSelector("i.icon-logout.leave-room");
- private static By PopUpWindow = By.cssSelector("div.sweet-alert.showSweetAlert.visible");
- private static By PopUpYes = By.className("confirm");
- private static By buttonOK = By.cssSelector("button.confirm");
- private static By leftPanel = By.cssSelector("div.rooms-list");
- private static By rightPanelOptions = By.cssSelector("div.tab-button");
- private static By rightPanelClose = By.cssSelector("div.tab-button.active");
- private static By trashButt = By.cssSelector("button.button.danger.delete");
- private static boolean detectElement(By element) {
- boolean Elementdetected = driver.findElements(element).size() > 0;
- if (Elementdetected) {
- return true;
- }
- else {
- return false;
- }
- }
- private static void waitForLeftPanelToLoadToClickCreateChannel() {
- new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(addRoomElementActive));
- WebElement dmElement = driver.findElement(addRoomElementActive);
- dmElement.click();
- }
- private static void generateUniqueName(){
- uniqueChannelName = UUID.randomUUID().toString().substring(0, 8);
- uniqueFriendName = UUID.randomUUID().toString().substring(0, 8);
- }
- private static void createChannelWithUser(String channelName, String friendName) {
- waitForLeftPanelToLoadToClickCreateChannel();
- if ((channelName == "unique") && (friendName == "unique")) {
- generateUniqueName();
- }
- else if (channelName == "unique") {
- generateUniqueName();
- uniqueFriendName = friendName;
- }
- else if (friendName == "unique") {
- generateUniqueName();
- uniqueChannelName = channelName;
- }
- else {
- uniqueChannelName = channelName;
- uniqueFriendName = friendName;
- }
- driver.findElement(channelCreate).sendKeys(uniqueChannelName);
- driver.findElement(channelUsers).sendKeys(uniqueFriendName);
- driver.findElement(buttonCreate).click();
- }
- private static void creatingValidChannels(String validChannelName, String friendName) {
- createChannelWithUser("unique", ValidFriend);
- System.out.println(uniqueChannelName + " has been created.");
- }
- private static void creatingInvalidChannels(String invalidChannelName, String friendName) {
- createChannelWithUser(invalidChannelName, friendName);
- invalidChannelClickCancel(invalidChannelName);
- System.out.println(invalidChannelName + " is not a valid channel name.");
- }
- private static void typeChannelNameWhenSearchChannelPanelIsOpened(String channelName) throws Exception {
- new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(channelSearch));
- driver.findElement(channelSearch).sendKeys(channelName);
- Thread.sleep(1500);
- driver.findElement(channelFound).click();
- }
- private static void searchChannelsUsingMoreChannels(String channelName) throws Exception {
- waitForLeftPanelToLoadToClickCreateChannel();
- Thread.sleep(2000);
- driver.findElement(moreChannels).click();
- //Thread.sleep(2000);
- typeChannelNameWhenSearchChannelPanelIsOpened(channelName);
- }
- private static void searchChannelsUsingAllChannels(String channelName) throws Exception {
- waitForLeftPanelToLoadToClickCreateChannel();
- driver.findElement(addRoomElementActive).click();
- System.out.println("Search 1");
- //new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(allChannels));
- Thread.sleep(2000);
- driver.findElement(allChannels).click();
- System.out.println("Search 2");
- typeChannelNameWhenSearchChannelPanelIsOpened(channelName);
- }
- private static void searchChannelsUsingLeftPanelListOfChannels(String channelName) {
- waitForLeftPanelToLoadToClickCreateChannel();
- int size = driver.findElements(channelLocation).size();
- //System.out.print(size);
- int i;
- for (i = 0; i < size; i++) {
- WebElement channelArrayElement = driver.findElements(channelLocation).get(i);
- String channelAttribute = channelArrayElement.getAttribute("title");
- if (channelAttribute == channelName) {
- channelArrayElement.click();
- break;
- }
- }
- }
- private static void invalidChannelClickCancel(String channelName) {
- new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(buttonCancel));
- driver.findElement(buttonCancel).click();
- System.out.println(channelName + " is not a valid channel name.");
- }
- private static void clicksAchannelToMakeItActiveIfNot() {
- if (!(detectElement(addRoomElementActive))) {
- WebElement firstChannel = driver.findElement(channelLocation);
- firstChannel.click();
- }
- }
- public void LeaveOrDeleteChannel() throws Exception {
- //Leave Channel - Must NOT be the OWNER of the channel
- // WebElement channelLeaveIcon = driver.findElements(leaveArrow).get(0);
- // channelLeaveIcon.click();
- //
- // driver.findElement(PopUpYes).click();
- //
- //
- //Window will pop up if you are the OWNER of the channel
- // if (detectElement(PopUpWindow)) {
- //
- // driver.findElement(buttonOK).click();
- //
- // new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(rightPanelOptions));
- //Delete Channel - Must be the OWNER to delete a channel
- WebElement infoIconButt = driver.findElements(rightPanelOptions).get(1);
- infoIconButt.click();
- Thread.sleep(1000);
- driver.findElement(trashButt).click();
- Thread.sleep(1500);
- driver.findElement(PopUpYes).click();
- Thread.sleep(2500);
- driver.findElement(rightPanelClose).click();
- System.out.print("Channel has been deleted.");
- // }
- }
- /*
- * TEST Table of Contents:
- *
- *
- * CREATE CHANNEL (CHANNEL NAME / FRIEND NAME):
- * ** A Valid Channel name consists of ONLY letters, numbers, hyphens, and underscores**
- *
- * 1. Valid / Valid
- * 2. Valid / Invalid
- * 3. Invalid1 / Valid
- * 4. Invalid1 / Invalid
- * 5. Invalid2 / Valid
- * 6. Invalid2 / Invalid
- * 7. Invalid3 / Valid
- * 8. Invalid3 / Invalid
- * 9. Duplicated Channel (Invalid)
- *
- * ***NOTE: Invalid USER will STILL create the channel if the channel name is valid.***
- *
- *
- * Channel Search and Messaging
- *
- * 10. Method 1
- * 11. Method 2 // @TODO - CANNOT DETECT "All Channels" BUTTON
- * 12. Method 3
- *
- * Other
- *
- * 13. Creating Different Channel Types // @TODO Not Yet Implemented
- *
- */
- @BeforeClass
- public static void setupTest() throws Exception{
- // Sets webdriver to Chrome
- System.setProperty(
- "webdriver.chrome.driver",
- "/home/osboxes/Documents/Selenium Library/chromedriver.exe"
- );
- driver = new ChromeDriver();
- driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
- // Logging In
- driver.get(Home);
- //driver.manage().window().maximize();
- driver.findElement(userName).sendKeys(loginUser);
- driver.findElement(password).sendKeys(loginPW);
- driver.findElement(loginButton).click();
- //Thread.sleep(500);
- }
- @Before
- public void navLogin() {
- //driver.get(Home);
- clicksAchannelToMakeItActiveIfNot();
- }
- @Test // 1
- public void createValidChannelWithValidFriend() throws Exception {
- creatingValidChannels("unique", ValidFriend);
- LeaveOrDeleteChannel();
- }
- @Test // 2
- public void createValidChannelWithInvalidFriend() throws Exception {
- creatingValidChannels("unique", "unique");
- LeaveOrDeleteChannel();
- }
- @Test // 3
- public void createInvalidChannel1WithValidFriend() {
- creatingInvalidChannels(InvalidChannelName1, ValidFriend);
- }
- @Test // 4
- public void createInvalidChannel1WithInvalidFriend() {
- creatingInvalidChannels(InvalidChannelName1, "unique");
- }
- @Test // 5
- public void createInvalidChannel2WithValidFriend() {
- creatingInvalidChannels(InvalidChannelName2, ValidFriend);
- }
- @Test // 6
- public void createInvalidChannel2WithInvalidFriend() {
- creatingInvalidChannels(InvalidChannelName2, "unique");
- }
- @Test // 7
- public void createInvalidChannel3WithValidFriend() {
- creatingInvalidChannels(InvalidChannelName3, ValidFriend);
- }
- @Test // 8
- public void createInvalidChannel3WithInvalidFriend() {
- creatingInvalidChannels(InvalidChannelName3, "unique");
- }
- @Test // 9
- public void createDuplicateChannelFail() throws Exception {
- creatingValidChannels("unique", ValidFriend);
- new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(addRoomElementActive));
- WebElement dmElement = driver.findElement(addRoomElementActive);
- Thread.sleep(1000);
- dmElement.click();
- creatingInvalidChannels(uniqueChannelName, ValidFriend);
- // new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(channelCreate));
- // driver.findElement(channelCreate).sendKeys(uniqueChannelName);
- // driver.findElement(channelUsers).sendKeys(ValidFriend);
- // driver.findElement(buttonCreate).click();
- //
- // invalidChannelClickCancel(uniqueChannelName);
- System.out.println("The channel " + uniqueChannelName + " already exists.");
- LeaveOrDeleteChannel();
- }
- @Test // 10
- public void channelSearchAndMessagingMethod1() throws Exception {
- creatingValidChannels("unique", ValidFriend);
- //Search Method 1
- clicksAchannelToMakeItActiveIfNot();
- searchChannelsUsingMoreChannels(uniqueChannelName);
- new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(textFriend));
- driver.findElement(textFriend).sendKeys("Hello " + uniqueChannelName + "!");
- driver.findElement(sendMessageButton).click();
- LeaveOrDeleteChannel();
- }
- //@TODO @Test // 11
- public void channelSearchAndMessagingMethod2() throws Exception {
- creatingValidChannels("unique", ValidFriend);
- //Search Method 2
- clicksAchannelToMakeItActiveIfNot();
- searchChannelsUsingAllChannels(uniqueChannelName); //UNABLE TO DETECT THE "All Channels" BUTTON
- //System.out.println("Test Passed");
- new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(textFriend));
- //System.out.println("Test 2 Passed");
- driver.findElement(textFriend).sendKeys("Hello " + uniqueChannelName + " !");
- driver.findElement(sendMessageButton).click();
- LeaveOrDeleteChannel();
- }
- @Test // 12
- public void channelSearchAndMessagingMethod3() throws Exception {
- creatingValidChannels("unique", ValidFriend);
- //Search Method 3
- clicksAchannelToMakeItActiveIfNot();
- searchChannelsUsingLeftPanelListOfChannels(uniqueChannelName);
- driver.findElement(textFriend).sendKeys("Hello " + uniqueChannelName + " !");
- driver.findElement(sendMessageButton).click();
- LeaveOrDeleteChannel();
- }
- //@TODO @Test // 13
- public void creatingDifferentChannelTypes() throws Exception {
- clicksAchannelToMakeItActiveIfNot();
- Thread.sleep(2000);
- waitForLeftPanelToLoadToClickCreateChannel();
- //driver.findElement(optionAll).click();
- //driver.findElement(optionSort).click();
- //driver.findElement(optionSort).click();
- //driver.findElement(optionPrivacy).click();
- //new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(rightPanelOptions));
- LeaveOrDeleteChannel();
- }
- @AfterClass
- public static void endTesting(){
- driver.close();
- driver.quit();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement