import os
import time
import datetime
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import pytest


class TestBaiDuMap:
    
    def setup_method(self, method):
        // 设置Chrome驱动路径和选项
        chrome_options = Options()
        chrome_options.add_argument("--remote-allow-origins=*")
        self.driver = webdriver.Chrome(executable_path="C:\\Program Files\\Google\\Chrome\\Application\\chromedriver.exe", 
                                      options=chrome_options)
        // 打开百度地图网站并最大化窗口
        self.driver.get("https://map.baidu.com/")
        self.driver.maximize_window()
        self.driver.implicitly_wait(10)
        self.actions = ActionChains(self.driver)
    
    def bustest(self):
        // 测试公交路线功能的基础设置
        route = self.driver.find_element(By.XPATH, '//*[@id="sole-searchbox-content"]/div[2]')
        bus = self.driver.find_element(By.XPATH, '//*[@id="route-searchbox-content"]/div[1]/div[1]/div[1]')
        
        // 点击路线按钮,然后点击公交选项
        self.actions.move_to_element(route).click().perform()
        time.sleep(1)
        self.actions.move_to_element(bus).click().perform()
        time.sleep(1)
    
    @pytest.mark.parametrize("start_location,end_location,file_name", [
        ("南京大学(鼓楼校区)", "新街口商业步行区", "BaiDuMap_R001_001.png"),
        ("南京大学(鼓楼校区)", "先锋书店(五台山店)", "BaiDuMap_R001_002.png"),
        ("东南大学(四牌楼校区)", "新街口商业步行区", "BaiDuMap_R001_003.png"),
        ("东南大学(四牌楼校区)", "先锋书店(五台山店)", "BaiDuMap_R001_004.png"),
    ])
    def test_BaiDuMap_R001(self, start_location, end_location, file_name):
        // 测试公交路线规划功能
        self.bustest()
        
        // 定位起点、终点输入框和搜索按钮
        formda = self.driver.find_element(By.XPATH, '//*[@id="route-searchbox-content"]/div[2]/div/div[2]/div[1]/input')
        toda = self.driver.find_element(By.XPATH, '//*[@id="route-searchbox-content"]/div[2]/div/div[2]/div[2]/input')
        souso = self.driver.find_element(By.XPATH, '//*[@id="search-button"]')
        
        // 输入起点和终点,执行搜索
        formda.click()
        formda.send_keys(start_location)
        time.sleep(1)
        toda.click()
        toda.send_keys(end_location)
        time.sleep(1)
        souso.click()
        time.sleep(6)
        self.take_screenshot(file_name)
    
    def test_BaiDuMap_R002(self):
        // 测试不同路线规划策略(时间短、少换乘、少步行)
        self.bustest()
        
        // 定位输入元素
        formda = self.driver.find_element(By.XPATH, '//*[@id="route-searchbox-content"]/div[2]/div/div[2]/div[1]/input')
        toda = self.driver.find_element(By.XPATH, '//*[@id="route-searchbox-content"]/div[2]/div/div[2]/div[2]/input')
        souso = self.driver.find_element(By.XPATH, '//*[@id="search-button"]')
        
        // 输入起点和终点
        formda.click()
        for mda.send_keys("南京大学(鼓楼校区)")
        time.sleep(1)
        toda.click()
        toda.send_keys("南京大学(仙林校区)")
        time.sleep(1)
        souso.click()
        time.sleep(5)
        self.take_screenshot("BaiDuMap_R002_001.png")
        
        // 定位不同路线策略按钮
        time_short = self.driver.find_element(By.XPATH, '//*[@id="type5"]')
        cost_low = self.driver.find_element(By.XPATH, '//*[@id="type2"]')
        walk_less = self.driver.find_element(By.XPATH, '//*[@id="type3"]')
        
        // 测试"时间短"策略
        time_short.click()
        time.sleep(3)
        self.take_screenshot("BaiDuMap_R002_002.png")
        
        // 测试"少换乘"策略
        cost_low.click()
        time.sleep(3)
        self.take_screenshot("BaiDuMap_R002_003.png")
        
        // 测试"少步行"策略
        walk_less.click()
        time.sleep(3)
        self.take_screenshot("BaiDuMap_R002_004.png")
    
    @pytest.mark.parametrize("start_location,end_location,file_name", [
        ("玄武湖景区", "先锋书店(五台山店)", "BaiDuMap_R003_001.png"),
    ])
    def test_BaiDuMap_R003(self, start_location, end_location, file_name):
        // 测试公交路线详细信息和街景功能
        self.bustest()
        
        // 定位输入元素
        formda = self.driver.find_element(By.XPATH, '//*[@id="route-searchbox-content"]/div[2]/div/div[2]/div[1]/input')
        toda = self.driver.find_element(By.XPATH, '//*[@id="route-searchbox-content"]/div[2]/div/div[2]/div[2]/input')
        souso = self.driver.find_element(By.XPATH, '//*[@id="search-button"]')
        
        // 输入起点和终点并搜索
        formda.click()
        formda.send_keys(start_location)
        time.sleep(1)
        toda.click()
        toda.send_keys(end_location)
        time.sleep(1)
        souso.click()
        time.sleep(3)
        
        // 定位公交路线相关元素
        bus1 = self.driver.find_element(By.XPATH, '//*[@id="RA_ResItem_0"]/table/tbody/tr[1]/td[1]')
        bus2 = self.driver.find_element(By.XPATH, '//*[@id="RA_ResItem_0"]/table/tbody/tr[1]/td[2]/div')
        bus3 = self.driver.find_element(By.XPATH, '//*[@id="strategySel"]/div[2]/a')
        bus4 = self.driver.find_element(By.XPATH, '//*[@id="scheme_2"]/div[1]')
        bus5 = self.driver.find_element(By.XPATH, '//*[@id="route_2"]/h3[2]/div/a')
        
        // 执行路线详细查看操作
        self.actions.move_to_element(bus1).perform()
        time.sleep(1)
        bus2.click()
        time.sleep(1)
        bus3.click()
        time.sleep(2)
        bus4.click()
        time.sleep(1)
        self.actions.move_to_element(bus5).perform()
        self.actions.double_click(bus5).perform()
        time.sleep(3)
        self.take_screenshot(file_name)
    
    def lukuangtest(self):
        // 路况测试的基础设置
        change = self.driver.find_element(By.XPATH, '//*[@id="ui3_city_change"]/a')
        // 点击城市切换按钮
        self.actions.move_to_element(change).click().perform()
        time.sleep(1)
        // 选择南京城市
        self.driver.find_element(By.NAME, "南京").click()
        time.sleep(1)
        // 打开路况图层
        traffic = self.driver.find_element(By.XPATH, '//*[@id="traffic_control"]')
        traffic.click()
        time.sleep(2)
    
    def test_BaiDuMap_R004(self):
        // 测试路况刷新功能
        self.lukuangtest()
        // 定位并点击刷新按钮
        shuaxin = self.driver.find_element(By.XPATH, '//*[@id="bt_trafficCtrl"]')
        shuaxin.click()
        time.sleep(2)
        self.take_screenshot("BaiDuMap_R004_001.png")
    
    def test_BaiDuMap_R005(self):
        // 测试一周内不同日期的路况预测
        self.lukuangtest()
        // 打开路况预测功能
        luyou = self.driver.find_element(By.XPATH, '//*[@id="tarffic_yc"]')
        luyou.click()
        time.sleep(2)
        
        // 定义一周七天的元素定位和截图文件名
        days = [
            ('//*[@id="week_trafficCtrl_0"]', "BaiDuMap_R005_001.png"),  // 星期一
            ('//*[@id="week_trafficCtrl_1"]', "BaiDuMap_R005_002.png"),  // 星期二
            ('//*[@id="week_trafficCtrl_2"]', "BaiDuMap_R005_003.png"),  // 星期三
            ('//*[@id="week_trafficCtrl_3"]', "BaiDuMap_R005_004.png"),  // 星期四
            ('//*[@id="week_trafficCtrl_4"]', "BaiDuMap_R005_005.png"),  // 星期五
            ('//*[@id="week_trafficCtrl_5"]', "BaiDuMap_R005_006.png"),  // 星期六
            ('//*[@id="week_trafficCtrl_6"]', "BaiDuMap_R005_007.png")   // 星期日
        ]
        
        // 循环点击每一天并截图
        for day_xpath, screenshot_name in days:
            day_element = self.driver.find_element(By.XPATH, day_xpath)
            day_element.click()
            time.sleep(3)
            self.take_screenshot(screenshot_name)
    
    @pytest.mark.parametrize("target_time,file_name", [
        ("21:00", "BaiDuMap_R006_001.png"),
    ])
    def test_BaiDuMap_R006(self, target_time, file_name):
        // 测试路况预测时间轴滑动功能
        self.lukuangtest()
        // 打开路况预测
        luyou = self.driver.find_element(By.XPATH, '//*[@id="tarffic_yc"]')
        luyou.click()
        time.sleep(1)
        
        // 定位时间轴滑块和时间显示元素
        time_line = self.driver.find_element(By.XPATH, '//*[@id="bar_trafficCtrl"]')
        time_element = self.driver.find_element(By.XPATH, '//*[@id="timeBox_trafficCtrl"]')
        
        step = 5
        max_attempts = 50
        attempts = 0
        
        // 循环滑动时间轴直到找到目标时间
        while attempts < max_attempts:
            current_time = time_element.text.strip()
            print(f"当前时间: {current_time},目标时间: {target_time}")
            
            // 如果找到目标时间则退出循环
            if current_time == target_time:
                break
            
            // 根据当前时间与目标时间的比较决定滑动方向
            if current_time < target_time:
                step = 5  // 向右滑动
            else:
                step = -5  // 向左滑动
            
            // 执行滑动操作
            self.actions.click_and_hold(time_line).move_by_offset(step, 0).release().perform()
            attempts += 1
            time.sleep(1)
        
        time.sleep(2)
        self.take_screenshot(file_name)
    
    def ditietest(self):
        // 地铁测试的基础设置
        change = self.driver.find_element(By.XPATH, '//*[@id="ui3_city_change"]/a')
        // 切换城市到南京
        self.actions.move_to_element(change).click().perform()
        time.sleep(1)
        self.driver.find_element(By.NAME, "南京").click()
        time.sleep(1)
        // 打开地铁图层
        subway = self.driver.find_element(By.XPATH, '//*[@id="subway_control"]')
        subway.click()
        time.sleep(2)
    
    @pytest.mark.parametrize("start_location,end_location,file_name", [
        ("珠江路", "南京站", "BaiDuMap_R007_001.png"),
    ])
    def test_BaiDuMap_R007(self, start_location, end_location, file_name):
        // 测试地铁路线规划功能
        self.ditietest()
        time.sleep(2)
        
        // 定位地铁起点、终点输入框和搜索按钮
        start = self.driver.find_element(By.XPATH, '//*[@id="sub_start_input"]')
        end = self.driver.find_element(By.XPATH, '//*[@id="sub_end_input"]')
        souso01 = self.driver.find_element(By.XPATH, '//*[@id="search-button"]')
        
        // 输入起点和终点并搜索
        start.click()
        start.send_keys(start_location)
        time.sleep(1)
        end.click()
        end.send_keys(end_location)
        time.sleep(1)
        souso01.click()
        time.sleep(3)
        self.take_screenshot(file_name)
    
    def test_BaiDuMap_R008(self):
        // 测试地铁站点选择和路线显示
        self.ditietest()
        time.sleep(1)
        
        // 定位大行宫站和马群站
        daxinggong_station = self.driver.find_element(By.NAME, "大行宫")
        maqun_station = self.driver.find_element(By.NAME, "马群")
        
        // 依次点击两个站点查看路线
        self.actions.move_to_element(daxinggong_station).click().perform()
        time.sleep(1)
        self.actions.move_to_element(maqun_station).click().perform()
        time.sleep(3)
        self.take_screenshot("BaiDuMap_R008_001.png")
    
    def teardown_method(self, method):
        // 测试结束后关闭浏览器
        self.driver.quit()
    
    def take_screenshot(self, file_name):
        // 截图功能:生成带时间戳的文件名并保存截图
        timestamp = datetime.datetime.now().strftime("%H%M%S%d%f")[:-3]
        timestamped_file_name = timestamp + "_" + file_name
        
        // 创建screenshots目录(如果不存在)
        screenshots_dir = "screenshots"
        if not os.path.exists(screenshots_dir):
            os.makedirs(screenshots_dir)
        
        // 保存截图到指定路径
        screenshot_file_path = os.path.join(screenshots_dir, timestamped_file_name)
        self.driver.save_screenshot(screenshot_file_path)


if __name__ == "__main__":
    // 运行测试
    pytest.main([__file__])

Logo

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

更多推荐