search-modal.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { Page, Locator, ElementHandle } from '@playwright/test'
  2. import { randomString } from './basic'
  3. export async function closeSearchBox(page: Page): Promise<void> {
  4. await page.keyboard.press("Escape") // escape (potential) search box typing
  5. await page.waitForTimeout(500)
  6. await page.keyboard.press("Escape") // escape modal
  7. }
  8. export async function createRandomPage(page: Page) {
  9. const randomTitle = randomString(20)
  10. await closeSearchBox(page)
  11. // Click #search-button
  12. await page.click('#search-button')
  13. // Fill [placeholder="What are you looking for?"]
  14. await page.fill('[placeholder="What are you looking for?"]', randomTitle)
  15. await page.keyboard.press('Enter', { delay: 50 })
  16. // Wait for h1 to be from our new page
  17. await page.waitForSelector(`h1 >> text="${randomTitle}"`, { state: 'visible' })
  18. // wait for textarea of first block
  19. await page.waitForSelector('textarea >> nth=0', { state: 'visible' })
  20. return randomTitle;
  21. }
  22. export async function createPage(page: Page, page_name: string) {// Click #search-button
  23. await closeSearchBox(page)
  24. await page.click('#search-button')
  25. // Fill [placeholder="What are you looking for?"]
  26. await page.fill('[placeholder="What are you looking for?"]', page_name)
  27. await page.keyboard.press('Enter', { delay: 50 })
  28. // wait for textarea of first block
  29. await page.waitForSelector('textarea >> nth=0', { state: 'visible' })
  30. return page_name;
  31. }
  32. export async function searchAndJumpToPage(page: Page, pageTitle: string) {
  33. await closeSearchBox(page)
  34. await page.click('#search-button')
  35. await page.type('[placeholder="What are you looking for?"]', pageTitle)
  36. await page.waitForSelector(`[data-page-ref="${pageTitle}"]`, { state: 'visible' })
  37. page.click(`[data-page-ref="${pageTitle}"]`)
  38. await page.waitForNavigation()
  39. return pageTitle;
  40. }
  41. /**
  42. * type a search query into the search box
  43. * stop at the point where search box shows up
  44. *
  45. * @param page the pw page object
  46. * @param query the search query to type into the search box
  47. * @returns the HTML element for the search results ui
  48. */
  49. export async function searchPage(page: Page, query: string): Promise<ElementHandle<SVGElement | HTMLElement>[]>{
  50. await closeSearchBox(page)
  51. await page.click('#search-button')
  52. await page.waitForSelector('[placeholder="What are you looking for?"]')
  53. await page.fill('[placeholder="What are you looking for?"]', query)
  54. await page.waitForTimeout(2000) // wait longer for search contents to render
  55. return page.$$('.search-results>div');
  56. }