1. 确认博客系统的环境搭建
http://49.235.129.183:8080/java109_blog_system/blog_list.html,即可访问我的小项目;
2. 确定测试用例
测试用例如下所示:
3. 关于登录的测试用例
3.1 初始化和退出浏览器
代码如下:
java
package BlogAutoTests;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class InitAndEndBrowser {
static WebDriver webDriver;
@BeforeAll
static void OpenBlogSystem() {
webDriver = new ChromeDriver();
}
@AfterAll
static void CloseBlogSystem() {
webDriver.quit();
}
}
2.2 测试登录页面成功
java
package BlogAutoTests;
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 org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import static java.lang.Thread.sleep;
public class Tests {
static WebDriver webDriver = new ChromeDriver();
WebDriver CreateChromeDriver(){
WebDriver webDriver = new ChromeDriver();
return webDriver;
}
// 登录测试用例
@Test
void Login() throws InterruptedException {
WebDriver webDriver = CreateChromeDriver();
webDriver.get("http://49.235.129.183:8080/java109_blog_system/blog_list.html");
sleep(3000);
//输入用户名
webDriver.findElement(By.cssSelector("#username")).sendKeys("smallye");
//输入密码
webDriver.findElement(By.cssSelector("#password")).sendKeys("111");
//点击提交
webDriver.findElement(By.cssSelector("#submit")).click();
sleep(3000);
// 登录成功后,校验是否登陆成功
String username = webDriver.findElement(By.cssSelector("body > div.container > div.container-left > div > h3")).getText();
if(username.equals("smallye")) {
System.out.println("测试通过");
} else {
System.out.println("测试不通过");
}
}
void DestroyChromeDriver(){
webDriver.quit();
}
}
测试结果如下所示:
2.3 验证当前的url
java
String cur_url = webDriver.getCurrentUrl();
if(cur_url.equals("http://49.235.129.183:8080/java109_blog_system/blog_list.html")) {
System.out.println("测试通过");
} else {
System.out.println("测试不通过");
}
结果如下:
2.4 多参数接收修改代码
代码如下:将@test修改为多参数注解
java
@ParameterizedTest
@CsvSource({"'http://49.235.129.183:8080/java109_blog_system/blog_list.html', 'smallye', 'http://49.235.129.183:8080/java109_blog_system/blog_list.html', '111'"})
void Login(String url, String username, String expected_jump_url, String password) throws InterruptedException {
WebDriver webDriver = CreateChromeDriver();
webDriver.get("http://49.235.129.183:8080/java109_blog_system/blog_list.html");
sleep(3000);
//输入用户名
webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
//输入密码
webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
//点击提交
webDriver.findElement(By.cssSelector("#submit")).click();
sleep(3000);
// 登录成功后,校验是否登陆成功
String uname = webDriver.findElement(By.cssSelector("body > div.container > div.container-left > div > h3")).getText();
if(username.equals("smallye")) {
System.out.println("测试通过");
} else {
System.out.println("测试不通过");
}
String cur_url = webDriver.getCurrentUrl();
if(cur_url.equals(expected_jump_url)) {
System.out.println("测试通过");
} else {
System.out.println("测试不通过");
}
}
结果如下:
2.5 登录失败测试
当登录失败之后会跳转到新的如下页面:
代码如下:
java
@Test
void LoginError() throws InterruptedException {
String username = "smallye";
String password = "111111";
WebDriver webDriver1 = new ChromeDriver();
webDriver1.get("http://49.235.129.183:8080/java109_blog_system/blog_list.html");
sleep(3000);
webDriver1.findElement(By.cssSelector("#username")).sendKeys(username);
webDriver1.findElement(By.cssSelector("#password")).sendKeys(password);
webDriver1.findElement(By.cssSelector("#submit")).click();
sleep(3000);
WebElement webElement = webDriver1.findElement(By.cssSelector("body"));
// Assertions.assertEquals( "登录失败! 用户名或者密码错误!",webElement.getText());
if(webElement.getText().equals("您输入的用户名或密码不正确!")){
System.out.println("测试通过");
}else {
System.out.println("测试不通过");
}
}
结果显示测试通过!
将判断实际结果与预期结果的代码转化为断言的格式:代码如下所示:
java
Assertions.assertEquals( "您输入的用户名或密码不正确!",webElement.getText());
ps:本文就写到这里了,谢谢观看。