一、测试用例
1.1功能测试
1.2性能测试
1.3界面测试
1.4兼容性测试
1.5易用性测试
1.6安全性测试
二、自动化测试
那就对上面的部分用例进行自动化测试
Selenium3 + Junit5
2.1注册页面自动化测试
/**
* 针对两次输入密码不一致进行验证
* @param username
* @param password
* @param password2
* @throws InterruptedException
*/
@Order(1)
@ParameterizedTest
@CsvFileSource(resources = "test01.csv")
public void falseLogin1(String username,String password,String password2) throws InterruptedException {
//打开注册页面
webDriver.get("http://101.201.119.191:8080/reg.html");
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
webDriver.findElement(By.cssSelector("#password2")).sendKeys(password2);
webDriver.findElement(By.cssSelector("#submit")).click();
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
Alert alert = webDriver.switchTo().alert();
String alertText = alert.getText();
Assertions.assertEquals("两次密码输入不一致,请先检查,重新输入!",alertText);
}
/**
* 分别针对空账号,空密码,空账号和空密码进行测试
* @param username
* @param password
* @param password2
* @throws InterruptedException
*/
@Order(2)
@ParameterizedTest
@CsvSource({"123,'',123"})
//密码为空
void nullLogin1(String username,String password,String password2) throws InterruptedException {
//打开注册页面
webDriver.get("http://101.201.119.191:8080/reg.html");
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
webDriver.findElement(By.cssSelector("#password2")).sendKeys(password2);
webDriver.findElement(By.cssSelector("#submit")).click();
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
Alert alert = webDriver.switchTo().alert();
String alertText = alert.getText();
Assertions.assertEquals("请先输入密码!",alertText);
}
@Order(3)
@ParameterizedTest
@CsvSource({"'',123,123"})
//用户名为空
void nullLogin2(String username,String password,String password2) throws InterruptedException {
//打开注册页面
webDriver.get("http://101.201.119.191:8080/reg.html");
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
webDriver.findElement(By.cssSelector("#password2")).sendKeys(password2);
webDriver.findElement(By.cssSelector("#submit")).click();
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
Alert alert = webDriver.switchTo().alert();
String alertText = alert.getText();
Assertions.assertEquals("请先输入用户名!",alertText);
}
/**
* 注册成功
* @throws InterruptedException
*/
@Order(4)
@ParameterizedTest
@CsvFileSource(resources = "test03.csv")
void LoginSuccess(String username,String password,String password2) throws InterruptedException {
//打开注册页面
webDriver.get("http://101.201.119.191:8080/reg.html");
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//输入账号xiao
webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//输入密码123456
webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//输入确认密码123456
webDriver.findElement(By.cssSelector("#password2")).sendKeys(password2);
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//点击提交按钮
webDriver.findElement(By.cssSelector("#submit")).click();
//webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
WebDriverWait wait = new WebDriverWait(webDriver, 10);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = webDriver.switchTo().alert();
String alertText = alert.getText();
Assertions.assertEquals("恭喜:注册成功!",alertText);
// 关闭弹框
alert.accept();
}
2.2登录页面自动化测试
/**
* 用户名错误的情况
* @param username
* @param password
* @throws InterruptedException
*/
@Order(1)
@ParameterizedTest
@CsvFileSource(resources = "test02.csv")
public void falseLogin1(String username,String password,String valicode) throws InterruptedException {
//打开登录页面
webDriver.get("http://101.201.119.191:8080/login.html");
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
webDriver.findElement(By.cssSelector("#valicode")).sendKeys(valicode);
webDriver.findElement(By.cssSelector("#submit")).click();
WebDriverWait wait = new WebDriverWait(webDriver, 10);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = webDriver.switchTo().alert();
String alertText = alert.getText();
Assertions.assertEquals("抱歉:登录失败!用户名不存在!",alertText);
// 关闭弹框
alert.accept();
}
/**
* 密码错误的情况
* @param username
* @param password
* @throws InterruptedException
*/
@Order(2)
@ParameterizedTest
@CsvFileSource(resources = "test04.csv")
public void falseLogin2(String username,String password,String valicode) throws InterruptedException {
//打开登录页面
webDriver.get("http://101.201.119.191:8080/login.html");
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
webDriver.findElement(By.cssSelector("#valicode")).sendKeys(valicode);
webDriver.findElement(By.cssSelector("#submit")).click();
WebDriverWait wait = new WebDriverWait(webDriver, 10);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = webDriver.switchTo().alert();
String alertText = alert.getText();
Assertions.assertEquals("抱歉:登录失败!一天内密码输入错误五次,账户将会被冻结!还剩余:3次机会!",alertText);
// 关闭弹框
alert.accept();
}
/**
* 用户名为空的情况
* @param username
* @param password
* @throws InterruptedException
*/
@Order(3)
@ParameterizedTest
@CsvSource({"'',123456,1234"})
public void nullLogin1(String username,String password,String valicode) throws InterruptedException {
//打开登录页面
webDriver.get("http://101.201.119.191:8080/login.html");
webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
webDriver.findElement(By.cssSelector("#valicode")).sendKeys(valicode);
webDriver.findElement(By.cssSelector("#submit")).click();
WebDriverWait wait = new WebDriverWait(webDriver, 10);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = webDriver.switchTo().alert();
String alertText = alert.getText();
Assertions.assertEquals("请先输入用户名!",alertText);
// 关闭弹框
alert.accept();
}
/**
* 密码为空的情况
* @param username
* @param password
* @throws InterruptedException
*/
@Order(4)
@ParameterizedTest
@CsvSource({"res,'',1234"})
public void nullLogin2(String username,String password,String valicode) throws InterruptedException {
//打开登录页面
webDriver.get("http://101.201.119.191:8080/login.html");
webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
webDriver.findElement(By.cssSelector("#valicode")).sendKeys(valicode);
webDriver.findElement(By.cssSelector("#submit")).click();
WebDriverWait wait = new WebDriverWait(webDriver, 10);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = webDriver.switchTo().alert();
String alertText = alert.getText();
Assertions.assertEquals("请先输入密码!",alertText);
// 关闭弹框
alert.accept();
}
/**
* 验证码错误的情况
* @param username
* @param password
* @throws InterruptedException
*/
@Order(5)
@ParameterizedTest
@CsvFileSource(resources = "test06.csv")
public void falseLogin3(String username,String password,String valicode) throws InterruptedException {
//打开登录页面
webDriver.get("http://101.201.119.191:8080/login.html");
webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
webDriver.findElement(By.cssSelector("#valicode")).sendKeys(valicode);
webDriver.findElement(By.cssSelector("#submit")).click();
WebDriverWait wait = new WebDriverWait(webDriver, 10);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = webDriver.switchTo().alert();
String alertText = alert.getText();
Assertions.assertEquals("抱歉:登录失败!验证码错误!",alertText);
// 关闭弹框
alert.accept();
}
/**
* 登陆成功
* @param username
* @param password
* @param valicode
* @throws InterruptedException
*/
@Order(6)
@ParameterizedTest
@CsvFileSource(resources = "test05.csv")
void LoginSuccess(String username,String password,String valicode) throws InterruptedException {
//打开登录页面
webDriver.get("http://101.201.119.191:8080/login.html");
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
webDriver.findElement(By.cssSelector("#valicode")).sendKeys(valicode);
//点击提交按钮
webDriver.findElement(By.cssSelector("#submit")).click();
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//跳转到列表页
//获取到当前页面的url
String cur_url = webDriver.getCurrentUrl();
Assertions.assertEquals("http://101.201.119.191:8080/myblog_list.html",cur_url);
String cur_name = webDriver.findElement(By.cssSelector("#userNickName")).getText();
Assertions.assertEquals("用户36",cur_name);
}
2.3个人列表页自动化测试
/**
* 发布的博客数量不为0
*/
@Test
void MyBlogList1(){
webDriver.get("http://101.201.119.191:8080/myblog_list.html");
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
int title_num = webDriver.findElements(By.cssSelector("#artlist > div:nth-child(1) > div.title")).size();
Assertions.assertNotEquals(0,title_num);
}
/**
* 点击查看全文按钮能跳转到对应页面
* @throws InterruptedException
*/
@Test
void MyBlogList2() throws InterruptedException {
webDriver.get("http://101.201.119.191:8080/myblog_list.html");
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
webDriver.findElement(By.cssSelector("#artlist > div:nth-child(1) > a:nth-child(4)")).click();
sleep(40);
String cur_url = webDriver.getCurrentUrl();
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
Assertions.assertEquals("http://101.201.119.191:8080/blog_content.html?id=69",cur_url);
}
/**
* 点击修改按钮能够跳转到对应页面
* @throws InterruptedException
*/
@Test
void MyBlogList3() throws InterruptedException {
webDriver.get("http://101.201.119.191:8080/myblog_list.html");
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
webDriver.findElement(By.cssSelector("#artlist > div:nth-child(1) > a:nth-child(5)")).click();
sleep(40);
String cur_url = webDriver.getCurrentUrl();
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
Assertions.assertEquals("http://101.201.119.191:8080/blog_edit.html?id=68",cur_url);
}
2.4个人主页自动化测试
package MyblogTest;
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.CsvFileSource;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.util.concurrent.TimeUnit;
import static java.lang.Thread.sleep;
public class BlogList extends InitAndEnd{
/**
* 登陆成功
* @param username
* @param password
* @param valicode
* @throws InterruptedException
*/
@Order(1)
@ParameterizedTest
@CsvFileSource(resources = "test05.csv")
void LoginSuccess(String username,String password,String valicode) throws InterruptedException {
//打开登录页面
webDriver.get("http://101.201.119.191:8080/login.html");
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
webDriver.findElement(By.cssSelector("#valicode")).sendKeys(valicode);
//点击提交按钮
webDriver.findElement(By.cssSelector("#submit")).click();
sleep(40);
//跳转到列表页
//获取到当前页面的url
String cur_url = webDriver.getCurrentUrl();
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
Assertions.assertEquals("http://101.201.119.191:8080/myblog_list.html",cur_url);
String cur_name = webDriver.findElement(By.cssSelector("#userNickName")).getText();
Assertions.assertEquals("小六",cur_name);
}
/**
* 点击下一页按钮页面能正常跳转
* @throws InterruptedException
*/
@Test
void BlogList2() throws InterruptedException {
webDriver.get("http://101.201.119.191:8080/blog_list.html");
WebDriverWait wait = new WebDriverWait(webDriver, 10);
webDriver.findElement(By.xpath("//*[@id=\"pageDiv\"]/button[3]")).click();
sleep(40);
String cur_url = webDriver.getCurrentUrl();
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
Assertions.assertEquals("http://101.201.119.191:8080/blog_list.html?pageIndex=2&pageSize=3&searchName=null",cur_url);
}
/**
* 点击首页按钮页面能正常显示
* @throws InterruptedException
*/
@Test
void BlogList3() throws InterruptedException {
webDriver.get("http://101.201.119.191:8080/blog_list.html");
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
webDriver.findElement(By.cssSelector("#pageDiv > button.button.is-primary")).click();
WebDriverWait wait = new WebDriverWait(webDriver, 10);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = webDriver.switchTo().alert();
String alertText = alert.getText();
Assertions.assertEquals("抱歉:不能跳转啦,当前已经在首页!",alertText);
// 关闭弹框
alert.accept();
}
/**
* 点击查看全文按钮能跳转到对应页面
* @throws InterruptedException
*/
@Test
void BlogList1() throws InterruptedException {
webDriver.get("http://101.201.119.191:8080/blog_list.html");
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
webDriver.findElement(By.cssSelector("#artlist > div:nth-child(1) > a")).click();
sleep(40);
String cur_url = webDriver.getCurrentUrl();
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
Assertions.assertEquals("http://101.201.119.191:8080/blog_content.html?id=71",cur_url);
}
/**
* 点击博客添加按钮跳转至文章添加页
*/
@Test
void BlogList4(){
webDriver.get("http://101.201.119.191:8080/myblog_list.html");
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
String cur_url = webDriver.getCurrentUrl();
webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Assertions.assertEquals("http://101.201.119.191:8080/blog_add.html",cur_url);
}
}
2.5文章添加页自动化测试
package MyblogTest;
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.CsvFileSource;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import java.util.concurrent.TimeUnit;
import static java.lang.Thread.sleep;
public class BlogAdd extends InitAndEnd{
/**
* 登陆成功
* @param username
* @param password
* @param valicode
* @throws InterruptedException
*/
@Order(1)
@ParameterizedTest
@CsvFileSource(resources = "test05.csv")
void LoginSuccess(String username,String password,String valicode) throws InterruptedException {
//打开登录页面
webDriver.get("http://101.201.119.191:8080/login.html");
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
webDriver.findElement(By.cssSelector("#valicode")).sendKeys(valicode);
//点击提交按钮
webDriver.findElement(By.cssSelector("#submit")).click();
sleep(40);
//跳转到列表页
//获取到当前页面的url
String cur_url = webDriver.getCurrentUrl();
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
Assertions.assertEquals("http://101.201.119.191:8080/myblog_list.html",cur_url);
String cur_name = webDriver.findElement(By.cssSelector("#userNickName")).getText();
Assertions.assertEquals("小六",cur_name);
}
/**
* 左侧的文章草稿列数目不应该为0(即左侧的草稿列表应该能正常显示)
*/
@Test
void BlogAdd2(){
webDriver.get("http://101.201.119.191:8080/blog_add.html");
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
int title_num = webDriver.findElements(By.cssSelector("#leftContain > div > header > p")).size();
Assertions.assertNotEquals(0,title_num);
}
/**
* 点击发布文章按钮文章能正常发布
*/
@Test
void BlogAdd3() throws InterruptedException {
webDriver.get("http://101.201.119.191:8080/blog_add.html");
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//通过JS脚本进行标题输入
((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"title\").value=\"自动化测试\"");
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
///点击发布
webDriver.findElement(By.cssSelector("body > div.edit-contain > div.blog-edit-container > div.title > button:nth-child(2)")).click();
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
Alert alert = webDriver.switchTo().alert();
String alertText = alert.getText();
Assertions.assertEquals("恭喜:添加成功!是否继续添加文章?",alertText);
alert.dismiss();
String cur_url = webDriver.getCurrentUrl();
Assertions.assertEquals("http://101.201.119.191:8080/myblog_list.html",cur_url);
}
}
2.6文章详情页自动化测试
package MyblogTest;
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.CsvFileSource;
import org.openqa.selenium.By;
import java.util.concurrent.TimeUnit;
import static java.lang.Thread.sleep;
public class BlogContent extends InitAndEnd{
/**
* 登陆成功
* @param username
* @param password
* @param valicode
* @throws InterruptedException
*/
@Order(1)
@ParameterizedTest
@CsvFileSource(resources = "test05.csv")
void LoginSuccess(String username,String password,String valicode) throws InterruptedException {
//打开登录页面
webDriver.get("http://101.201.119.191:8080/login.html");
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
webDriver.findElement(By.cssSelector("#valicode")).sendKeys(valicode);
//点击提交按钮
webDriver.findElement(By.cssSelector("#submit")).click();
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//跳转到列表页
//获取到当前页面的url
String cur_url = webDriver.getCurrentUrl();
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
Assertions.assertEquals("http://101.201.119.191:8080/myblog_list.html",cur_url);
String cur_name = webDriver.findElement(By.cssSelector("#userNickName")).getText();
Assertions.assertEquals("小六",cur_name);
}
/**
* 点击关注按钮是否能正常关注
* @throws InterruptedException
*/
@Test
void BlogContent1() throws InterruptedException {
webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(4)")).click();
webDriver.get("http://101.201.119.191:8080/blog_content.html?id=66");
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
webDriver.findElement(By.cssSelector("#pink")).click();
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
String cur_str = webDriver.findElement(By.cssSelector("#pink")).getText();
Assertions.assertEquals("取消关注",cur_str);
}
/**
* 刷新进入一次页面阅读量是否加1
* @throws InterruptedException
*/
@Test
void BlogContent2() throws InterruptedException {
webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(4)")).click();
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
webDriver.findElement(By.cssSelector("#artlist > div:nth-child(3) > a")).click();
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
String cur_count = webDriver.findElement(By.cssSelector("#rcount")).getText();
Assertions.assertEquals("5",cur_count);
}
}
2.7文章编辑页自动化测试
package MyblogTest;
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.CsvFileSource;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import java.util.concurrent.TimeUnit;
import static java.lang.Thread.sleep;
public class BlogEdit extends InitAndEnd{
/**
* 登陆成功
* @param username
* @param password
* @param valicode
* @throws InterruptedException
*/
@Order(1)
@ParameterizedTest
@CsvFileSource(resources = "test05.csv")
void LoginSuccess(String username,String password,String valicode) throws InterruptedException {
//打开登录页面
webDriver.get("http://101.201.119.191:8080/login.html");
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
webDriver.findElement(By.cssSelector("#valicode")).sendKeys(valicode);
//点击提交按钮
webDriver.findElement(By.cssSelector("#submit")).click();
sleep(40);
//跳转到列表页
//获取到当前页面的url
String cur_url = webDriver.getCurrentUrl();
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
Assertions.assertEquals("http://101.201.119.191:8080/myblog_list.html",cur_url);
String cur_name = webDriver.findElement(By.cssSelector("#userNickName")).getText();
Assertions.assertEquals("小六",cur_name);
}
/**
* 修改文章后点击发布按钮,文章是否能发布成功
* @throws InterruptedException
*/
@Test
void BlogEdit1() throws InterruptedException {
webDriver.findElement(By.cssSelector("#artlist > div:nth-child(1) > a:nth-child(5)")).click();
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//通过JS脚本进行标题输入
((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"title\").value=\"自动化测试111\"");
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
///点击发布
webDriver.findElement(By.cssSelector("body > div.edit-contain > div > div.title > button:nth-child(2)")).click();
sleep(5000);
//
Alert alert = webDriver.switchTo().alert();
String alertText = alert.getText();
Assertions.assertEquals("恭喜:成功!",alertText);
alert.accept();
String cur_url = webDriver.getCurrentUrl();
Assertions.assertEquals("http://101.201.119.191:8080/myblog_list.html",cur_url);
}
}
2.8个人中心页自动化测试
package MyblogTest;
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.CsvFileSource;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import java.util.concurrent.TimeUnit;
import static java.lang.Thread.sleep;
public class PersonalCenter extends InitAndEnd{
/**
* 登陆成功
* @param username
* @param password
* @param valicode
* @throws InterruptedException
*/
@Order(1)
@ParameterizedTest
@CsvFileSource(resources = "test05.csv")
void LoginSuccess(String username,String password,String valicode) throws InterruptedException {
//打开登录页面
webDriver.get("http://101.201.119.191:8080/login.html");
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
webDriver.findElement(By.cssSelector("#valicode")).sendKeys(valicode);
//点击提交按钮
webDriver.findElement(By.cssSelector("#submit")).click();
sleep(40);
//跳转到列表页
//获取到当前页面的url
String cur_url = webDriver.getCurrentUrl();
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
Assertions.assertEquals("http://101.201.119.191:8080/myblog_list.html",cur_url);
String cur_name = webDriver.findElement(By.cssSelector("#userNickName")).getText();
Assertions.assertEquals("小六",cur_name);
}
/**
* 自己关注列表的数量是否显示正确
* @throws InterruptedException
*/
@Test
void PersonalCenter1() throws InterruptedException {
webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
String cur_str = webDriver.findElement(By.cssSelector("#pinkMyCount")).getText();
Assertions.assertEquals("2",cur_str);
}
/**
* 修改个人信息是否能提交成功
* @throws InterruptedException
*/
@Test
void PersonalCenter2() throws InterruptedException {
//通过JS脚本进行标题输入
//输入名称
((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"nickname\").value=\"小刘\"");
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//输入登录名
((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"username\").value=\"xioaliu\"");
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//输入邮箱
((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"email\").value=\"3073862723@qq.com\"");
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//点击提交按钮
webDriver.findElement(By.cssSelector("#personInfo > div.btn_submit > button.btn")).click();
sleep(5000);
//验证是否正确
Alert alert = webDriver.switchTo().alert();
String alertText = alert.getText();
Assertions.assertEquals("恭喜:修改成功!",alertText);
alert.accept();
}
}