isReady.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { Page } from 'puppeteer';
  2. export const isPageReady = async (page: Page, url: string) => {
  3. await page.goto(url);
  4. await isElementThere(page, 'main ul li');
  5. };
  6. export const isElementThere = async (
  7. page: Page,
  8. selector: string,
  9. timeout: number = 4000
  10. ) =>
  11. await page.waitForFunction(
  12. (selector) => !!document.querySelector(selector),
  13. {
  14. timeout,
  15. },
  16. selector
  17. );
  18. export const isElementGone = async (
  19. page: Page,
  20. selector: string,
  21. timeout: number = 4000
  22. ) =>
  23. await page.waitForFunction(
  24. (selector) => !document.querySelector(selector),
  25. {
  26. timeout,
  27. },
  28. selector
  29. );
  30. export const isLightboxShown = async (page: Page) => {
  31. await isElementThere(page, '.basicLightbox--visible');
  32. return page.$('.basicLightbox--visible');
  33. };
  34. export const isLightboxClosed = async (page: Page) => {
  35. const lightbox = await isLightboxShown(page);
  36. // Click on the overlay to close the lightbox
  37. await lightbox.click({
  38. offset: {
  39. x: 10,
  40. y: 10,
  41. },
  42. });
  43. await isElementGone(page, '.basicLightbox--visible');
  44. };
  45. export const expectToastShown = async (
  46. page: Page,
  47. text: string,
  48. type: string
  49. ) => {
  50. let i = 0;
  51. await isElementThere(page, '.toast__container .toast');
  52. const toast = await page.$('.toast__container .toast');
  53. await expect(toast).not.toBeNull();
  54. const toastText = await toast.evaluate(
  55. (toast: HTMLElement) => toast.childNodes[1].textContent
  56. );
  57. await expect(toastText).toEqual(text);
  58. await expect(
  59. await toast.evaluate(
  60. (toast: HTMLElement, type: string) =>
  61. toast.classList.contains(`toast--${type}`),
  62. type
  63. )
  64. ).toBeTruthy();
  65. await toast.evaluate((toast: HTMLElement) => toast.remove());
  66. };