之前写过的SSM项目,现在我们通过 selenium 自动化套件 + junit 单元测试框架,来对其功能进行自动化测试
回顾项目功能:登录、注册、修改密码、播放音乐、搜索音乐、收藏音乐、上传音乐、删除音乐(单个删除 + 批量删除)
①、登录测试:
package com.muisictest;
import com.common.commonDriver;
import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import sun.reflect.generics.tree.Tree;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.*;
/**
* Created with IntelliJ IDEA.
* Description:
* User: Lenovo
* Date: 2022-11-16
* Time: 13:22
*/
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class LoginTest extends commonDriver {
private static EdgeDriver edgeDriver = getDriver();
@Test
@BeforeAll
static void start(){
edgeDriver.get("http://124.222.242.143:8080/login.html");
}
//测试正常情况下 页面的元素是否正常
@Test
@Order(1)
public void appearance(){
String register = edgeDriver.findElement(By.cssSelector("body > section > div.register-full > div.register-right > div:nth-child(1) > a")).getText();
Assertions.assertEquals(register, "注 册 »");
String login = edgeDriver.findElement(By.cssSelector("body > section > div.register-full > div.register-right > div.register-in.middle > a")).getText();
Assertions.assertEquals(login, "登 录 »");
String csdnLink = edgeDriver.findElement(By.cssSelector("body > section > div.register-full > div.register-right > div:nth-child(3) > a")).getText();
Assertions.assertEquals(csdnLink, "项目解析 (CSDN) »");
String text = edgeDriver.findElement(By.cssSelector("#tourInfo")).getText();
Assertions.assertEquals(text, "Tourist status");
String text1 = edgeDriver.findElement(By.cssSelector("#userInfo")).getText();
Assertions.assertEquals(text1, "User identity");
String text2 = edgeDriver.findElement(By.cssSelector("body > section > div.header > div.logo > a")).getText();
Assertions.assertEquals(text2, "MUSIC");
String placeholder = edgeDriver.findElement(By.cssSelector("#newUsername")).getAttribute("placeholder");
Assertions.assertEquals(placeholder, "username");
String placeholder1 = edgeDriver.findElement(By.cssSelector("#newPassword")).getAttribute("placeholder");
Assertions.assertEquals(placeholder1, "Password");
String placeholder2 = edgeDriver.findElement(By.cssSelector("#repassword")).getAttribute("placeholder");
Assertions.assertEquals(placeholder2, "Confirm the password");
WebElement element = edgeDriver.findElement(By.cssSelector("#small-dialog2 > div > div > div > div > label > input[type=checkbox]"));
Assertions.assertNotNull(element);
String value = edgeDriver.findElement(By.cssSelector("#rsb")).getAttribute("value");
Assertions.assertEquals(value, "Register");
String placeholder3 = edgeDriver.findElement(By.cssSelector("#user")).getAttribute("placeholder");
Assertions.assertEquals(placeholder3, "Username");
String placeholder4 = edgeDriver.findElement(By.cssSelector("#password")).getAttribute("placeholder");
Assertions.assertEquals(placeholder4, "Password");
WebElement element1 = edgeDriver.findElement(By.cssSelector("#submit"));
Assertions.assertNotNull(element1);
String href = edgeDriver.findElement(By.cssSelector("body > section > div.register-full > div.register-right > div:nth-child(3) > a")).getAttribute("href");
Assertions.assertEquals(href, "https://blog.csdn.net/baiyang2001/article/details/125936231?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22125936231%22%2C%22source%22%3A%22baiyang2001%22%7D&ctrtid=7kAqu");
}
//测试窗口
@Test
@Order(2)
void windowSize(){
edgeDriver.manage().window().maximize();
appearance();
edgeDriver.manage().window().minimize();
appearance();
}
//username都是错误的 4种长度密码 都不能登录 username对 4种长度密码
@ParameterizedTest
@CsvSource(value = {"a,123456", "b,12345", "c,1234567", "d,12345678",
"e,123456", "f,12345", "g,1234567", "h,12345678"})
@Order(3)
void Flogin(String username, String password) throws InterruptedException {
edgeDriver.findElement(By.cssSelector("body > section > div.register-full > div.register-right > div.register-in.middle > a")).click();
edgeDriver.findElement(By.cssSelector("#user")).sendKeys(username);
edgeDriver.findElement(By.cssSelector("#password")).sendKeys(password);
edgeDriver.findElement(By.cssSelector("#submit")).click();
Thread.sleep(2000);
Alert alert = edgeDriver.switchTo().alert();
alert.accept();
edgeDriver.findElement(By.cssSelector("#small-dialog > button")).click();
}
//正确登录
@ParameterizedTest
@CsvSource(value = {"bit,123456", "cit,1234567", "dit,12345678"})
@Order(4)
void Tlogin(String username, String password) throws InterruptedException {
String windowHandle = edgeDriver.getWindowHandle();
edgeDriver.switchTo().window(windowHandle);
edgeDriver.findElement(By.cssSelector("body > section > div.register-full > div.register-right > div.register-in.middle > a")).click();
edgeDriver.findElement(By.cssSelector("#user")).clear();
edgeDriver.findElement(By.cssSelector("#password")).clear();
edgeDriver.findElement(By.cssSelector("#user")).sendKeys(username);
edgeDriver.findElement(By.cssSelector("#password")).sendKeys(password);
edgeDriver.findElement(By.cssSelector("#submit")).click();
Thread.sleep(2000);
Alert alert = edgeDriver.switchTo().alert();
alert.accept();
Set<String> windowHandles = edgeDriver.getWindowHandles();
for(String window : windowHandles){
if(window != windowHandle){
edgeDriver.switchTo().window(window);
break;
}
}
String text2 = edgeDriver.findElement(By.cssSelector("#search")).getAttribute("value");
Assertions.assertEquals(text2, "点击查询");
String text = edgeDriver.findElement(By.xpath("/html/body/div/div[3]/div[1]/div[1]/div/h2")).getText();
Assertions.assertNotNull(text);
edgeDriver.navigate().back();
edgeDriver.switchTo().window(windowHandle);
}
//账户名 密码为空
@Test
@Order(5)
void Nlogin() throws InterruptedException {
edgeDriver.findElement(By.cssSelector("body > section > div.register-full > div.register-right > div.register-in.middle > a")).click();
edgeDriver.findElement(By.cssSelector("#submit")).click();
Thread.sleep(2000);
Alert alert = edgeDriver.switchTo().alert();
alert.accept();
edgeDriver.findElement(By.cssSelector("#small-dialog > button")).click();
}
@Test
@Order(6)
void ONlogin1() throws InterruptedException {
Thread.sleep(1000);
edgeDriver.findElement(By.cssSelector("body > section > div.register-full > div.register-right > div.register-in.middle > a")).click();
edgeDriver.findElement(By.cssSelector("#user")).sendKeys("bit");
edgeDriver.findElement(By.cssSelector("#submit")).click();
Thread.sleep(3000);
Alert alert = edgeDriver.switchTo().alert();
alert.accept();
edgeDriver.findElement(By.cssSelector("#small-dialog > button")).click();
}
@Test
@Order(7)
void ONlogin2() throws InterruptedException {
Thread.sleep(1000);
edgeDriver.findElement(By.cssSelector("body > section > div.register-full > div.register-right > div.register-in.middle > a")).click();
edgeDriver.findElement(By.cssSelector("#password")).sendKeys("123456");
edgeDriver.findElement(By.cssSelector("#submit")).click();
Thread.sleep(3000);
Alert alert = edgeDriver.switchTo().alert();
alert.accept();
edgeDriver.findElement(By.cssSelector("#small-dialog > button")).click();
}
@Test
@Order(8)
void changePassword() throws InterruptedException {
Thread.sleep(1000);
String windowHandle = edgeDriver.getWindowHandle();
edgeDriver.findElement(By.cssSelector("body > section > div.register-full > div.register-right > div.register-in.middle > a")).click();
edgeDriver.findElement(By.cssSelector("#small-dialog > div > div > div.login-agileits-bottom > h6 > a")).click();
Set<String> windowHandles = edgeDriver.getWindowHandles();
for(String window : windowHandles){
if(window != windowHandle){
edgeDriver.switchTo().window(window);
break;
}
}
WebElement element = edgeDriver.findElement(By.cssSelector("#usb"));
Assertions.assertNotNull(element);
}
}
②、注册测试:
package com.muisictest;
import com.common.commonDriver;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import static org.junit.Assert.*;
/**
* Created with IntelliJ IDEA.
* Description:
* User: Lenovo
* Date: 2022-11-16
* Time: 21:49
*/
public class RegistTest extends commonDriver {
private static EdgeDriver edgeDriver = getDriver();
@Test
@BeforeAll
static void start(){
edgeDriver.get("http://124.222.242.143:8080/login.html");
}
@Test
void registUI(){
String placeholder1 = edgeDriver.findElement(By.cssSelector("#newUsername")).getAttribute("placeholder");
Assertions.assertEquals(placeholder1, "username");
String placeholder2 = edgeDriver.findElement(By.cssSelector("#newPassword")).getAttribute("placeholder");
Assertions.assertEquals(placeholder2, "Password");
String placeholder3 = edgeDriver.findElement(By.cssSelector("#repassword")).getAttribute("placeholder");
Assertions.assertEquals(placeholder3, "Confirm the password");
WebElement element = edgeDriver.findElement(By.cssSelector("#small-dialog2 > div > div > div > div > label > input[type=checkbox]"));
Assertions.assertNotNull(element);
WebElement element1 = edgeDriver.findElement(By.cssSelector("#rsb"));
Assertions.assertNotNull(element1);
}
//测试注册功能 现有用户 和 不存在的用户 以及两次密码不一致的用户
@ParameterizedTest
@CsvSource(value = {"bit,123456,123456", "by,123456,123456", "dy,123456,1234567", "cy,123,123"})
void registGN(String username, String password, String newPassword) throws InterruptedException {
edgeDriver.findElement(By.cssSelector("body > section > div.register-full > div.register-right > div:nth-child(1) > a")).click();
edgeDriver.findElement(By.cssSelector("#newUsername")).sendKeys(username);
edgeDriver.findElement(By.cssSelector("#newPassword")).sendKeys(password);
edgeDriver.findElement(By.cssSelector("#repassword")).sendKeys(newPassword);
edgeDriver.findElement(By.cssSelector("#rsb")).click();
Thread.sleep(3000);
Alert alert = edgeDriver.switchTo().alert();
alert.accept();
Thread.sleep(1000);
edgeDriver.findElement(By.cssSelector("#small-dialog2 > button")).click();
}
}
③、修改密码测试:
package com.muisictest;
import com.common.commonDriver;
import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import java.util.Set;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
/**
* Created with IntelliJ IDEA.
* Description:
* User: Lenovo
* Date: 2022-11-16
* Time: 23:10
*/
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class modifyTest extends commonDriver {
private static EdgeDriver edgeDriver = getDriver();
@Test
@BeforeAll
static void start(){
edgeDriver.get("http://124.222.242.143:8080/modif.html");
}
@Test
@Order(1)
void registUI(){
WebElement element = edgeDriver.findElement(By.cssSelector("body > div > div.site-blocks-cover.overlay.aos-init.aos-animate > div > div > div > div.login-dialog > div:nth-child(1) > span"));
Assertions.assertNotNull(element);
WebElement element2 = edgeDriver.findElement(By.cssSelector("body > div > div.site-blocks-cover.overlay.aos-init.aos-animate > div > div > div > div.login-dialog > div:nth-child(2) > span"));
Assertions.assertNotNull(element2);
WebElement element3 = edgeDriver.findElement(By.cssSelector("body > div > div.site-blocks-cover.overlay.aos-init.aos-animate > div > div > div > div.login-dialog > div:nth-child(3) > span"));
Assertions.assertNotNull(element3);
WebElement element1 = edgeDriver.findElement(By.cssSelector("#usb"));
Assertions.assertNotNull(element1);
}
//测试注册功能 现有用户 和 不存在的用户 以及两次密码不一致的用户
@ParameterizedTest
@CsvSource(value = { "by,1234567,123456", "dy,123456,1234567", "cy,123,123"})
void registGN(String username, String password, String newPassword) throws InterruptedException {
edgeDriver.findElement(By.cssSelector("#username")).clear();
edgeDriver.findElement(By.cssSelector("#password")).clear();
edgeDriver.findElement(By.cssSelector("#newPassword")).clear();
edgeDriver.findElement(By.cssSelector("#username")).sendKeys(username);
edgeDriver.findElement(By.cssSelector("#password")).sendKeys(password);
edgeDriver.findElement(By.cssSelector("#newPassword")).sendKeys(newPassword);
edgeDriver.findElement(By.cssSelector("#usb")).click();
Thread.sleep(3000);
Alert alert = edgeDriver.switchTo().alert();
alert.accept();
Thread.sleep(2000);
}
@ParameterizedTest
@CsvSource(value = {"bit,123456,123456"})
void registGY(String username, String password, String newPassword) throws InterruptedException {
String windowHandle = edgeDriver.getWindowHandle();
edgeDriver.findElement(By.cssSelector("#username")).sendKeys(username);
edgeDriver.findElement(By.cssSelector("#password")).sendKeys(password);
edgeDriver.findElement(By.cssSelector("#newPassword")).sendKeys(newPassword);
edgeDriver.findElement(By.cssSelector("#usb")).click();
Thread.sleep(3000);
Alert alert = edgeDriver.switchTo().alert();
alert.accept();
Set<String> windowHandles = edgeDriver.getWindowHandles();
for(String window : windowHandles){
if(window != windowHandle){
edgeDriver.switchTo().window(window);
break;
}
}
WebElement element = edgeDriver.findElement(By.cssSelector("body > section > div.header > div.logo > a"));
assertNotNull(element);
}
}
④、音乐列表页的功能测试:(搜索音乐、删除音乐、批量删除、UI测试……)
package com.muisictest;
import com.common.commonDriver;
import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import java.util.Set;
/**
* Created with IntelliJ IDEA.
* Description:
* User: Lenovo
* Date: 2022-11-16
* Time: 23:32
*/
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class MusicListTest extends commonDriver {
private static EdgeDriver edgeDriver = getDriver();
@Test
@BeforeAll
static void start() throws InterruptedException {
edgeDriver.get("http://124.222.242.143:8080/login.html");
String windowHandle = edgeDriver.getWindowHandle();
edgeDriver.findElement(By.cssSelector("body > section > div.register-full > div.register-right > div.register-in.middle > a")).click();
edgeDriver.findElement(By.cssSelector("#user")).sendKeys("bit");
edgeDriver.findElement(By.cssSelector("#password")).sendKeys("123456");
edgeDriver.findElement(By.cssSelector("#submit")).click();
Thread.sleep(3000);
Alert alert = edgeDriver.switchTo().alert();
alert.accept();
Set<String> windowHandles = edgeDriver.getWindowHandles();
for(String window : windowHandles){
if(window != windowHandle){
edgeDriver.switchTo().window(window);
break;
}
}
}
@Test
@Order(1)
void ListUI() throws InterruptedException {
String text = edgeDriver.findElement(By.xpath("/html/body/div/div[3]/div[1]/div[1]/div/h2")).getText();
Assertions.assertNotNull(text);
String text1 = edgeDriver.findElement(By.cssSelector("body > div > div.site-section.bg-light > div.container > div:nth-child(2) > span > b")).getText();
Assertions.assertEquals(text1, "查询歌曲:");
String text2 = edgeDriver.findElement(By.cssSelector("#search")).getAttribute("value");
Assertions.assertEquals(text2, "点击查询");
String value = edgeDriver.findElement(By.cssSelector("#delete")).getText();
Assertions.assertEquals(value, "删除选中");
}
//模糊查询 和 全值匹配
@ParameterizedTest
@ValueSource(strings = {"云", "云烟成雨", "雨"})
@Order(2)
void search(String name) throws InterruptedException {
// String text3 = edgeDriver.findElement(By.cssSelector("body > div > div.site-section.bg-light > div.container > div.row.mb-5.aos-init.aos-animate > div > h2")).getText();
// Assertions.assertEquals(text3, "Music list");
edgeDriver.findElement(By.cssSelector("#musictitle")).clear();
edgeDriver.findElement(By.cssSelector("#musictitle")).sendKeys(name);
edgeDriver.findElement(By.cssSelector("#search")).click();
String text = edgeDriver.findElement(By.cssSelector("#MusicList > div:nth-child(1) > div > h3")).getText();
Assertions.assertEquals(text, "云烟成雨");
Thread.sleep(1000);
//检测一下搜索后的 UI
String text1 = edgeDriver.findElement(By.cssSelector("#MusicList > div:nth-child(1) > div > input:nth-child(4)")).getAttribute("value");
Assertions.assertEquals(text1, "收藏音乐");
String value = edgeDriver.findElement(By.cssSelector("#MusicList > div:nth-child(1) > div > input:nth-child(5)")).getAttribute("value");
Assertions.assertEquals(value, "删除音乐");
WebElement element = edgeDriver.findElement(By.cssSelector("#player2"));
Assertions.assertNotNull(element);
}
@Test
@Order(5)
void loveMusic() throws InterruptedException {
edgeDriver.findElement(By.cssSelector("#MusicList > div:nth-child(1) > div > input:nth-child(4)")).click();
Thread.sleep(3000);
Alert alert = edgeDriver.switchTo().alert();
alert.accept();
edgeDriver.findElement(By.cssSelector("body > div > header > div > div > div.col-9.aos-init.aos-animate > nav > div > a > span")).click();
edgeDriver.findElement(By.cssSelector("body > div > div.site-mobile-menu > div.site-mobile-menu-body > ul > li:nth-child(4) > a")).click();
Thread.sleep(3000);
String text = edgeDriver.findElement(By.cssSelector("#MusicList > div > div > h3")).getAttribute("innerText");
Assertions.assertEquals(text, "云烟成雨");
}
@Test
@Order(3)
void deleteMusic() throws InterruptedException {
edgeDriver.navigate().refresh();
Thread.sleep(1000);
edgeDriver.findElement(By.cssSelector("#MusicList > div:nth-child(2) > div > input:nth-child(5)")).click();
Thread.sleep(3000);
Alert alert = edgeDriver.switchTo().alert();
alert.accept();
Thread.sleep(3000);
try {
WebElement element = edgeDriver.findElement(By.cssSelector("#MusicList > div:nth-child(3) > div > h3"));
}catch (Exception e){
System.out.println("删除成功");
}
}
@Test
@Order(4)
void deleteAllMusic() throws InterruptedException {
edgeDriver.navigate().refresh();
edgeDriver.findElement(By.cssSelector("#\\34 2")).click();
edgeDriver.findElement(By.cssSelector("#delete")).click();
Thread.sleep(3000);
Alert alert = edgeDriver.switchTo().alert();
alert.accept();
Thread.sleep(1000);
try {
edgeDriver.findElement(By.cssSelector("#MusicList > div:nth-child(2) > div > h3"));
}catch (Exception e){
System.out.println("通过勾选框删除成功");
}
}
}
⑤、上传文件测试:(从上传文件的多种情况测试,还有文件的格式情况考虑)
package com.muisictest;
import com.common.commonDriver;
import org.junit.jupiter.api.*;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.edge.EdgeDriver;
import java.util.Set;
/**
* Created with IntelliJ IDEA.
* Description:
* User: Lenovo
* Date: 2022-11-17
* Time: 10:09
*/
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class FileUploadTest extends commonDriver {
private static EdgeDriver edgeDriver = getDriver();
@Test
@BeforeAll
static void start() throws InterruptedException {
edgeDriver.get("http://124.222.242.143:8080/login.html");
String windowHandle = edgeDriver.getWindowHandle();
edgeDriver.findElement(By.cssSelector("body > section > div.register-full > div.register-right > div.register-in.middle > a")).click();
edgeDriver.findElement(By.cssSelector("#user")).sendKeys("bit");
edgeDriver.findElement(By.cssSelector("#password")).sendKeys("123456");
edgeDriver.findElement(By.cssSelector("#submit")).click();
Thread.sleep(3000);
Alert alert = edgeDriver.switchTo().alert();
alert.accept();
Set<String> windowHandles = edgeDriver.getWindowHandles();
for(String window : windowHandles){
if(window != windowHandle){
edgeDriver.switchTo().window(window);
break;
}
}
}
@Test
@Order(1)
void uploadUI() throws InterruptedException {
edgeDriver.findElement(By.cssSelector("body > div > header > div > div > div.col-9.aos-init.aos-animate > nav > div > a > span")).click();
edgeDriver.findElement(By.cssSelector("body > div > div.site-mobile-menu > div.site-mobile-menu-body > ul > li:nth-child(3) > a")).click();
Thread.sleep(3000);
String text = edgeDriver.findElement(By.cssSelector("#username")).getAttribute("placeholder");
Assertions.assertEquals(text, "请输入歌手名");
String text1 = edgeDriver.findElement(By.cssSelector("body > div > div.site-section.bg-light > div.login-container > form > div > div:nth-child(1) > span")).getText();
Assertions.assertEquals(text1, "文件上传");
}
//成功上传
@Test
void uploadFile1() throws InterruptedException {
edgeDriver.findElement(By.cssSelector("body > div > header > div > div > div.col-9.aos-init.aos-animate > nav > div > a > span")).click();
edgeDriver.findElement(By.cssSelector("body > div > div.site-mobile-menu > div.site-mobile-menu-body > ul > li:nth-child(3) > a")).click();
Thread.sleep(3000);
edgeDriver.findElement(By.cssSelector("body > div > div.site-section.bg-light > div.login-container > form > div > div:nth-child(1) > input[type=file]")).sendKeys("C:\\Users\\Lenovo\\Music\\起风了.mp3");
edgeDriver.findElement(By.cssSelector("#username")).sendKeys("周深");
edgeDriver.findElement(By.cssSelector("#submit")).click();
try{
String text = edgeDriver.findElement(By.cssSelector("#search")).getAttribute("value");
Assertions.assertEquals(text, "点击查询");
}catch (Exception e){
System.out.println("已用同名同歌手歌曲,上传失败");
}
}
//同名不同歌手
@Test
void uploadFile2() throws InterruptedException {
edgeDriver.findElement(By.cssSelector("body > div > header > div > div > div.col-9.aos-init.aos-animate > nav > div > a > span")).click();
edgeDriver.findElement(By.cssSelector("body > div > div.site-mobile-menu > div.site-mobile-menu-body > ul > li:nth-child(3) > a")).click();
Thread.sleep(3000);
edgeDriver.findElement(By.cssSelector("body > div > div.site-section.bg-light > div.login-container > form > div > div:nth-child(1) > input[type=file]")).sendKeys("C:\\Users\\Lenovo\\Music\\起风了.mp3");
edgeDriver.findElement(By.cssSelector("#username")).sendKeys("买辣椒不用券");
edgeDriver.findElement(By.cssSelector("#submit")).click();
try{
String text = edgeDriver.findElement(By.cssSelector("#search")).getAttribute("value");
Assertions.assertEquals(text, "点击查询");
}catch (Exception e){
System.out.println("已用同名歌曲,上传失败");
}
}
//不传文件 报错
@Test
void uploadFile3() throws InterruptedException {
edgeDriver.findElement(By.cssSelector("body > div > header > div > div > div.col-9.aos-init.aos-animate > nav > div > a > span")).click();
edgeDriver.findElement(By.cssSelector("body > div > div.site-mobile-menu > div.site-mobile-menu-body > ul > li:nth-child(3) > a")).click();
Thread.sleep(3000);
edgeDriver.findElement(By.cssSelector("#username")).sendKeys("周深");
edgeDriver.findElement(By.cssSelector("#submit")).click();
try{
String text = edgeDriver.findElement(By.cssSelector("#search")).getAttribute("value");
Assertions.assertEquals(text, "点击查询");
}catch (Exception e){
System.out.println("上传文件失败");
edgeDriver.navigate().back();
}
}
//上传的不是mp3文件
@Test
void uploadFile4() throws InterruptedException {
Thread.sleep(1000);
edgeDriver.findElement(By.cssSelector("body > div > header > div > div > div.col-9.aos-init.aos-animate > nav > div > a > span")).click();
edgeDriver.findElement(By.cssSelector("body > div > div.site-mobile-menu > div.site-mobile-menu-body > ul > li:nth-child(3) > a")).click();
Thread.sleep(3000);
edgeDriver.findElement(By.cssSelector("body > div > div.site-section.bg-light > div.login-container > form > div > div:nth-child(1) > input[type=file]")).sendKeys("C:\\Users\\Lenovo\\Music\\路过人间.txt");
edgeDriver.findElement(By.cssSelector("#username")).sendKeys("房东的猫-胡海泉");
edgeDriver.findElement(By.cssSelector("#submit")).click();
try{
String text = edgeDriver.findElement(By.cssSelector("#search")).getAttribute("value");
Assertions.assertEquals(text, "点击查询");
}catch (Exception e){
System.out.println("文件格式错误");
edgeDriver.navigate().back();
}
}
//上传修改后缀名得到的 MP3 文件 格式错误
@Test
void uploadFile5() throws InterruptedException {
Thread.sleep(1000);
edgeDriver.findElement(By.cssSelector("body > div > header > div > div > div.col-9.aos-init.aos-animate > nav > div > a > span")).click();
edgeDriver.findElement(By.cssSelector("body > div > div.site-mobile-menu > div.site-mobile-menu-body > ul > li:nth-child(3) > a")).click();
Thread.sleep(3000);
edgeDriver.findElement(By.cssSelector("body > div > div.site-section.bg-light > div.login-container > form > div > div:nth-child(1) > input[type=file]")).sendKeys("C:\\Users\\Lenovo\\Music\\城市动物园.mp3");
edgeDriver.findElement(By.cssSelector("#username")).sendKeys("房东的猫");
edgeDriver.findElement(By.cssSelector("#submit")).click();
try{
String text = edgeDriver.findElement(By.cssSelector("#search")).getAttribute("value");
Assertions.assertEquals(text, "点击查询");
}catch (Exception e){
System.out.println("文件格式错误");
}
}
}
⑥、退出测试:分别从音乐播放页、收藏页、上传页测试退出
package com.muisictest;
import com.common.commonDriver;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import java.util.Set;
/**
* Created with IntelliJ IDEA.
* Description:
* User: Lenovo
* Date: 2022-11-17
* Time: 11:14
*/
public class ExitTest extends commonDriver {
private static EdgeDriver edgeDriver = getDriver();
@Test
@BeforeAll
static void start() throws InterruptedException {
Thread.sleep(1000);
edgeDriver.get("http://124.222.242.143:8080/login.html");
}
//从列表页 退出
@Test
void ListExit() throws InterruptedException {
String windowHandle = edgeDriver.getWindowHandle();
edgeDriver.findElement(By.cssSelector("body > section > div.register-full > div.register-right > div.register-in.middle > a")).click();
edgeDriver.findElement(By.cssSelector("#user")).sendKeys("bit");
edgeDriver.findElement(By.cssSelector("#password")).sendKeys("123456");
edgeDriver.findElement(By.cssSelector("#submit")).click();
Thread.sleep(3000);
Alert alert = edgeDriver.switchTo().alert();
alert.accept();
Set<String> windowHandles = edgeDriver.getWindowHandles();
for(String window : windowHandles){
if(window != windowHandle){
edgeDriver.switchTo().window(window);
break;
}
}
edgeDriver.findElement(By.cssSelector("body > div > header > div > div > div.col-9.aos-init.aos-animate > nav > div > a > span")).click();
edgeDriver.findElement(By.cssSelector("#exit")).click();
try{
WebElement element = edgeDriver.findElement(By.cssSelector("body > section > div.header > div.logo > a"));
Assertions.assertNotNull(element);
}catch (Exception e){
System.out.println("退出失败");
}
}
//从收藏页 退出
@Test
void LoveExit() throws InterruptedException {
String windowHandle = edgeDriver.getWindowHandle();
edgeDriver.findElement(By.cssSelector("body > section > div.register-full > div.register-right > div.register-in.middle > a")).click();
edgeDriver.findElement(By.cssSelector("#user")).sendKeys("bit");
edgeDriver.findElement(By.cssSelector("#password")).sendKeys("123456");
edgeDriver.findElement(By.cssSelector("#submit")).click();
Thread.sleep(3000);
Alert alert = edgeDriver.switchTo().alert();
alert.accept();
Set<String> windowHandles = edgeDriver.getWindowHandles();
for(String window : windowHandles){
if(window != windowHandle){
edgeDriver.switchTo().window(window);
break;
}
}
edgeDriver.findElement(By.cssSelector("body > div > header > div > div > div.col-9.aos-init.aos-animate > nav > div > a > span")).click();
edgeDriver.findElement(By.cssSelector("body > div > div.site-mobile-menu > div.site-mobile-menu-body > ul > li:nth-child(4) > a")).click();
Thread.sleep(1000);
edgeDriver.findElement(By.cssSelector("body > div > header > div > div > div.col-9.aos-init.aos-animate > nav > div > a > span")).click();
edgeDriver.findElement(By.cssSelector("body > div > div.site-mobile-menu > div.site-mobile-menu-body > ul > li:nth-child(5) > a")).click();
try{
edgeDriver.navigate().refresh();
WebElement element = edgeDriver.findElement(By.cssSelector("body > section > div.header > div.logo > a"));
Assertions.assertNotNull(element);
}catch (Exception e){
System.out.println("退出失败");
}
}
//从上传页 退出
@Test
void UploadExit() throws InterruptedException {
String windowHandle = edgeDriver.getWindowHandle();
edgeDriver.findElement(By.cssSelector("body > section > div.register-full > div.register-right > div.register-in.middle > a")).click();
edgeDriver.findElement(By.cssSelector("#user")).sendKeys("bit");
edgeDriver.findElement(By.cssSelector("#password")).sendKeys("123456");
edgeDriver.findElement(By.cssSelector("#submit")).click();
Thread.sleep(3000);
Alert alert = edgeDriver.switchTo().alert();
alert.accept();
Set<String> windowHandles = edgeDriver.getWindowHandles();
for(String window : windowHandles){
if(window != windowHandle){
edgeDriver.switchTo().window(window);
break;
}
}
edgeDriver.findElement(By.cssSelector("body > div > header > div > div > div.col-9.aos-init.aos-animate > nav > div > a > span")).click();
edgeDriver.findElement(By.cssSelector("body > div > div.site-mobile-menu > div.site-mobile-menu-body > ul > li:nth-child(3) > a")).click();
Thread.sleep(1000);
edgeDriver.findElement(By.cssSelector("body > div > header > div > div > div.col-9.aos-init.aos-animate > nav > div > a > span")).click();
edgeDriver.findElement(By.cssSelector("body > div > div.site-mobile-menu > div.site-mobile-menu-body > ul > li:nth-child(5) > a")).click();
try{
edgeDriver.navigate().refresh();
WebElement element = edgeDriver.findElement(By.cssSelector("body > section > div.header > div.logo > a"));
Assertions.assertNotNull(element);
}catch (Exception e){
System.out.println("退出失败");
}
}
}
其他工具类:
①、创建驱动:
package com.common;
import org.openqa.selenium.edge.EdgeDriver;
import java.time.Duration;
/**
* Created with IntelliJ IDEA.
* Description:
* User: Lenovo
* Date: 2022-11-16
* Time: 14:11
*/
//建立浏览器驱动
public class commonDriver {
private static EdgeDriver edgeDriver;
public static EdgeDriver getDriver(){
if(edgeDriver == null){
edgeDriver = new EdgeDriver();
//添加隐式等待
edgeDriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
}
return edgeDriver;
}
}
②、关闭驱动:
package com.muisictest;
import com.common.commonDriver;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.edge.EdgeDriver;
/**
* Created with IntelliJ IDEA.
* Description:
* User: Lenovo
* Date: 2022-11-16
* Time: 15:55
*/
public class driverQuitTest extends commonDriver {
private static EdgeDriver edgeDriver = getDriver();
@Test
void driverQuit(){
edgeDriver.quit();
}
}
③、通过 suit 套件执行所有的测试类
package com.muisictest;
import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;
/**
* Created with IntelliJ IDEA.
* Description:
* User: Lenovo
* Date: 2022-11-16
* Time: 16:01
*/
@Suite
@SelectClasses({LoginTest.class,RegistTest.class,
modifyTest.class,MusicListTest.class,FileUploadTest.class,
ExitTest.class, driverQuitTest.class})
public class RunSuit {
}
最后的执行结果:全员通过,测试完毕