Skip to content

Commit

Permalink
Some changes to try to solve problems with threads
Browse files Browse the repository at this point in the history
  • Loading branch information
augustocristian committed Aug 3, 2024
1 parent 93a9279 commit 0d26b62
Show file tree
Hide file tree
Showing 11 changed files with 382 additions and 491 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,15 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import static com.fullteaching.e2e.no_elastest.common.Constants.*;
import static java.lang.invoke.MethodHandles.lookup;
import static org.openqa.selenium.logging.LogType.BROWSER;
import static org.slf4j.LoggerFactory.getLogger;

public class BaseLoggedTest {

public static final String CHROME = "chrome";
// For use another host
//protected static final String host= SetUp.getHost();
public static final String FIREFOX = "firefox";
public static final String EDGE = "edge";
public static final Logger log = LoggerFactory.getLogger(BaseLoggedTest.class);
Expand All @@ -41,7 +38,6 @@ public class BaseLoggedTest {
public static String TEACHER_BROWSER;
public static String STUDENT_BROWSER;
public static String BROWSER_NAME;
public static String course_title;
protected static String HOST = LOCALHOST;
protected static String userName;
protected static String userMail;
Expand All @@ -52,7 +48,9 @@ public class BaseLoggedTest {
protected final static int DEPTH = 3;

public WebDriver driver;
protected BrowserUser user;
protected BrowserUser teacher;
protected BrowserUser student;
protected List<BrowserUser> studentBrowserUserList;

public BaseLoggedTest() {
}
Expand All @@ -66,7 +64,7 @@ static void setupAll() { // 28 lines
// Load a properties file for reading
properties.load(new FileInputStream("src/test/resources/inputs/test.properties"));
} catch (IOException ex) {
ex.printStackTrace(); // Consider logging the exception instead of printing the stack trace
log.error("Properties file could not be loaded");
}

// Check if running outside ElasTest
Expand All @@ -85,7 +83,7 @@ static void setupAll() { // 28 lines
TJOB_NAME = envTJobName;
PORT = envPort;
APP_URL = envUrl + TJOB_NAME + ":" + PORT + "/";
log.debug("The URL is" + APP_URL);
log.debug("The URL is {}" , APP_URL);
HOST = APP_URL;
} else {
// Check if app.url system property is defined
Expand All @@ -110,14 +108,13 @@ static void setupAll() { // 28 lines

@BeforeEach
void setup(TestInfo info) { //65 lines
if (info.getTestMethod().isPresent()) {
TEST_NAME = info.getTestMethod().get().getName();
}
log.info("##### Start test: " + TEST_NAME);
log.info("##### Start test: {}" , info.getDisplayName());
TJOB_NAME = System.getProperty("dirtarget");

user = setupBrowser("chrome", TJOB_NAME + "_" + TEST_NAME, userMail, WAIT_SECONDS);
driver = user.getDriver();
teacher = setupBrowser("chrome", TJOB_NAME + "_" + info.getDisplayName(), "Teacher", WAIT_SECONDS);

driver = teacher.getDriver();

}

protected BrowserUser setupBrowser(String browser, String testName,
Expand Down Expand Up @@ -161,19 +158,41 @@ protected BrowserUser setupBrowser(String browser, String testName,

@AfterEach
void tearDown(TestInfo testInfo) { //13 lines
if (this.teacher != null) {
log.info("##### Finish test: {} - Driver {}", testInfo.getDisplayName(), this.teacher.getDriver());
log.info("Browser console at the end of the test");
LogEntries logEntries = teacher.getDriver().manage().logs().get(BROWSER);
logEntries.forEach(entry -> log.info("[{}] {} {}",
new Date(entry.getTimestamp()), entry.getLevel(),
entry.getMessage()));
if (this.teacher.isOnSession()) {
this.logout(this.teacher);
}

this.teacher.dispose();
}

if (user != null) {
log.info("##### Finish test: {} - Driver {}", TEST_NAME, this.user.getDriver());
if (this.student != null) {
log.info("##### Finish test: {} - Driver {}", testInfo.getDisplayName(), this.student.getDriver());
log.info("Browser console at the end of the test");
LogEntries logEntries = user.getDriver().manage().logs().get(BROWSER);
logEntries.forEach((entry) -> log.info("[{}] {} {}",
LogEntries logEntries = student.getDriver().manage().logs().get(BROWSER);
logEntries.forEach(entry -> log.info("[{}] {} {}",
new Date(entry.getTimestamp()), entry.getLevel(),
entry.getMessage()));
if (user.isOnSession()) {
this.logout(user);
//TO-DO- ERROR with the logout
if (this.student.isOnSession()) {
this.logout(student);
}

user.dispose();
student.dispose();}
//Logout and exit list of Students
if (this.studentBrowserUserList!=null) {
for (BrowserUser memberStudent : this.studentBrowserUserList) {
if (memberStudent.isOnSession()) {
this.logout(memberStudent);
}
memberStudent.dispose();
}
}
}

Expand Down Expand Up @@ -233,10 +252,9 @@ private void login(BrowserUser user, String userEmail, String userPass,
}

protected void logout(BrowserUser user) { //43 lines
// log.info("Logging out {}", user.getClientData());
log.info("Logging out {}", user.getClientData());

if (user.getDriver().findElements(By.cssSelector("#fixed-icon"))
.size() > 0) {
if (!user.getDriver().findElements(By.cssSelector("#fixed-icon")).isEmpty()) {
// Get out of video session page
if (!isClickable("#exit-icon", user)) { // Side menu not opened
user.getDriver().findElement(By.cssSelector("#fixed-icon"))
Expand Down Expand Up @@ -347,7 +365,7 @@ protected void waitSeconds(int seconds) {
try {
Thread.sleep(1000L * seconds);
} catch (InterruptedException e) {
e.printStackTrace();
log.error("Thread.sleep interrupted");
}

}
Expand All @@ -372,14 +390,14 @@ public String getUserName(BrowserUser user, boolean goBack, String host) throws

WebElement name_placeholder = Wait.notTooMuch(user.getDriver()).until(ExpectedConditions.visibilityOfElementLocated(By.xpath(USERNAME_XPATH)));

String userName = name_placeholder.getText().trim();
String userNameTag = name_placeholder.getText().trim();

if (goBack) {
user.getDriver().navigate().back();
}
//Check if the username is the expected
log.info("[END] getUserName");
return userName;
return userNameTag;

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import giis.retorch.annotations.AccessMode;
import giis.retorch.annotations.Resource;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.params.ParameterizedTest;
Expand All @@ -23,17 +22,14 @@
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.slf4j.Logger;

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 java.lang.invoke.MethodHandles.lookup;
import static org.junit.jupiter.api.Assertions.*;
import static org.slf4j.LoggerFactory.getLogger;


@Tag("e2e")
Expand Down Expand Up @@ -66,15 +62,16 @@ public static Stream<Arguments> data() throws IOException {
@AccessMode(resID = "OpenVidu", concurrency = 10, sharing = true, accessMode = "NOACCESS")
@Resource(resID = "Course", replaceable = {"Forum"})
@AccessMode(resID = "Course", concurrency = 10, sharing = true, accessMode = "READONLY")
@DisplayName("studentCourseMainTest")
@ParameterizedTest
@MethodSource("data")
void forumLoadEntriesTest(String mail, String password, String role) { //47lines +115 +28 set up +13 lines teardown =203
this.slowLogin(user, mail, password);//24 lines
this.slowLogin(teacher, mail, password);//24 lines
try {
//navigate to courses.
NavigationUtilities.toCoursesHome(driver);//3lines
List<String> courses = CourseNavigationUtilities.getCoursesList(driver);//13lines
assertTrue(courses.size() > 0, "No courses in the list");
assertFalse(courses.isEmpty(), "No courses in the list");
//find course with forum activated
boolean activated_forum_on_some_test = false;
boolean has_comments = false;
Expand All @@ -92,7 +89,7 @@ void forumLoadEntriesTest(String mail, String password, String role) { //47lines
log.info("Loading the entries list");
//Load list of entries
List<String> entries_list = ForumNavigationUtilities.getFullEntryList(driver);//6lines
if (entries_list.size() > 0) {
if (!entries_list.isEmpty()) {
//Go into first entry
for (String entry_name : entries_list) {
log.info("Checking the entry with name: {}", entry_name);
Expand All @@ -102,7 +99,7 @@ void forumLoadEntriesTest(String mail, String password, String role) { //47lines
Wait.notTooMuch(driver).until(ExpectedConditions.visibilityOfElementLocated(FORUM_COMMENT_LIST));
List<WebElement> comments = ForumNavigationUtilities.getComments(driver);
log.info("Checking if the entry has comments");
if (comments.size() > 0) {
if (!comments.isEmpty()) {
has_comments = true;
log.info("Comments found, saving them");

Expand Down Expand Up @@ -134,10 +131,11 @@ void forumLoadEntriesTest(String mail, String password, String role) { //47lines
@AccessMode(resID = "OpenVidu", concurrency = 10, sharing = true, accessMode = "NOACCESS")
@Resource(resID = "Course", replaceable = {"Forum"})
@AccessMode(resID = "Course", concurrency = 1, sharing = false, accessMode = "READWRITE")
@DisplayName("studentCourseMainTest")
@ParameterizedTest
@MethodSource("data")
void forumNewEntryTest(String mail, String password, String role) {// 48+ 104 + 28 set up +13 lines teardown =193
this.slowLogin(user, mail, password); //24 lines
this.slowLogin(teacher, mail, password); //24 lines
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
int mYear = calendar.get(Calendar.YEAR);
Expand Down Expand Up @@ -180,7 +178,7 @@ void forumNewEntryTest(String mail, String password, String role) {// 48+ 104 +
//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");
assertFalse(comments.isEmpty(), "No comments on the entry");
Wait.notTooMuch(driver).until(ExpectedConditions.visibilityOfElementLocated(FORUM_COMMENT_LIST));
Wait.waitForPageLoaded(driver);
WebElement newComment = comments.get(0);
Expand All @@ -193,7 +191,7 @@ void forumNewEntryTest(String mail, String password, String role) {// 48+ 104 +
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);
teacher.getDriver().get(APP_URL);
}

/**
Expand All @@ -211,10 +209,11 @@ void forumNewEntryTest(String mail, String password, String role) {// 48+ 104 +
@AccessMode(resID = "OpenVidu", concurrency = 10, sharing = true, accessMode = "NOACCESS")
@Resource(resID = "Course", replaceable = {"Forum"})
@AccessMode(resID = "Course", concurrency = 1, sharing = false, accessMode = "READWRITE")
@DisplayName("forumNewCommentTest")
@ParameterizedTest
@MethodSource("data")
void forumNewCommentTest(String mail, String password, String role) { // 69+142 + 28 set up +13 lines teardown =252
this.slowLogin(user, mail, password); //24 lines
this.slowLogin(teacher, mail, password); //24 lines
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
int mYear = calendar.get(Calendar.YEAR);
Expand Down Expand Up @@ -260,16 +259,16 @@ void forumNewCommentTest(String mail, String password, String role) { // 69+142
Wait.notTooMuch(driver).until(ExpectedConditions.visibilityOfElementLocated(FORUM_COMMENT_LIST));
//TO-DO think in other better way to solve this problem
Wait.waitForPageLoaded(driver);
user.waitUntil(ExpectedConditions.visibilityOfElementLocated(FORUM_COMMENT_LIST_COMMENT), "The comment list are not visible");
user.waitUntil((ExpectedCondition<Boolean>) driver -> {
teacher.waitUntil(ExpectedConditions.visibilityOfElementLocated(FORUM_COMMENT_LIST_COMMENT), "The comment list are not visible");
teacher.waitUntil((ExpectedCondition<Boolean>) driver -> {
int elementCount = ForumNavigationUtilities.getComments(driver).size();
return elementCount > numberCommentsOld;
}, "Comment not attached");
List<WebElement> comments = ForumNavigationUtilities.getComments(driver);
//asserts
assertTrue(comments.size() > numberCommentsOld, "Comment list empty or only original comment");

user.waitUntil(ExpectedConditions.visibilityOfElementLocated(FORUM_COMMENT_LIST_COMMENT), "The comment list are not visible");
teacher.waitUntil(ExpectedConditions.visibilityOfElementLocated(FORUM_COMMENT_LIST_COMMENT), "The comment list are not visible");

boolean commentFound = false;
for (WebElement comment : comments) {
Expand Down Expand Up @@ -299,17 +298,19 @@ void forumNewCommentTest(String mail, String password, String role) { // 69+142
* previously created, go to the first and replies to the same comment.After it, we check
* that the comment was correctly published.
*/
@Disabled
@ParameterizedTest
@MethodSource("data")


@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")
@DisplayName("forumNewReply2CommentTest")
@ParameterizedTest
@MethodSource("data")
void forumNewReply2CommentTest(String mail, String password, String role) { // 63+137+ 28 set up +13 lines teardown = 242
this.slowLogin(user, mail, password);//24 lines
this.slowLogin(teacher, mail, password);//24 lines
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
int mYear = calendar.get(Calendar.YEAR);
Expand Down Expand Up @@ -350,14 +351,14 @@ void forumNewReply2CommentTest(String mail, String password, String role) { // 6
Wait.notTooMuch(driver).until(ExpectedConditions.visibilityOfElementLocated(FORUM_COMMENT_LIST_MODAL_NEW_REPLY));
WebElement textField = driver.findElement(FORUM_COMMENT_LIST_MODAL_NEW_REPLY_TEXT_FIELD);
textField.sendKeys(newReplyContent);
Click.element(user.getDriver(), FORUM_NEW_COMMENT_MODAL_POST_BUTTON);
Click.element(teacher.getDriver(), FORUM_NEW_COMMENT_MODAL_POST_BUTTON);

user.waitUntil(ExpectedConditions.visibilityOfElementLocated(FORUM_COMMENT_LIST), "The comments are not visible");
teacher.waitUntil(ExpectedConditions.visibilityOfElementLocated(FORUM_COMMENT_LIST), "The comments are not visible");

user.waitUntil(ExpectedConditions.visibilityOfElementLocated(FORUM_COMMENT_LIST_COMMENT), "The comment list are not visible");
comments = ForumNavigationUtilities.getComments(user.getDriver()); //2lines
teacher.waitUntil(ExpectedConditions.visibilityOfElementLocated(FORUM_COMMENT_LIST_COMMENT), "The comment list are not visible");
comments = ForumNavigationUtilities.getComments(teacher.getDriver()); //2lines
//getComment replies
List<WebElement> replies = ForumNavigationUtilities.getReplies(user.getDriver(), comments.get(0)); // 7 lines
List<WebElement> replies = ForumNavigationUtilities.getReplies(teacher.getDriver(), comments.get(0)); // 7 lines
WebElement newReply = null;
for (WebElement reply : replies) {

Expand Down
Loading

0 comments on commit 0d26b62

Please sign in to comment.