目录
产品分析
https://ceshiren.com
测试用例分析
编写脚本
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class CeshirenTest {
public static WebDriver driver;
@Test
void search() throws InterruptedException {
driver = new ChromeDriver();
// 进入测试人论坛首页 前提条件
driver.get("https://ceshiren.com/");
// 1. 点击搜索按钮
driver.findElement(By.id("search-button")).click();
// 2. 输入搜索关键词
driver.findElement(By.id("search-term")).sendKeys("appium");
// 3. 点击搜索按钮
driver.findElement(By.cssSelector(".show-advanced-search")).click();
Thread.sleep(3000);
}
}
脚本优化-前置与后置
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
// CeshirenTest+search 测试人论坛的搜索功能 对应 用例标题
public class CeshirenTest {
public static WebDriver driver;
@BeforeAll
static void setUpClass(){
driver = new ChromeDriver();
driver.get("https://ceshiren.com/");
}
@AfterAll
static void tearDownClass(){
driver.quit();
}
@Test
void search() throws InterruptedException {
driver = new ChromeDriver();
// 进入测试人论坛首页 前提条件
driver.get("https://ceshiren.com/");
// 1. 点击搜索按钮
driver.findElement(By.id("search-button")).click();
// 2. 输入搜索关键词
driver.findElement(By.id("search-term")).sendKeys("appium");
// 3. 点击搜索按钮
driver.findElement(By.cssSelector(".show-advanced-search")).click();
Thread.sleep(3000);
}
}
脚本优化-添加断言
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.List;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
/*
* @Author: 霍格沃兹测试开发学社
* @Desc: '更多测试开发技术探讨,请访问:https://ceshiren.com/t/topic/15860'
*/
//测试人论坛的测试用例
public class CeshirenTest {
public static WebDriver driver;
// 前置、
@BeforeAll
static void setUpClass(){
driver = new ChromeDriver();
// 进入测试人论坛首页 前提条件
driver.get("https://ceshiren.com/");
}
@AfterAll
// 后置
static void tearDownClass(){
driver.quit();
}
@Test
void search() throws InterruptedException {
//1. 定位到搜索按钮 点击搜索按钮
driver.findElement(By.id("search-button")).click();
//2. 定位到输入框 输入搜索关键词
driver.findElement(By.id("search-term")).sendKeys("selenium");
//3. 定位到高级搜索按钮, 点击搜索按钮
driver.findElement(By.cssSelector(".show-advanced-search")).click();
Thread.sleep(3000);
WebElement title = driver.findElement(By.cssSelector(".topic-title"));
// 获取该元素的文本信息
String title_text = title.getText();
System.out.println(title_text);
assertThat(title_text, containsString("selenium"));
}
}