From 036b034ab59bf97c95b8307901748debfb420fc7 Mon Sep 17 00:00:00 2001 From: Javi <102818127+jrodriguezs2020@users.noreply.github.com> Date: Mon, 13 Jan 2025 20:16:45 +0100 Subject: [PATCH] fix login --- .../tests/api/UploadDocumentApiTest.java | 2 - .../backend/tests/ui/LoginUiTestService.java | 44 +++++++++++++------ 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/backend/src/test/java/com/snapstudy/backend/tests/api/UploadDocumentApiTest.java b/backend/src/test/java/com/snapstudy/backend/tests/api/UploadDocumentApiTest.java index 347fd22..26d07b2 100644 --- a/backend/src/test/java/com/snapstudy/backend/tests/api/UploadDocumentApiTest.java +++ b/backend/src/test/java/com/snapstudy/backend/tests/api/UploadDocumentApiTest.java @@ -22,8 +22,6 @@ @SpringBootTest public class UploadDocumentApiTest { - @Autowired - private SubjectService subjectService; @Autowired private DegreeService degreeService; private static Map cookies; diff --git a/backend/src/test/java/com/snapstudy/backend/tests/ui/LoginUiTestService.java b/backend/src/test/java/com/snapstudy/backend/tests/ui/LoginUiTestService.java index 1f14555..9a70619 100644 --- a/backend/src/test/java/com/snapstudy/backend/tests/ui/LoginUiTestService.java +++ b/backend/src/test/java/com/snapstudy/backend/tests/ui/LoginUiTestService.java @@ -12,25 +12,41 @@ public class LoginUiTestService { public void login(WebDriver driver, String email, String password, String successUrl) { - driver.get("https://localhost:8443/login"); + try { + System.out.println("Navigating to the login page..."); + driver.get("https://localhost:8443/login"); - WebDriverWait wait2 = new WebDriverWait(driver, Duration.ofSeconds(10)); -WebElement emailField = wait2.until(ExpectedConditions.visibilityOfElementLocated(By.id("email"))); - WebElement passwordField = driver.findElement(By.id("password")); - WebElement loginButton = driver.findElement(By.xpath("//button[contains(text(),'Sign in!')]")); + WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); - emailField.sendKeys(email); - passwordField.sendKeys(password); + System.out.println("Waiting for email field..."); + WebElement emailField = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("email"))); - loginButton.click(); + System.out.println("Waiting for password field..."); + WebElement passwordField = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("password"))); - // Wait for the URL to change to the expected one - WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5)); - wait.until(ExpectedConditions.urlToBe(successUrl)); + System.out.println("Waiting for login button..."); + WebElement loginButton = wait.until(ExpectedConditions.elementToBeClickable( + By.xpath("//button[contains(text(),'Sign in!')]"))); - // Verify that the login has been successful by navigating to the main page - Assertions.assertEquals(successUrl, driver.getCurrentUrl(), - "The user was correctly redirected."); + System.out.println("Entering email and password..."); + emailField.sendKeys(email); + passwordField.sendKeys(password); + + System.out.println("Clicking the login button..."); + loginButton.click(); + + System.out.println("Waiting for successful redirect..."); + wait.until(ExpectedConditions.urlContains(successUrl)); + + System.out.println("Verifying the current URL..."); + Assertions.assertTrue(driver.getCurrentUrl().contains(successUrl), + "The user was successfully redirected to the expected URL."); + + System.out.println("Login successful!"); + } catch (Exception e) { + System.err.println("Error during login: " + e.getMessage()); + throw new RuntimeException("Login failed", e); + } } }