2021-07-13 19:38:05 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2021-06-24 20:08:16 +00:00
|
|
|
import { ElementHandle } from 'puppeteer';
|
2021-07-13 19:38:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2022-02-02 12:55:10 +00:00
|
|
|
import {
|
|
|
|
waitForElementByText,
|
|
|
|
waitUntilElementStopsMoving,
|
|
|
|
} from '../utils/actions';
|
2021-06-24 20:08:16 +00:00
|
|
|
import { Analytics } from './Analytics';
|
|
|
|
|
|
|
|
type Section = {
|
|
|
|
title: string;
|
|
|
|
element: ElementHandle< Element >;
|
|
|
|
};
|
|
|
|
const isSection = ( item: Section | undefined ): item is Section => {
|
|
|
|
return !! item;
|
|
|
|
};
|
|
|
|
|
|
|
|
export class AnalyticsOverview extends Analytics {
|
2022-03-17 11:13:37 +00:00
|
|
|
async navigate(): Promise< void > {
|
2021-06-24 20:08:16 +00:00
|
|
|
await this.navigateToSection( 'overview' );
|
|
|
|
}
|
|
|
|
|
2022-03-17 11:13:37 +00:00
|
|
|
async getSections(): Promise< Section[] > {
|
2021-06-24 20:08:16 +00:00
|
|
|
const list = await this.page.$$(
|
|
|
|
'.woocommerce-dashboard-section .woocommerce-section-header'
|
|
|
|
);
|
|
|
|
const sections = await Promise.all(
|
|
|
|
list.map( async ( item ) => {
|
|
|
|
const title = await item.evaluate( ( element ) => {
|
|
|
|
const header = element.querySelector( 'h2' );
|
|
|
|
return header?.textContent;
|
|
|
|
} );
|
|
|
|
if ( title ) {
|
|
|
|
return {
|
|
|
|
title,
|
|
|
|
element: item,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
} )
|
|
|
|
);
|
|
|
|
return sections.filter( isSection );
|
|
|
|
}
|
|
|
|
|
2022-03-17 11:13:37 +00:00
|
|
|
async getSectionTitles(): Promise< string[] > {
|
2021-10-06 16:24:10 +00:00
|
|
|
const sections = ( await this.getSections() ).map(
|
|
|
|
( section ) => section.title
|
|
|
|
);
|
|
|
|
return sections;
|
|
|
|
}
|
|
|
|
|
2022-03-17 11:13:37 +00:00
|
|
|
async openSectionEllipsis( sectionTitle: string ): Promise< void > {
|
2021-06-24 20:08:16 +00:00
|
|
|
const section = ( await this.getSections() ).find(
|
2021-07-13 19:38:05 +00:00
|
|
|
( thisSection ) => thisSection.title === sectionTitle
|
2021-06-24 20:08:16 +00:00
|
|
|
);
|
|
|
|
if ( section ) {
|
|
|
|
const ellipsisMenu = await section.element.$(
|
|
|
|
'.woocommerce-ellipsis-menu .woocommerce-ellipsis-menu__toggle'
|
|
|
|
);
|
|
|
|
await ellipsisMenu?.click();
|
|
|
|
await this.page.waitForSelector(
|
|
|
|
'.woocommerce-ellipsis-menu div[role=menu]'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-17 11:13:37 +00:00
|
|
|
async closeSectionEllipsis( sectionTitle: string ): Promise< void > {
|
2021-06-24 20:08:16 +00:00
|
|
|
const section = ( await this.getSections() ).find(
|
2021-07-13 19:38:05 +00:00
|
|
|
( thisSection ) => thisSection.title === sectionTitle
|
2021-06-24 20:08:16 +00:00
|
|
|
);
|
|
|
|
if ( section ) {
|
|
|
|
const ellipsisMenu = await section.element.$(
|
|
|
|
'.woocommerce-ellipsis-menu .woocommerce-ellipsis-menu__toggle'
|
|
|
|
);
|
|
|
|
await ellipsisMenu?.click();
|
2021-10-06 14:07:09 +00:00
|
|
|
await page.waitForFunction(
|
2021-06-24 20:08:16 +00:00
|
|
|
() =>
|
|
|
|
! document.querySelector(
|
|
|
|
'.woocommerce-ellipsis-menu div[role=menu]'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-17 11:13:37 +00:00
|
|
|
async removeSection( sectionTitle: string ): Promise< void > {
|
2021-06-24 20:08:16 +00:00
|
|
|
await this.openSectionEllipsis( sectionTitle );
|
|
|
|
const item = await waitForElementByText( 'div', 'Remove section' );
|
|
|
|
await item?.click();
|
|
|
|
}
|
|
|
|
|
2022-03-17 11:13:37 +00:00
|
|
|
async addSection( sectionTitle: string ): Promise< void > {
|
2021-06-24 20:08:16 +00:00
|
|
|
await this.page.waitForSelector( "button[title='Add more sections']" );
|
|
|
|
await this.page.click( "button[title='Add more sections']" );
|
2022-02-02 12:55:10 +00:00
|
|
|
const addSectionSelector = `button[title='Add ${ sectionTitle } section']`;
|
|
|
|
await this.page.waitForSelector( addSectionSelector );
|
|
|
|
await waitUntilElementStopsMoving( addSectionSelector );
|
|
|
|
await this.page.click( addSectionSelector );
|
2021-06-24 20:08:16 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 11:13:37 +00:00
|
|
|
async moveSectionDown( sectionTitle: string ): Promise< void > {
|
2021-06-24 20:08:16 +00:00
|
|
|
await this.openSectionEllipsis( sectionTitle );
|
|
|
|
const item = await waitForElementByText( 'div', 'Move down' );
|
|
|
|
await item?.click();
|
|
|
|
}
|
|
|
|
|
2022-03-17 11:13:37 +00:00
|
|
|
async moveSectionUp( sectionTitle: string ): Promise< void > {
|
2021-06-24 20:08:16 +00:00
|
|
|
await this.openSectionEllipsis( sectionTitle );
|
|
|
|
const item = await waitForElementByText( 'div', 'Move up' );
|
|
|
|
await item?.click();
|
|
|
|
}
|
|
|
|
|
2022-03-17 11:13:37 +00:00
|
|
|
async getEllipsisMenuItems(
|
|
|
|
sectionTitle: string
|
|
|
|
): Promise<
|
|
|
|
{ title: string | null; element: ElementHandle< Element > }[]
|
|
|
|
> {
|
2021-06-24 20:08:16 +00:00
|
|
|
await this.openSectionEllipsis( sectionTitle );
|
|
|
|
const list = await this.page.$$(
|
|
|
|
'.woocommerce-ellipsis-menu div[role=menuitem]'
|
|
|
|
);
|
|
|
|
return Promise.all(
|
|
|
|
list.map( async ( item ) => ( {
|
|
|
|
title: await item.evaluate(
|
|
|
|
( element ) => element?.textContent
|
|
|
|
),
|
|
|
|
element: item,
|
|
|
|
} ) )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-03-17 11:13:37 +00:00
|
|
|
async getEllipsisMenuCheckboxItems(
|
|
|
|
sectionTitle: string
|
|
|
|
): Promise<
|
|
|
|
{ title: string | null; element: ElementHandle< Element > }[]
|
|
|
|
> {
|
2021-06-24 20:08:16 +00:00
|
|
|
await this.openSectionEllipsis( sectionTitle );
|
|
|
|
const list = await this.page.$$(
|
|
|
|
'.woocommerce-ellipsis-menu div[role=menuitemcheckbox]'
|
|
|
|
);
|
|
|
|
return Promise.all(
|
|
|
|
list.map( async ( item ) => ( {
|
|
|
|
title: await item.evaluate(
|
|
|
|
( element ) => element?.textContent
|
|
|
|
),
|
|
|
|
element: item,
|
|
|
|
} ) )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|