本人不太会java语法,主要是本专业老师主讲java,所以不得不使用java语言进行编写,不当之处请见谅!
背景说一哈,有点摸鱼的成分,主要是期末课设和这个有关,想学着看看,不知道能不能得奖,得奖了再说~
官方给的代码不太对,自己简单改了改,感觉还可以。
这个题目是12306,自己搜一搜吧,我就不写了
主要用的插件:
第1.4.5还有junit
附上资源:
java源代码:
package Test12306;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 java.io.File;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver.Timeouts;public class test12306 {private WebDriver driver;@SuppressWarnings("deprecation")@BeforeEachpublic void setup() {System.setProperty("webdriver.chrome.driver", "C:\\Users\\lenovo\\AppData\\Local\\Google\\Chrome\\Application\\chromedriver.exe");ChromeOptions options = new ChromeOptions();options.addArguments("--remote-allow-origins=*");driver = new ChromeDriver(options);driver.get("https://www.12306.cn/index/index.html");driver.manage().window().maximize();driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);}// 请在这里插入JUnit5测试代码@Testpublic void testUI() {takeScreenshot("img/12306_search_01.png");}@ParameterizedTest@CsvSource({
// "北京北,南京,2024-10-14,12306_search_001.png",
// "北京北,南京,2024-10-15,12306_search_002.png",
// "北京北,南京,2024-10-20,12306_search_003.png","上海虹桥,郑州东,2024-10-29,12306_search_004.png",
// "北京北,南京,2024-10-30,12306_search_005.png" })public void test_12306_search(String from, String to, String fromTime, String screenShot) throws InterruptedException {WebElement fromField = driver.findElement(By.id("fromStationText"));fromField.clear();fromField.sendKeys(from + Keys.ENTER);WebElement toField = driver.findElement(By.id("toStationText"));toField.clear();toField.sendKeys(to + Keys.ENTER);WebElement fromTimeField = driver.findElement(By.id("train_date"));fromTimeField.clear();fromTimeField.sendKeys(fromTime);WebElement query = driver.findElement(By.id("search_one"));query.click();driver.switchTo().window(driver.getWindowHandles().toArray()[1].toString());Thread.sleep(1000);takeScreenshot(screenShot);}@AfterEachpublic void teardown() {this.driver.quit();}private void takeScreenshot(String fileName) {File screenshotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);try {FileUtils.copyFile(screenshotFile, new File(fileName));} catch (Exception e) {e.printStackTrace();}}
}
预选赛的题目真的做不完,测试用例要表格形式,要编写测试用例还要写代码,难死我了,反正没做完,不知道预赛能不能过,球球了过了吧!!!