目录
一、为竞赛管理系统设计测试用例
二、根据测试用例进行测试
登录页面测试
功能测试
界面测试
用户注册页面
功能测试
界面测试
竞赛信息列表页面测试
界面测试
添加竞赛信息页面测试
功能测试
界面测试
修改竞赛信息页面测试
功能测试
定义驱动为全局变量,在测试之前,需要获取到驱动。
public static EdgeDriver getDriver(){
if(driver == null){
synchronized (PrepareTest.class){
if(driver == null){
EdgeOptions options = new EdgeOptions();
options.addArguments("--remote-allow-origins=*");
driver = new EdgeDriver(options);
}
}
}
return driver;
}
定义start()和close()方法确保测试之前和测试之后都执行一次这两个方法。
@BeforeAll
public static void start(){
driver = getDriver();
driver.get("http://43.143.208.132:8086/login.html");
//使用隐式等待渲染页面完成
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
}
@AfterAll
public static void close(){
driver.quit();
}
对正确的用户名和密码进行接口测试:
/**
* 测试正确登录
*/
@ParameterizedTest
@CsvSource(value = {"Jake,12345","lucky,12345"})
public void testLoginTrue(String user,String password) throws InterruptedException {
driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(user);
driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys(password);
driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
String url = driver.getCurrentUrl();
Assertions.assertEquals("http://43.143.208.132:8086/list.html",url);
driver.navigate().back();
//使用隐式等待渲染页面完成
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
}
测试结果:
对登录名为空进行接口测试:
@ParameterizedTest
@CsvSource(value = {",12345"})
public void testLoginNullUser(String user,String password){
driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
if(password != null){
driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys(password);
}
driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
Alert alert = driver.switchTo().alert();
String tips =alert.getText();
Assertions.assertEquals("请输入登录名",tips);
}
测试结果:
对密码为空进行接口测试:
@ParameterizedTest
@CsvSource(value = {"Jake,"})
public void testLoginNullPassword(String user,String password){
driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
if(user != null){
driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(user);
}
driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
Alert alert = driver.switchTo().alert();
String tips =alert.getText();
Assertions.assertEquals("请输入密码",tips);
}
测试结果:
对用户名或密码错误接口测试:
@ParameterizedTest
@CsvSource(value = {"ioio,12345","jake,0980"})
public void testLoginErrorUser(String user,String password) throws InterruptedException {
driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(user);
driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys(password);
driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
Alert alert = driver.switchTo().alert();
String tips =alert.getText();
System.out.println("alert: " + tips);
Assertions.assertEquals("用户名或密码输入错误",tips);
alert.accept();
}
测试结果:
测试用户名中含有特殊字符的接口测试:
@ParameterizedTest
@CsvSource(value = {"yu^%$@,12345"})
public void testLoginPartUser(String user,String password) throws InterruptedException {
driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(user);
driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys(password);
driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
String url = driver.getCurrentUrl();
Assertions.assertEquals("http://43.143.208.132:8086/list.html",url);
driver.navigate().back();
//使用隐式等待渲染页面完成
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
}
测试结果:
public class InterfaceTest {
public static EdgeDriver driver;
public static EdgeDriver getDriver(){
if(driver == null){
synchronized (PrepareTest.class){
if(driver == null){
EdgeOptions options = new EdgeOptions();
options.addArguments("--remote-allow-origins=*");
driver = new EdgeDriver(options);
}
}
}
return driver;
}
@BeforeAll
public static void start(){
driver = getDriver();
driver.get("http://43.143.208.132:8086/login.html");
//使用隐式等待渲染页面完成
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
}
@AfterAll
public static void close(){
driver.quit();
}
/**
* 测试登陆文字
*/
@Test
public void testDengLu(){
String dengLu = driver.findElement(By.xpath("//*[@id=\"body\"]/div/h3")).getText();
Assertions.assertEquals(dengLu,"用户登录");
}
/**
* 测试提交按钮的文字
*/
@Test
public void testTiJiao(){
String tiJiao = driver.findElement(By.xpath("//*[@id=\"submit\"]")).getAttribute("value");
Assertions.assertEquals(tiJiao,"登录");
}
/**
* 测试用户名输入框
*/
@Test
public void testUserInput(){
WebElement webElement = driver.findElement(By.xpath("//*[@id=\"username\"]"));
Assertions.assertNotNull(webElement);
}
/**
* 测试密码输入框
*/
@Test
public void testPasswordInput(){
WebElement webElement = driver.findElement(By.xpath("//*[@id=\"password\"]"));
Assertions.assertNotNull(webElement);
}
/**
* 测试提交按钮
*/
@Test
public void testSubmit(){
WebElement webElement = driver.findElement(By.xpath("//*[@id=\"submit\"]"));
Assertions.assertNotNull(webElement);
}
}
测试结果:
首先定义start()方法和close()方法:
@BeforeAll
public static void start(){
driver = getDriver();
driver.get("http://43.143.208.132:8086/addUser.html");
//使用隐式等待渲染页面完成
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
}
@AfterAll
public static void close(){
driver.quit();
}
对空的用户名进行接口测试:
/**
* 测试用户名为空
*/
@ParameterizedTest
@CsvSource(value = {",12345,12345"})
public void testNullUsername(String username,String password,String password2){
driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys(password);
driver.findElement(By.xpath("//*[@id=\"password2\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password2\"]")).sendKeys(password2);
driver.findElement(By.xpath("//*[@id=\"btn_sub\"]")).click();
Alert alert = driver.switchTo().alert();
String tips =alert.getText();
Assertions.assertEquals("请先输入用户名",tips);
alert.accept();
}
测试结果:
对密码为空进行接口测试:
/**
* 测试密码为空
*/
@ParameterizedTest
@CsvSource(value = {"tester,,12345"})
public void testNullPassword(String username,String password,String password2){
driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(username);
driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password2\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password2\"]")).sendKeys(password2);
driver.findElement(By.xpath("//*[@id=\"btn_sub\"]")).click();
Alert alert = driver.switchTo().alert();
String tips =alert.getText();
Assertions.assertEquals("请先输入密码",tips);
alert.accept();
}
测试结果:
对确认密码为空进行接口测试:
/**
* 测试确认密码为空
*/
@ParameterizedTest
@CsvSource(value = {"tester,12345,"})
public void testNullPassword2(String username,String password,String password2){
driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(username);
driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys(password);
driver.findElement(By.xpath("//*[@id=\"password2\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"btn_sub\"]")).click();
Alert alert = driver.switchTo().alert();
String tips =alert.getText();
Assertions.assertEquals("请先输入确认密码",tips);
alert.accept();
}
测试结果:
密码与确认密码不一致的接口测试:
/**
* 测试两次密码不一致
*/
@ParameterizedTest
@CsvSource(value = {"tester,12345,09876"})
public void testNOtEqual(String username,String password,String password2){
driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(username);
driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys(password);
driver.findElement(By.xpath("//*[@id=\"password2\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password2\"]")).sendKeys(password2);
driver.findElement(By.xpath("//*[@id=\"btn_sub\"]")).click();
Alert alert = driver.switchTo().alert();
String tips =alert.getText();
Assertions.assertEquals("两次密码输入不一致,请重新输入",tips);
alert.accept();
}
测试结果:
用户已存在的接口测试:
/**
* 测试用户已存在
*/
@ParameterizedTest
@CsvSource(value = {"jake,12345,12345"})
public void testUserExit(String username,String password,String password2){
driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(username);
driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys(password);
driver.findElement(By.xpath("//*[@id=\"password2\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password2\"]")).sendKeys(password2);
driver.findElement(By.xpath("//*[@id=\"btn_sub\"]")).click();
Alert alert = driver.switchTo().alert();
String tips =alert.getText();
Assertions.assertEquals("用户已存在",tips);
alert.accept();
}
测试结果:
用户名或密码含有特殊字符的接口测试:
/**
* 测试用户名或密码中含有特殊字符
*/
@ParameterizedTest
@CsvSource(value = {"j*@^,12345,12345","pong,*&^%),*&^%)"})
public void testPartUser(String username,String password,String password2){
driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(username);
driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys(password);
driver.findElement(By.xpath("//*[@id=\"password2\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password2\"]")).sendKeys(password2);
driver.findElement(By.xpath("//*[@id=\"btn_sub\"]")).click();
Alert alert = driver.switchTo().alert();
String tips =alert.getText();
Assertions.assertEquals("添加成功",tips);
alert.accept();
start();
}
测试结果:
正常注册的接口测试:
/**
* 正常注册
*/
@ParameterizedTest
@CsvSource(value = {"kuqi,12345,12345"})
public void testPartUser(String username,String password,String password2){
driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(username);
driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys(password);
driver.findElement(By.xpath("//*[@id=\"password2\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password2\"]")).sendKeys(password2);
driver.findElement(By.xpath("//*[@id=\"btn_sub\"]")).click();
Alert alert = driver.switchTo().alert();
String tips =alert.getText();
Assertions.assertEquals("添加成功",tips);
alert.accept();
}
测试结果:
public class InterfaceTest {
public static EdgeDriver driver;
public static EdgeDriver getDriver(){
if(driver == null){
synchronized (PrepareTest.class){
if(driver == null){
EdgeOptions options = new EdgeOptions();
options.addArguments("--remote-allow-origins=*");
driver = new EdgeDriver(options);
}
}
}
return driver;
}
@BeforeAll
public static void start(){
driver = getDriver();
driver.get("http://43.143.208.132:8086/addUser.html");
//使用隐式等待渲染页面完成
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
}
@AfterAll
public static void close(){
driver.quit();
}
/**
* 测试登陆文字
*/
@Test
public void testDengLu(){
String dengLu = driver.findElement(By.xpath("/html/body/div/h3")).getText();
Assertions.assertEquals(dengLu,"注册用户");
}
/**
* 测试提交按钮的文字
*/
@Test
public void testTiJiao(){
String tiJiao = driver.findElement(By.xpath("//*[@id=\"btn_sub\"]")).getAttribute("value");
Assertions.assertEquals(tiJiao,"提交");
}
/**
* 测试用户名输入框
*/
@Test
public void testUserInput(){
WebElement webElement = driver.findElement(By.xpath("//*[@id=\"username\"]"));
Assertions.assertNotNull(webElement);
}
/**
* 测试密码输入框
*/
@Test
public void testPasswordInput(){
WebElement webElement = driver.findElement(By.xpath("//*[@id=\"password\"]"));
Assertions.assertNotNull(webElement);
}
/**
* 测试确认密码输入框
*/
@Test
public void testPassword2Input(){
WebElement webElement = driver.findElement(By.xpath("//*[@id=\"password2\"]"));
Assertions.assertNotNull(webElement);
}
/**
* 测试提交按钮
*/
@Test
public void testSubmit(){
WebElement webElement = driver.findElement(By.xpath("//*[@id=\"btn_sub\"]"));
Assertions.assertNotNull(webElement);
}
}
测试结果:
首先定义start()方法和close()方法:
@BeforeAll
public static void start(){
driver = getDriver();
driver.get("http://43.143.208.132:8086/login.html");
//使用隐式等待渲染页面完成
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys("jake");
driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys("12345");
driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
}
@AfterAll
public static void close(){
driver.quit();
}
点击修改按钮跳转到登录页面的测试:
/**
* 修改按钮跳转
*/
@Test
public void testPartUser() {
driver.findElement(By.xpath("//*[@id=\"info\"]/tr[1]/th[9]/a[1]")).click();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
String url = driver.getCurrentUrl();
Assertions.assertEquals("http://43.143.208.132:8086/update.html?competition_id=1",url);
}
测试结果:
测试删除按钮:
/**
* 测试删除按钮
*/
@Test
public void DeleteTest() {
driver.findElement(By.xpath("//*[@id=\"info\"]/tr[1]/th[9]/a[2]")).click();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
Alert alert = driver.switchTo().alert();
String tips =alert.getText();
System.out.println("alert: " + tips);
Assertions.assertEquals("确认删除本条竞赛信息吗?一旦删除,数据不可恢复!",tips);
alert.accept();
}
测试结果:
测试退出按钮:
/**
* 测试退出按钮
*/
@Test
public void exitTest() {
driver.findElement(By.xpath("/html/body/nav/div/button[1]")).click();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
String url = driver.getCurrentUrl();
Assertions.assertEquals("http://43.143.208.132:8086/login.html",url);
}
测试结果:
测试注销按钮:
/**
* 测试注销按钮
*/
@Test
public void logoutTest() {
driver.findElement(By.xpath("/html/body/nav/div/button[2]")).click();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
Alert alert = driver.switchTo().alert();
String tips =alert.getText();
System.out.println("alert: " + tips);
Assertions.assertEquals("确认注销当前用户吗",tips);
alert.dismiss();
String url = driver.getCurrentUrl();
Assertions.assertEquals("http://43.143.208.132:8086/list.html",url);
}
测试结果:
public class InterfaceTest {
public static EdgeDriver driver;
public static EdgeDriver getDriver(){
if(driver == null){
synchronized (PrepareTest.class){
if(driver == null){
EdgeOptions options = new EdgeOptions();
options.addArguments("--remote-allow-origins=*");
driver = new EdgeDriver(options);
}
}
}
return driver;
}
@BeforeAll
public static void start() throws InterruptedException {
driver = getDriver();
driver.get("http://43.143.208.132:8086/login.html");
//使用隐式等待渲染页面完成
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(18));
driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys("lucky");
driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys("12345");
driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
Thread.sleep(5);
}
@AfterAll
public static void close(){
driver.quit();
}
/**
* 测试页面文字
*/
@Test
public void testWord(){
String word = driver.findElement(By.xpath("/html/body/nav/div/div/h4")).getText();
Assertions.assertEquals(word,"欢迎进入竞赛信息访问系统");
}
/**
* 测试按钮
*/
@Test
public void testButton(){
WebElement delete = driver.findElement(By.xpath("//*[@id=\"delete\"]"));
Assertions.assertNotNull(delete);
WebElement exit = driver.findElement(By.xpath("/html/body/nav/div/button[1]"));
Assertions.assertNotNull(exit);
WebElement logout = driver.findElement(By.xpath("/html/body/nav/div/button[2]"));
Assertions.assertNotNull(logout);
}
/**
* 测试条件查询输入框
*/
@Test
public void testSelectInput(){
WebElement webElement = driver.findElement(By.xpath("//*[@id=\"competition_name\"]"));
Assertions.assertNotNull(webElement);
WebElement webElement1 = driver.findElement(By.xpath("//*[@id=\"publication_date\"]"));
Assertions.assertNotNull(webElement1);
WebElement webElement2 = driver.findElement(By.xpath("//*[@id=\"submission_deadline\"]"));
Assertions.assertNotNull(webElement2);
}
/**
* 测试分页按钮
*/
@Test
public void limitButton(){
WebElement first = driver.findElement(By.xpath("//*[@id=\"all\"]/li[1]/a"));
Assertions.assertNotNull(first);
WebElement pre = driver.findElement(By.xpath("//*[@id=\"all\"]/li[2]"));
Assertions.assertNotNull(pre);
WebElement next = driver.findElement(By.xpath("//*[@id=\"all\"]/li[3]/a"));
Assertions.assertNotNull(next);
WebElement last = driver.findElement(By.xpath("//*[@id=\"all\"]/li[4]/a"));
Assertions.assertNotNull(last);
}
/**
* 测试分页信息
*/
@Test
public void limitWord(){
String text = driver.findElement(By.xpath("//*[@id=\"pageinfo\"]")).getText();
Assertions.assertEquals("共10条数据,共1页",text);
}
}
测试结果:
此处的输入框为空的逻辑与注册用户的逻辑基本一致,便不再赘述。
的定义start()和close()方法。
@BeforeAll
public static void start(){
driver = getDriver();
driver.get("http://43.143.208.132:8086/login.html");
//使用隐式等待渲染页面完成
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(18));
driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys("lucky");
driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys("12345");
driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
driver.findElement(By.xpath("/html/body/div/div[2]/a[1]")).click();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
}
@AfterAll
public static void close(){
driver.quit();
}
添加时间格式不正确测试:
/**
* 时间格式不符合
*/
@Test
public void TimeTest(){
driver.findElement(By.xpath("//*[@id=\"competition_name\"]")).sendKeys("互联网+大赛");
driver.findElement(By.xpath("//*[@id=\"competition_description\"]")).sendKeys("科创");
driver.findElement(By.xpath("//*[@id=\"publication_date\"]")).sendKeys("2023-09-12");
driver.findElement(By.xpath("//*[@id=\"submission_deadline\"]")).sendKeys("2023-09-15");
driver.findElement(By.xpath("//*[@id=\"sponsor\"]")).sendKeys("lucky");
driver.findElement(By.xpath("//*[@id=\"venue\"]")).sendKeys("实验楼");
driver.findElement(By.xpath("//*[@id=\"btn_sub\"]")).click();
Alert alert = driver.switchTo().alert();
String tips =alert.getText();
Assertions.assertEquals("时间格式输入不正确,请重新输入",tips);
alert.accept();
}
测试结果:
竞赛已存在添加测试:
/**
* 竞赛信息已存在
*/
@Test
public void ExitTest(){
driver.findElement(By.xpath("//*[@id=\"competition_name\"]")).sendKeys("乘风破浪的姐姐");
driver.findElement(By.xpath("//*[@id=\"competition_description\"]")).sendKeys("综艺");
driver.findElement(By.xpath("//*[@id=\"publication_date\"]")).sendKeys("2023-09-12 09:12:12");
driver.findElement(By.xpath("//*[@id=\"submission_deadline\"]")).sendKeys("2023-09-15 12:12:12");
driver.findElement(By.xpath("//*[@id=\"sponsor\"]")).sendKeys("lucky");
driver.findElement(By.xpath("//*[@id=\"venue\"]")).sendKeys("实验楼");
driver.findElement(By.xpath("//*[@id=\"btn_sub\"]")).click();
Alert alert = driver.switchTo().alert();
String tips =alert.getText();
Assertions.assertEquals("添加失败",tips);
alert.accept();
}
测试结果:
正确添加竞赛信息测试:
/**
* 正确添加竞赛信息
*/
@Test
public void NormalTest(){
driver.findElement(By.xpath("//*[@id=\"competition_name\"]")).sendKeys("披风战绩的哥哥");
driver.findElement(By.xpath("//*[@id=\"competition_description\"]")).sendKeys("综艺");
driver.findElement(By.xpath("//*[@id=\"publication_date\"]")).sendKeys("2023-09-12 09:12:12");
driver.findElement(By.xpath("//*[@id=\"submission_deadline\"]")).sendKeys("2023-09-15 12:12:12");
driver.findElement(By.xpath("//*[@id=\"sponsor\"]")).sendKeys("lucky");
driver.findElement(By.xpath("//*[@id=\"venue\"]")).sendKeys("实验楼");
driver.findElement(By.xpath("//*[@id=\"btn_sub\"]")).click();
Alert alert = driver.switchTo().alert();
String tips =alert.getText();
Assertions.assertEquals("添加成功",tips);
alert.accept();
}
测试结果:
测试返回按钮:
/**
* 测试返回按钮
*/
@Test
public void ExitTest(){
driver.findElement(By.xpath("//*[@id=\"btn_back\"]")).click();
String url = driver.getCurrentUrl();
Assertions.assertEquals("http://43.143.208.132:8086/list.html",url);
}
测试结果:
public class InterfaceTest {
public static EdgeDriver driver;
public static EdgeDriver getDriver(){
if(driver == null){
synchronized (PrepareTest.class){
if(driver == null){
EdgeOptions options = new EdgeOptions();
options.addArguments("--remote-allow-origins=*");
driver = new EdgeDriver(options);
}
}
}
return driver;
}
@BeforeAll
public static void start(){
driver = getDriver();
driver.get("http://43.143.208.132:8086/login.html");
//使用隐式等待渲染页面完成
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(18));
driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys("lucky");
driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys("12345");
driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
driver.findElement(By.xpath("/html/body/div/div[2]/a[1]")).click();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
}
@AfterAll
public static void close(){
driver.quit();
}
/**
* 测试输入框
*/
@Test
public void InputTest(){
WebElement element1 = driver.findElement(By.xpath("//*[@id=\"competition_name\"]"));
Assertions.assertNotNull(element1);
WebElement element2 = driver.findElement(By.xpath("//*[@id=\"competition_description\"]"));
Assertions.assertNotNull(element2);
WebElement element3 = driver.findElement(By.xpath("//*[@id=\"publication_date\"]"));
Assertions.assertNotNull(element3);
WebElement element4 = driver.findElement(By.xpath("//*[@id=\"submission_deadline\"]"));
Assertions.assertNotNull(element4);
WebElement element5 = driver.findElement(By.xpath("//*[@id=\"sponsor\"]"));
Assertions.assertNotNull(element5);
WebElement element6 = driver.findElement(By.xpath("//*[@id=\"venue\"]"));
Assertions.assertNotNull(element6);
}
/**
* 测试标题
*/
@Test
public void testWord(){
String word = driver.findElement(By.xpath("/html/body/div/h3")).getText();
Assertions.assertEquals(word,"添加竞赛信息");
}
/**
* 测试按钮
*/
@Test
public void ButtonTest(){
WebElement element1 = driver.findElement(By.xpath("//*[@id=\"btn_sub\"]"));
Assertions.assertNotNull(element1);
WebElement element2 = driver.findElement(By.xpath("//*[@id=\"btn_back\"]"));
Assertions.assertNotNull(element2);
}
}
测试结果:
输入框为空、时间格式不符合以及按钮测试与上述页面类似,就不再赘述。
定义start()和close()方法。
@BeforeAll
public static void start(){
driver = getDriver();
driver.get("http://43.143.208.132:8086/login.html");
//使用隐式等待渲染页面完成
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(18));
driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys("lucky");
driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys("12345");
driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
driver.findElement(By.xpath("//*[@id=\"info\"]/tr[1]/th[9]/a[1]")).click();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
}
@AfterAll
public static void close(){
driver.quit();
}
数据回显是否正常测试:
/**
* 数据回显是否正常
*/
@Test
public void backTest(){
String Competition_name = driver.findElement(By.xpath("//*[@id=\"competition_name\"]")).getText();
Assertions.assertEquals(Competition_name, "乘风破浪的姐姐");
String des = driver.findElement(By.xpath("//*[@id=\"competition_description\"]")).getText();
Assertions.assertEquals(des, "综艺");
String publication_time = driver.findElement(By.xpath("//*[@id=\"publication_date\"]")).getText();
Assertions.assertEquals(publication_time, "2022-12-10 09:00:00");
String deadline_time =driver.findElement(By.xpath("//*[@id=\"submission_deadline\"]")).getText();
Assertions.assertEquals(deadline_time, "2022-12-19 09:00:00");
String sponsor = driver.findElement(By.xpath("//*[@id=\"sponsor\"]")).getText();
Assertions.assertEquals(sponsor, "娃哈哈企业");
String spot = driver.findElement(By.xpath("//*[@id=\"venue\"]")).getText();
Assertions.assertEquals(spot, "长沙");
}
测试结果: