|
| 1 | +import { Page, Locator } from '@playwright/test'; |
| 2 | + |
| 3 | +export interface BaseSelectors { |
| 4 | + header: string; |
| 5 | + headerLogo: string; |
| 6 | + headerNavItems: string; |
| 7 | + footer: string; |
| 8 | + footerNav: string; |
| 9 | + footerSocialLinks: string; |
| 10 | + copyright: string; |
| 11 | +} |
| 12 | + |
| 13 | +export class BasePage { |
| 14 | + readonly page: Page; |
| 15 | + readonly selectors: BaseSelectors; |
| 16 | + |
| 17 | + constructor(page: Page) { |
| 18 | + this.page = page; |
| 19 | + this.selectors = { |
| 20 | + header: 'header.header', |
| 21 | + headerLogo: 'header.header .logo', |
| 22 | + headerNavItems: 'header.header .nav-li', |
| 23 | + footer: 'footer', |
| 24 | + footerNav: 'footer .nav-ul', |
| 25 | + footerSocialLinks: 'footer .social-nav a', |
| 26 | + copyright: '.copyright', |
| 27 | + }; |
| 28 | + } |
| 29 | + |
| 30 | + async goto(path: string = '/'): Promise<void> { |
| 31 | + await this.page.goto(path); |
| 32 | + await this.waitForPageLoad(); |
| 33 | + } |
| 34 | + |
| 35 | + async waitForPageLoad(): Promise<void> { |
| 36 | + await this.page.waitForLoadState('domcontentloaded'); |
| 37 | + await this.page.locator(this.selectors.header).waitFor({ state: 'visible', timeout: 10000 }); |
| 38 | + } |
| 39 | + |
| 40 | + async getPageTitle(): Promise<string> { |
| 41 | + return await this.page.title(); |
| 42 | + } |
| 43 | + |
| 44 | + async isHeaderVisible(): Promise<boolean> { |
| 45 | + return await this.page.locator(this.selectors.header).isVisible(); |
| 46 | + } |
| 47 | + |
| 48 | + async isFooterVisible(): Promise<boolean> { |
| 49 | + return await this.page.locator(this.selectors.footer).isVisible(); |
| 50 | + } |
| 51 | + |
| 52 | + async getHeaderNavItems(): Promise<string[]> { |
| 53 | + await this.page.locator(this.selectors.headerNavItems).first().waitFor({ state: 'visible' }); |
| 54 | + return await this.page.locator(`${this.selectors.headerNavItems} a`).allTextContents(); |
| 55 | + } |
| 56 | + |
| 57 | + async clickNavItem(text: string): Promise<void> { |
| 58 | + const currentUrl = this.page.url(); |
| 59 | + const link = this.page.locator(`${this.selectors.headerNavItems} a`).filter({ hasText: text }); |
| 60 | + await link.click(); |
| 61 | + await this.page.waitForFunction( |
| 62 | + (oldUrl) => window.location.href !== oldUrl, |
| 63 | + currentUrl, |
| 64 | + { timeout: 10000 } |
| 65 | + ); |
| 66 | + await this.waitForPageLoad(); |
| 67 | + } |
| 68 | + |
| 69 | + async getFooterNavItems(): Promise<string[]> { |
| 70 | + return await this.page.locator(`${this.selectors.footerNav} a`).allTextContents(); |
| 71 | + } |
| 72 | + |
| 73 | + async getFooterSocialLinksCount(): Promise<number> { |
| 74 | + return await this.page.locator(this.selectors.footerSocialLinks).count(); |
| 75 | + } |
| 76 | + |
| 77 | + async getCopyrightText(): Promise<string | null> { |
| 78 | + return await this.page.locator(this.selectors.copyright).textContent(); |
| 79 | + } |
| 80 | + |
| 81 | + async clickLogo(): Promise<void> { |
| 82 | + const currentUrl = this.page.url(); |
| 83 | + await this.page.waitForTimeout(200); |
| 84 | + await this.page.evaluate(() => { |
| 85 | + const logo = document.querySelector('header.header .logo') as HTMLElement; |
| 86 | + if (logo) { |
| 87 | + const link = logo.closest('a') as HTMLAnchorElement; |
| 88 | + if (link) link.click(); |
| 89 | + else logo.click(); |
| 90 | + } |
| 91 | + }); |
| 92 | + if (!currentUrl.match(/\/$|:9000\/?$|:8000\/?$/)) { |
| 93 | + await this.page.waitForFunction( |
| 94 | + (oldUrl) => window.location.href !== oldUrl, |
| 95 | + currentUrl, |
| 96 | + { timeout: 10000 } |
| 97 | + ); |
| 98 | + } |
| 99 | + await this.waitForPageLoad(); |
| 100 | + } |
| 101 | + |
| 102 | + async getCurrentUrl(): Promise<string> { |
| 103 | + return this.page.url(); |
| 104 | + } |
| 105 | + |
| 106 | + getLocator(selector: string): Locator { |
| 107 | + return this.page.locator(selector); |
| 108 | + } |
| 109 | +} |
0 commit comments