后面没做的原因:总是不知道为什么会卡住
package com.sky.test;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.BeforeAll;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
public class InitForum {
public static ChromeOptions options = new ChromeOptions();
public static ChromeDriver webDriver=null;
public static ChromeDriver createDriver(){
options.addArguments("--remote-allow-origins=*");
if(webDriver==null){
webDriver=new ChromeDriver(options);
webDriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
}
return webDriver;
}
// static WebDriver webDriver;
//程序执行之前,保证驱动对象的创建
@BeforeAll
static void Setup(){
System.setProperty("webdriver.chrome.driver", "C:\\Users\\yauklt\\Desktop\\chromedriver-win64\\chromedriver.exe");
ChromeDriver webDriver = createDriver();
webDriver.get("http://127.0.0.1:80");
}
public List<String> getTime(){
//文件按照天数进行保存
//文件格式如: 20231122-123456毫秒
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyyMMdd-HHmmssSS");
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyyMMdd");
String fileName = simpleDateFormat1.format(System.currentTimeMillis());
String dirname = simpleDateFormat2.format(System.currentTimeMillis());
List<String> list =new ArrayList<>();
list.add(dirname);
list.add(fileName);
return list;
}
public void getScreenShot(String classname) throws IOException {
List<String> list = getTime();
String filename = "./src/test/java/ForumWebTestSc"+list.get(0)+"/"+classname+"_"+list.get(1)+".png";
File file = ((TakesScreenshot)webDriver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(file, new File(filename));
}
}
package com.sky.test;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
@SpringBootTest
public class HttpClientTest {
/**
* 通过http发送get请求
*/
@Test
public void testGET() throws Exception{
//创建httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建请求对象
HttpGet httpGet = new HttpGet("http://localhost:8080/user/shop/status");
//发送请求,接收响应结果
CloseableHttpResponse response=httpClient.execute(httpGet);
//获取服务端返回的状态码
int statusCode =response.getStatusLine().getStatusCode();
System.out.println("服务端响应的状态码为"+statusCode);
HttpEntity entity=response.getEntity();
String body = EntityUtils.toString(entity);
System.out.println("服务端返回的数据为"+body);
//关闭资源,response,httpClient
response.close();
httpClient.close();
}
/**
* 通过http发送post请求
*/
@Test
public void testPOST() throws Exception {
// 创建httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建请求对象
HttpPost httpPost = new HttpPost("http://localhost:8080/admin/employee/login");
//设置请求参数
JSONObject jsonObject = new JSONObject();
jsonObject.put("username","admin");
jsonObject.put("password","123456");
StringEntity entity = new StringEntity(jsonObject.toString());
//指定请求编码方式
entity.setContentEncoding("utf-8");
//数据格式
entity.setContentType("application/json");
httpPost.setEntity(entity);
//发送请求
CloseableHttpResponse response = httpClient.execute(httpPost);
//解析返回结果
int statusCode = response.getStatusLine().getStatusCode();
System.out.println("响应码为:" + statusCode);
HttpEntity entity1 = response.getEntity();
String body = EntityUtils.toString(entity1);
System.out.println("响应数据为:" + body);
//关闭资源
response.close();
httpClient.close();
}
}
package com.sky.test;
import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.junit.jupiter.params.provider.CsvSource;
import org.openqa.selenium.By;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import static java.lang.Thread.sleep;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class ForumRegTest extends InitForum{
@Order(1)
@Test
void RegExitTest() throws IOException, InterruptedException {
webDriver.get("http://127.0.0.1");
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
String url = webDriver.getCurrentUrl();
String title = webDriver.getTitle();
//效验url 和 title
//断言判断是否正确
sleep(1000);
Assertions.assertEquals("http://127.0.0.1/#/login",url);
Assertions.assertEquals("苍穹外卖",title);
System.out.println("进入注册页面测试成功");
getScreenShot(String.valueOf(ForumRegTest.class));
}
/**
* 用户名、昵称、密码和确认密码都为空、协议未选。直接点击注册
*/
@Test
@Order(2)
void RegUserNullTest() throws IOException, InterruptedException {
//定位注册按钮 并点击
webDriver.get("http://127.0.0.1/#/login");
//
webDriver.findElement(By.xpath("//*[@id=\"app\"]/div/div/div/form/div[2]/div/div/input")).clear();
webDriver.findElement(By.xpath("//*[@id=\"app\"]/div/div/div/form/div[3]/div/div/input")).clear();
sleep(500);
webDriver.findElement(By.xpath("//*[@id=\"app\"]/div/div/div/form/div[2]/div/div/input")).click();
webDriver.findElement(By.xpath("//*[@id=\"app\"]/div/div/div/form/div[3]/div/div/input")).click();
sleep(500);
//webDriver.findElement(By.cssSelector("#app > div > div > div > form > div:nth-child(4) > div > button > span > span")).click();
//查找每个输入框下的红字提示
String username = webDriver.findElement(By.xpath("//*[@id=\"app\"]/div/div/div/form/div[2]/div/div/input")).getText();
//String nickname = webDriver.findElement(By.xpath("//*[@id=\"app\"]/div/div/div/form/div[3]/div/div")).getText();
String password = webDriver.findElement(By.xpath("//*[@id=\"app\"]/div/div/div/form/div[3]/div/div/input")).getText();
//String passwordRepeat = webDriver.findElement(By.xpath("//*[@id=\"signUpForm\"]/div/div[4]/div/div")).getText();
//设置标志位 利用标志值效验测试是否通过&& nickname.equals("昵称不能为空") && passwordRepeat.equals("请检查确认密码")
sleep(1000);
int flag = 0;
if(username.equals("") && password.equals("")){
flag = 1;
}
//如果标志位为1 测试通过
Assertions.assertEquals(1,flag);
//截图保存测试画面
getScreenShot(String.valueOf(ForumRegTest.class));
System.out.println("异常注册,用户名和密码为空情况效验成功");
}
/**
* 注册测试:正常测试:用户名:testUser1 昵称:testUser1 密码:123 确认密码:123 勾选协议
* 用户名:testUser2 昵称:testUser2 密码:123456 确认密码:123456 勾选协议
* @param username
* @param password
* @param url
*/
@Order(4)
@ParameterizedTest
@CsvFileSource(resources = "/reg.csv")
void RegUserTest(String username,String password,String url) throws IOException, InterruptedException {
webDriver.get("http://127.0.0.1/#/login");
//清空输入框中的内容
webDriver.findElement(By.xpath("//*[@id=\"app\"]/div/div/div/form/div[2]/div/div/input")).clear();
webDriver.findElement(By.xpath("//*[@id=\"app\"]/div/div/div/form/div[3]/div/div/input")).clear();
//定位用户名 昵称 密码 确认密码并填入数据
webDriver.findElement(By.xpath("//*[@id=\"app\"]/div/div/div/form/div[2]/div/div/input")).sendKeys(username);
webDriver.findElement(By.xpath("//*[@id=\"app\"]/div/div/div/form/div[3]/div/div/input")).sendKeys(password);
//点击注册按钮
//webDriver.findElement(By.cssSelector("#submit")).click();
webDriver.findElement(By.cssSelector("#app > div > div > div > form > div:nth-child(4) > div > button > span > span")).click();
//获取当前页面的url 并且对url进行效验
//等待一秒 避免操作太快无法及时获取地址
sleep(1000);
String cur_url = webDriver.getCurrentUrl();
//判断当前页面url是否为登录页 如果是 测试通过
Assertions.assertEquals(url,cur_url);
System.out.println("用户:"+username+"注册用例测试成功");
getScreenShot(String.valueOf(ForumRegTest.class));
//返回注册页面
webDriver.navigate().back();
}
@Order(5)
@ParameterizedTest
@CsvSource({"bitboy,111111,账号不存在"})
void RegExitsUserTest(String username,String password,String alert) throws InterruptedException, IOException {
webDriver.get("http://127.0.0.1/#/login");
//清空输入框中的内容
webDriver.findElement(By.xpath("//*[@id=\"app\"]/div/div/div/form/div[2]/div/div/input")).clear();
webDriver.findElement(By.xpath("//*[@id=\"app\"]/div/div/div/form/div[3]/div/div/input")).clear();
//定位用户名 昵称 密码 确认密码并填入数据
webDriver.findElement(By.xpath("//*[@id=\"app\"]/div/div/div/form/div[2]/div/div/input")).sendKeys(username);
webDriver.findElement(By.xpath("//*[@id=\"app\"]/div/div/div/form/div[3]/div/div/input")).sendKeys(password);
//点击注册按钮
webDriver.findElement(By.cssSelector("#app > div > div > div > form > div:nth-child(4) > div > button > span > span")).click();
sleep(1000);
//定位 警告弹窗
String cur_alert = webDriver.findElement(By.xpath("/html/body/div[2]/p")).getText();
//效验弹窗内容 内容为 警告 的话 代表测试通过
Assertions.assertEquals(alert,cur_alert);
//截图
getScreenShot(String.valueOf(ForumRegTest.class));
System.out.println("异常登录,账号不存在");
}
}
package com.sky.test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.openqa.selenium.By;
import java.io.IOException;
import java.time.Duration;
import static java.lang.Thread.sleep;
public class ForumIndexTest extends InitForum{
/**
* 无登录状态进入工作台测试
*/
@Order(1)
@Test
void EntryIndexTest() throws IOException, InterruptedException {
//进入帖子列表页
webDriver.get("http://127.0.0.1/#/dashboard");
sleep(2000);
String cur_url = webDriver.getCurrentUrl();
//利用断言判断 如果获取的url地址是登录页 那么测试通过
Assertions.assertEquals("http://127.0.0.1/#/login",cur_url);
//截屏
getScreenShot(String.valueOf(ForumIndexTest.class));
}
/**
* 登陆状态进入工作台
* @param username
* @param password
*/
@Order(2)
@ParameterizedTest
@CsvSource({"admin,123456,http://127.0.0.1/#/dashboard,工作台"})
void UserEntryIndexTest(String username,String password,String url,String title) throws InterruptedException, IOException {
webDriver.get("http://127.0.0.1/#/login");
//清空输入框中的内容
webDriver.findElement(By.xpath("//*[@id=\"app\"]/div/div/div/form/div[2]/div/div/input")).clear();
webDriver.findElement(By.xpath("//*[@id=\"app\"]/div/div/div/form/div[3]/div/div/input")).clear();
//定位用户名 昵称 密码 确认密码并填入数据
webDriver.findElement(By.xpath("//*[@id=\"app\"]/div/div/div/form/div[2]/div/div/input")).sendKeys(username);
webDriver.findElement(By.xpath("//*[@id=\"app\"]/div/div/div/form/div[3]/div/div/input")).sendKeys(password);
webDriver.findElement(By.cssSelector("#app > div > div > div > form > div:nth-child(4) > div > button > span > span")).click();
sleep(1000);
//获取当前页面的url title 用户昵称 和 身份
String cur_url = webDriver.getCurrentUrl();
String cur_title = webDriver.getTitle();
//通过断言效验title 和 url 用户昵称 身份确认是否正确进入帖子列表页
Assertions.assertEquals(url,cur_url);
Assertions.assertEquals(title,cur_title);
System.out.println("登录状态进入首页测试通过");
// webDriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
// webDriver.findElement(By.xpath("/html/body/div[1]/header[1]/div/div/div[3]/a")).click();
//截图
getScreenShot(String.valueOf(ForumIndexTest.class));
}
/**
* 点击对应按钮是否正常跳转到对应详情页
* @param title
* @param username
* @param content_title
* @throws IOException
*/
@Order(3)
@ParameterizedTest
@CsvSource({"订单管理,管理员,订单管理"})
void EntryArticleDetail(String title,String username,String content_title) throws IOException {
//定位第一个按钮 并进入
webDriver.manage().window().maximize();
webDriver.findElement(By.xpath("//*[@id=\"app\"]/div/div[2]/section/div/div[1]/h2/span/a")).click();
//获取文章的标题 和 作者名称。以及正文标题 内容 效验是否正常进入帖子详情页
String cur_title = webDriver.findElement(By.xpath("/html/head/title")).getText();
String cur_username = webDriver.findElement(By.xpath("#article_details_author_name")).getText();
String cur_content_title = webDriver.findElement(By.xpath("//*[@id=\"app\"]/div/div[1]/div[2]/div[1]/div/ul/div[4]/div/a/li/span")).getText();
//通过断言判断是否正常进入详情页 判断当前详情页的 页面标题 作者名是否都正确
Assertions.assertEquals(title,cur_title);
Assertions.assertEquals(username,cur_username);
Assertions.assertEquals(content_title,cur_content_title);
//截图
getScreenShot(String.valueOf(ForumIndexTest.class));
System.out.println("点击对应按钮是否正常跳转到对应详情页");
//返回首页
webDriver.findElement(By.xpath("//*[@id=\"app\"]/div/div[1]/div[2]/div[1]/div/ul/div[1]/div/a/li/span")).click();
}
}