Skip to content

Commit b2876bd

Browse files
e2e test for setting and non-admin user
1 parent af67882 commit b2876bd

File tree

7 files changed

+93
-11
lines changed

7 files changed

+93
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
node_modules/
55
build/
66
artifacts/
7+
test-results/
78

89
# Packages #
910
############

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
{
22
"name": "comment-mention",
3-
"version": "1.7.14",
3+
"version": "1.7.15",
44
"description": "Comment Mention plugin",
55
"main": "Gruntfile.js",
66
"scripts": {
77
"start": "npx wp-scripts start",
88
"build": "npx wp-scripts build",
99
"readme": "grunt readme",
1010
"i18n": "grunt i18n",
11-
"wp-env": "wp-env",
12-
"start:env": "wp-env start",
13-
"stop:env": "wp-env stop",
1411
"env": "wp-env",
1512
"env:start": "wp-env start",
1613
"env:clean": "wp-env clean",

tests/e2e/config/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ const admin = {
33
password: 'password',
44
};
55

6+
const testuser = {
7+
username: 'testuser',
8+
password: 'testuser',
9+
};
10+
611
module.exports = {
712
admin,
13+
testuser,
814
}

tests/e2e/global-setup.js

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const { chromium, expect } = require('@playwright/test');
22

33

44
const fs = require('fs');
5-
const { admin } = require('./config');
5+
const { admin, testuser } = require('./config');
66

77
module.exports = async (config) => {
88
const { stateDir, baseURL, userAgent } = config.projects[0].use;
@@ -12,20 +12,23 @@ module.exports = async (config) => {
1212

1313
// used throughout tests for authentication
1414
process.env.ADMINSTATE = `${stateDir}adminState.json`;
15+
process.env.TESTUSERSTATE = `${stateDir}testuserState.json`;
1516

1617
// Clear out the previous save states
1718
try {
1819
fs.unlinkSync(process.env.ADMINSTATE);
19-
console.log('Admin state file deleted successfully.');
20+
fs.unlinkSync(process.env.TESTUSERSTATE);
21+
console.log('Admin and Test User state files deleted successfully.');
2022
} catch (err) {
2123
if (err.code === 'ENOENT') {
22-
console.log('Admin state file does not exist.');
24+
console.log('Admin or Test User state file does not exist.');
2325
} else {
24-
console.log('Admin state file could not be deleted: ' + err);
26+
console.log('Admin or Test User state file could not be deleted: ' + err);
2527
}
2628
}
2729

2830
let adminLoggedIn = false;
31+
let testuserLoggedIn = false;
2932

3033
const contextOptions = { baseURL, userAgent };
3134

@@ -70,6 +73,46 @@ module.exports = async (config) => {
7073
process.exit(1);
7174
}
7275

76+
const testuserContext = await browser.newContext(contextOptions);
77+
const testuserPage = await testuserContext.newPage();
78+
79+
const testuserRetries = 5;
80+
for (let i = 0; i < testuserRetries; i++) {
81+
try {
82+
console.log('Login as testuser...');
83+
await testuserPage.goto(`/wp-admin`);
84+
await testuserPage.fill('input[name="log"]', testuser.username);
85+
await testuserPage.fill('input[name="pwd"]', testuser.password);
86+
await testuserPage.locator("#wp-submit").click();
87+
await testuserPage.waitForLoadState('networkidle');
88+
await testuserPage.goto(`/wp-admin`);
89+
await testuserPage.waitForLoadState('domcontentloaded');
90+
91+
await expect(testuserPage.locator('div.wrap > h1')).toHaveText(
92+
'Dashboard'
93+
);
94+
await testuserPage
95+
.context()
96+
.storageState({ path: process.env.TESTUSERSTATE });
97+
console.log('Logged-in as testuser successfully.');
98+
testuserLoggedIn = true;
99+
break;
100+
} catch (e) {
101+
console.log(
102+
`Testuser login failed, Re-trying... ${i}/${testuserRetries}`
103+
);
104+
console.log(e);
105+
}
106+
}
107+
108+
if (!testuserLoggedIn) {
109+
console.error(
110+
'Testuser login failed... Check your credentials.'
111+
);
112+
process.exit(1);
113+
}
114+
73115
await adminContext.close();
116+
await testuserContext.close();
74117
await browser.close();
75118
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const { test, expect } = require( '@playwright/test' );
2+
3+
test.use( { storageState: process.env.TESTUSERSTATE } );
4+
5+
test( 'Mention users in Post Comment by Subscriber', async ( { page } ) => {
6+
await page.goto('/hello-world');
7+
await page.getByRole('textbox', { name: 'Comment *' }).click();
8+
await page.getByRole('textbox', { name: 'Comment *' }).pressSequentially('@ad',{ delay: 200 });
9+
10+
const mentionItem = page.locator('.tribute-container li.highlight');
11+
12+
// Wait for the mention suggestion to appear
13+
await expect(mentionItem).toBeVisible({ timeout: 3000 });
14+
await expect(mentionItem).toContainText('admin');
15+
16+
await mentionItem.click();
17+
18+
// await page.getByText('testuser (testuser)').click();
19+
await page.getByRole('textbox', { name: 'Comment *' }).pressSequentially(' this is test comment',{ delay: 100 });
20+
await page.getByRole('button', { name: 'Post Comment' }).click();
21+
await expect(page.getByText('@admin this is test comment').last()).toBeVisible({ timeout: 10000 });
22+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const { test, expect } = require( '@playwright/test' );
2+
3+
test.use( { storageState: process.env.ADMINSTATE } );
4+
5+
test( 'Check Plugin Setting Page', async ( { page } ) => {
6+
await page.goto('/wp-admin/');
7+
await page.getByRole('link', { name: 'Comment Mention' }).click();
8+
await expect(page.getByRole('heading', { name: 'Comment Mention' })).toBeVisible();
9+
await page.getByRole('cell', { name: 'Choose which user roles' }).getByLabel('Subscriber').check();
10+
await page.locator('input[name="cmt_mntn_email_enable"]').check();
11+
await page.locator('#submit').click();
12+
await expect(page.getByText('Settings Saved')).toBeVisible();
13+
});

tests/e2e/specs/example.spec.js renamed to tests/e2e/specs/comment-mention.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ test.use( { storageState: process.env.ADMINSTATE } );
55
test( 'Mention users in Post Comment', async ( { page } ) => {
66
await page.goto('/hello-world');
77
await page.getByRole('textbox', { name: 'Comment *' }).click();
8-
await page.getByRole('textbox', { name: 'Comment *' }).pressSequentially('@test');
8+
await page.getByRole('textbox', { name: 'Comment *' }).pressSequentially('@test',{ delay: 200 });
99

1010
const mentionItem = page.locator('.tribute-container li.highlight');
1111

1212
// Wait for the mention suggestion to appear
13-
await expect(mentionItem).toBeVisible({ timeout: 10000 });
13+
await expect(mentionItem).toBeVisible({ timeout: 3000 });
1414
await expect(mentionItem).toContainText('testuser');
1515

1616
await mentionItem.click();
1717

1818
// await page.getByText('testuser (testuser)').click();
19-
await page.getByRole('textbox', { name: 'Comment *' }).pressSequentially(' this is test comment');
19+
await page.getByRole('textbox', { name: 'Comment *' }).pressSequentially(' this is test comment',{ delay: 100 });
2020
await page.getByRole('button', { name: 'Post Comment' }).click();
2121
await expect(page.getByText('@testuser this is test comment').last()).toBeVisible({ timeout: 10000 });
2222
});

0 commit comments

Comments
 (0)