Skip to content
Snippets Groups Projects
Commit 771fca16 authored by Haoyu Sun's avatar Haoyu Sun
Browse files

ChromeDriverTest and seleniumTest

parent 8eeceabb
No related branches found
No related tags found
2 merge requests!62chrome test and selenuim-testing,!61ChromeDriverTest and seleniumTest
...@@ -44,6 +44,10 @@ dependencies { ...@@ -44,6 +44,10 @@ dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher' testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
testImplementation 'org.springframework.security:spring-security-test' testImplementation 'org.springframework.security:spring-security-test'
testImplementation("io.github.bonigarcia:webdrivermanager:5.1.0")
//selenium
testImplementation group: 'net.sourceforge.htmlunit', name: 'htmlunit', version: '2.32'
testImplementation group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '4.1.0'
} }
tasks.named('test') { tasks.named('test') {
......
...@@ -27,34 +27,34 @@ function loadNavbar(targetElementId) { ...@@ -27,34 +27,34 @@ function loadNavbar(targetElementId) {
} }
// Update authentication buttons based on user login state // Update authentication buttons based on user login state
function updateAuthButtons() { // function updateAuthButtons() {
fetch('/users/current', { method: 'GET' }) // fetch('/users/current', { method: 'GET' })
.then(response => { // .then(response => {
if (!response.ok) { // if (!response.ok) {
throw new Error('User not logged in'); // throw new Error('User not logged in');
} // }
return response.json(); // return response.json();
}) // })
.then(data => { // .then(data => {
// If logged in, update Login button to display username // // If logged in, update Login button to display username
const authBtn = document.getElementById('auth-btn'); // const authBtn = document.getElementById('auth-btn');
authBtn.textContent = data.username; // authBtn.textContent = data.username;
authBtn.href = '#'; // Disable navigation for username display // authBtn.href = '#'; // Disable navigation for username display
authBtn.classList.remove('login-btn'); // authBtn.classList.remove('login-btn');
authBtn.classList.add('username-btn'); // authBtn.classList.add('username-btn');
//
// Show the Logout button // // Show the Logout button
document.getElementById('logout-btn').style.display = 'block'; // document.getElementById('logout-btn').style.display = 'block';
}) // })
.catch(() => { // .catch(() => {
// If not logged in, reset the buttons to default state // // If not logged in, reset the buttons to default state
const authBtn = document.getElementById('auth-btn'); // const authBtn = document.getElementById('auth-btn');
authBtn.textContent = 'Login'; // authBtn.textContent = 'Login';
authBtn.href = '/html/login.html'; // Link back to the login page // authBtn.href = '/html/login.html'; // Link back to the login page
authBtn.classList.add('login-btn'); // authBtn.classList.add('login-btn');
authBtn.classList.remove('username-btn'); // authBtn.classList.remove('username-btn');
//
// Hide the Logout button // // Hide the Logout button
document.getElementById('logout-btn').style.display = 'none'; // document.getElementById('logout-btn').style.display = 'none';
}); // });
} // }
{
"id": "16ffd25e-f706-4a5e-812c-fbb9b11e75cc",
"version": "2.0",
"name": "sportsLeagueTest",
"url": "http://localhost:8080",
"tests": [{
"id": "0664bd87-4218-41d5-833e-1d7ce52c2608",
"name": "testLoginandMatchTest",
"commands": [{
"id": "209c5b55-a832-4a2b-9372-e728e2c7aca6",
"comment": "",
"command": "open",
"target": "/html/login.html",
"targets": [],
"value": ""
}, {
"id": "5c887cdc-2af2-4872-b805-045796a73e08",
"comment": "",
"command": "setWindowSize",
"target": "652x672",
"targets": [],
"value": ""
}, {
"id": "6159b882-bf7e-4a0c-a26a-699e36d738fd",
"comment": "",
"command": "click",
"target": "id=email",
"targets": [
["id=email", "id"],
["name=email", "name"],
["css=#email", "css:finder"],
["xpath=//input[@id='email']", "xpath:attributes"],
["xpath=//form[@id='loginForm']/input", "xpath:idRelative"],
["xpath=//input", "xpath:position"]
],
"value": ""
}, {
"id": "b4176d53-16e3-4162-8c95-d4dbf0a4ba3c",
"comment": "",
"command": "type",
"target": "id=email",
"targets": [
["id=email", "id"],
["name=email", "name"],
["css=#email", "css:finder"],
["xpath=//input[@id='email']", "xpath:attributes"],
["xpath=//form[@id='loginForm']/input", "xpath:idRelative"],
["xpath=//input", "xpath:position"]
],
"value": "shy@creditsafe.com"
}, {
"id": "8248a227-e5d9-43e7-859a-aff3c895e870",
"comment": "",
"command": "click",
"target": "id=password",
"targets": [
["id=password", "id"],
["name=password", "name"],
["css=#password", "css:finder"],
["xpath=//input[@id='password']", "xpath:attributes"],
["xpath=//form[@id='loginForm']/input[2]", "xpath:idRelative"],
["xpath=//input[2]", "xpath:position"]
],
"value": ""
}, {
"id": "73cfb95a-0326-48e4-b97a-06369d4eb339",
"comment": "",
"command": "type",
"target": "id=password",
"targets": [
["id=password", "id"],
["name=password", "name"],
["css=#password", "css:finder"],
["xpath=//input[@id='password']", "xpath:attributes"],
["xpath=//form[@id='loginForm']/input[2]", "xpath:idRelative"],
["xpath=//input[2]", "xpath:position"]
],
"value": "shy"
}, {
"id": "44b3048d-06b1-4a31-be86-28dff3f52d98",
"comment": "",
"command": "click",
"target": "css=button",
"targets": [
["css=button", "css:finder"],
["xpath=//button[@type='submit']", "xpath:attributes"],
["xpath=//form[@id='loginForm']/button", "xpath:idRelative"],
["xpath=//button", "xpath:position"],
["xpath=//button[contains(.,'Login')]", "xpath:innerText"]
],
"value": ""
}, {
"id": "5f6090f4-5d83-417c-8a49-7b85e60a580b",
"comment": "",
"command": "click",
"target": "id=sportSelect",
"targets": [
["id=sportSelect", "id"],
["css=#sportSelect", "css:finder"],
["xpath=//select[@id='sportSelect']", "xpath:attributes"],
["xpath=//select", "xpath:position"]
],
"value": ""
}, {
"id": "983cf95d-9003-4cda-9e7f-b6ad1b8873a8",
"comment": "",
"command": "select",
"target": "id=sportSelect",
"targets": [],
"value": "label=Pools"
}, {
"id": "b286ccc1-6cad-4701-b408-50086c7184a1",
"comment": "",
"command": "click",
"target": "id=sportSelect",
"targets": [
["id=sportSelect", "id"],
["css=#sportSelect", "css:finder"],
["xpath=//select[@id='sportSelect']", "xpath:attributes"],
["xpath=//select", "xpath:position"]
],
"value": ""
}, {
"id": "1441088d-98fa-436d-b89c-923b26c5c272",
"comment": "",
"command": "select",
"target": "id=sportSelect",
"targets": [],
"value": "label=Darts"
}, {
"id": "565747ec-9749-4145-9313-0a5ae850264c",
"comment": "",
"command": "click",
"target": "id=sportSelect",
"targets": [
["id=sportSelect", "id"],
["css=#sportSelect", "css:finder"],
["xpath=//select[@id='sportSelect']", "xpath:attributes"],
["xpath=//select", "xpath:position"]
],
"value": ""
}, {
"id": "7a51ec33-f510-4479-8aea-4a1f3522174a",
"comment": "",
"command": "select",
"target": "id=sportSelect",
"targets": [],
"value": "label=Table Tennis"
}, {
"id": "950a1838-f63d-4d69-a83f-01926ff620c2",
"comment": "",
"command": "click",
"target": "id=sportSelect",
"targets": [
["id=sportSelect", "id"],
["css=#sportSelect", "css:finder"],
["xpath=//select[@id='sportSelect']", "xpath:attributes"],
["xpath=//select", "xpath:position"]
],
"value": ""
}, {
"id": "d5e44974-5e91-4255-8715-1794a9f6cbfe",
"comment": "",
"command": "select",
"target": "id=sportSelect",
"targets": [],
"value": "label=All Sports"
}, {
"id": "3d13848c-e203-471a-bc44-e0b8d60a8fd9",
"comment": "",
"command": "click",
"target": "id=statusSelect",
"targets": [
["id=statusSelect", "id"],
["css=#statusSelect", "css:finder"],
["xpath=//select[@id='statusSelect']", "xpath:attributes"],
["xpath=//select[2]", "xpath:position"]
],
"value": ""
}, {
"id": "8e084da0-e59b-4ac9-b50e-dc609086e34e",
"comment": "",
"command": "select",
"target": "id=statusSelect",
"targets": [],
"value": "label=Pending"
}, {
"id": "e023b21b-ed4a-4454-9aab-a93a741636f8",
"comment": "",
"command": "click",
"target": "id=statusSelect",
"targets": [
["id=statusSelect", "id"],
["css=#statusSelect", "css:finder"],
["xpath=//select[@id='statusSelect']", "xpath:attributes"],
["xpath=//select[2]", "xpath:position"]
],
"value": ""
}, {
"id": "cabca20b-76d7-493d-bee1-bf3dbaa34760",
"comment": "",
"command": "select",
"target": "id=statusSelect",
"targets": [],
"value": "label=Confirmed"
}, {
"id": "fb3a64d1-e62c-41db-acde-91dca7b84a39",
"comment": "",
"command": "click",
"target": "id=statusSelect",
"targets": [
["id=statusSelect", "id"],
["css=#statusSelect", "css:finder"],
["xpath=//select[@id='statusSelect']", "xpath:attributes"],
["xpath=//select[2]", "xpath:position"]
],
"value": ""
}, {
"id": "b372f9e9-eb79-47c2-b82a-11cbdf162f93",
"comment": "",
"command": "select",
"target": "id=statusSelect",
"targets": [],
"value": "label=Completed"
}, {
"id": "d6905fbf-9814-49c2-bff6-b7cd4ffd0186",
"comment": "",
"command": "click",
"target": "id=statusSelect",
"targets": [
["id=statusSelect", "id"],
["css=#statusSelect", "css:finder"],
["xpath=//select[@id='statusSelect']", "xpath:attributes"],
["xpath=//select[2]", "xpath:position"]
],
"value": ""
}, {
"id": "6eed2c7c-107f-48e9-a0dd-06ad5555caea",
"comment": "",
"command": "select",
"target": "id=statusSelect",
"targets": [],
"value": "label=All Sports"
}, {
"id": "0f56e0a9-75b0-40c7-a7c6-5ad2e4a07fd5",
"comment": "",
"command": "click",
"target": "id=sortTimeBtn",
"targets": [
["id=sortTimeBtn", "id"],
["css=#sortTimeBtn", "css:finder"],
["xpath=//button[@id='sortTimeBtn']", "xpath:attributes"],
["xpath=//div[2]/button", "xpath:position"],
["xpath=//button[contains(.,'Sort by Time')]", "xpath:innerText"]
],
"value": ""
}, {
"id": "0a21d7ae-0c6f-4136-94a6-8d544f42633c",
"comment": "",
"command": "click",
"target": "id=sortTimeBtn",
"targets": [
["id=sortTimeBtn", "id"],
["css=#sortTimeBtn", "css:finder"],
["xpath=//button[@id='sortTimeBtn']", "xpath:attributes"],
["xpath=//div[2]/button", "xpath:position"],
["xpath=//button[contains(.,'Sort by Time')]", "xpath:innerText"]
],
"value": ""
}, {
"id": "b985fc2e-1068-455b-b212-23c940790db5",
"comment": "",
"command": "click",
"target": "css=.match-item:nth-child(1)",
"targets": [
["css=.match-item:nth-child(1)", "css:finder"],
["xpath=//div[@id='matchesList']/div", "xpath:idRelative"],
["xpath=//div[3]/div[3]/div", "xpath:position"]
],
"value": ""
}, {
"id": "a02a6189-a333-4149-bd8c-696869367f06",
"comment": "",
"command": "click",
"target": "id=updateBtn",
"targets": [
["id=updateBtn", "id"],
["css=#updateBtn", "css:finder"],
["xpath=//button[@id='updateBtn']", "xpath:attributes"],
["xpath=//div[@id='matchDetails']/button", "xpath:idRelative"],
["xpath=//button", "xpath:position"],
["xpath=//button[contains(.,'Update Score')]", "xpath:innerText"]
],
"value": ""
}, {
"id": "69365172-4d89-44d1-8959-1bf8d3f6db26",
"comment": "",
"command": "click",
"target": "id=matchDetails",
"targets": [
["id=matchDetails", "id"],
["css=#matchDetails", "css:finder"],
["xpath=//div[@id='matchDetails']", "xpath:attributes"],
["xpath=//div[3]/div", "xpath:position"]
],
"value": ""
}, {
"id": "d3db7118-ff77-4602-8e55-fcb0c7d1264f",
"comment": "",
"command": "type",
"target": "id=scoreA",
"targets": [
["id=scoreA", "id"],
["name=scoreA", "name"],
["css=#scoreA", "css:finder"],
["xpath=//input[@id='scoreA']", "xpath:attributes"],
["xpath=//form[@id='updateMatchForm']/input", "xpath:idRelative"],
["xpath=//input", "xpath:position"]
],
"value": "1"
}, {
"id": "d78e77a7-1fd2-4acc-a5b1-0f785d6b6ccc",
"comment": "",
"command": "click",
"target": "css=button:nth-child(23)",
"targets": [
["css=button:nth-child(23)", "css:finder"],
["xpath=//button[@type='submit']", "xpath:attributes"],
["xpath=//form[@id='updateMatchForm']/button", "xpath:idRelative"],
["xpath=//button", "xpath:position"],
["xpath=//button[contains(.,'Update Score')]", "xpath:innerText"]
],
"value": ""
}]
}],
"suites": [{
"id": "c5b624ea-1ae7-4147-b21a-7ebf68e013d9",
"name": "Default Suite",
"persistSession": false,
"parallel": false,
"timeout": 300,
"tests": ["0664bd87-4218-41d5-833e-1d7ce52c2608"]
}],
"urls": ["http://localhost:8080/"],
"plugins": []
}
\ No newline at end of file
package uk.ac.cf.spring.demo;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import io.github.bonigarcia.wdm.WebDriverManager;
import static org.junit.Assert.assertTrue;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ChromeDriverTest {
@Value("${local.server.port}")
private int port;
WebDriver webDriver;
@BeforeEach
void setupTest() {
ChromeOptions options = new ChromeOptions();
options.addArguments("--remote-debugging-port=42227");
options.addArguments("--headless");
webDriver = new ChromeDriver(options);
}
@AfterEach
public void teardown() {
// 关闭浏览器
if (webDriver != null) {
webDriver.quit();
}
}
// 测试首页是否正确加载,并包含特定内容
@Test
public void greetingShouldReturnDefaultMessage() throws Exception {
// 访问首页
webDriver.get("http://localhost:" + port + "/html/matchSchedule.html");
// 验证页面中包含 "Sport" 字符串
assertTrue(webDriver.getPageSource().contains("Sport"));
}
@Test
public void loginAndSeeLoginMessage() throws Exception {
// 打开登录页面
webDriver.get("http://localhost:" + port + "/html/login.html");
// 模拟登录:找到输入框并输入数据
webDriver.findElement(By.name("email")).sendKeys("shy@creditsafe.com");
webDriver.findElement(By.name("password")).sendKeys("shy");
webDriver.findElement(By.xpath("//button[text()='Login']")).click();
// 等待页面加载,确保登录完成
Thread.sleep(2000); // 这里可以使用更专业的显式等待来代替
// 验证登录后页面中显示 "Login successful"
assertTrue(webDriver.getPageSource().contains("Login successful"));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment