Skip to content

Commit

Permalink
Creating Forum entry test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
augustocristian committed Jul 29, 2024
1 parent 93a9279 commit d43c522
Showing 1 changed file with 327 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,327 @@
package com.fullteaching.e2e.no_elastest.functional.test.llmexperimentation;

import com.fullteaching.e2e.no_elastest.common.BaseLoggedTest;
import com.fullteaching.e2e.no_elastest.common.CourseNavigationUtilities;
import com.fullteaching.e2e.no_elastest.common.ForumNavigationUtilities;
import com.fullteaching.e2e.no_elastest.common.NavigationUtilities;
import com.fullteaching.e2e.no_elastest.common.exception.ElementNotFoundException;
import com.fullteaching.e2e.no_elastest.utils.Click;
import com.fullteaching.e2e.no_elastest.utils.ParameterLoader;
import com.fullteaching.e2e.no_elastest.utils.Wait;
import giis.retorch.annotations.AccessMode;
import giis.retorch.annotations.Resource;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;

import java.io.IOException;
import java.util.Calendar;
import java.util.List;
import java.util.stream.Stream;

import static com.fullteaching.e2e.no_elastest.common.Constants.*;
import static org.junit.jupiter.api.Assertions.*;

@Tag("e2e")
@DisplayName("E2E tests for FullTeaching Login Session")
class CreateForumEntryTest extends BaseLoggedTest {
protected final String courseName = "Pseudoscientific course for treating the evil eye";
protected final String[] months = {"January", "February", "March", "April",
"May", "June", "July", "August", "September",
"October", "November", "December"};

public CreateForumEntryTest() {
super();
}

public static Stream<Arguments> data() throws IOException {
return ParameterLoader.getTestUsers();
}

/**
* This test get login and create a custom title and content with the current date.
* After that, navigate to course for access the forum section.In the forum creates
* a new entry with the previous created title and content. Secondly, we ensure that
* the entry was created correctly and ensures that there are only one comment that
* corresponds with the body of that entry.
*/
@Resource(resID = "LoginService", replaceable = {})
@AccessMode(resID = "LoginService", concurrency = 10, sharing = true, accessMode = "READONLY")
@Resource(resID = "OpenVidu", replaceable = {"OpenViduMock"})
@AccessMode(resID = "OpenVidu", concurrency = 10, sharing = true, accessMode = "NOACCESS")
@Resource(resID = "Course", replaceable = {"Forum"})
@AccessMode(resID = "Course", concurrency = 1, sharing = false, accessMode = "READWRITE")
@ParameterizedTest
@MethodSource("data")
void forumNewEntryOriginalTest(String mail, String password, String role) {// 48+ 104 + 28 set up +13 lines teardown =193
this.slowLogin(user, mail, password); //24 lines
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
int mYear = calendar.get(Calendar.YEAR);
int mMonth = calendar.get(Calendar.MONTH);
int mDay = calendar.get(Calendar.DAY_OF_MONTH);
int mHour = calendar.get(Calendar.HOUR_OF_DAY);
int mMinute = calendar.get(Calendar.MINUTE);
int mSecond = calendar.get(Calendar.SECOND);
log.info("Setting new entry title and content");
String newEntryTitle = "New Entry Test " + mDay + mMonth + mYear + mHour + mMinute + mSecond;
String newEntryContent = "This is the content written on the " + mDay + " of " + months[mMonth] + ", " + mHour + ":" + mMinute + "," + mSecond;
log.info("Navigating to courses tab");
try {
log.info("Navigating to courses tab");
//navigate to courses.
if (NavigationUtilities.amINotHere(driver, COURSES_URL.replace("__HOST__", HOST))) {
NavigationUtilities.toCoursesHome(driver);//3lines
}
WebElement course = CourseNavigationUtilities.getCourseByName(driver, courseName);//14lines
log.info("Entering the course List");
course.findElement(COURSE_LIST_COURSE_TITLE).click();

Wait.notTooMuch(driver).until(ExpectedConditions.visibilityOfElementLocated(By.id(TABS_DIV_ID)));
log.info("Entering the Forum");
CourseNavigationUtilities.go2Tab(driver, FORUM_ICON); //4lines
assertTrue(ForumNavigationUtilities.isForumEnabled(CourseNavigationUtilities.getTabContent(driver, FORUM_ICON)), "Forum not activated"); //6lines
Wait.waitForPageLoaded(driver);
ForumNavigationUtilities.newEntry(driver, newEntryTitle, newEntryContent); //16lines
//Retorch Modification, this test fails due to the speed of the browser
Wait.waitForPageLoaded(driver);
//Check entry... Flake Here
WebElement newEntry = ForumNavigationUtilities.getEntry(driver, newEntryTitle);//16lines
Wait.waitForPageLoaded(driver);
assertEquals(newEntry.findElement(FORUM_ENTRY_LIST_ENTRY_USER).getText(), userName, "Incorrect user");
Click.element(driver, newEntry.findElement(FORUM_ENTRY_LIST_ENTRY_TITLE));
Wait.notTooMuch(driver).until(ExpectedConditions.visibilityOfElementLocated(FORUM_COMMENT_LIST));
WebElement entryTitleRow = driver.findElement(FORUM_COMMENT_LIST_ENTRY_TITLE);
assertEquals(entryTitleRow.getText().split("\n")[0], newEntryTitle, "Incorrect Entry Title");
assertEquals(entryTitleRow.findElement(FORUM_COMMENT_LIST_ENTRY_USER).getText(), userName, "Incorrect User for Entry");
//first comment should be the inserted while creating the entry
Wait.waitForPageLoaded(driver);
List<WebElement> comments = ForumNavigationUtilities.getComments(driver);
assertFalse(comments.size() < 1, "No comments on the entry");
Wait.notTooMuch(driver).until(ExpectedConditions.visibilityOfElementLocated(FORUM_COMMENT_LIST));
Wait.waitForPageLoaded(driver);
WebElement newComment = comments.get(0);
String commentContent = newComment.findElement(FORUM_COMMENT_LIST_COMMENT_CONTENT).getText();
assertEquals(commentContent, newEntryContent, "Bad content of comment");
Wait.waitForPageLoaded(driver);
String comment = newComment.findElement(FORUM_COMMENT_LIST_COMMENT_USER).getText();
assertEquals(comment, userName, "Bad user in comment");
} catch (ElementNotFoundException notFoundException) {
Assertions.fail("Failed to navigate to course forum:: " + notFoundException.getClass() + ": " + notFoundException.getLocalizedMessage());
}
//Fix Flaky test Navigating to the mainpage to logout...
user.getDriver().get(APP_URL);
}

/**
* This test logs in and creates a new forum entry with a custom title and content.
* After that, it verifies that the entry appears in the forum.
*/
@ParameterizedTest
@MethodSource("data")
@AccessMode(resID = "loginservice", concurrency = 10, sharing = true, accessMode = "READONLY")
@AccessMode(resID = "openvidumock", concurrency = 10, sharing = true, accessMode = "NOACCESS")
@AccessMode(resID = "forum", concurrency = 1, sharing = false, accessMode = "READWRITE")
@AccessMode(resID = "executor", concurrency = 1, accessMode = "READWRITE")
@AccessMode(resID = "webbrowser", concurrency = 1, accessMode = "READWRITE")
@AccessMode(resID = "webserver", concurrency = 1, accessMode = "READWRITE")
void forumNewEntryFS4oTest(String mail, String password, String role) {
// Perform login
this.slowLogin(user, mail, password);

// Get current time for unique entry
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);

try {
// Navigate to courses
NavigationUtilities.toCoursesHome(driver);

// Find and select course
WebElement course = CourseNavigationUtilities.getCourseByName(driver, COURSE_NAME);
course.findElement(COURSE_LIST_COURSE_TITLE).click();
Wait.notTooMuch(driver).until(ExpectedConditions.visibilityOfElementLocated(By.id(TABS_DIV_ID)));

// Go to forum tab
CourseNavigationUtilities.go2Tab(driver, FORUM_ICON);
assertTrue(ForumNavigationUtilities.isForumEnabled(CourseNavigationUtilities.getTabContent(driver, FORUM_ICON)), "Forum not activated");

// Create new entry
String entryTitle = String.format("New Entry Test %d%d%d%d%d%d", day, month, year, hour, minute, second);
String entryContent = String.format("This is the content written on %d of %s, %d:%d:%d", day, MONTHS[month], hour, minute, second);
ForumNavigationUtilities.newEntry(driver, entryTitle, entryContent);

// Verify the new entry
WebElement entry = ForumNavigationUtilities.getEntry(driver, entryTitle);
assertNotNull(entry, "New entry not found");
Click.element(driver, entry.findElement(FORUM_ENTRY_LIST_ENTRY_TITLE));
WebElement entryContentElement = Wait.notTooMuch(driver).until(ExpectedConditions.visibilityOfElementLocated(FORUM_ENTRY_CONTENT));
assertEquals(entryContent, entryContentElement.getText(), "Entry content does not match");

} catch (ElementNotFoundException e) {
fail("Failed to navigate to course forum: " + e.getClass() + ": " + e.getLocalizedMessage());
}
}

@ParameterizedTest
@MethodSource("data")
void createForumEntryCOT4oTest(String mail, String password, String role) {
this.slowLogin(user, mail, password);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
int mYear = calendar.get(Calendar.YEAR);
int mMonth = calendar.get(Calendar.MONTH);
int mDay = calendar.get(Calendar.DAY_OF_MONTH);
int mHour = calendar.get(Calendar.HOUR_OF_DAY);
int mMinute = calendar.get(Calendar.MINUTE);
int mSecond = calendar.get(Calendar.SECOND);

try {
// Navigate to courses
NavigationUtilities.toCoursesHome(driver);
List<String> courses = CourseNavigationUtilities.getCoursesList(driver);
assertTrue(courses.size() > 0, "No courses in the list");

// Select a course and navigate to its forum
WebElement course = CourseNavigationUtilities.getCourseByName(driver, courseName);
course.findElement(COURSE_LIST_COURSE_TITLE).click();
Wait.notTooMuch(driver).until(ExpectedConditions.visibilityOfElementLocated(By.id(TABS_DIV_ID)));
CourseNavigationUtilities.go2Tab(driver, FORUM_ICON);
assertTrue(ForumNavigationUtilities.isForumEnabled(CourseNavigationUtilities.getTabContent(driver, FORUM_ICON)), "Forum not activated");

// Create a new forum entry
String newEntryTitle = "New Entry Test " + mDay + mMonth + mYear + mHour + mMinute + mSecond;
String newEntryContent = "This is the content written on the " + mDay + " of " + months[mMonth] + ", " + mHour + ":" + mMinute + "," + mSecond;
ForumNavigationUtilities.newEntry(driver, newEntryTitle, newEntryContent);

// Verify the new entry
WebElement entry = ForumNavigationUtilities.getEntry(driver, newEntryTitle);
assertNotNull(entry, "New entry not found");
Click.element(driver, entry.findElement(FORUM_ENTRY_LIST_ENTRY_TITLE));
Wait.notTooMuch(driver).until(ExpectedConditions.visibilityOfElementLocated(FORUM_COMMENT_LIST));
String entryContent = entry.findElement(FORUM_ENTRY_LIST_ENTRY_CONTENT).getText();
assertEquals(newEntryContent, entryContent, "Entry content does not match");

} catch (ElementNotFoundException notFoundException) {
fail("Failed to navigate to course forum:: " + notFoundException.getClass() + ": " + notFoundException.getLocalizedMessage());
}
}

@ParameterizedTest
@MethodSource("data")
@AccessMode(resID = "loginservice", concurrency = 10, sharing = true, accessMode = "READONLY")
@AccessMode(resID = "forum", concurrency = 1, sharing = false, accessMode = "READWRITE")
@AccessMode(resID = "executor", concurrency = 1, accessMode = "READWRITE")
@AccessMode(resID = "webbrowser", concurrency = 1, accessMode = "READWRITE")
@AccessMode(resID = "webserver", concurrency = 1, accessMode = "READWRITE")
void forumCreateEntryFS4oMiniTest(String mail, String password, String role) {
// Login with provided user credentials
this.slowLogin(user, mail, password);

// Get the current date and time
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
int mYear = calendar.get(Calendar.YEAR);
int mMonth = calendar.get(Calendar.MONTH);
int mDay = calendar.get(Calendar.DAY_OF_MONTH);
int mHour = calendar.get(Calendar.HOUR_OF_DAY);
int mMinute = calendar.get(Calendar.MINUTE);
int mSecond = calendar.get(Calendar.SECOND);

try {
// Navigate to courses home
NavigationUtilities.toCoursesHome(driver);

// Access the specific course
WebElement course = CourseNavigationUtilities.getCourseByName(driver, courseName);
course.findElement(COURSE_LIST_COURSE_TITLE).click();
Wait.notTooMuch(driver).until(ExpectedConditions.visibilityOfElementLocated(By.id(TABS_DIV_ID)));

// Go to the forum tab
CourseNavigationUtilities.go2Tab(driver, FORUM_ICON);
assertTrue(
ForumNavigationUtilities.isForumEnabled(CourseNavigationUtilities.getTabContent(driver, FORUM_ICON)),
"Forum not activated"
);

// Create a new entry
String newEntryTitle = String.format("New Entry Test %d%d%d%d%d%d", mDay, mMonth, mYear, mHour, mMinute, mSecond);
String newEntryContent = String.format(
"This is the content of the new entry created on %d of %s, %d:%d,%d",
mDay,
months[mMonth],
mHour,
mMinute,
mSecond
);
ForumNavigationUtilities.newEntry(driver, newEntryTitle, newEntryContent);

// Verify the entry is created
List<String> entriesList = ForumNavigationUtilities.getFullEntryList(driver);
assertTrue(
entriesList.contains(newEntryTitle),
"The new entry was not found in the forum entries list"
);

// Optionally, verify the content of the newly created entry
WebElement createdEntry = ForumNavigationUtilities.getEntry(driver, newEntryTitle);
Click.element(driver, createdEntry.findElement(FORUM_ENTRY_LIST_ENTRY_TITLE));
Wait.notTooMuch(driver).until(ExpectedConditions.visibilityOfElementLocated(FORUM_COMMENT_LIST));
String displayedContent = driver.findElement(FORUM_ENTRY_CONTENT).getText();
assertEquals(
newEntryContent,
displayedContent,
"The content of the entry does not match the expected content"
);

} catch (ElementNotFoundException notFoundException) {
fail(
"Failed to navigate to course forum:: " + notFoundException.getClass() + ": " + notFoundException.getLocalizedMessage()
);
}
}

