File size: 3,016 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
 * External dependencies
 */
import { Page } from 'playwright';
/**
 * Internal dependencies
 */
import { getCalypsoURL } from '../../data-helper';

/**
 * Dashboard page class for the new v2 dashboard.
 *
 * This Page Object represents the new dashboard implementation
 * accessible under the /v2 path.
 */
export class DashboardPage {
	/**
	 * Reference to the Playwright page object.
	 */
	private page: Page;

	/**
	 * Constructs a new DashboardPage instance.
	 *
	 * @param page - The Playwright page object.
	 */
	constructor( page: Page ) {
		this.page = page;
	}

	/**
	 * Visits the dashboard entry page.
	 *
	 * @returns Promise that resolves when navigation is complete.
	 */
	async visit(): Promise< void > {
		await this.page.goto( getCalypsoURL( 'v2' ) );
		// Wait for the main content to be visible
		await this.page.getByRole( 'main' ).waitFor();
	}

	/**
	 * Checks if the dashboard has loaded correctly.
	 *
	 * @returns Promise that resolves to true if the dashboard is loaded.
	 */
	async isLoaded(): Promise< boolean > {
		const isMainContentVisible = await this.page.getByRole( 'main' ).isVisible();
		const hasCorrectUrl = this.page.url().includes( '/v2' );
		return isMainContentVisible && hasCorrectUrl;
	}

	/**
	 * Gets the visible heading text on the dashboard.
	 *
	 * @returns Promise that resolves to the heading text.
	 */
	async getHeadingText(): Promise< string | null > {
		const heading = this.page.getByRole( 'heading' ).first();
		return heading ? await heading.textContent() : null;
	}

	/**
	 * Navigates to a specific section of the dashboard by clicking on
	 * a navigation item with the given name.
	 *
	 * @param name - The name of the navigation item to click.
	 * @returns Promise that resolves when navigation is complete.
	 */
	async navigateToSection( name: string ): Promise< void > {
		await this.page.getByRole( 'link', { name } ).click();
		// Wait for navigation to complete
		await this.page.waitForLoadState( 'networkidle' );
	}

	/**
	 * Visits a specific subpath within the dashboard.
	 *
	 * @param subpath - The subpath to visit under /v2.
	 * @returns Promise that resolves when navigation is complete.
	 */
	async visitPath( subpath: string ): Promise< void > {
		const path = subpath.startsWith( '/' ) ? subpath : `/${ subpath }`;
		await this.page.goto( getCalypsoURL( `v2${ path }` ) );
	}

	/**
	 * Checks if the current page is a 404 error page.
	 *
	 * @returns Promise that resolves to true if the page is a 404 error page.
	 */
	async is404Page(): Promise< boolean > {
		await this.page.getByRole( 'heading', { name: '/not found/i' } );
		return true;

		// // Look for typical 404 page elements
		// const notFoundHeading = this.page.getByRole( 'heading', { name: /not found|404/i } );
		// const notFoundText = this.page.getByText(
		// 	/page (does not exist|not found|couldn't be found)/i
		// );

		// // Check if either indicator is visible
		// return ( await notFoundHeading.isVisible() ) || ( await notFoundText.isVisible() );
	}
}