前言:
- 之前写过的SSM项目,现在我们通过 selenium 自动化套件 + junit 单元测试框架,来对其功能进行自动化测试
- 回顾项目功能:登录、注册、修改密码、播放音乐、搜索音乐、收藏音乐、上传音乐、删除音乐(单个删除 + 批量删除)
测试用例:
- 测试用例我主要从:功能、性能、界面、兼容性、易用性、安全性。这六个方面进行考虑的
我主要完成的是功能的测试:
①、登录测试:
packagecom.muisictest;importcom.common.commonDriver;importorg.junit.jupiter.api.*;importorg.junit.jupiter.params.ParameterizedTest;importorg.junit.jupiter.params.provider.CsvSource;importorg.openqa.selenium.Alert;importorg.openqa.selenium.By;importorg.openqa.selenium.WebElement;importorg.openqa.selenium.edge.EdgeDriver;importsun.reflect.generics.tree.Tree;importjava.util.Set;importjava.util.concurrent.TimeUnit;importstaticorg.junit.Assert.*;/**
* Created with IntelliJ IDEA.
* Description:
* User: Lenovo
* Date: 2022-11-16
* Time: 13:22
*/@TestMethodOrder(MethodOrderer.OrderAnnotation.class)publicclassLoginTestextends commonDriver {privatestaticEdgeDriver edgeDriver =getDriver();@Test@BeforeAllstaticvoidstart(){
edgeDriver.get("http://124.222.242.143:8080/login.html");}//测试正常情况下 页面的元素是否正常@Test@Order(1)publicvoidappearance(){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)voidwindowSize(){
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)voidFlogin(String username,String password)throwsInterruptedException{
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)voidTlogin(String username,String password)throwsInterruptedException{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)voidNlogin()throwsInterruptedException{
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)voidONlogin1()throwsInterruptedException{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)voidONlogin2()throwsInterruptedException{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)voidchangePassword()throwsInterruptedException{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);}}
②、注册测试:
packagecom.muisictest;importcom.common.commonDriver;importorg.junit.jupiter.api.Assertions;importorg.junit.jupiter.api.BeforeAll;importorg.junit.jupiter.api.Test;importorg.junit.jupiter.params.ParameterizedTest;importorg.junit.jupiter.params.provider.CsvSource;importorg.openqa.selenium.Alert;importorg.openqa.selenium.By;importorg.openqa.selenium.WebElement;importorg.openqa.selenium.edge.EdgeDriver;importstaticorg.junit.Assert.*;/**
* Created with IntelliJ IDEA.
* Description:
* User: Lenovo
* Date: 2022-11-16
* Time: 21:49
*/publicclassRegistTestextends commonDriver {privatestaticEdgeDriver edgeDriver =getDriver();@Test@BeforeAllstaticvoidstart(){
edgeDriver.get("http://124.222.242.143:8080/login.html");}@TestvoidregistUI(){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"})voidregistGN(String username,String password,String newPassword)throwsInterruptedException{
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();}}
③、修改密码测试:
packagecom.muisictest;importcom.common.commonDriver;importorg.junit.jupiter.api.*;importorg.junit.jupiter.params.ParameterizedTest;importorg.junit.jupiter.params.provider.CsvSource;importorg.openqa.selenium.Alert;importorg.openqa.selenium.By;importorg.openqa.selenium.WebElement;importorg.openqa.selenium.edge.EdgeDriver;importjava.util.Set;importstaticorg.junit.Assert.assertEquals;importstaticorg.junit.Assert.assertNotNull;/**
* Created with IntelliJ IDEA.
* Description:
* User: Lenovo
* Date: 2022-11-16
* Time: 23:10
*/@TestMethodOrder(MethodOrderer.OrderAnnotation.class)publicclass modifyTest extends commonDriver {privatestaticEdgeDriver edgeDriver =getDriver();@Test@BeforeAllstaticvoidstart(){
edgeDriver.get("http://124.222.242.143:8080/modif.html");}@Test@Order(1)voidregistUI(){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"})voidregistGN(String username,String password,String newPassword)throwsInterruptedException{
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"})voidregistGY(String username,String password,String newPassword)throwsInterruptedException{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测试……)
packagecom.muisictest;importcom.common.commonDriver;importorg.junit.jupiter.api.*;importorg.junit.jupiter.params.ParameterizedTest;importorg.junit.jupiter.params.provider.CsvSource;importorg.junit.jupiter.params.provider.ValueSource;importorg.openqa.selenium.Alert;importorg.openqa.selenium.By;importorg.openqa.selenium.WebElement;importorg.openqa.selenium.edge.EdgeDriver;importjava.util.Set;/**
* Created with IntelliJ IDEA.
* Description:
* User: Lenovo
* Date: 2022-11-16
* Time: 23:32
*/@TestMethodOrder(MethodOrderer.OrderAnnotation.class)publicclassMusicListTestextends commonDriver {privatestaticEdgeDriver edgeDriver =getDriver();@Test@BeforeAllstaticvoidstart()throwsInterruptedException{
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)voidListUI()throwsInterruptedException{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)voidsearch(String name)throwsInterruptedException{// 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);//检测一下搜索后的 UIString 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)voidloveMusic()throwsInterruptedException{
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)voiddeleteMusic()throwsInterruptedException{
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)voiddeleteAllMusic()throwsInterruptedException{
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("通过勾选框删除成功");}}}
⑤、上传文件测试:(从上传文件的多种情况测试,还有文件的格式情况考虑)
packagecom.muisictest;importcom.common.commonDriver;importorg.junit.jupiter.api.*;importorg.openqa.selenium.Alert;importorg.openqa.selenium.By;importorg.openqa.selenium.edge.EdgeDriver;importjava.util.Set;/**
* Created with IntelliJ IDEA.
* Description:
* User: Lenovo
* Date: 2022-11-17
* Time: 10:09
*/@TestMethodOrder(MethodOrderer.OrderAnnotation.class)publicclassFileUploadTestextends commonDriver {privatestaticEdgeDriver edgeDriver =getDriver();@Test@BeforeAllstaticvoidstart()throwsInterruptedException{
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)voiduploadUI()throwsInterruptedException{
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,"文件上传");}//成功上传@TestvoiduploadFile1()throwsInterruptedException{
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("已用同名同歌手歌曲,上传失败");}}//同名不同歌手@TestvoiduploadFile2()throwsInterruptedException{
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("已用同名歌曲,上传失败");}}//不传文件 报错@TestvoiduploadFile3()throwsInterruptedException{
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文件@TestvoiduploadFile4()throwsInterruptedException{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 文件 格式错误@TestvoiduploadFile5()throwsInterruptedException{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("文件格式错误");}}}
⑥、退出测试:分别从音乐播放页、收藏页、上传页测试退出
packagecom.muisictest;importcom.common.commonDriver;importorg.junit.jupiter.api.Assertions;importorg.junit.jupiter.api.BeforeAll;importorg.junit.jupiter.api.Test;importorg.openqa.selenium.Alert;importorg.openqa.selenium.By;importorg.openqa.selenium.WebElement;importorg.openqa.selenium.edge.EdgeDriver;importjava.util.Set;/**
* Created with IntelliJ IDEA.
* Description:
* User: Lenovo
* Date: 2022-11-17
* Time: 11:14
*/publicclassExitTestextends commonDriver {privatestaticEdgeDriver edgeDriver =getDriver();@Test@BeforeAllstaticvoidstart()throwsInterruptedException{Thread.sleep(1000);
edgeDriver.get("http://124.222.242.143:8080/login.html");}//从列表页 退出@TestvoidListExit()throwsInterruptedException{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("退出失败");}}//从收藏页 退出@TestvoidLoveExit()throwsInterruptedException{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("退出失败");}}//从上传页 退出@TestvoidUploadExit()throwsInterruptedException{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("退出失败");}}}
其他工具类:
①、创建驱动:
packagecom.common;importorg.openqa.selenium.edge.EdgeDriver;importjava.time.Duration;/**
* Created with IntelliJ IDEA.
* Description:
* User: Lenovo
* Date: 2022-11-16
* Time: 14:11
*///建立浏览器驱动publicclass commonDriver {privatestaticEdgeDriver edgeDriver;publicstaticEdgeDrivergetDriver(){if(edgeDriver ==null){
edgeDriver =newEdgeDriver();//添加隐式等待
edgeDriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));}return edgeDriver;}}
②、关闭驱动:
packagecom.muisictest;importcom.common.commonDriver;importorg.junit.jupiter.api.AfterAll;importorg.junit.jupiter.api.Test;importorg.openqa.selenium.edge.EdgeDriver;/**
* Created with IntelliJ IDEA.
* Description:
* User: Lenovo
* Date: 2022-11-16
* Time: 15:55
*/publicclass driverQuitTest extends commonDriver {privatestaticEdgeDriver edgeDriver =getDriver();@TestvoiddriverQuit(){
edgeDriver.quit();}}
③、通过 suit 套件执行所有的测试类
packagecom.muisictest;importorg.junit.platform.suite.api.SelectClasses;importorg.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})publicclassRunSuit{}
最后的执行结果:全员通过,测试完毕
版权归原作者 梦の澜 所有, 如有侵权,请联系我们删除。