HomePage.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { Locator, Page } from '@playwright/test'
  2. export class HomePage {
  3. readonly page: Page
  4. // 页面元素定位器
  5. readonly title: Locator
  6. readonly subtitle: Locator
  7. readonly vehicleTypeButtons: Locator
  8. readonly departureAreaSelector: Locator
  9. readonly destinationAreaSelector: Locator
  10. readonly departureLocationSearch: Locator
  11. readonly destinationLocationSearch: Locator
  12. readonly datePicker: Locator
  13. readonly searchButton: Locator
  14. readonly selectedLocationText: Locator
  15. readonly provinceList: Locator
  16. readonly cityList: Locator
  17. readonly districtList: Locator
  18. readonly locationSearchResults: Locator
  19. readonly noResultsText: Locator
  20. constructor(page: Page) {
  21. this.page = page
  22. // 初始化定位器
  23. this.title = page.locator('text=便捷出行')
  24. this.subtitle = page.locator('text=专业出行服务,安全舒适')
  25. this.vehicleTypeButtons = page.locator('text=大巴拼车, text=商务车, text=包车')
  26. this.departureAreaSelector = page.locator('text=请选择出发地区')
  27. this.destinationAreaSelector = page.locator('text=请选择目的地区')
  28. this.departureLocationSearch = page.locator('input[placeholder="搜索出发地点"]')
  29. this.destinationLocationSearch = page.locator('input[placeholder="搜索目的地点"]')
  30. this.datePicker = page.locator('input[type="date"]')
  31. this.searchButton = page.locator('text=查询路线')
  32. this.selectedLocationText = page.locator('text=已选择:')
  33. this.provinceList = page.locator('text=北京市, text=广东省, text=上海市')
  34. this.cityList = page.locator('text=广州市, text=深圳市')
  35. this.districtList = page.locator('text=天河区, text=越秀区')
  36. this.locationSearchResults = page.locator('text=北京首都国际机场, text=北京南站, text=上海虹桥机场')
  37. this.noResultsText = page.locator('text=未找到相关地点')
  38. }
  39. // 页面操作方法
  40. async goto() {
  41. await this.page.goto('/mini/')
  42. }
  43. async selectVehicleType(type: '大巴拼车' | '商务车' | '包车') {
  44. await this.page.click(`text=${type}`)
  45. }
  46. async selectDepartureArea(province: string, city?: string, district?: string) {
  47. await this.departureAreaSelector.click()
  48. await this.page.click(`text=${province}`)
  49. if (city) {
  50. await this.page.click('text=请选择城市')
  51. await this.page.click(`text=${city}`)
  52. }
  53. if (district) {
  54. await this.page.click('text=请选择区县')
  55. await this.page.click(`text=${district}`)
  56. }
  57. }
  58. async selectDestinationArea(province: string, city?: string, district?: string) {
  59. await this.destinationAreaSelector.click()
  60. await this.page.click(`text=${province}`)
  61. if (city) {
  62. await this.page.click('text=请选择城市')
  63. await this.page.click(`text=${city}`)
  64. }
  65. if (district) {
  66. await this.page.click('text=请选择区县')
  67. await this.page.click(`text=${district}`)
  68. }
  69. }
  70. async searchDepartureLocation(keyword: string) {
  71. await this.departureLocationSearch.fill(keyword)
  72. await this.page.waitForTimeout(500) // 等待防抖
  73. }
  74. async searchDestinationLocation(keyword: string) {
  75. await this.destinationLocationSearch.fill(keyword)
  76. await this.page.waitForTimeout(500) // 等待防抖
  77. }
  78. async selectLocation(locationName: string) {
  79. await this.page.click(`text=${locationName}`)
  80. }
  81. async setDate(date: string) {
  82. await this.datePicker.fill(date)
  83. }
  84. async searchRoutes() {
  85. await this.searchButton.click()
  86. }
  87. async getSelectedLocationText(): Promise<string> {
  88. return await this.selectedLocationText.textContent() || ''
  89. }
  90. async isVehicleTypeSelected(type: string): Promise<boolean> {
  91. const button = this.page.locator(`text=${type}`)
  92. const parent = button.locator('..')
  93. const className = await parent.getAttribute('class')
  94. return className?.includes('bg-blue-500') || false
  95. }
  96. }