@Test
void createForumEntryCOT4oMiniTest() {
// Step 1: User Login
this.slowLogin(user, "[email protected]", "password123");

// Step 2: Navigate to Courses
NavigationUtilities.toCoursesHome(driver);
Assertions.assertTrue(CourseNavigationUtilities.getCoursesList(driver).size() > 0, "No courses available");

// Step 3: Select a Course
WebElement course = CourseNavigationUtilities.getCourseByName(driver, courseName);
Click.element(driver, course.findElement(By.cssSelector(".course-title"))); // Assuming .course-title is the selector
Wait.notTooMuch(driver).until(ExpectedConditions.visibilityOfElementLocated(By.id("tabsDivId"))); // Replace with actual ID

// Step 4: Navigate to Forum
CourseNavigationUtilities.go2Tab(driver, "forumIcon"); // Replace with actual forum icon identifier
Assertions.assertTrue(ForumNavigationUtilities.isForumEnabled(CourseNavigationUtilities.getTabContent(driver, "forumIcon")), "Forum not activated");

// Step 5: Create a New Forum Entry
Click.element(driver, driver.findElement(By.id("newEntryButton"))); // Replace with actual button ID
String newEntryTitle = "Test Entry Title";
String newEntryContent = "This is a test entry content.";
driver.findElement(By.id("entryTitleField")).sendKeys(newEntryTitle); // Replace with actual field ID
driver.findElement(By.id("entryContentField")).sendKeys(newEntryContent); // Replace with actual field ID
Click.element(driver, driver.findElement(By.id("submitEntryButton"))); // Replace with actual submit button ID

// Step 6: Verify Entry Creation
Wait.notTooMuch(driver).until(ExpectedConditions.visibilityOfElementLocated(By.id("forumEntriesList"))); // Replace with actual ID
Assertions.assertTrue(ForumNavigationUtilities.getFullEntryList(driver).contains(newEntryTitle), "New entry not found in the forum");
}

}

0 comments on commit d43c522

Please sign in to comment.