携程网测试脚本(全国大学生软件检测大赛Web测试题目)
package test;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.io.File;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class TestCtripFlight {
private WebDriver driver;
@BeforeEach
public void setup() {
//提交最终代码脚本时,请将驱动路径换回官方路径"C:\\Users\\86153\\AppData\\Local\\Google\\Chrome\\Application\\chromedriver.exe"
System.setProperty("webdriver.chrome.driver", "C:\\Users\\86153\\AppData\\Local\\Google\\Chrome\\Application\\chromedriver.exe");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--remote-allow-origins=*");
driver = new ChromeDriver(chromeOptions);
driver.get("https://www.ctrip.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
// test-code-start
// 请在此处插入Selenium+JUnit5代码
@Test
public void testUI() {
takeScreenshot("CtripFlight_search_UI_001.png");
}
@ParameterizedTest
@CsvSource({
"北京, 广州, 2025-10-16, CtripFlight_R001_001.png",
"北京, 成都, 2025-10-19, CtripFlight_R001_002.png",
"上海, 广州, 2025-10-26, CtripFlight_R001_003.png",
"上海, 成都, 2025-11-09, CtripFlight_R001_004.png"
})
public void test_CtripFlight_R001(String from, String to, String fromTime, String screenShot) throws InterruptedException {
driver.findElement(By.className("lsn_button_icon_dvatN")).click();
driver.findElement(By.xpath("//span[@class=\"lsn_top_nav_font_4h1KU lsn_top_nav_font_line_0iVuu\" and text()=\"机票\"]")).click();
driver.findElement(By.xpath("//span[@class=\"radio-label\" and text()=\"单程 \"]")).click();
WebElement owDCity = driver.findElement(By.name("owDCity"));
owDCity.click();
owDCity.sendKeys(Keys.CONTROL, "a");
owDCity.sendKeys(Keys.DELETE);
owDCity.sendKeys(from);
Thread.sleep(1000);
owDCity.sendKeys(Keys.ENTER);
WebElement owACity = driver.findElement(By.name("owACity"));
owACity.sendKeys(Keys.CONTROL, "a");
owACity.sendKeys(Keys.DELETE);
owACity.sendKeys(to);
Thread.sleep(1000);
owACity.sendKeys(Keys.ENTER);
pickCalendarDate(fromTime);
Thread.sleep(1000);
driver.findElement(By.xpath("//span[@class=\"label-tool-tip-wrap\" and text()=\"带儿童\"]")).click();
driver.findElement(By.xpath("//span[text()=\"不限舱等\"]")).click();
Thread.sleep(1000);
driver.findElement(By.xpath("//div[text()=\"经济舱\"]")).click();
Thread.sleep(1000);
driver.findElement(By.xpath("//button[@class=\"search-btn\" and text()=\"搜索\"]")).click();
Thread.sleep(6000);
takeScreenshot(screenShot);
}
@ParameterizedTest
@CsvSource({
"111111, CtripFlight_R002_001.png",
"是不是, CtripFlight_R002_002.png",
})
public void test_CtripFlight_R002(String from, String screenShot) throws InterruptedException {
driver.findElement(By.className("lsn_button_icon_dvatN")).click();
driver.findElement(By.xpath("//span[@class=\"lsn_top_nav_font_4h1KU lsn_top_nav_font_line_0iVuu\" and text()=\"机票\"]")).click();
driver.findElement(By.xpath("//span[@class=\"radio-label\" and text()=\"单程 \"]")).click();
WebElement owDCity = driver.findElement(By.name("owDCity"));
owDCity.click();
owDCity.sendKeys(Keys.CONTROL, "a");
owDCity.sendKeys(Keys.DELETE);
owDCity.sendKeys(from);
// owDCity.sendKeys(Keys.ENTER);
Thread.sleep(2000);
takeScreenshot(screenShot);
}
@ParameterizedTest
@CsvSource({
"北京, 东京, 2025-10-16, CtripFlight_R003_001.png",
"北京, 大阪, 2025-10-19, CtripFlight_R003_002.png",
"上海, 东京, 2025-10-26, CtripFlight_R003_003.png",
"上海, 大阪, 2025-11-09, CtripFlight_R003_004.png"
})
public void test_CtripFlight_R003(String from, String to, String fromTime, String screenShot) throws InterruptedException {
driver.findElement(By.className("lsn_button_icon_dvatN")).click();
driver.findElement(By.xpath("//span[@class=\"lsn_top_nav_font_4h1KU lsn_top_nav_font_line_0iVuu\" and text()=\"机票\"]")).click();
driver.findElement(By.xpath("//span[@class=\"radio-label\" and text()=\"单程 \"]")).click();
WebElement owDCity = driver.findElement(By.name("owDCity"));
owDCity.click();
owDCity.sendKeys(Keys.CONTROL, "a");
owDCity.sendKeys(Keys.DELETE);
owDCity.sendKeys(from);
// owDCity.sendKeys(Keys.ENTER);
Thread.sleep(1000);
WebElement owACity = driver.findElement(By.name("owACity"));
owACity.sendKeys(Keys.CONTROL, "a");
owACity.sendKeys(Keys.DELETE);
owACity.sendKeys(to);
Thread.sleep(1000);
owACity.sendKeys(Keys.ENTER);
pickCalendarDate(fromTime);
Thread.sleep(1000);
driver.findElement(By.xpath("//div[@class=\"form-item-v3 flt-field-v3 \"]")).click();
driver.findElement(By.xpath("(//div[@class='passenger-item'])[1]//div[@class='control']//i[@class='iconf-count-del']")).click();
driver.findElement(By.xpath("(//div[@class='passenger-item'])[2]//div[@class='control']//i[@class='iconf-count-del']")).click();
driver.findElement(By.xpath("(//div[@class='passenger-item last'])//div[@class='control']//i[@class='iconf-count-del']")).click();
driver.findElement(By.xpath("//div[@class=\"btns\"]//a[@class=\"btn-sure\"]")).click();
Thread.sleep(3000);
driver.findElement(By.xpath("//button[@class=\"search-btn\" and text()=\"搜索\"]")).click();
Thread.sleep(6000);
takeScreenshot(screenShot);
}
// test-code-end
@AfterEach
public void teardown() {
this.driver.quit();
}
private void takeScreenshot(String fileName) {
SimpleDateFormat dateFormat = new SimpleDateFormat("HHmmssddSSS");
String timestamp = dateFormat.format(new Date());
String timestampedFileName = timestamp + "_" + fileName;
File screenshotsDir = new File("screenshots");
if (!screenshotsDir.exists()) {
screenshotsDir.mkdirs();
}
String screenshotFilePath = screenshotsDir.getAbsolutePath() + File.separator + timestampedFileName;
File screenshotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(screenshotFile, new File(screenshotFilePath));
} catch (Exception e) {
e.printStackTrace();
}
}
// private void takeScreenshotWithCounter(String baseName) {
// // 格式化计数器为3位数字(001, 002, ...)
// String fileName = String.format("%s_%03d.png", baseName, screenshotCounter);
// takeScreenshot(fileName);
// // 计数器递增
// screenshotCounter++;
// }
private void pickCalendarDate(String dateStr)throws InterruptedException {
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate target = LocalDate.parse(dateStr, fmt);
int targetYear = target.getYear();
int targetMonth = target.getMonthValue();
int targetDay = target.getDayOfMonth();
String targetMonthText = targetYear + "年" + targetMonth + "月"; // 目标月份文本,用于匹配
System.out.println("目标月份:" + targetMonthText);
// 弹出日历
WebElement dateInput = new WebDriverWait(driver, 5, 500)
.until(ExpectedConditions.elementToBeClickable(
By.xpath("//input[@aria-label='请选择日期' and @readonly]")));
dateInput.click();
WebDriverWait wait = new WebDriverWait(driver, 5, 500);
int maxPages = 24; // 最多翻24页,防止无限循环
int pageCount = 0;
// 循环翻页直到目标月出现或达到最大翻页次数
while (pageCount < maxPages) {
pageCount++;
// 一次性拿当前面板里所有月份块
List<WebElement> monthBlocks = driver.findElements(By.xpath("//div[@class=\"date-picker date-picker-block \"]"));
System.out.println("月份块数量:" + monthBlocks.size());
for (WebElement block : monthBlocks) {
// 获取当前月份块的月份文本
String currentMonthText = block.findElement(By.cssSelector(".date-m")).getText()
.replaceAll("\\s+", "");
// 匹配目标月份
if (currentMonthText.equals(targetMonthText)) {
// 在当前匹配的月份块里点日期,然后直接结束
WebElement dayCell = block.findElement(
By.xpath(".//span[@class='date-d' and text()='" + targetDay + "']"));
// 检查日期是否可点击
if (wait.until(ExpectedConditions.elementToBeClickable(dayCell)).isEnabled()) {
dayCell.click();
return; // 选完退出
} else {
throw new IllegalStateException("目标日期 " + dateStr + " 不可选");
}
}
}
// 两块都没找到 → 点“下一月”按钮
WebElement nextBtn = wait.until(ExpectedConditions.elementToBeClickable(
By.xpath("//div[@class=\"date-picker date-picker-block \"][2]//div[@class=\"date-title\"][1]//span[@class=\"in-date-picker icon next-ico iconf-right\"]")));
nextBtn.click();
}
// 达到最大翻页次数仍未找到
throw new TimeoutException("超过最大翻页次数,未找到目标日期: " + dateStr);
}
}

雷达卡


京公网安备 11010802022788号







