代码实践:
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 static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
public class CeshirenTest {
private static WebDriver driver;
@BeforeAll
public static void setup(){
driver =new ChromeDriver();
}
@AfterAll
public static void tearDown(){
driver.quit();
}
@Test
void searchTest() throws InterruptedException {
//进入测试人首页
driver.get("https://ceshiren.com/");
//定位到搜索按钮,并点击
driver.findElement(By.id("search-button")).click();
//定位到搜索框,并输入信息
driver.findElement(By.id("search-term")).sendKeys("selenium");
//定位到高级搜索,并点击
driver.findElement(By.xpath("//a[@title='打开高级搜索']")).click();
Thread.sleep(3000);
//问题:还没有预期和实际结果,需要添加断言
//当定位到多个元素时,会默认选择第一个元素,
WebElement title = driver.findElement(By.cssSelector(".topic-title"));
// System.out.println(“title值为:”+title.getText());
assertThat(title.getText(),containsString(“selenium”));
}
}