-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestHelper.ts
More file actions
214 lines (197 loc) · 7.17 KB
/
testHelper.ts
File metadata and controls
214 lines (197 loc) · 7.17 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
* tests/testHelper.ts - utility methods used in the tests
*
* GPLv3 License, Copyright (c) 2023 - 2024 Heiko Lübbe
* WordPress-plugin random-quote-zitat-service, see https://github.com/muhme/quote_wordpress
*
*/
import { Page, Locator } from 'playwright';
import { expect, Editor, Admin } from '@wordpress/e2e-test-utils-playwright';
/**
* Showing the post in frontend and verifying:
* 1. expect to have div.quote
* 2. expect to have div.quotation
* 3. expect quotation link goes to given language code
*
* optional parameters are only checked if they are given (not null)
*
* @param page - to be passed from WordPress utils for Playwright
* @param postId - unique post number, as returned from createPostWithPlugin()
* @param languageCode - two-letter language code, e.g. 'de' or defaults to null for all languages
* @param quotation - the quote or null, defaults to null
* @param quotationLink - the zitat-service.de-link or null, defaults to null
* @param source - quotation source or null, defaults to null
* @param sourceLink - quotation source link or null, defaults to null
* @param author - auhors name or null, defaults to null
* @param authorLink - authors link or null, defaults to null
*/
async function checkQuote(
page: Page,
postId: number | null,
languageCode: string | null = null,
quotation: string | null = null,
quotationLink: string | null = null,
source: string | null = null,
sourceLink: string | null = null,
author: string | null = null,
authorLink: string | null = null
) {
// Navigate to the page with the specified post ID
await page.goto( `/?p=${ postId }` );
// Check for visibility of the 'div.quote' element
const quoteDiv = page.locator( 'div.quote' );
await expect( quoteDiv ).toBeVisible();
// Check for visibility of the 'div.quotation' inside 'div.quote'
const quotationDiv = page.locator( 'div.quote div.quotation' );
await expect( quotationDiv ).toBeVisible();
// Check for visibility of the zitat-service.de-link with language-specific URL
let linkLocator: Locator;
if (
languageCode === null ||
languageCode === 'all' ||
languageCode === 'frontend'
) {
// if no language is given, then check beginning and it contains '/quotations/'
linkLocator = page.locator(
`div.quotation a[href^="https://www.zitat-service.de/"][href*="/quotations/"]`
);
} else {
linkLocator = page.locator(
`div.quotation a[href^="https://www.zitat-service.de/${ languageCode }/quotations/"]`
);
}
await expect( linkLocator ).toBeVisible();
/*
* tests the optional arguments with exactly comparing the results
*/
if ( quotation ) {
const found = await page.textContent( 'div.quote div.quotation a' );
expect( found ).toBe( quotation );
}
if ( quotationLink ) {
const found = await page.getAttribute(
'div.quote div.quotation a',
'href'
);
expect( found ).toBe( quotationLink );
}
if ( source ) {
const found = await page.textContent( 'div.quote div.source' );
expect( found ).toBe( source );
}
if ( author ) {
const found = await page.textContent( 'div.quote div.source' );
expect( found ).toContain( author );
}
if ( sourceLink || authorLink ) {
// .source can have two links, if source is existing it is always the first;
// authorLink is the second, if sourceLink exists, else the first
const sourceLinks = await page.$$eval(
'div.quote div.source a',
( links ) =>
links.map( ( link ) => ( link as HTMLAnchorElement ).href )
);
if ( sourceLink ) {
const found = sourceLinks[ 0 ];
// some sourceLink's are URL encoded
expect( decodeURIComponent( found ) ).toBe( sourceLink );
}
if ( authorLink ) {
const found = sourceLinks[ sourceLink ? 1 : 0 ];
// some authorLink's are URL encoded
expect( decodeURIComponent( found ) ).toBe( authorLink );
}
}
}
export { checkQuote };
/**
* Showing the post in frontend and verifying error message
*
* @param page - to be passed from WordPress utils for Playwright
* @param postId - unique post number, as returned from createPostWithPlugin()
*/
async function checkNoQuoteFound( page: Page, postId: number | null ) {
// Navigate to the page with the specified post ID
await page.goto( `/?p=${ postId }` );
// Check for visibility of the plugin
const quoteDiv = page.locator(
'.wp-block-random-quote-zitat-service-random-quote'
);
await expect( quoteDiv ).toHaveCount( 1 );
await expect( quoteDiv ).toContainText( 'Error 404' );
await expect( quoteDiv ).toContainText(
'No quote found for given parameter'
);
}
export { checkNoQuoteFound };
/**
* Creates a post with [zitat_service] block widget plugin and given parameters e.g. language="de" in backend.
*
* Does it simplified as with code editor (not selecting values in the GUI).
*
* @param editor - active editor on the page, to be passed from WordPress utils for Playwright
* @param admin - to be passed from WordPress utils for Playwright
* @param title - used as post title and second time as paragraph in the blog post
* @param attributes - keys/values object to create the plugin attributes
* @return postId - unique post number or null
*/
async function createPostWithPlugin<
T extends Record< string, string | number >,
>(
editor: Editor,
admin: Admin,
title: string,
attributes: T | null
): Promise< number | null > {
let attributesStringified = '';
if (
attributes &&
attributes !== undefined &&
attributes !== null &&
Object.keys( attributes ).length > 0
) {
attributesStringified = JSON.stringify( attributes );
}
await admin.createNewPost( { title } );
// post is created via REST, set the content in additional step to prevent encoding HTML special chars
await editor.setContent( `
<p>${ title }</p>
<!-- wp:random-quote-zitat-service/random-quote ${ attributesStringified } -->
<div class="zitat-service-quote"> ...</div>
<!-- /wp:random-quote-zitat-service/random-quote -->
` );
return editor.publishPost();
}
export { createPostWithPlugin };
/**
* Do admin login and store cookies if desired.
*
* Implemented it myself and didn't use @wordpress/e2e-test-utils-playwright (with the ?REST login?) because I didn't get it work.
*
* @param page - to be passed from WordPress utils for Playwright
* @param user - WordPress user name, e.g. 'admin'
* @param password - users password
* @param storagePath - Cookie storage file to reuse in Brwoser context, or null if not needed
*/
async function userLogin(
page: Page,
user: string,
password: string,
storagePath: string | null
) {
await page.goto( '/wp-admin' );
await page.getByLabel( 'Username or Email Address' ).fill( user );
// sometimes it fails, so wait until the element is finished
// await page.locator( 'input#user_pass' ).fill( password );
const passwordInput = page.locator( 'input#user_pass' );
await passwordInput.waitFor( { state: 'visible' } );
await passwordInput.fill( password );
// Changed with WordPress 6.7 from 'Log in' to 'Log In'
await page.getByRole('button', { name: /log in/i }).click();
// '#wpadminbar' is visible on Desktop and mobile
await expect( page.locator( 'div#wpadminbar' ) ).toBeVisible();
if ( storagePath !== null ) {
await page.context().storageState( { path: storagePath } );
}
}
export { userLogin };