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.*; import static org.junit.jupiter.api.Assertions.*; public class TestPoster { private static final boolean maximiseWindow = true; private static final String domain = DomainGrabber.getDomain(); 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 POST_TITLE_ID = "title"; private static final String POST_CONTENT_ID = "content"; private static final String LOGIN_BUTTON_ID = "wp-submit"; private static final String POST_BUTTON_NAV_ID = "wp-admin-bar-new-content"; private static final String POST_EDITOR_SWITCH_ID = "content-html"; private static final String PUBLISH_BUTTON_ID = "publish"; private static final String DELETE_BUTTON_ID = "delete-action"; private static final String LOGGEDIN_URL = "https://" + domain + "/wp-admin/"; private static final String NEW_POST_URL = "https://" + domain + "/wp-admin/post-new.php"; private static final String POST_CONTENT_TEXT = "Lorem ipsum dolor amet"; private static final String POST_TITLE_TEXT = "lipsum4u"; private static final String POST_URL = "https://" + domain + "/" + POST_TITLE_TEXT + "/"; @BeforeAll public static void initReport() { CsvBuilder.createTestReportFile(); } @ParameterizedTest @CsvFileSource(resources = "/csv/built/credentials_permissions.csv", numLinesToSkip = 0) public void testPostEditDelete(String role, String usernameText, String passwordText) { WebDriver driver = ConfigureDriver.configureDriver(); if (maximiseWindow) { driver.manage().window().maximize(); } driver.get(LOGIN_PAGE); try { // Step 1: Login boolean loginAllowed = (!role.equals("unauthorised")); 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); password.sendKeys(passwordText); login.click(); boolean loginSuccess = driver.getCurrentUrl().startsWith(LOGGEDIN_URL); String LoginPassText; String LoginFailText; if (loginAllowed) { LoginPassText = "PASS (Logged in succesfully)"; LoginFailText = "FAIL (Login failed, it shouldn't have)"; } else { LoginPassText = "FAIL (Should be unable to login)"; LoginFailText = "PASS (Login failed, as expected)"; } CsvBuilder.appendTestResult(role, usernameText, "Login", loginSuccess ? LoginPassText : LoginFailText); if (loginAllowed) { assertTrue(loginSuccess, "Login failed for user: " + usernameText); } else { assertFalse(loginSuccess, "Login successfully failed for user: " + usernameText); } // Determine permission boolean canPost = role.equals("author"); boolean postAccessible = true; if (loginAllowed) { // Step 2: Try accessing new post page try { WebElement postButton = driver.findElement(By.id(POST_BUTTON_NAV_ID)); postButton.click(); } catch (NoSuchElementException e) { postAccessible = false; } if (canPost) { CsvBuilder.appendTestResult(role, usernameText, "New Post Access", postAccessible ? "PASS" : "FAIL"); assertTrue(postAccessible, "User should be able to access post creation but can't"); // Step 3: Create post WebElement title = driver.findElement(By.id(POST_TITLE_ID)); WebElement content = driver.findElement(By.id(POST_CONTENT_ID)); WebElement textSwitch = driver.findElement(By.id(POST_EDITOR_SWITCH_ID)); WebElement publishButton = driver.findElement(By.id(PUBLISH_BUTTON_ID)); textSwitch.click(); title.sendKeys(POST_TITLE_TEXT); content.sendKeys(POST_CONTENT_TEXT); publishButton.click(); CsvBuilder.appendTestResult(role, usernameText, "Post Creation", "PASS"); // Step 4: Verify post is visible String editURL = driver.getCurrentUrl(); driver.get(POST_URL); String postedTitle = driver.getTitle(); boolean postVisible = !postedTitle.startsWith("Page not found"); CsvBuilder.appendTestResult(role, usernameText, "Post Visible", postVisible ? "PASS" : "FAIL"); assertTrue(postVisible, "Post is not visible after publishing"); // Step 5: Delete post driver.get(editURL); WebElement deleteButton = driver.findElement(By.id(DELETE_BUTTON_ID)); deleteButton.click(); driver.get(POST_URL); postedTitle = driver.getTitle(); boolean postDeleted = postedTitle.startsWith("Page not found"); CsvBuilder.appendTestResult(role, usernameText, "Post Deleted", postDeleted ? "PASS" : "FAIL"); assertTrue(postDeleted, "Post was not properly deleted"); } else { CsvBuilder.appendTestResult(role, usernameText, "Post Access", postAccessible ? "FAIL (Should not access)" : "PASS"); assertFalse(postAccessible, "User without permissions accessed post creation page"); CsvBuilder.appendTestResult(role, usernameText, "Post Creation", "SKIPPED (Not permitted)"); CsvBuilder.appendTestResult(role, usernameText, "Post Visible", "SKIPPED"); CsvBuilder.appendTestResult(role, usernameText, "Post Deleted", "SKIPPED"); } } } catch (Throwable t) { CsvBuilder.appendTestResult(role, usernameText, "Unexpected Error", "FAIL - " + t.getMessage()); fail("Unexpected exception: " + t.getMessage()); } finally { driver.quit(); } } }