Skip to content
Snippets Groups Projects
Commit 8642633b authored by Xiemuqing Xiao's avatar Xiemuqing Xiao
Browse files

Merge remote-tracking branch 'origin/main'

parent bf6370e0
No related merge requests found
......@@ -14,6 +14,10 @@ java {
}
}
//test {
// useJUnitPlatform() // 启用 JUnit 5 测试引擎
//}
configurations {
compileOnly {
extendsFrom annotationProcessor
......@@ -30,6 +34,11 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
//from https://mariadb.com/kb/en/java-connector-using-gradle/
implementation 'junit:junit:4.12'
// testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
// testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
// testImplementation 'org.seleniumhq.selenium:selenium-java:4.0.0'
// testImplementation 'io.github.bonigarcia:webdrivermanager:5.0.3'
// testImplementation 'org.assertj:assertj-core:3.24.2'
implementation 'org.mariadb.jdbc:mariadb-java-client:3.3.3'
// implementation 'org.mariadb.jdbc:mariadb-java-client:2.1.2'
......@@ -45,7 +54,8 @@ dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
testImplementation 'org.springframework.security:spring-security-test'
// testImplementation("io.github.bonigarcia:webdrivermanager:5.2.0")
testImplementation 'org.seleniumhq.selenium:selenium-java:4.3.0'
testImplementation("io.github.bonigarcia:webdrivermanager:5.2.0")
// testImplementation group: 'net.sourceforge.htmlunit', name: 'htmlunit', version: '2.32'
// testImplementation group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '4.1.0'
......
package uk.ac.cf.spring.demo.user.controller;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.runner.RunWith;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.junit4.SpringRunner;
import io.github.bonigarcia.wdm.WebDriverManager;
import java.io.File;
import java.time.Duration;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertTrue;
//@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class DriverPageTests {
@Value("${local.server.port}")
private int port;
WebDriver webDriver;
//package uk.ac.cf.spring.demo.user.controller;
//
//import org.junit.After;
//import org.junit.Before;
//import org.junit.BeforeClass;
//import org.junit.Test;
//import org.junit.jupiter.api.AfterEach;
//import org.junit.jupiter.api.BeforeAll;
//import org.junit.jupiter.api.BeforeEach;
//import org.junit.jupiter.api.TestInstance;
//import org.junit.jupiter.api.extension.ExtendWith;
//import org.junit.runner.RunWith;
//import org.openqa.selenium.By;
//import org.openqa.selenium.WebDriver;
//import org.openqa.selenium.WebElement;
//import org.openqa.selenium.firefox.FirefoxDriver;
//import org.openqa.selenium.firefox.FirefoxOptions;
//import org.openqa.selenium.support.ui.ExpectedConditions;
//import org.openqa.selenium.support.ui.WebDriverWait;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.boot.test.context.SpringBootTest;
//import org.springframework.test.context.junit.jupiter.SpringExtension;
//import org.springframework.test.context.junit4.SpringRunner;
//import io.github.bonigarcia.wdm.WebDriverManager;
//
//import java.io.File;
//import java.time.Duration;
//
////import static org.junit.Assert.assertTrue;
//import static org.junit.jupiter.api.Assertions.assertTrue;
//import static org.assertj.core.api.Assertions.assertThat;
//
//
////@RunWith(SpringRunner.class)
//@ExtendWith(SpringExtension.class)
//@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
////@TestInstance(TestInstance.Lifecycle.PER_CLASS)
//public class DriverPageTests {
//
// @Value("${local.server.port}")
// private int port;
//
// private WebDriver webDriver;
// private WebDriverWait wait;
@BeforeAll
public static void setupClass() {
WebDriverManager.firefoxdriver().setup();
}
@BeforeEach
public void setupTest() {
// Get the current working directory
String currentDir = System.getProperty("user.dir");
// Set the path to the local geckodriver executable
System.setProperty("webdriver.gecko.driver", currentDir + File.separator + "geckodriver.exe");
FirefoxOptions options = new FirefoxOptions();
options.addArguments("--headless");
webDriver = new FirefoxDriver(options);
//
//
// @BeforeAll
// public static void setupClass() {
// WebDriverManager.firefoxdriver().setup();
// }
//
// @BeforeEach
// public void setupTest() {
// // Get the current working directory
// String currentDir = System.getProperty("user.dir");
//
// // Set the path to the local geckodriver executable
// System.setProperty("webdriver.gecko.driver", currentDir + File.separator + "geckodriver.exe");
// FirefoxOptions options = new FirefoxOptions();
// options.addArguments("--headless");
// webDriver = new FirefoxDriver(options);
// wait = new WebDriverWait(webDriver, Duration.ofSeconds(10));
}
@AfterEach
public void teardown() {
if (webDriver != null) {
webDriver.quit();
}
}
@Test
public void testUserManagementPage() {
// Test fetching and displaying users
webDriver.get("http://localhost:" + port + "/html/backGroundUser.html");
// }
//
// @AfterEach
// public void teardown() {
// if (webDriver != null) {
// webDriver.quit();
// }
// }
//
// @Test
// public void testUserManagementPage() {
// // Test fetching and displaying users
// webDriver.get("http://localhost:" + port + "/html/backGroundUser.html");
// assertTrue(webDriver.findElement(By.cssSelector("#userTable")).isDisplayed());
// Test adding a new user
webDriver.findElement(By.id("username")).sendKeys("shy");
webDriver.findElement(By.id("email")).sendKeys("shy@creditsafe.com");
webDriver.findElement(By.id("password")).sendKeys("$2a$16$R0aSGzbklUhpRfIMhocewephgUDMFOffLb7faSwwHusqHh81G026i");
webDriver.findElement(By.id("role")).sendKeys("USER");
webDriver.findElement(By.xpath("//button[text()='Add User']")).click();
assertTrue(webDriver.findElement(By.cssSelector("#userTable")).getText().contains("shy"));
// Test editing a user
webDriver.findElement(By.xpath("//tr[td[text()='shy']]//button[text()='Edit']")).click();
webDriver.findElement(By.id("email")).clear();
webDriver.findElement(By.id("email")).sendKeys("upu@creditsafe.com");
webDriver.findElement(By.xpath("//button[text()='Update User']")).click();
assertTrue(webDriver.findElement(By.cssSelector("#userTable")).getText().contains("upu@creditsafe.com"));
// Test deleting a user
webDriver.findElement(By.xpath("//tr[td[text()='shy']]//button[text()='Delete']")).click();
assertThat(webDriver.findElement(By.cssSelector("#userTable")).getText()).doesNotContain("shy");
}
}
\ No newline at end of file
//
// // Test adding a new user
// webDriver.findElement(By.id("username")).sendKeys("shy");
// webDriver.findElement(By.id("email")).sendKeys("shy@creditsafe.com");
// webDriver.findElement(By.id("password")).sendKeys("$2a$16$R0aSGzbklUhpRfIMhocewephgUDMFOffLb7faSwwHusqHh81G026i");
// webDriver.findElement(By.id("role")).sendKeys("USER");
// webDriver.findElement(By.xpath("//button[text()='Add User']")).click();
// assertTrue(webDriver.findElement(By.cssSelector("#userTable")).getText().contains("shy"));
//
// // Test editing a user
// webDriver.findElement(By.xpath("//tr[td[text()='shy']]//button[text()='Edit']")).click();
// webDriver.findElement(By.id("email")).clear();
// webDriver.findElement(By.id("email")).sendKeys("upu@creditsafe.com");
// webDriver.findElement(By.xpath("//button[text()='Update User']")).click();
// assertTrue(webDriver.findElement(By.cssSelector("#userTable")).getText().contains("upu@creditsafe.com"));
//
// // Test deleting a user
// webDriver.findElement(By.xpath("//tr[td[text()='shy']]//button[text()='Delete']")).click();
// assertThat(webDriver.findElement(By.cssSelector("#userTable")).getText()).doesNotContain("shy");
// }
//}
\ No newline at end of file
......@@ -12,6 +12,7 @@ import org.openqa.selenium.WebDriver;
//import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
......@@ -21,6 +22,8 @@ import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
//import io.github.bonigarcia.wdm.WebDriverManager;
import java.time.Duration;
import static org.junit.jupiter.api.Assertions.assertTrue;
@ExtendWith(SpringExtension.class)
......@@ -64,6 +67,36 @@ public class RegistrationAndLoginTest {
}
}
// @Test
// public void testUserRegistrationAndLogin() throws Exception {
// // Step 1: Navigate to the registration page
// webDriver.get("http://localhost:8080/html/register.html");
//
// // Fill out the registration form
// webDriver.findElement(By.id("username")).sendKeys("testuser");
// webDriver.findElement(By.id("email")).sendKeys("testuser@creditsafe.com");
// webDriver.findElement(By.id("password")).sendKeys("password123");
// webDriver.findElement(By.cssSelector("button[type='submit']")).click();
//
// // Wait for the registration to complete and redirect to the login page
// Thread.sleep(2000); // Simple wait for demonstration purposes
//
// // Step 2: Navigate to the login page
// webDriver.get("http://localhost:8080/html/login.html");
//
// // Fill out the login form
// webDriver.findElement(By.id("email")).sendKeys("testuser@creditsafe.com");
// webDriver.findElement(By.id("password")).sendKeys("password123");
// webDriver.findElement(By.cssSelector("button[type='submit']")).click();
//
// // Wait for the login to complete and redirect to the match schedule page
// Thread.sleep(2000); // Simple wait for demonstration purposes
//
// // Verify that the user is redirected to the match schedule page
// String currentUrl = webDriver.getCurrentUrl();
// assert currentUrl.endsWith("/html/matchSchedule.html");
// }
@Test
public void testUserRegistrationAndLogin() throws Exception {
// Step 1: Navigate to the registration page
......@@ -76,7 +109,9 @@ public class RegistrationAndLoginTest {
webDriver.findElement(By.cssSelector("button[type='submit']")).click();
// Wait for the registration to complete and redirect to the login page
Thread.sleep(2000); // Simple wait for demonstration purposes
new WebDriverWait(webDriver, Duration.ofSeconds(10)).until(
webDriver -> webDriver.getCurrentUrl().endsWith("/html/login.html")
);
// Step 2: Navigate to the login page
webDriver.get("http://localhost:8080/html/login.html");
......@@ -87,11 +122,13 @@ public class RegistrationAndLoginTest {
webDriver.findElement(By.cssSelector("button[type='submit']")).click();
// Wait for the login to complete and redirect to the match schedule page
Thread.sleep(2000); // Simple wait for demonstration purposes
new WebDriverWait(webDriver, Duration.ofSeconds(10)).until(
webDriver -> webDriver.getCurrentUrl().endsWith("/html/matchSchedule.html")
);
// Verify that the user is redirected to the match schedule page
String currentUrl = webDriver.getCurrentUrl();
assert currentUrl.endsWith("/html/matchSchedule.html");
assertTrue(currentUrl.endsWith("/html/matchSchedule.html"));
}
@Test
......
......@@ -33,19 +33,20 @@ public class RegistrationControllerMockMVCTest {
private ObjectMapper objectMapper;
@BeforeEach
public void setUp() {
MockitoAnnotations.openMocks(this);
objectMapper = new ObjectMapper(); // Initialize ObjectMapper
}
@Test
public void testShowRegistrationForm() throws Exception {
mockMvc.perform(get("/register"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(view().name("html/register.html"));
}
// @Test
// public void testShowRegistrationForm() throws Exception {
// mockMvc.perform(get("/register"))
// .andDo(print())
// .andExpect(status().isOk())
// .andExpect(view().name("html/register.html"));
// }
@Test
public void testRegisterUser_Success() throws Exception {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment