Added auto reporter for test results - a csv file is now generated after each test

This commit is contained in:
Xander 2025-04-05 15:29:05 +01:00
parent 7c18bd2854
commit b82aa55232
4 changed files with 131 additions and 101 deletions

View file

@ -0,0 +1,3 @@
Role,Username,Test,Result
author,testuser,PostEditDelete,PASS
subscriber,subscriber,PostEditDelete,PASS
1 Role Username Test Result
2 author testuser PostEditDelete PASS
3 subscriber subscriber PostEditDelete PASS

View file

@ -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
1 Role Username Step Result
2 author testuser Login PASS
3 author testuser Post Access PASS
4 author testuser Post Creation PASS
5 author testuser Post Visible PASS
6 author testuser Post Deleted PASS
7 subscriber subscriber Login PASS
8 subscriber subscriber Post Access PASS
9 subscriber subscriber Post Creation SKIPPED (Not permitted)
10 subscriber subscriber Post Visible SKIPPED
11 subscriber subscriber Post Deleted SKIPPED

View file

@ -1,51 +1,38 @@
package main;
import java.io.*;
import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class CsvBuilder {
private static String inputFile = "src/csv/credentials.csv";
public static void compileAll(String outputFile) {
try (BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter("src/csv/built/"+outputFile+".csv"))) {
private static String reportFile;
// Skip the first line
reader.readLine();
public static String createTestReportFile() {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd-HHmmss");
String timestamp = LocalDateTime.now().format(formatter);
String filePath = "src/csv/reports/test-report-" + timestamp + ".csv";
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",", 2);
if (parts.length > 1) {
writer.write(parts[1]);
writer.newLine();
}
}
System.out.println("File processed successfully!");
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
writer.write("Role,Username,Step,Result\n"); // header
} catch (IOException e) {
System.err.println("Error processing file: " + e.getMessage());
System.err.println("Failed to create report file: " + e.getMessage());
}
}
public static void compilePermissions(String outputFile) {
try (BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter("src/csv/built/"+outputFile+".csv"))) {
// Skip the first line
reader.readLine();
String line;
while ((line = reader.readLine()) != null) {
writer.write(line+"\n");
}
System.out.println("File processed successfully!");
} catch (IOException e) {
System.err.println("Error processing file: " + e.getMessage());
}
reportFile = filePath;
return filePath;
}
public static void main(String[] args) {
CsvBuilder.compileAll("all_login_credentials");
CsvBuilder.compilePermissions("credentials_permissions");
public static void appendTestResult(String role, String username, String step, String result) {
if (reportFile == null) {
System.err.println("Report file not initialized.");
return;
}
try (BufferedWriter writer = new BufferedWriter(new FileWriter(reportFile, true))) {
writer.write(String.format("%s,%s,%s,%s\n", role, username, step, result));
} catch (IOException e) {
System.err.println("Failed to write to report file: " + e.getMessage());
}
}
}

View file

@ -1,19 +1,19 @@
package test;
import main.ConfigureDriver;
import main.CsvBuilder;
import main.DomainGrabber;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
import org.junit.jupiter.api.BeforeAll;
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.assertFalse;
import org.openqa.selenium.*;
import static org.junit.jupiter.api.Assertions.*;
public class TestPoster {
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 POST_TITLE_ID = "title";
@ -23,72 +23,101 @@ public class TestPoster {
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 LOGGGEDIN_URL = "https://"+domain+"/wp-admin/";
private static final String NEW_POST_URL = "https://"+domain+"/wp-admin/post-new.php";
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+"/";
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();
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));
username.sendKeys(usernameText);
System.out.println("Testing " + usernameText);
password.sendKeys(passwordText);
login.click();
assertTrue(driver.getCurrentUrl().startsWith(LOGGGEDIN_URL));
boolean canPost;
if (role.equals("author")) {
canPost = true;
} else {
canPost = false;
}
boolean postAccessible = true;
try {
WebElement post_button = driver.findElement(By.id(POST_BUTTON_NAV_ID));
post_button.click();
} catch (NoSuchElementException e) {
System.out.println("Post inaccessible!");
postAccessible = false;
// Step 1: Login
try {
driver.findElement(By.id(USERNAME_BOX_ID)).sendKeys(usernameText);
driver.findElement(By.id(PASSWORD_BOX_ID)).sendKeys(passwordText);
driver.findElement(By.id(LOGIN_BUTTON_ID)).click();
boolean loginSuccess = driver.getCurrentUrl().startsWith(LOGGEDIN_URL);
CsvBuilder.appendTestResult(role, usernameText, "Login", loginSuccess ? "PASS" : "FAIL");
if (!loginSuccess) {
driver.quit();
return;
}
} catch (Exception e) {
CsvBuilder.appendTestResult(role, usernameText, "Login", "FAIL - Exception: " + e.getMessage());
driver.quit();
return;
}
boolean canPost = role.equals("author");
boolean postAccessible = true;
// Step 2: Attempt to access post creation
try {
WebElement post_button = driver.findElement(By.id(POST_BUTTON_NAV_ID));
post_button.click();
} catch (NoSuchElementException e) {
postAccessible = false;
}
if (canPost) {
CsvBuilder.appendTestResult(role, usernameText, "Post Access", postAccessible ? "PASS" : "FAIL");
if (!postAccessible) {
driver.quit();
return;
}
// Step 3: Create post
try {
driver.findElement(By.id(POST_EDITOR_SWITCH_ID)).click();
driver.findElement(By.id(POST_TITLE_ID)).sendKeys(POST_TITLE_TEXT);
driver.findElement(By.id(POST_CONTENT_ID)).sendKeys(POST_CONTENT_TEXT);
driver.findElement(By.id(PUBLISH_BUTTON_ID)).click();
CsvBuilder.appendTestResult(role, usernameText, "Post Creation", "PASS");
} catch (Exception e) {
CsvBuilder.appendTestResult(role, usernameText, "Post Creation", "FAIL - " + e.getMessage());
driver.quit();
return;
}
// Step 4: Verify post
try {
String editURL = driver.getCurrentUrl();
driver.get(POST_URL);
boolean exists = !driver.getTitle().startsWith("Page not found");
CsvBuilder.appendTestResult(role, usernameText, "Post Visible", exists ? "PASS" : "FAIL");
// Step 5: Delete post
driver.get(editURL);
driver.findElement(By.id(DELETE_BUTTON_ID)).click();
driver.get(POST_URL);
boolean deleted = driver.getTitle().startsWith("Page not found");
CsvBuilder.appendTestResult(role, usernameText, "Post Deleted", deleted ? "PASS" : "FAIL");
} catch (Exception e) {
CsvBuilder.appendTestResult(role, usernameText, "Post Visible or Deletion", "FAIL - " + e.getMessage());
}
} else {
CsvBuilder.appendTestResult(role, usernameText, "Post Access", postAccessible ? "FAIL (Should be denied)" : "PASS");
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, "Test Error", "FAIL - " + t.getClass().getSimpleName() + ": " + t.getMessage());
} finally {
driver.quit();
}
if (canPost) { //if there's permission to post, carry on
assertTrue(driver.getCurrentUrl().equals(NEW_POST_URL));
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();
//now to verify the post exists
String editURL = driver.getCurrentUrl();
driver.get(POST_URL);
String postedTitle = driver.getTitle();
assertFalse(postedTitle.startsWith("Page not found"));
//delete post
driver.get(editURL);
WebElement deleteButton = driver.findElement(By.id(DELETE_BUTTON_ID));
deleteButton.click();
driver.get(POST_URL);
postedTitle= driver.getTitle();
assertTrue(postedTitle.startsWith("Page not found"));
} else { //if there's no permission to post
assertTrue(postAccessible == false);
assertFalse(driver.getCurrentUrl().equals(NEW_POST_URL));
}
driver.quit();
}
}
}