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

Merge remote-tracking branch 'origin/main'

parent 87201cea
Branches
No related tags found
No related merge requests found
package uk.ac.cf.spring.demo.user.controller; //package uk.ac.cf.spring.demo.user.controller;
//
import org.junit.jupiter.api.AfterEach; //import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll; //import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach; //import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; //import org.junit.jupiter.api.Test;
//import org.junit.runner.RunWith; ////import org.junit.runner.RunWith;
import org.junit.jupiter.api.extension.ExtendWith; //import org.junit.jupiter.api.extension.ExtendWith;
import org.openqa.selenium.By; //import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver; //import org.openqa.selenium.WebDriver;
//import org.openqa.selenium.chrome.ChromeDriver; ////import org.openqa.selenium.chrome.ChromeDriver;
//import org.openqa.selenium.chrome.ChromeOptions; ////import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver; //import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions; //import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.support.ui.WebDriverWait; //import org.openqa.selenium.support.ui.WebDriverWait;
import org.springframework.beans.factory.annotation.Autowired; //import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value; ////import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; //import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest; //import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension; //import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.junit4.SpringRunner; //import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc; //import org.springframework.test.web.servlet.MockMvc;
//import io.github.bonigarcia.wdm.WebDriverManager; ////import io.github.bonigarcia.wdm.WebDriverManager;
//
import java.time.Duration; //import java.time.Duration;
//
import static org.junit.jupiter.api.Assertions.assertTrue; //import static org.junit.jupiter.api.Assertions.assertTrue;
//
@ExtendWith(SpringExtension.class) //@ExtendWith(SpringExtension.class)
@AutoConfigureMockMvc //@AutoConfigureMockMvc
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) //@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class RegistrationAndLoginTest { //public class RegistrationAndLoginTest {
//
// @Value("${local.server.port}") //// @Value("${local.server.port}")
// private int port; //// private int port;
//
// an alternative really good resource: https://github.com/bonigarcia/webdrivermanager // // an alternative really good resource: https://github.com/bonigarcia/webdrivermanager
//
@Autowired // @Autowired
private MockMvc mockMvc; // private MockMvc mockMvc;
WebDriver webDriver; // WebDriver webDriver;
//
@BeforeAll
static void setupClass() {
// set the path of geckodriver
System.setProperty("webdriver.gecko.driver", "C:\\UniProjects\\geckodriver-v0.35.0-win64\\geckodriver.exe");
}
// @BeforeAll // @BeforeAll
// static void setupClass() { // static void setupClass() {
// WebDriverManager.firefoxdriver().setup(); // // set the path of geckodriver
// System.setProperty("webdriver.gecko.driver", "C:\\UniProjects\\geckodriver-v0.35.0-win64\\geckodriver.exe");
// }
//
//// @BeforeAll
//// static void setupClass() {
//// WebDriverManager.firefoxdriver().setup();
//// }
//
// @BeforeEach
// void setupTest() {
// FirefoxOptions options = new FirefoxOptions();
//// options.addArguments("--remote-debugging-port=42227");
// options.addArguments("--headless");
// webDriver = new FirefoxDriver(options);
// }
//
// @AfterEach
// void teardown() {
// // Close the browser after each test
// if (webDriver != null) {
// webDriver.quit();
// }
// } // }
//
@BeforeEach
void setupTest() {
FirefoxOptions options = new FirefoxOptions();
// options.addArguments("--remote-debugging-port=42227");
options.addArguments("--headless");
webDriver = new FirefoxDriver(options);
}
@AfterEach
void teardown() {
// Close the browser after each test
if (webDriver != null) {
webDriver.quit();
}
}
// @Test // @Test
// public void testUserRegistrationAndLogin() throws Exception { // public void testUserRegistrationAndLogin() throws Exception {
// // Step 1: Navigate to the registration page // // Step 1: Navigate to the registration page
...@@ -96,7 +96,7 @@ public class RegistrationAndLoginTest { ...@@ -96,7 +96,7 @@ public class RegistrationAndLoginTest {
// String currentUrl = webDriver.getCurrentUrl(); // String currentUrl = webDriver.getCurrentUrl();
// assert currentUrl.endsWith("/html/matchSchedule.html"); // assert currentUrl.endsWith("/html/matchSchedule.html");
// } // }
//
// @Test // @Test
// public void testUserRegistrationAndLogin() throws Exception { // public void testUserRegistrationAndLogin() throws Exception {
// // Step 1: Navigate to the registration page // // Step 1: Navigate to the registration page
...@@ -130,23 +130,23 @@ public class RegistrationAndLoginTest { ...@@ -130,23 +130,23 @@ public class RegistrationAndLoginTest {
// String currentUrl = webDriver.getCurrentUrl(); // String currentUrl = webDriver.getCurrentUrl();
// assertTrue(currentUrl.endsWith("/html/matchSchedule.html")); // assertTrue(currentUrl.endsWith("/html/matchSchedule.html"));
// } // }
//
@Test // @Test
public void testLoginWithInvalidCredentials() throws Exception { // public void testLoginWithInvalidCredentials() throws Exception {
// Step 1: Navigate to the login page // // Step 1: Navigate to the login page
webDriver.get("http://localhost:8080/html/login.html"); // webDriver.get("http://localhost:8080/html/login.html");
//
// Fill out the login form with invalid credentials // // Fill out the login form with invalid credentials
webDriver.findElement(By.id("email")).sendKeys("invalid@example.com"); // webDriver.findElement(By.id("email")).sendKeys("invalid@example.com");
webDriver.findElement(By.id("password")).sendKeys("wrongpassword"); // webDriver.findElement(By.id("password")).sendKeys("wrongpassword");
webDriver.findElement(By.cssSelector("button[type='submit']")).click(); // webDriver.findElement(By.cssSelector("button[type='submit']")).click();
//
// Wait for the error message to appear // // Wait for the error message to appear
Thread.sleep(2000); // Simple wait for demonstration purposes // Thread.sleep(2000); // Simple wait for demonstration purposes
//
// Verify that the error message is displayed // // Verify that the error message is displayed
String errorMessage = webDriver.findElement(By.id("message")).getText(); // String errorMessage = webDriver.findElement(By.id("message")).getText();
assert errorMessage.contains("Invalid email or password"); // assert errorMessage.contains("Invalid email or password");
} // }
//
} //}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment