目录
1、项目简介
本项目是一个基于SpringMVC、SpringBoot、MyBatis实现的Web项目,该项目主要有 上传音乐、播放音乐、收藏音乐 三大功能。用户登录成功后,即可在这里尽情的享受"云端乐潮"!
2、自动化测试
本次将在Junit + Selenium 的环境下对该Web项目进行自动化测试。
2.1 编写测试用例
2.2 封装公共API
在编写自动化测试代码的过程中,会反复用到一些常量,如 页面的URL,浏览器驱动等,可以将其封装在一个类中,这样就可以在需要的时候直接调用相应的API,提高测试效率。
2.3 编写"登录功能"自动化测试代码
(1)检查页面的URL、元素是否完整
webDriver.get(Constants.LOGIN_URL);
Constants.addImpWait();
System.out.println("###[界面]-->测试开始!###");
/**
* 界面测试
*/
//1.检查url
String actUrl = webDriver.getCurrentUrl();
String excUrl = Constants.LOGIN_URL;
Assertions.assertEquals(excUrl,actUrl,"[登录页面]-->URL测试失败!");
//2.检查页面元素是否完整
//2.1 外框
WebElement box = webDriver.findElement(By.cssSelector("#body > div"));
Assertions.assertNotNull(box,"[登录外框]-->加载失败!");
//2.2 欢迎信息
WebElement text = webDriver.findElement(By.cssSelector("#body > div > h3"));
Assertions.assertNotNull(text,"[欢迎信息]-->加载失败!");
//2.3 输入框 及 提示信息("用户名:" "密码:")
WebElement username = webDriver.findElement(By.cssSelector("#user"));
WebElement password = webDriver.findElement(By.cssSelector("#password"));
WebElement usernameText = webDriver.findElement(By.cssSelector("#body > div > div:nth-child(2) > label"));
WebElement passwordText = webDriver.findElement(By.cssSelector("#body > div > div:nth-child(3) > label"));
Assertions.assertEquals("用户名:",usernameText.getText(),"[用户名标签]-->加载失败!");
Assertions.assertEquals("密码:",passwordText.getText(),"[密码标签]-->加载失败!");
Assertions.assertNotNull(username,"[账号输入框]-->加载失败!");
Assertions.assertNotNull(password,"[密码输入框]-->加载失败!");
//2.4 登录按钮
WebElement loginButton = webDriver.findElement(By.cssSelector("#submit"));
Assertions.assertNotNull(loginButton,"[登录按钮]-->加载失败!");
System.out.println("###[界面]-->测试通过!###");
(2)输入错误的用户名或密码
/**
* 功能测试
*/
System.out.println("###[功能]-->测试开始!###");
//1. 输入 错误的 用户名和密码
username.sendKeys(Constants.WRONG_USERNAME);
password.sendKeys(Constants.WRONG_PASSWORD);
loginButton.click();
//1.1 页面出现alert弹框,点击"确定"
sleep(1000);
webDriver.switchTo().alert().accept();
Constants.addImpWait();
//1.2 页面不跳转
Assertions.assertEquals(excUrl,actUrl,"[错误的用户名和密码]-->URL不正确");
Constants.addImpWait();
(3)用户名或密码为空
//2. 输入的 用户名或密码 为空
loginButton.click();
sleep(1000);
webDriver.switchTo().alert().accept();
Constants.addImpWait();
//2.1 页面不跳转
Assertions.assertEquals(excUrl,actUrl,"[错误的用户名和密码]-->URL不正确");
(4)输入正确的用户名和密码
//3. 输入正确的 登录信息
Constants.addImpWait();
username.sendKeys(Constants.RIGHT_USERNAME);
password.sendKeys(Constants.RIGHT_PASSWORD);
loginButton.click();
sleep(1000);
webDriver.switchTo().alert().accept();
//3.1 页面跳转
Constants.addImpWait();
Assertions.assertEquals(Constants.LIST_URL,webDriver.getCurrentUrl(),"[正确的的用户名和密码]-->URL不正确");
System.out.println("###[功能]-->测试通过!###");
执行代码,【登录功能】测试结果:
2.4 编写"播放音乐" 自动化测试代码
Constants.addImpWait();
System.out.println("******[播放音乐]-->测试开始!******");
/**
* 界面测试
*/
System.out.println("###[界面]-->测试开始!###");
//1.检查URL
String excUrl = Constants.LIST_URL;
String ActUrl = webDriver.getCurrentUrl();
Assertions.assertEquals(excUrl,ActUrl,"[播放音乐]-->URL不正确!");
Constants.addImpWait();
//2.检查页面元素
// 喜欢、删除、添加歌曲、删除选中、喜欢列表、播放歌曲
WebElement love = webDriver.findElement(By.cssSelector("#info > tr:nth-child(1) > td:nth-child(5) > button:nth-child(2)"));
Constants.addImpWait();
WebElement remove = webDriver.findElement(By.cssSelector("#info > tr:nth-child(1) > td:nth-child(5) > button:nth-child(1)"));
Constants.addImpWait();
WebElement add = webDriver.findElement(By.cssSelector("#body > div.container > div:nth-child(3) > a:nth-child(2)"));
Constants.addImpWait();
WebElement removeMore = webDriver.findElement(By.cssSelector("#delete"));
Constants.addImpWait();
WebElement loveList = webDriver.findElement(By.cssSelector("#body > div.container > div:nth-child(3) > a:nth-child(1)"));
Constants.addImpWait();
WebElement playMusic = webDriver.findElement(By.cssSelector("#info > tr:nth-child(1) > td:nth-child(4) > button"));
Constants.addImpWait();
Assertions.assertNotNull(love,"[喜欢]-->元素为空!");
Assertions.assertNotNull(remove,"[删除]-->元素为空!");
Assertions.assertNotNull(removeMore,"[批量删除]-->元素为空!");
Assertions.assertNotNull(loveList,"[喜欢列表]-->元素为空!");
Assertions.assertNotNull(playMusic,"[播放音乐]-->查询元素为空!");
Assertions.assertNotNull(add,"[添加音乐]-->元素为空!");
System.out.println("###[界面]-->测试通过!###");
/**
* 功能测试
*/
System.out.println("###[功能]-->测试开始!###");
playMusic.click();
sleep(15000);
System.out.println("###[功能]-->测试通过!###");
System.out.println("******[播放音乐]-->测试结束!******");
执行代码,【播放音乐】测试结果:
2.5 编写"收藏音乐" 自动化测试代码
System.out.println("******[收藏音乐]-->测试开始!******");
/**
* 界面测试
*/
System.out.println("###[界面]-->测试开始!###");
//1.点击 喜欢列表
WebElement loveList = webDriver.findElement(By.cssSelector("#body > div.container > div:nth-child(3) > a:nth-child(1)"));
loveList.click();
sleep(2000);
//2.检查URL
String excUrl = Constants.LOVE_URL;
Assertions.assertEquals(excUrl,webDriver.getCurrentUrl(),"[收藏音乐]-->URL不正确!");
//3.检查页面元素
// 喜欢、删除、添加歌曲、删除选中、喜欢列表、播放歌曲
WebElement back = webDriver.findElement(By.cssSelector("body > div.container > div:nth-child(3) > a"));
WebElement remove = webDriver.findElement(By.cssSelector("#info > tr:nth-child(1) > td:nth-child(4) > button"));
Assertions.assertNotNull(back,"[回到首页]-->元素为空!");
Assertions.assertNotNull(remove,"[移除]-->元素为空!");
System.out.println("###[界面]-->测试通过!###");
//4.回到 "音乐列表"页面
back.click();
Constants.addImpWait();
/**
* 功能测试
*/
System.out.println("###[功能]-->测试开始!###");
//1. 检查URL
Assertions.assertEquals(Constants.LIST_URL,webDriver.getCurrentUrl(),"[回到首页]-->URL跳转失败!");
sleep(1000);
//2. 点击 "喜欢"
webDriver.findElement(By.xpath("//*[@id=\"info\"]/tr[1]/td[4]/button[2]")).click();
sleep(1000);
webDriver.switchTo().alert().accept();
//3.页面不跳转
Assertions.assertEquals(Constants.LIST_URL,webDriver.getCurrentUrl(),"[收藏音乐]-->URL非法跳转!");
System.out.println("###[功能]-->测试通过!###");
System.out.println("******[收藏音乐]-->测试结束!******");
执行代码,【收藏功能】测试结果:
2.6 编写"删除音乐" 自动化测试代码
System.out.println("******[删除音乐]-->测试开始!******");
/**
* 界面测试
*/
System.out.println("###[界面]-->测试开始!###");
//1. 检查URL
Assertions.assertEquals(Constants.LIST_URL,webDriver.getCurrentUrl(),"[当前URL]-->URL不正确!");
//2.检查页面元素
//检查 删除、批量删除 元素
WebElement remove = webDriver.findElement(By.cssSelector("#info > tr:nth-child(1) > td:nth-child(5) > button:nth-child(1)"));
WebElement removeMore = webDriver.findElement(By.cssSelector("#delete"));
Assertions.assertNotNull(remove,"[删除一首歌曲]-->元素为空!");
Assertions.assertNotNull(removeMore,"[删除选中]-->元素为空!");
System.out.println("###[界面]-->测试通过!###");
/**
* 功能测试
*/
System.out.println("###[功能]-->测试开始!###");
//1.删除一首音乐
sleep(1000);
webDriver.findElement(By.cssSelector("#info > tr:nth-child(3) > td:nth-child(5) > button:nth-child(1)")).click();
sleep(1000);
webDriver.switchTo().alert().accept();
sleep(1000);
//2. 删除多首音乐
WebElement choice1 = webDriver.findElement(By.cssSelector("#\\36 "));
WebElement choice2 = webDriver.findElement(By.cssSelector("#\\37 "));
choice1.click();
choice2.click();
sleep(1000);
webDriver.findElement(By.cssSelector("#delete")).click();
sleep(1000);
webDriver.switchTo().alert().accept();
sleep(1000);
System.out.println("###[功能]-->测试通过!###");
System.out.println("******[删除音乐]-->测试结束!******");
执行代码,【删除音乐】测试结果:
2.7 编写"上传音乐" 自动化测试代码
System.out.println("******[上传音乐]-->测试开始!******");
/**
* 界面测试
*/
System.out.println("###[界面]-->测试开始!###");
//1. 检查URL
Assertions.assertEquals(Constants.LIST_URL,webDriver.getCurrentUrl(),"[音乐列表URL]-->URL不正确!");
//2.点击 添加歌曲
WebElement add = webDriver.findElement(By.cssSelector("#body > div.container > div:nth-child(3) > a:nth-child(2)"));
add.click();
Constants.addImpWait();
//3.再次检查URL
Assertions.assertEquals(Constants.UPLOAD_URL,webDriver.getCurrentUrl(),"[上传音乐URL]-->URL跳转不正确!");
//4.检查页面元素
WebElement selectFile = webDriver.findElement(By.cssSelector("body > form > input[type=file]:nth-child(1)"));
WebElement inputSinger = webDriver.findElement(By.cssSelector("body > form > label > input[type=text]"));
Assertions.assertNotNull(selectFile,"[选择文件]-->元素为空!");
Assertions.assertNotNull(inputSinger,"[输入框]-->元素为空!");
System.out.println("###[界面]-->测试通过!###");
System.out.println("******[上传音乐]-->测试结束!******");
执行代码,【上传音乐】测试结果:
以上就是本项目的测试流程和测试代码。