|
|
import { Locator, Page } from 'playwright'; |
|
|
import { envVariables } from '../..'; |
|
|
import { getCalypsoURL } from '../../data-helper'; |
|
|
import { clickNavTab } from '../../element-helper'; |
|
|
|
|
|
export type StatsTabs = 'Traffic' | 'Insights' | 'Subscribers' | 'Store'; |
|
|
type TrafficActivityType = 'Views' | 'Visitors' | 'Likes' | 'Comments'; |
|
|
type StoreActivityType = 'Gross Sales' | 'Net sales' | 'Orders' | 'Avg. Order Value'; |
|
|
|
|
|
type ActivityTypes = |
|
|
| { tab: 'Traffic'; type: TrafficActivityType } |
|
|
| { tab: 'Store'; type: StoreActivityType }; |
|
|
type StatsPeriod = 'Days' | 'Weeks' | 'Months' | 'Years'; |
|
|
type SubscriberOrigin = 'WordPress.com' | 'Email'; |
|
|
|
|
|
const selectors = { |
|
|
graph: '.chart__bars', |
|
|
statsTabs: '.stats-tabs', |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export class StatsPage { |
|
|
private page: Page; |
|
|
private anchor: Locator; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constructor( page: Page ) { |
|
|
this.page = page; |
|
|
this.anchor = this.page.getByRole( 'main' ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async visit( siteSlug?: string ) { |
|
|
await this.page.goto( getCalypsoURL( `/stats/${ siteSlug }` ) ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private async dismissTooltip() { |
|
|
|
|
|
const tooltipButton = this.page.getByRole( 'button', { name: 'Got it' } ); |
|
|
|
|
|
if ( ( await tooltipButton.count() ) > 0 ) { |
|
|
await tooltipButton.click(); |
|
|
await tooltipButton.waitFor( { state: 'hidden' } ); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async clickTab( name: StatsTabs ): Promise< void > { |
|
|
await this.dismissTooltip(); |
|
|
await clickNavTab( this.page, name, { isCoreTabs: true } ); |
|
|
|
|
|
|
|
|
|
|
|
if ( name === 'Traffic' ) { |
|
|
await this.page.waitForURL( /stats\/day/ ); |
|
|
} |
|
|
if ( name === 'Insights' ) { |
|
|
await this.page.waitForURL( /stats\/insights/ ); |
|
|
} |
|
|
if ( name === 'Subscribers' ) { |
|
|
await this.page.waitForURL( /stats\/subscribers/ ); |
|
|
} |
|
|
if ( name === 'Store' ) { |
|
|
await this.page.waitForURL( /store\/stats\/orders/ ); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async selectStatsPeriod( period: StatsPeriod ) { |
|
|
const target = this.anchor.getByRole( 'radiogroup' ).getByRole( 'radio', { name: period } ); |
|
|
await target.click(); |
|
|
|
|
|
if ( ! ( await target.isChecked() ) ) { |
|
|
throw new Error( `Failed to select the Stats Period ${ period }` ); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async selectStatsPeriodFromDropdown( period: StatsPeriod ) { |
|
|
const expandDropdownButton = this.anchor |
|
|
.locator( '.stats-interval-dropdown' ) |
|
|
.getByRole( 'button' ) |
|
|
.first(); |
|
|
await expandDropdownButton.click(); |
|
|
|
|
|
const target = this.page |
|
|
.locator( '.components-popover' ) |
|
|
.getByRole( 'radio', { name: period } ); |
|
|
await target.click(); |
|
|
|
|
|
if ( ! ( await target.isChecked() ) ) { |
|
|
throw new Error( `Failed to select the Stats Period ${ period }` ); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async showStatsOfType( activityType: ActivityTypes ): Promise< void > { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const locators = await this.page.locator( '.is-loading' ).all(); |
|
|
for ( const locator of locators ) { |
|
|
await locator.waitFor( { state: 'detached' } ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const target = this.anchor |
|
|
.locator( selectors.statsTabs ) |
|
|
.getByRole( 'listitem' ) |
|
|
.filter( { hasText: activityType.type } ); |
|
|
await target.waitFor(); |
|
|
await target.click(); |
|
|
|
|
|
|
|
|
if ( envVariables.VIEWPORT_NAME === 'desktop' ) { |
|
|
await this.anchor |
|
|
.locator( '.chart__legend-options' ) |
|
|
.getByRole( 'listitem' ) |
|
|
.filter( { hasText: activityType.type } ) |
|
|
.waitFor(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await this.anchor |
|
|
.locator( selectors.statsTabs ) |
|
|
.locator( '.is-selected' ) |
|
|
.filter( { hasText: activityType.type } ) |
|
|
.waitFor(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async clickViewAllAnnualInsights() { |
|
|
await this.anchor.getByRole( 'link', { name: 'View all annual insights' } ).click(); |
|
|
|
|
|
await this.page.waitForURL( /annualstats/ ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async annualInsightPresentForYear( year: number ) { |
|
|
await this.page |
|
|
.getByRole( 'main' ) |
|
|
.getByRole( 'table' ) |
|
|
.getByRole( 'rowheader' ) |
|
|
.filter( { hasText: year.toString() } ) |
|
|
.waitFor(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async selectSubscriberType( type: SubscriberOrigin ) { |
|
|
const target = this.anchor.getByRole( 'radiogroup' ).getByRole( 'radio', { name: type } ); |
|
|
await target.click(); |
|
|
|
|
|
if ( ! ( await target.isChecked() ) ) { |
|
|
throw new Error( `Failed to select the Subscriber type ${ type }` ); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
|