isReady.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 (page: Page, selector: string) =>
  7. await page.waitForFunction(
  8. (selector) => !!document.querySelector(selector),
  9. {},
  10. selector
  11. );
  12. export const isElementGone = async (page: Page, selector: string) =>
  13. await page.waitForFunction(
  14. (selector) => !document.querySelector(selector),
  15. {},
  16. selector
  17. );
  18. export const isLightboxShown = async (page: Page) => {
  19. await isElementThere(page, '.basicLightbox--visible');
  20. return page.$('.basicLightbox--visible');
  21. };
  22. export const isLightboxClosed = async (page: Page) => {
  23. const lightbox = await isLightboxShown(page);
  24. // Click on the overlay to close the lightbox
  25. await lightbox.click({
  26. offset: {
  27. x: 10,
  28. y: 10,
  29. },
  30. });
  31. await isElementGone(page, '.basicLightbox--visible');
  32. };
  33. export const expectToastShown = async (
  34. page: Page,
  35. text: string,
  36. type: string
  37. ) => {
  38. await page.waitForTimeout(100);
  39. const toast = await page.$('.toast__container .toast');
  40. await expect(
  41. await toast.evaluate((toast) => toast.childNodes[1].textContent)
  42. ).toEqual(text);
  43. await expect(
  44. await toast.evaluate(
  45. (toast, type: string) => toast.classList.contains(`toast--${type}`),
  46. type
  47. )
  48. ).toBeTruthy();
  49. await toast.evaluate((toast) => toast.remove());
  50. };