CMP329-SOFTWARE-TESTING-CW2/src/test/TestPoster.java

94 lines
No EOL
4.2 KiB
Java

package test;
import main.ConfigureDriver;
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.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;
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 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 LOGGGEDIN_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+"/";
@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;
}
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();
}
}