home-page.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { BrowserContext, expect, Page } from '@playwright/test';
  2. class Homepage {
  3. readonly page: Page;
  4. readonly context: BrowserContext;
  5. /**
  6. * Construct a new Homepage instance.
  7. *
  8. * @param {Page} page
  9. * @param {BrowserContext} context
  10. */
  11. constructor(page: Page, context: BrowserContext) {
  12. this.page = page;
  13. this.context = context;
  14. }
  15. async goTo() {
  16. await this.page.goto('/');
  17. }
  18. /**
  19. * Access a value from the local storage. An example of the localStorage:
  20. * [
  21. * {
  22. * name: 'persist:commandBar',
  23. * value: '{"recentNavigations":"[]","_persist":"{\\"version\\":1,\\"rehydrated\\":true}"}'
  24. * },
  25. * {
  26. * name: 'persist:root',
  27. *value: '{"quranReaderStyles":"{\\"tafsirFontScale\\":3,\\"quranTextFontScale\\":3,\\"translationFontScale\\":3,\\"quranFont\\":\\"code_v1\\",\\"mushafLines\\":\\"16_lines\\",\\"isUsingDefaultFont\\":true}","readingPreferences":"{\\"readingPreference\\":\\"translation\\",\\"showWordByWordTranslation\\":false,\\"selectedWordByWordTranslation\\":20,\\"showWordByWordTransliteration\\":false,\\"selectedWordByWordTransliteration\\":12,\\"selectedWordByWordLocale\\":\\"en\\",\\"isUsingDefaultWordByWordLocale\\":true,\\"showTooltipFor\\":[\\"translation\\"],\\"wordClickFunctionality\\":\\"play-audio\\"}","translations":"{\\"selectedTranslations\\":[131],\\"isUsingDefaultTranslations\\":true}","theme":"{\\"type\\":\\"auto\\"}","tafsirs":"{\\"selectedTafsirs\\":[\\"en-tafisr-ibn-kathir\\"],\\"isUsingDefaultTafsirs\\":true}","bookmarks":"{\\"bookmarkedVerses\\":{}}","search":"{\\"searchHistory\\":[]}","readingTracker":"{\\"lastReadVerse\\":{\\"verseKey\\":null,\\"chapterId\\":null,\\"page\\":null,\\"hizb\\":null},\\"recentReadingSessions\\":{}}","welcomeMessage":"{\\"isVisible\\":true}","defaultSettings":"{\\"isUsingDefaultSettings\\":true}","sidebarNavigation":"{\\"isVisible\\":false,\\"selectedNavigationItem\\":\\"surah\\"}","radio":"{\\"id\\":\\"1\\",\\"type\\":\\"curated\\",\\"chapterId\\":\\"53\\",\\"reciterId\\":\\"3\\"}","banner":"{\\"isBannerVisible\\":true}","_persist":"{\\"version\\":17,\\"rehydrated\\":true}"}'
  28. * }
  29. * ]
  30. *
  31. * @param {string} name
  32. * @param {string} storageKey
  33. * @returns {any|null}
  34. */
  35. async getPersistedValue(name: string, storageKey = 'persist:root'): Promise<any | null> {
  36. await this.page.waitForTimeout(1000);
  37. const storage = await this.context.storageState();
  38. expect(storage?.origins?.[0]?.localStorage).not.toBe(undefined);
  39. const localStorageArray = storage?.origins?.[0]?.localStorage;
  40. const persistedRoot = localStorageArray.filter(
  41. (localStorageObject) => localStorageObject.name === storageKey,
  42. );
  43. if (!persistedRoot) {
  44. return null;
  45. }
  46. const parentObject = JSON.parse(persistedRoot[0].value);
  47. if (!parentObject[name]) {
  48. return null;
  49. }
  50. return JSON.parse(parentObject[name]);
  51. }
  52. async openSettingsDrawer() {
  53. await this.page.locator('[aria-label="Change Settings"]').click();
  54. }
  55. }
  56. export default Homepage;