package Test_BaiDuMap;

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.interactions.Actions;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

import java.io.File;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.Date;



public class Test_BaiDuMap {
    private WebDriver driver;

    @BeforeEach
    public void setup() {
        //提交最终代码脚本时,请将驱动路径换回官方路径"C:\\Users\\86153\\AppData\\Local\\Google\\Chrome\\Application\\chromedriver.exe"
        System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\Google\\Chrome\\Application\\chromedriver.exe");
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("--remote-allow-origins=*");
        driver = new ChromeDriver(chromeOptions);
        driver.get("https://map.baidu.com/");
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
    }

    // test-code-start

    // 请在此处插入Selenium+JUnit5代码
    @FindBy(xpath = "//*[@id=\"sole-searchbox-content\"]/div[2]")
    private WebElement roadway;
    @FindBy(xpath = "//*[@id=\"route-searchbox-content\"]/div[1]/div[1]/div[1]/span" )
    private WebElement bus;
    @FindBy(className = "route-start-input")
    private WebElement startStop;
    @FindBy(className = "route-end-input")
    private WebElement endStop;
    @FindBy(id = "search-button")
    private WebElement searchButton;
    @FindBy(xpath = "//*[@id=\"ui3_city_change\"]")
    private WebElement changeCity;
    @FindBy(name = "南京")
    private WebElement nanjing;
    @FindBy(id = "traffic_control")
    private WebElement traffic_control;
    @FindBy(id = "subway_control")
    private WebElement subway_control;

    @Test
    public void meth_luxian() throws InterruptedException {
        PageFactory.initElements(driver,this);
        Actions actions = new Actions(driver);
        actions.moveToElement(roadway).perform();
        roadway.click();
        Thread.sleep(1000);
        bus.click();
        Thread.sleep(1000);

    }
    @Test
    public void meth_lukuang() throws InterruptedException {
        PageFactory.initElements(driver,this);
        Actions actions = new Actions(driver);
        actions.moveToElement(changeCity).perform();
        changeCity.click();
        Thread.sleep(1000);
        nanjing.click();
        Thread.sleep(1000);
        traffic_control.click();
        Thread.sleep(1000);

    }
    @Test
    public void meth_luxiantu() throws InterruptedException {
        PageFactory.initElements(driver,this);
        Actions actions = new Actions(driver);
        actions.moveToElement(changeCity).perform();
        changeCity.click();
        Thread.sleep(1000);
        nanjing.click();
        Thread.sleep(1000);
        subway_control.click();
        Thread.sleep(1000);
    }

   @ParameterizedTest
   @CsvSource({
           "南京大学(鼓楼校区),新街口商业步行区,Test_BaiDuMap_R001_001.png",
           "南京大学(鼓楼校区),先锋书店(五台山店),Test_BaiDuMap_R001_002.png",
           "东南大学(四牌楼校区).新街口商业步行区,Test_BaiDuMap_R001_003.png",
           "东南大学(四牌楼校区).先锋书店(五台山店),Test_BaiDuMap_R001_004.png",
   })

