From 9314fcea8fb0e0cfb31848a2c007ba04b79d818c Mon Sep 17 00:00:00 2001 From: xander Date: Sat, 5 Apr 2025 15:35:28 +0100 Subject: [PATCH] login test now reports back as well --- .../reports/test-report-20250405-153243.csv | 11 +++++ .../reports/test-report-20250405-153420.csv | 3 ++ src/test/TestLogin.java | 48 +++++++++++++------ 3 files changed, 47 insertions(+), 15 deletions(-) create mode 100644 src/csv/reports/test-report-20250405-153243.csv create mode 100644 src/csv/reports/test-report-20250405-153420.csv diff --git a/src/csv/reports/test-report-20250405-153243.csv b/src/csv/reports/test-report-20250405-153243.csv new file mode 100644 index 0000000..e02c6c6 --- /dev/null +++ b/src/csv/reports/test-report-20250405-153243.csv @@ -0,0 +1,11 @@ +Role,Username,Step,Result +author,testuser,Login,PASS +author,testuser,Post Access,PASS +author,testuser,Post Creation,PASS +author,testuser,Post Visible,PASS +author,testuser,Post Deleted,PASS +subscriber,subscriber,Login,PASS +subscriber,subscriber,Post Access,PASS +subscriber,subscriber,Post Creation,SKIPPED (Not permitted) +subscriber,subscriber,Post Visible,SKIPPED +subscriber,subscriber,Post Deleted,SKIPPED diff --git a/src/csv/reports/test-report-20250405-153420.csv b/src/csv/reports/test-report-20250405-153420.csv new file mode 100644 index 0000000..02e2e99 --- /dev/null +++ b/src/csv/reports/test-report-20250405-153420.csv @@ -0,0 +1,3 @@ +Role,Username,Step,Result +N/A,testuser,Login,PASS +N/A,subscriber,Login,PASS diff --git a/src/test/TestLogin.java b/src/test/TestLogin.java index 80e6c65..ab09d14 100644 --- a/src/test/TestLogin.java +++ b/src/test/TestLogin.java @@ -1,39 +1,57 @@ package test; import main.ConfigureDriver; +import main.CsvBuilder; import main.DomainGrabber; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvFileSource; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.By; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.CsvFileSource; + import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; public class TestLogin { private static final String domain = DomainGrabber.getDomain(); - private static final String LOGIN_PAGE = "https://"+domain+"/wp-login.php"; + private static final String LOGIN_PAGE = "https://" + domain + "/wp-login.php"; private static final String USERNAME_BOX_ID = "user_login"; private static final String PASSWORD_BOX_ID = "user_pass"; private static final String LOGIN_BUTTON_ID = "wp-submit"; - private static final String EXPECTED_URL = "https://"+domain+"/wp-admin/"; + private static final String EXPECTED_URL = "https://" + domain + "/wp-admin/"; + + @BeforeAll + public static void initReport() { + CsvBuilder.createTestReportFile(); // Reuses the same report for consistency + } + @ParameterizedTest @CsvFileSource(resources = "/csv/built/all_login_credentials.csv", numLinesToSkip = 0) - public void testLogin(String usernameText, String passwordText) { WebDriver driver = ConfigureDriver.configureDriver(); driver.manage().window().maximize(); driver.get(LOGIN_PAGE); - WebElement username = driver.findElement(By.id(USERNAME_BOX_ID)); - WebElement password = driver.findElement(By.id(PASSWORD_BOX_ID)); - WebElement login = driver.findElement(By.id(LOGIN_BUTTON_ID)); + try { + WebElement username = driver.findElement(By.id(USERNAME_BOX_ID)); + WebElement password = driver.findElement(By.id(PASSWORD_BOX_ID)); + WebElement login = driver.findElement(By.id(LOGIN_BUTTON_ID)); - username.sendKeys(usernameText); - System.out.println("Testing "+usernameText); - password.sendKeys(passwordText); - login.click(); + username.sendKeys(usernameText); + System.out.println("Testing " + usernameText); + password.sendKeys(passwordText); + login.click(); - assertTrue(driver.getCurrentUrl().startsWith(EXPECTED_URL)); - driver.quit(); + boolean loginSuccessful = driver.getCurrentUrl().startsWith(EXPECTED_URL); + CsvBuilder.appendTestResult("N/A", usernameText, "Login", loginSuccessful ? "PASS" : "FAIL"); + assertTrue(loginSuccessful, "Login failed for user: " + usernameText); + + } catch (Exception e) { + CsvBuilder.appendTestResult("N/A", usernameText, "Login", "FAIL - Exception: " + e.getMessage()); + fail("Exception during login test: " + e.getMessage()); + } finally { + driver.quit(); + } } -} \ No newline at end of file +}