@@ -2,81 +2,70 @@ import { test, expect } from '@playwright/test';
22
33const TEST_SNIPPET_NAME = 'E2E Test Snippet' ;
44
5- test . describe ( 'Code Snippets Admin Page @admin' , ( ) => {
6- test . beforeEach ( async ( { page } ) => {
7- await page . goto ( '/wp-admin/admin.php?page=snippets' ) ;
8- await page . waitForLoadState ( 'networkidle' ) ;
9-
10- // Wait for WordPress admin content to be fully loaded
11- await page . waitForSelector ( '#wpbody-content, .wrap, #wpcontent' , { timeout : 10000 } ) ;
12-
13- // Debug: Check if we're actually logged in and on the right page
14- const currentUrl = page . url ( ) ;
15- console . log ( 'Current URL:' , currentUrl ) ;
16-
17- // If we're redirected to login, something is wrong with auth
18- if ( currentUrl . includes ( 'wp-login.php' ) ) {
19- throw new Error ( 'Authentication failed - redirected to login page' ) ;
20- }
21- } ) ;
5+ test . describe ( 'Code Snippets Plugin' , ( ) => {
6+ test ( 'Complete snippet workflow' , async ( { page } ) => {
7+ await test . step ( 'Login to WordPress' , async ( ) => {
8+ await page . goto ( '/wp-login.php' ) ;
9+ await page . waitForSelector ( '#user_login' ) ;
10+ await page . fill ( '#user_login' , 'admin' ) ;
11+ await page . fill ( '#user_pass' , 'password' ) ;
12+ await page . click ( '#wp-submit' ) ;
13+ await page . waitForURL ( / w p - a d m i n / ) ;
14+ } ) ;
2215
23- test ( 'Can access admin page', async ( { page } ) => {
24- // Simple test to verify we can access the admin
25- const title = await page . title ( ) ;
26- console . log ( 'Page title: ', title ) ;
16+ await test . step ( 'Navigate to snippets page', async ( ) => {
17+ await page . goto ( '/wp-admin/ admin.php?page=snippets' ) ;
18+ await page . waitForLoadState ( 'networkidle' ) ;
19+ await page . waitForSelector ( '#wpbody-content, .wrap, #wpcontent ', { timeout : 10000 } ) ;
2720
28- // Check if we're actually on the snippets page
29- const url = page . url ( ) ;
30- console . log ( 'Current URL:' , url ) ;
21+ const currentUrl = page . url ( ) ;
22+ expect ( currentUrl ) . toContain ( 'page=snippets' ) ;
23+ } ) ;
3124
32- // Verify the page has some expected content
33- const bodyText = await page . textContent ( 'body' ) ;
34- const hasSnippetsContent = bodyText ?. includes ( 'Snippets' ) || bodyText ?. includes ( 'snippet' ) ;
35- console . log ( 'Has snippets content:' , hasSnippetsContent ) ;
25+ await test . step ( 'Add a new snippet' , async ( ) => {
26+ await page . waitForSelector ( 'h1, .page-title' , { timeout : 10000 } ) ;
3627
37- expect ( url ) . toContain ( ' page=snippets ') ;
38- } ) ;
28+ await page . click ( '. page-title-action, .wrap .page-title-action ') ;
29+ await page . waitForLoadState ( 'networkidle' ) ;
3930
40- test ( 'Can add a new snippet' , async ( { page } ) => {
41- // Wait for the page to load
42- await page . waitForSelector ( 'h1, .page-title' , { timeout : 10000 } ) ;
43-
44- // Click the correct "Add New" button (should be in the page header, not admin menu)
45- await page . click ( '.page-title-action, .wrap .page-title-action' ) ;
46- await page . waitForLoadState ( 'networkidle' ) ;
47-
48- // Wait for the form to be ready
49- await page . waitForSelector ( '#title' ) ;
50- await page . fill ( '#title' , TEST_SNIPPET_NAME ) ;
51-
52- // Wait for CodeMirror to be ready
53- await page . waitForSelector ( '.CodeMirror textarea' ) ;
54- await page . fill ( '.CodeMirror textarea' , 'echo "Hello World!";' ) ;
55-
56- await page . click ( 'text=Save Snippet' ) ;
57- await expect ( page . locator ( '#message.notice' ) ) . toContainText ( 'Snippet created' ) ;
58- } ) ;
31+ await page . waitForSelector ( '#title' ) ;
32+ await page . fill ( '#title' , TEST_SNIPPET_NAME ) ;
5933
60- test ( 'Can activate and deactivate a snippet' , async ( { page } ) => {
61- // Wait for the snippet to exist before clicking
62- await page . waitForSelector ( `text=${ TEST_SNIPPET_NAME } ` ) ;
63- await page . click ( `text=${ TEST_SNIPPET_NAME } ` ) ;
64- await page . waitForLoadState ( 'networkidle' ) ;
34+ await page . waitForSelector ( '.CodeMirror textarea' ) ;
35+ await page . fill ( '.CodeMirror textarea' , 'echo "Hello World!";' ) ;
6536
66- await page . click ( 'text=Save and Activate' ) ;
67- await expect ( page . locator ( '#message.notice p' ) ) . toContainText ( 'Snippet updated and activated.' ) ;
68- await page . click ( 'text=Save and Deactivate' ) ;
69- await expect ( page . locator ( '#message.notice p' ) ) . toContainText ( 'Snippet updated and deactivated' ) ;
70- } ) ;
37+ await page . click ( 'text=Save Snippet' ) ;
38+ await expect ( page . locator ( '#message.notice' ) ) . toContainText ( 'Snippet created' ) ;
39+ } ) ;
40+
41+ await test . step ( 'Activate the snippet' , async ( ) => {
42+ await page . goto ( '/wp-admin/admin.php?page=snippets' ) ;
43+ await page . waitForLoadState ( 'networkidle' ) ;
44+
45+ await page . waitForSelector ( `text=${ TEST_SNIPPET_NAME } ` ) ;
46+ await page . click ( `text=${ TEST_SNIPPET_NAME } ` ) ;
47+ await page . waitForLoadState ( 'networkidle' ) ;
48+
49+ await page . click ( 'text=Save and Activate' ) ;
50+ await expect ( page . locator ( '#message.notice p' ) ) . toContainText ( 'Snippet updated and activated' ) ;
51+ } ) ;
52+
53+ await test . step ( 'Deactivate the snippet' , async ( ) => {
54+ await page . click ( 'text=Save and Deactivate' ) ;
55+ await expect ( page . locator ( '#message.notice p' ) ) . toContainText ( 'Snippet updated and deactivated' ) ;
56+ } ) ;
57+
58+ await test . step ( 'Delete the snippet' , async ( ) => {
59+ await page . goto ( '/wp-admin/admin.php?page=snippets' ) ;
60+ await page . waitForLoadState ( 'networkidle' ) ;
7161
72- test ( 'Can delete a snippet' , async ( { page } ) => {
73- // Wait for the snippet to exist before clicking
74- await page . waitForSelector ( `text=${ TEST_SNIPPET_NAME } ` ) ;
75- await page . click ( `text=${ TEST_SNIPPET_NAME } ` ) ;
76- await page . waitForLoadState ( 'networkidle' ) ;
62+ await page . waitForSelector ( `text=${ TEST_SNIPPET_NAME } ` ) ;
63+ await page . click ( `text=${ TEST_SNIPPET_NAME } ` ) ;
64+ await page . waitForLoadState ( 'networkidle' ) ;
7765
78- await page . click ( 'text=Delete' ) ;
79- await page . click ( 'button.components-button.is-destructive.is-primary' ) ; // Confirm dialog
80- await expect ( page . locator ( 'body' ) ) . not . toContainText ( TEST_SNIPPET_NAME ) ;
66+ await page . click ( 'text=Delete' ) ;
67+ await page . click ( 'button.components-button.is-destructive.is-primary' ) ;
68+ await expect ( page . locator ( 'body' ) ) . not . toContainText ( TEST_SNIPPET_NAME ) ;
69+ } ) ;
8170 } ) ;
8271} ) ;
0 commit comments