在pom.xml文件中引入selenium和juint依赖
<modelVersion>4.0.0modelVersion>
<groupId>org.examplegroupId>
<artifactId>onlineMusicTestartifactId>
<version>1.0-SNAPSHOTversion>
<dependency>
<groupId>org.seleniumhq.seleniumgroupId>
<artifactId>selenium-javaartifactId>
<version>4.0.0version>
dependency>
<dependency>
<groupId>commons-iogroupId>
<artifactId>commons-ioartifactId>
<version>2.6version>
dependency>
<dependency>
<groupId>org.junit.jupitergroupId>
<artifactId>junit-jupiterartifactId>
<version>5.8.2version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.junit.platformgroupId>
<artifactId>junit-platform-suiteartifactId>
<version>1.8.2version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.junit.platformgroupId>
<artifactId>junit-platform-reportingartifactId>
<version>1.8.2version>
<scope>testscope>
dependency>
<dependency>
<groupId>commons-iogroupId>
<artifactId>commons-ioartifactId>
<version>2.6version>
dependency>
dependencies>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
properties>
project>
创建chromDriver对象,并且设置等待时间
import org.openqa.selenium.chrome.ChromeDriver;
import java.time.Duration;
public class commonDriver {
public static ChromeDriver chromeDriver;
public static ChromeDriver getConnection(){
if(chromeDriver == null){
chromeDriver = new ChromeDriver();
}
chromeDriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(20));
return chromeDriver;
}
}
关闭测试页面
import org.junit.jupiter.api.Test;
import org.openqa.selenium.chrome.ChromeDriver;
public class driverQuit extends commonDriver {
private static ChromeDriver chromeDriver = getConnection();
@Test
public void quit(){
chromeDriver.quit();
}
}
注册功能自动化测试代码:
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.chrome.ChromeDriver;
import java.util.Set;
//设置测试方法执行顺序
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class registerTest extends commonDriver{
public static ChromeDriver driver = getConnection();
@BeforeAll
public static void open(){
driver.get("http://124.223.222.249:8080/register.html");
}
//1.判断此时的页面是否加载成功
@Order(1)
@Test
public void goodPage(){
WebElement element = driver.findElement(By.cssSelector("body > div > h2"));
WebElement element1 = driver.findElement(By.cssSelector("body > div > div > p"));
String placeholder = driver.findElement(By.cssSelector("#username")).getAttribute("placeholder");
String placeholder1 = driver.findElement(By.cssSelector("#password")).getAttribute("placeholder");
String placeholder2 = driver.findElement(By.cssSelector("#confirmPassword")).getAttribute("placeholder");
driver.findElement(By.cssSelector("#submit")).getAttribute("submit");
Assertions.assertEquals(element.getText(),"onlineMusic注册页面");
Assertions.assertEquals(element1.getText(),"用户注册");
Assertions.assertEquals(placeholder,"请输入用户名");
Assertions.assertEquals(placeholder1,"请输入密码");
Assertions.assertEquals(placeholder2,"请输入确认密码");
}
//用户名为空,判断此时的前端给用户的响应
@Order(2)
@ParameterizedTest
@CsvSource(value = {"'','',''",
"'','',123456",
"'',123456,123456",
"'',123456,''"
})
public void nullUsernameRegister(String username,String password,String configPassWord) throws InterruptedException {
driver.findElement(By.cssSelector("#username")).clear();
driver.findElement(By.cssSelector("#password")).clear();
driver.findElement(By.cssSelector("#confirmPassword")).clear();
driver.findElement(By.cssSelector("#username")).sendKeys(username);
driver.findElement(By.cssSelector("#password")).sendKeys(password);
driver.findElement(By.cssSelector("#confirmPassword")).sendKeys(configPassWord);
driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
Alert alert = driver.switchTo().alert();
Assertions.assertEquals(alert.getText(),"用户名不能为空");
alert.accept();
//Thread.sleep(1000);
}
@Order(3)
@ParameterizedTest
@CsvSource(value = {"123,'',''",
"123,'',123456"
})
public void nullPasswordRegister(String username,String password,String configPassWord) throws InterruptedException {
driver.findElement(By.cssSelector("#username")).clear();
driver.findElement(By.cssSelector("#password")).clear();
driver.findElement(By.cssSelector("#confirmPassword")).clear();
driver.findElement(By.cssSelector("#username")).sendKeys(username);
driver.findElement(By.cssSelector("#password")).sendKeys(password);
driver.findElement(By.cssSelector("#confirmPassword")).sendKeys(configPassWord);
driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
Alert alert = driver.switchTo().alert();
Assertions.assertEquals(alert.getText(),"密码不能为空");
alert.accept();
// Thread.sleep(1000);
}
@Order(4)
@ParameterizedTest
@CsvSource(value = {
"123,123456,''"
})
//确认密码为空
public void nullConfirmPasswordRegister(String username,String password,String configPassWord) throws InterruptedException {
driver.findElement(By.cssSelector("#username")).clear();
driver.findElement(By.cssSelector("#password")).clear();
driver.findElement(By.cssSelector("#confirmPassword")).clear();
driver.findElement(By.cssSelector("#username")).sendKeys(username);
driver.findElement(By.cssSelector("#password")).sendKeys(password);
driver.findElement(By.cssSelector("#confirmPassword")).sendKeys(configPassWord);
driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
Alert alert = driver.switchTo().alert();
Assertions.assertEquals(alert.getText(),"确认密码不能为空");
alert.accept();
//Thread.sleep(1000);
}
//密码和确认密码不同
@Order(5)
@ParameterizedTest
@CsvSource(value = {
"123,123456,123457"
})
//确认密码为空
public void notSamePasswordAndConfirmRegister(String username,String password,String configPassWord) throws InterruptedException {
driver.findElement(By.cssSelector("#username")).clear();
driver.findElement(By.cssSelector("#password")).clear();
driver.findElement(By.cssSelector("#confirmPassword")).clear();
driver.findElement(By.cssSelector("#username")).sendKeys(username);
driver.findElement(By.cssSelector("#password")).sendKeys(password);
driver.findElement(By.cssSelector("#confirmPassword")).sendKeys(configPassWord);
driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
Alert alert = driver.switchTo().alert();
Assertions.assertEquals(alert.getText(),"密码和确认密码要相同!");
alert.accept();
//Thread.sleep(1000);
}
//数据库中已存在用户,进行注册
@Order(6)
@ParameterizedTest
@CsvSource(value = {
"xiaoqiao,123456,123456"
})
public void hadRegister(String username,String password,String configPassWord) throws InterruptedException {
driver.findElement(By.cssSelector("#username")).clear();
driver.findElement(By.cssSelector("#password")).clear();
driver.findElement(By.cssSelector("#confirmPassword")).clear();
driver.findElement(By.cssSelector("#username")).sendKeys(username);
driver.findElement(By.cssSelector("#password")).sendKeys(password);
driver.findElement(By.cssSelector("#confirmPassword")).sendKeys(configPassWord);
driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
Thread.sleep(1000);
Alert alert = driver.switchTo().alert();
Assertions.assertEquals(alert.getText(),"该用户已存在");
alert.accept();
}
//注册成功,跳转到登录页面,此时导航栏中点击回退,注册页面中的信息只有用户名
@Order(7)
@ParameterizedTest
@CsvSource(value = {
"zh2222,123456,123456"
})
public void successRegister(String username,String password,String configPassWord) throws InterruptedException {
String windowHandle = driver.getWindowHandle();
driver.findElement(By.cssSelector("#username")).clear();
driver.findElement(By.cssSelector("#password")).clear();
driver.findElement(By.cssSelector("#confirmPassword")).clear();
driver.findElement(By.cssSelector("#username")).sendKeys(username);
driver.findElement(By.cssSelector("#password")).sendKeys(password);
driver.findElement(By.cssSelector("#confirmPassword")).sendKeys(configPassWord);
driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
Thread.sleep(1000);
Alert alert = driver.switchTo().alert();
Assertions.assertEquals(alert.getText(),"注册成功!");
alert.accept();
Set<String> windowHandles = driver.getWindowHandles();
for(String window : windowHandles){
if(window .equals(windowHandle)){
driver.switchTo().window(window);
break;
}
}
Thread.sleep(1000);
driver.navigate().back();
String text = driver.findElement(By.cssSelector("#password")).getText();
String text1 = driver.findElement(By.cssSelector("#confirmPassword")).getText();
Assertions.assertEquals(text,"");
Assertions.assertEquals(text1,"");
}
}
用户登录自动化测试代码:
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.chrome.ChromeDriver;
import java.util.Set;
//设置用例执行顺序
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class loginTest extends commonDriver {
public static ChromeDriver driver = getConnection();
@BeforeAll
public static void open(){
driver.get("http://124.223.222.249:8080/login.html");
}
//验证登录页面加载成功
@Order(1)
@Test
public void accSuccess() throws InterruptedException {
Thread.sleep(1000);
String text = driver.findElement(By.cssSelector("body > div > h2")).getText();
String text1 = driver.findElement(By.cssSelector("body > div > div > p")).getText();
String placeholder = driver.findElement(By.cssSelector("#username")).getAttribute("placeholder");
String placeholder1 = driver.findElement(By.cssSelector("#password")).getAttribute("placeholder");
String value = driver.findElement(By.cssSelector("#submit")).getAttribute("value");
Assertions.assertEquals(text,"onlineMusic登录页面");
Assertions.assertEquals(text1,"用户登录");
Assertions.assertEquals(placeholder,"请输入用户名");
Assertions.assertEquals(placeholder1,"请输入密码");
Assertions.assertEquals(value,"登录");
}
//用户名为空,进行登录,登录失败,提示提示用户用户名不能为空
@Order(2)
@ParameterizedTest
@CsvSource(value = {"'',1234567","'',''"})
public void nullUsername(String username,String password){
driver.findElement(By.cssSelector("#username")).clear();
driver.findElement(By.cssSelector("#password")).clear();
driver.findElement(By.cssSelector("#username")).sendKeys(username);
driver.findElement(By.cssSelector("#password")).sendKeys(password);
driver.findElement(By.cssSelector("#submit")).click();
Alert alert = driver.switchTo().alert();
Assertions.assertEquals("输入内容不能为空!",alert.getText());
alert.accept();
}
//密码为空,进行登录,提示用户密码不能为空
@Order(3)
@ParameterizedTest
@CsvSource(value = {"1234567,''"})
public void nullPassword(String username,String password) throws InterruptedException {
driver.findElement(By.cssSelector("#username")).clear();
driver.findElement(By.cssSelector("#password")).clear();
driver.findElement(By.cssSelector("#username")).sendKeys(username);
driver.findElement(By.cssSelector("#password")).sendKeys(password);
driver.findElement(By.cssSelector("#submit")).click();
Thread.sleep(1000);
Alert alert = driver.switchTo().alert();
Assertions.assertEquals("输入内容不能为空!",alert.getText());
alert.accept();
}
//错误的密码或者错误的用户名进行登录
@Order(4)
@ParameterizedTest
@CsvSource(value = {"zhojin,'1234567'","lis,1234567"})
public void misUsernameOrMisPassword(String username,String password) throws InterruptedException {
driver.findElement(By.cssSelector("#username")).clear();
driver.findElement(By.cssSelector("#password")).clear();
driver.findElement(By.cssSelector("#username")).sendKeys(username);
driver.findElement(By.cssSelector("#password")).sendKeys(password);
driver.findElement(By.cssSelector("#submit")).click();
Thread.sleep(1000);
Alert alert = driver.switchTo().alert();
Assertions.assertEquals("登录失败,账号或密码错误,请重试!",alert.getText());
alert.accept();
}
//登录成功,并且跳转到首页
@Order(5)
@ParameterizedTest
@CsvSource(value = {"zhoujin,123456"})
public void ok(String username,String password) throws InterruptedException {
String windowHandle = driver.getWindowHandle();
driver.findElement(By.cssSelector("#username")).clear();
driver.findElement(By.cssSelector("#password")).clear();
driver.findElement(By.cssSelector("#username")).sendKeys(username);
driver.findElement(By.cssSelector("#password")).sendKeys(password);
driver.findElement(By.cssSelector("#submit")).click();
Thread.sleep(1000);
Alert alert = driver.switchTo().alert();
Assertions.assertEquals("登录成功!",alert.getText());
alert.accept();
Set<String> windowHandles = driver.getWindowHandles();
for(String window : windowHandles){
if(window .equals(windowHandle)){
driver.switchTo().window(window);
break;
}
}
Thread.sleep(1000);
driver.navigate().back();
String text = driver.findElement(By.cssSelector("#password")).getText();
Assertions.assertEquals(text,"");
}
}
音乐播放器主页面自动化测试脚本:
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.chrome.ChromeDriver;
import java.util.Set;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class musicListTest extends commonDriver{
public static ChromeDriver driver = getConnection();
@BeforeAll
static void open() throws InterruptedException {
driver.get("http://124.223.222.249:8080/login.html");
String windowHandle = driver.getWindowHandle();
driver.findElement(By.cssSelector("#username")).clear();
driver.findElement(By.cssSelector("#password")).clear();
driver.findElement(By.cssSelector("#username")).sendKeys("zhoujin");
driver.findElement(By.cssSelector("#password")).sendKeys("123456");
driver.findElement(By.cssSelector("#submit")).click();
Thread.sleep(1000);
Alert alert = driver.switchTo().alert();
Assertions.assertEquals("登录成功!",alert.getText());
alert.accept();
Set<String> windowHandles = driver.getWindowHandles();
for(String window : windowHandles){
if(window .equals(windowHandle)){
driver.switchTo().window(window);
break;
}
}
}
//页面是否展示成功
//播放是否成功
@Order(1)
@Test
public void UITest(){
String text1 = driver.findElement(By.cssSelector("#body > div.container > h3")).getText();
Assertions.assertEquals(text1,"在线音乐服务器实现");
//查询
String text = driver.findElement(By.cssSelector("#submit1")).getText();
Assertions.assertEquals(text,"查询");
String value1 = driver.findElement(By.cssSelector("#submit1")).getAttribute("type");
Assertions.assertEquals(value1,"button");
//喜欢列表
String text7 = driver.findElement(By.cssSelector("#body > div.container > div:nth-child(3) > a:nth-child(1)")).getText();
Assertions.assertEquals(text7,"喜欢列表");
String href = driver.findElement(By.cssSelector("#body > div.container > div:nth-child(3) > a:nth-child(1)")).getAttribute("href");
Assertions.assertEquals(href,"http://124.223.222.249:8080/loveMusic.html");
//添加歌曲
String text8 = driver.findElement(By.cssSelector("#body > div.container > div:nth-child(3) > a:nth-child(2)")).getText();
Assertions.assertEquals(text8,"添加歌曲");
String href1 = driver.findElement(By.cssSelector("#body > div.container > div:nth-child(3) > a:nth-child(2)")).getAttribute("href");
Assertions.assertEquals(href1,"http://124.223.222.249:8080/uploadmusic.html");
//删除选中
String text9 = driver.findElement(By.cssSelector("#delete")).getText();
Assertions.assertEquals(text9,"删除选中");
//退出登录
String text10 = driver.findElement(By.cssSelector("#logout")).getText();
Assertions.assertEquals(text10,"退出登录");
String text2 = driver.findElement(By.cssSelector("#body > div.container > table > tbody:nth-child(1) > tr > th:nth-child(1)")).getText();
Assertions.assertEquals(text2,"选择");
String text3 = driver.findElement(By.cssSelector("#body > div.container > table > tbody:nth-child(1) > tr > th:nth-child(2)")).getText();
Assertions.assertEquals(text3,"歌名");
String text4 = driver.findElement(By.cssSelector("#body > div.container > table > tbody:nth-child(1) > tr > th:nth-child(3)")).getText();
Assertions.assertEquals(text4,"歌手");
String text5 = driver.findElement(By.cssSelector("#body > div.container > table > tbody:nth-child(1) > tr > th:nth-child(4)")).getText();
Assertions.assertEquals(text5,"歌曲");
String text6 = driver.findElement(By.cssSelector("#body > div.container > table > tbody:nth-child(1) > tr > th:nth-child(5)")).getText();
Assertions.assertEquals(text6,"操作");
}
//查询音乐
@Order(2)
@ParameterizedTest
@CsvSource(value = {"浪","花姐 - 狂浪 3D环绕(RuiFeng Music)","''"})
public void checkMusic(String music) throws InterruptedException {
driver.findElement(By.cssSelector("#exampleInputName2")).clear();
driver.findElement(By.cssSelector("#exampleInputName2")).sendKeys(music);
driver.findElement(By.cssSelector("#submit1")).click();
Thread.sleep(2000);
}
//播放
@Order(3)
@Test
public void playMusic() throws InterruptedException {
driver.findElement(By.cssSelector("#list > tr:nth-child(1) > td:nth-child(4) > button")).click();
Thread.sleep(5000);
}
//收藏音乐
@Order(4)
@Test
public void likeMusic() throws InterruptedException {
driver.findElement(By.cssSelector("#list > tr:nth-child(1) > td:nth-child(5) > button:nth-child(2)")).click();
Alert alert = driver.switchTo().alert();
Thread.sleep(3000);
//Thread.sleep(1000);
alert.accept();
//Thread.sleep(2000);
}
//跳转置喜欢列表
@Order(5)
@Test
public void jumpLikeMusic() throws InterruptedException {
driver.findElement(By.cssSelector("#body > div.container > div:nth-child(3) > a:nth-child(1)")).click();
Thread.sleep(1000);
driver.navigate().back();
}
//添加音乐,跳转置上传页面
@Order(6)
@Test
public void jumpLoadMusic() throws InterruptedException {
driver.findElement(By.cssSelector("#body > div.container > div:nth-child(3) > a:nth-child(2)")).click();
Thread.sleep(1000);
driver.navigate().back();
}
//删除
@Order(7)
@Test
public void deleteMusic() throws InterruptedException {
try{
driver.findElement(By.cssSelector("#list > tr > td:nth-child(5) > button:nth-child(1)")).click();
Thread.sleep(1000);
Alert alert = driver.switchTo().alert();
Assertions.assertEquals(alert.getText(),"删除成功,重新加载页面");
alert.accept();
}catch (Exception e){
e.printStackTrace();
}
}
//批量删除
@Order(8)
@Test
public void deletePartTest() throws InterruptedException {
driver.findElement(By.cssSelector("#\\36 2")).click();
// driver.findElement(By.cssSelector("#\\34 7")).click();
driver.findElement(By.cssSelector("#delete")).click();
Alert alert = driver.switchTo().alert();
Assertions.assertEquals(alert.getText(),"批量删除成功");
alert.accept();
Thread.sleep(2000);
}
//退出登录
@Order(9)
@Test
public void logOutTest() throws InterruptedException {
driver.findElement(By.cssSelector("#logout")).click();
Thread.sleep(1000);
Alert alert = driver.switchTo().alert();
Assertions.assertEquals(alert.getText(),"退出成功,返回登录页面");
alert.accept();
Assertions.assertEquals(driver.getTitle(),"注册界面");
}
}
上传音乐页面自动化测试脚本:
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.chrome.ChromeDriver;
import java.util.Set;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class uploadMusic extends commonDriver {
public static ChromeDriver driver = getConnection();
@BeforeAll
public static void open() throws InterruptedException {
driver.get("http://124.223.222.249:8080/login.html");
String windowHandle = driver.getWindowHandle();
driver.findElement(By.cssSelector("#username")).clear();
driver.findElement(By.cssSelector("#password")).clear();
driver.findElement(By.cssSelector("#username")).sendKeys("zhoujin");
driver.findElement(By.cssSelector("#password")).sendKeys("123456");
driver.findElement(By.cssSelector("#submit")).click();
Thread.sleep(1000);
Alert alert = driver.switchTo().alert();
Assertions.assertEquals("登录成功!",alert.getText());
alert.accept();
Set<String> windowHandles = driver.getWindowHandles();
for(String window : windowHandles){
if(window .equals(windowHandle)){
driver.switchTo().window(window);
break;
}
}
driver.findElement(By.cssSelector("#body > div.container > div:nth-child(3) > a:nth-child(2)")).click();
}
//UI界面
@Order(1)
@Test
public void UITest(){
String type = driver.findElement(By.cssSelector("body > form > input[type=file]:nth-child(1)")).getAttribute("type");
Assertions.assertEquals("file",type);
String placeholder1= driver.findElement(By.cssSelector("body > form > label > input[type=text]")).getAttribute("placeholder");
Assertions.assertEquals(placeholder1,"请输入歌手名");
String placeholder2 = driver.findElement(By.cssSelector("body > form > label > input[type=text]")).getAttribute("type");
Assertions.assertEquals("text",placeholder2);
String text = driver.findElement(By.cssSelector("body > form > input[type=submit]:nth-child(3)")).getAttribute("type");
Assertions.assertEquals(text,"submit");
String text2 = driver.findElement(By.cssSelector("body > form > input[type=submit]:nth-child(3)")).getAttribute("value");
Assertions.assertEquals("上传",text2);
}
//上传后缀名不是mp3结尾的,还有上传文件是.mp3结尾的但是文件内容不是mp3格式
@Order(2)
@ParameterizedTest
@CsvSource(value = {"C:\\Users\\周津\\Desktop\\MSDN.lnk,zhoujin","C:\\Users\\周津\\Desktop\\255.mp3,zhoujin"})
public void misLoadMusic(String url) throws InterruptedException {
driver.findElement(By.cssSelector("body > form > input[type=file]:nth-child(1)")).clear();
driver.findElement(By.cssSelector("body > form > label > input[type=text]")).clear();
driver.findElement(By.cssSelector("body > form > input[type=file]:nth-child(1)")).sendKeys(url);
driver.findElement(By.cssSelector("body > form > label > input[type=text]")).sendKeys("zhoujin");
driver.findElement(By.cssSelector("body > form > input[type=submit]:nth-child(3)")).click();
// Thread.sleep(3000);
driver.navigate().back();
}
//上传成功
//上传不同的音乐相同的歌手
//相同的音乐,不同的歌手
@Order(3)
@ParameterizedTest
@CsvSource(value = {
"C:\\Users\\周津\\Desktop\\新建文件夹\\【 提音 】陈雅森 - 三杯酒(RuiFeng Music).mp3,zhoujin",
"C:\\Users\\周津\\Desktop\\新建文件夹\\【 提音 】陈雅森 - 三杯酒(RuiFeng Music).mp3,lisi",
"C:\\Users\\周津\\Desktop\\新建文件夹\\【 提音 】孤单的人 - 修行的红莲(RuiFeng Music).mp3,zhoujin"})
public void loadMusic(String music,String singer) throws InterruptedException {
driver.findElement(By.cssSelector("body > form > input[type=file]:nth-child(1)")).clear();
driver.findElement(By.cssSelector("body > form > label > input[type=text]")).clear();
driver.findElement(By.cssSelector("body > form > input[type=file]:nth-child(1)")).sendKeys(music);
driver.findElement(By.cssSelector("body > form > label > input[type=text]")).sendKeys(singer);
driver.findElement(By.cssSelector("body > form > input[type=submit]:nth-child(3)")).click();
//Thread.sleep(3000);
driver.navigate().back();
}
//重复上传歌曲,同名,同歌手
@Order(4)
@ParameterizedTest
@CsvSource(value = {
"C:\\Users\\周津\\Desktop\\新建文件夹\\【 提音 】陈雅森 - 三杯酒(RuiFeng Music).mp3,zhoujin"})
public void loadMusic2(String music,String singer) throws InterruptedException {
driver.findElement(By.cssSelector("body > form > input[type=file]:nth-child(1)")).clear();
driver.findElement(By.cssSelector("body > form > label > input[type=text]")).clear();
driver.findElement(By.cssSelector("body > form > input[type=file]:nth-child(1)")).sendKeys(music);
driver.findElement(By.cssSelector("body > form > label > input[type=text]")).sendKeys(singer);
driver.findElement(By.cssSelector("body > form > input[type=submit]:nth-child(3)")).click();
//Thread.sleep(3000);
driver.navigate().back();
}
@Order(5)
@Test
//未上传文件 输入歌手
public void badLoadMusicOrSinger1() throws InterruptedException {
driver.findElement(By.cssSelector("body > form > input[type=file]:nth-child(1)")).clear();
driver.findElement(By.cssSelector("body > form > label > input[type=text]")).clear();
driver.findElement(By.cssSelector("body > form > label > input[type=text]")).sendKeys("zhoujin");
driver.findElement(By.cssSelector("body > form > input[type=submit]:nth-child(3)")).click();
driver.navigate().back();
}
//未上传文件 未输入歌手
@Order(6)
@Test
public void badLoadMusicOrSinger2() throws InterruptedException {
driver.findElement(By.cssSelector("body > form > input[type=file]:nth-child(1)")).clear();
driver.findElement(By.cssSelector("body > form > label > input[type=text]")).clear();
driver.findElement(By.cssSelector("body > form > input[type=submit]:nth-child(3)")).click();
//Thread.sleep(3000);
driver.navigate().back();
}
//上传文件 未输入歌手
@Order(7)
@Test
public void badLoadMusicOrSinger3() throws InterruptedException {
driver.findElement(By.cssSelector("body > form > input[type=file]:nth-child(1)")).clear();
driver.findElement(By.cssSelector("body > form > label > input[type=text]")).clear();
driver.findElement(By.cssSelector("body > form > input[type=file]:nth-child(1)")).sendKeys("C:\\Users\\周津\\Desktop\\新建文件夹\\【 提音 】陈雅森 - 三杯酒(RuiFeng Music).mp3");
driver.findElement(By.cssSelector("body > form > input[type=submit]:nth-child(3)")).click();
//Thread.sleep(3000);
driver.navigate().back();
}
}
收藏页面自动化测试脚本:
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.chrome.ChromeDriver;
import java.util.Set;
//设置测试方法执行顺序
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class loveMusicTest extends commonDriver{
public static ChromeDriver driver = getConnection();
@BeforeAll
public static void open() throws InterruptedException {
driver.get("http://124.223.222.249:8080/login.html");
String windowHandle = driver.getWindowHandle();
driver.findElement(By.cssSelector("#username")).clear();
driver.findElement(By.cssSelector("#password")).clear();
driver.findElement(By.cssSelector("#username")).sendKeys("zhoujin");
driver.findElement(By.cssSelector("#password")).sendKeys("123456");
driver.findElement(By.cssSelector("#submit")).click();
Thread.sleep(1000);
Alert alert = driver.switchTo().alert();
Assertions.assertEquals("登录成功!",alert.getText());
alert.accept();
Set<String> windowHandles = driver.getWindowHandles();
for(String window : windowHandles){
if(window .equals(windowHandle)){
driver.switchTo().window(window);
break;
}
}
driver.findElement(By.cssSelector("#body > div.container > div:nth-child(3) > a:nth-child(1)")).click();
}
@Test
@Order(1)
public void UITest() throws InterruptedException {
Thread.sleep(1000);
String text1 = driver.findElement(By.cssSelector("#body > div.container > h3")).getText();
Assertions.assertEquals(text1,"在线音乐服务器实现");
//查询
String text = driver.findElement(By.cssSelector("#submit1")).getText();
Assertions.assertEquals(text,"查询");
String value1 = driver.findElement(By.cssSelector("#submit1")).getAttribute("type");
Assertions.assertEquals(value1,"button");
String text3 = driver.findElement(By.cssSelector("#body > div.container > table > tbody:nth-child(1) > tr > th:nth-child(1)")).getText();
Assertions.assertEquals(text3,"歌名");
String text4 = driver.findElement(By.cssSelector("#body > div.container > table > tbody:nth-child(1) > tr > th:nth-child(2)")).getText();
Assertions.assertEquals(text4,"歌手");
String text5 = driver.findElement(By.cssSelector("#body > div.container > table > tbody:nth-child(1) > tr > th:nth-child(3)")).getText();
Assertions.assertEquals(text5,"歌曲");
String text6 = driver.findElement(By.cssSelector("#body > div.container > table > tbody:nth-child(1) > tr > th:nth-child(4)")).getText();
Assertions.assertEquals(text6,"操作");
}
//查询
@Order(2)
@ParameterizedTest
@CsvSource(value = {"浪","花姐 - 狂浪 3D环绕(RuiFeng Music)","''"})
public void checkMusic(String music) throws InterruptedException {
driver.findElement(By.cssSelector("#exampleInputName2")).clear();
driver.findElement(By.cssSelector("#exampleInputName2")).sendKeys(music);
driver.findElement(By.cssSelector("#submit1")).click();
Thread.sleep(2000);
}
//播放
@Order(3)
@Test
public void playMusic() throws InterruptedException {
driver.findElement(By.cssSelector("#info > tr > td:nth-child(3) > button")).click();
Thread.sleep(2000);
}
//返回
@Order(4)
@Test
public void back() throws InterruptedException {
Thread.sleep(2000);
driver.findElement(By.cssSelector("#body > div.container > div:nth-child(3) > a")).click();
driver.navigate().back();
}
//删除
@Order(5)
@Test
public void deleteMusic() throws InterruptedException {
try{
driver.findElement(By.cssSelector("#info > tr > td:nth-child(4) > button")).click();
Thread.sleep(1000);
Alert alert = driver.switchTo().alert();
Assertions.assertEquals(alert.getText(),"删除成功,重新加载页面");
alert.accept();
}catch (Exception e){
e.printStackTrace();
}
}
}
使用测试套件,批量的执行测试类
import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;
//使用测试套件,可以批量的执行测试脚本
@Suite
@SelectClasses({uploadMusic.class,loveMusicTest.class,loginTest.class,registerTest.class,musicListTest.class,driverQuit.class})
public class autoSuite {
}
();
}
//删除
@Order(5)
@Test
public void deleteMusic() throws InterruptedException {
try{
driver.findElement(By.cssSelector("#info > tr > td:nth-child(4) > button")).click();
Thread.sleep(1000);
Alert alert = driver.switchTo().alert();
Assertions.assertEquals(alert.getText(),"删除成功,重新加载页面");
alert.accept();
}catch (Exception e){
e.printStackTrace();
}
}
}
使用测试套件,批量的执行测试类
import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;
//使用测试套件,可以批量的执行测试脚本
@Suite
@SelectClasses({uploadMusic.class,loveMusicTest.class,loginTest.class,registerTest.class,musicListTest.class,driverQuit.class})
public class autoSuite {
}