search-modal.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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", { delay: 50 }) // escape (potential) search box typing
  5. await page.waitForTimeout(500)
  6. await page.keyboard.press("Escape", { delay: 50 }) // 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: 100 })
  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.waitForTimeout(200)
  37. await page.keyboard.press('Enter', { delay: 50 })
  38. return pageTitle;
  39. }
  40. /**
  41. * type a search query into the search box
  42. * stop at the point where search box shows up
  43. *
  44. * @param page the pw page object
  45. * @param query the search query to type into the search box
  46. * @returns the HTML element for the search results ui
  47. */
  48. export async function searchPage(page: Page, query: string): Promise<ElementHandle<SVGElement | HTMLElement>[]>{
  49. await closeSearchBox(page)
  50. await page.click('#search-button')
  51. await page.waitForSelector('[placeholder="What are you looking for?"]')
  52. await page.fill('[placeholder="What are you looking for?"]', query)
  53. await page.waitForTimeout(2000) // wait longer for search contents to render
  54. return page.$$('.search-results>div');
  55. }