Skip to content

Commit

Permalink
Removing Chrome and Firefox instance creation
Browse files Browse the repository at this point in the history
  • Loading branch information
augustocristian committed Aug 2, 2024
1 parent 93a9279 commit 53cbbb2
Showing 1 changed file with 16 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@
import com.fullteaching.e2e.no_elastest.common.exception.NotLoggedException;
import com.fullteaching.e2e.no_elastest.utils.Click;
import com.fullteaching.e2e.no_elastest.utils.Wait;
import io.github.bonigarcia.wdm.managers.ChromeDriverManager;
import io.github.bonigarcia.wdm.managers.FirefoxDriverManager;
import org.junit.jupiter.api.*;
import org.openqa.selenium.By;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.logging.LogEntries;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.slf4j.Logger;
Expand All @@ -24,24 +20,17 @@
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);
protected static final Class<? extends WebDriver> chrome = ChromeDriver.class;
protected static final Class<? extends WebDriver> firefox = FirefoxDriver.class;
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 @@ -66,14 +55,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
}

// Check if running outside ElasTest
if (System.getenv("ET_EUS_API") == null) {
// Setup drivers for Chrome and Firefox
ChromeDriverManager.getInstance(chrome).setup();
FirefoxDriverManager.getInstance(firefox).setup();
log.error("The properties file could not be loaded");
}

String envUrl = System.getProperty("SUT_URL") != null ? System.getProperty("SUT_URL") : System.getenv("SUT_URL");
Expand All @@ -85,7 +67,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 Down Expand Up @@ -113,7 +95,7 @@ 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: {}", TEST_NAME);
TJOB_NAME = System.getProperty("dirtarget");

user = setupBrowser("chrome", TJOB_NAME + "_" + TEST_NAME, userMail, WAIT_SECONDS);
Expand Down Expand Up @@ -163,10 +145,10 @@ protected BrowserUser setupBrowser(String browser, String testName,
void tearDown(TestInfo testInfo) { //13 lines

if (user != null) {
log.info("##### Finish test: {} - Driver {}", TEST_NAME, this.user.getDriver());
log.info("##### Finish test: {} - Driver {}", testInfo.getDisplayName(), this.user.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.forEach(entry -> log.info("[{}] {} {}",
new Date(entry.getTimestamp()), entry.getLevel(),
entry.getMessage()));
if (user.isOnSession()) {
Expand Down Expand Up @@ -225,18 +207,22 @@ private void login(BrowserUser user, String userEmail, String userPass,
userName = getUserName(user, true, APP_URL);
} catch (NotLoggedException | ElementNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
if(e.getClass().isInstance(NotLoggedException.class)){
log.error("The user was not able to be logged in, more [INFO] {} [INFO]",e.getMessage());
}
else{
log.error("The web element was not found,[INFO]:{} [INFO]",e.getMessage());
}
}

log.info("Logging in successful for user {}", user.getClientData());

}

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 +333,7 @@ protected void waitSeconds(int seconds) {
try {
Thread.sleep(1000L * seconds);
} catch (InterruptedException e) {
e.printStackTrace();
log.error("Interrupted exception during the waiting with the Thread.sleep");
}

}
Expand All @@ -372,14 +358,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 userPlaceHolderName = name_placeholder.getText().trim();

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

}

Expand Down

0 comments on commit 53cbbb2

Please sign in to comment.