    public void Test_BaiDuMap_R001(String start,String end,String picture) throws InterruptedException {

        meth_luxian();
        startStop.click();
        startStop.sendKeys(start+Keys.ENTER);
        Thread.sleep(1000);
        endStop.click();
        endStop.sendKeys(end+Keys.ENTER);
        Thread.sleep(1000);
        searchButton.click();
        Thread.sleep(2000);
        takeScreenshot(picture);
    }
    //R002
    @FindBy(id = "search-button")
    private WebElement type0;
    @FindBy(id = "search-button")
    private WebElement type5;
    @FindBy(id = "search-button")
    private WebElement type2;
    @FindBy(id = "search-button")
    private WebElement type3;
@Test
    public void Test_BaiDuMap_R002() throws InterruptedException {

        meth_luxian();
        startStop.click();
        startStop.sendKeys("南京大学(鼓楼校区)"+Keys.ENTER);
        Thread.sleep(1000);
        endStop.click();
        endStop.sendKeys("南京大学(仙林校区)"+Keys.ENTER);
        Thread.sleep(1000);
        searchButton.click();
        Thread.sleep(2000);
    type0.click();
    takeScreenshot("Test_BaiDuMap_R002_001.png");
    Thread.sleep(2000);
    type5.click();
    takeScreenshot("Test_BaiDuMap_R002_002.png");
    Thread.sleep(2000);
    type2.click();
    takeScreenshot("Test_BaiDuMap_R002_003.png");
    Thread.sleep(2000);
    type3.click();
    takeScreenshot("Test_BaiDuMap_R002_004.png");
Thread.sleep(2000);
    }
    @FindBy(xpath = "//*[@id=\"RA_ResItem_0\"]/table/tbody/tr[1]")
    private WebElement resitem;
    @FindBy(xpath = "//*[@id=\"RA_ResItem_0\"]/table/tbody/tr[1]/td[2]/div")
    private WebElement resitem1;
    @FindBy(className = "route_show")
    private WebElement show_all;
    @FindBy(xpath= "//*[@id=\"scheme_2\"]")
    private WebElement bus48;
    @FindBy(xpath = "//*[@id=\"route_2\"]/h3[2]/div/a")
    private WebElement action;
    @Test
    public void Test_BaiDuMap_R003() throws InterruptedException {

        meth_luxian();
        startStop.click();
        startStop.sendKeys("玄武湖景区"+Keys.ENTER);
        Thread.sleep(1000);
        endStop.click();
        endStop.sendKeys("先锋书店(五台山店)"+Keys.ENTER);
        Thread.sleep(1000);
        searchButton.click();
        Thread.sleep(2000);
        Actions actions = new Actions(driver);
        actions.moveToElement(resitem).perform();
        Thread.sleep(2000);
        resitem1.click();
        Thread.sleep(2000);
        show_all.click();
        Thread.sleep(2000);
        bus48.click();
        Thread.sleep(2000);
        actions.moveToElement(action).perform();
        actions.doubleClick(action).perform();
        Thread.sleep(2000);
        takeScreenshot("Test_BaiDuMap_R003_001.png");
    }
    @FindBy(id = "tarffic_ss")
    private WebElement tarffic_ss;
    @FindBy(className = "update")
    private WebElement bt_traffic_ctrl;
    @Test
    public void Test_BaiDuMap_R004() throws InterruptedException {
        meth_lukuang();
        tarffic_ss.click();
        Thread.sleep(3000);
        bt_traffic_ctrl.click();
        Thread.sleep(3000);
        takeScreenshot("Test_BaiDuMap_R004_001.png");
    }
    //R005
    @FindBy(className= "tarffic_yc")
    private WebElement tarffic_yc;
    @FindBy(id = "week_trafficCtrl_0")
    private WebElement Monday;
    @FindBy(id = "week_trafficCtrl_1")
    private WebElement Thusrday;
    @FindBy(id = "week_trafficCtrl_2")
    private WebElement Wenseday;
    @FindBy(id = "week_trafficCtrl_3")
    private WebElement Thirtyday;
    @FindBy(id = "week_trafficCtrl_4")
    private WebElement Friday;
    @FindBy(id = "week_trafficCtrl_5")
    private WebElement Sixday;
    @FindBy(id = "week_trafficCtrl_6")
    private WebElement Sunday;
    @Test
    public void Test_BaiDuMap_R005() throws InterruptedException {
        meth_lukuang();
        tarffic_yc.click();
        Thread.sleep(3000);
        Monday.click();
        Thread.sleep(3000);
        takeScreenshot("Test_BaiDuMap_R005_001.png");
        Thusrday.click();
        Thread.sleep(3000);
        takeScreenshot("Test_BaiDuMap_R005_002.png");
        Wenseday.click();
        Thread.sleep(3000);
        takeScreenshot("Test_BaiDuMap_R005_003.png");
        Thirtyday.click();
        Thread.sleep(3000);
        takeScreenshot("Test_BaiDuMap_R005_004.png");
        Friday.click();
        Thread.sleep(3000);
        takeScreenshot("Test_BaiDuMap_R005_005.png");
        Sixday.click();
        Thread.sleep(3000);
        takeScreenshot("Test_BaiDuMap_R005_006.png");
        Sunday.click();
        Thread.sleep(3000);
        takeScreenshot("Test_BaiDuMap_R005_007.png");
    }
    @FindBy(id= "timeBox_trafficCtrl")
    private WebElement timebox_traffic_ctrl;
    @FindBy(id = "bar_trafficCtrl")
    private WebElement bar_traffic_ctrl;
    @Test
    public void Test_BaiDuMap_R006() throws InterruptedException {
        meth_lukuang();
        tarffic_yc.click();
        Thread.sleep(3000);
        Actions actions = new Actions(driver);
        int step=5;
        int maxtimes=50;
        int times=0;
        while(times<maxtimes){
            String current_time=timebox_traffic_ctrl.getText().trim();
            System.out.println("当前时间:"+current_time+"目标时间:"+"21:00");
            if(current_time.equals("21:00")){
                break;
            }
            if(current_time.compareTo("21:00")<0){
                step=5;
            } else{
                step=-5;
            }
            actions.clickAndHold(bar_traffic_ctrl)
                    .moveByOffset(step,0)
                    .release()
                    .perform();
            times++;
            Thread.sleep(3000);
        }
        Thread.sleep(3000);
        takeScreenshot("Test_BaiDuMap_R006_001.png");
    }
    //7
    @FindBy(id = "sub_start_input")
    private WebElement sub_start_input;
    @FindBy(id= "sub_end_input")
    private WebElement sub_end_input;
    @FindBy(id ="search-button")
    private WebElement search_button;
    @ParameterizedTest
    @CsvSource({
            "珠江路,南京站,Test_BaiDuMap_R007_001.png",
            "珠江路,卡门子,Test_BaiDuMap_R007_002.png",
            "新街口,南京站,Test_BaiDuMap_R007_003.png",
            "新街口,卡门子,Test_BaiDuMap_R007_004.png",
    })
    public void Test_BaiDuMap_R007(String sub_start,String sun_end,String picture) throws InterruptedException {
        meth_luxiantu();
        sub_start_input.click();
        Thread.sleep(3000);
        sub_start_input.sendKeys(sub_start+Keys.ENTER);
        Thread.sleep(3000);
        sub_end_input.click();
        Thread.sleep(3000);
        sub_end_input.sendKeys(sun_end);
        search_button.click();
        Thread.sleep(3000);

        takeScreenshot(picture);
    }
    //7
    @FindBy(name = "大行宫")
    public WebElement daxing;
    @FindBy(name = "马群")
    public WebElement maqun;
    @Test
    public void Test_BaiDuMap_R008() throws InterruptedException {
        meth_luxiantu();
        Thread.sleep(3000);
        daxing.click();
        Thread.sleep(3000);
        maqun.click();
        Thread.sleep(3000);
        search_button.click();
        Thread.sleep(3000);
        takeScreenshot("Test_BaiDuMap_R008_001.png");

    }

    // 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();
        }
    }
}

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