@webship-js/adminlte-steps 0.0.0-stage → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +348 -2
- package/demo-site/.ddev/config.yaml +10 -0
- package/demo-site/3.2/cards.html +89 -0
- package/demo-site/3.2/chat.html +84 -0
- package/demo-site/3.2/components.html +79 -0
- package/demo-site/3.2/css/demo.css +7 -0
- package/demo-site/3.2/index.html +93 -0
- package/demo-site/3.2/no-sidebar.html +2 -0
- package/demo-site/3.2/settings.html +74 -0
- package/demo-site/3.2/vendor/admin-lte-3.2.0/LICENSE +20 -0
- package/demo-site/3.2/vendor/admin-lte-3.2.0/css/adminlte.min.css +12 -0
- package/demo-site/3.2/vendor/admin-lte-3.2.0/js/adminlte.min.js +7 -0
- package/demo-site/3.2/vendor/bootstrap-4.6.2/LICENSE +22 -0
- package/demo-site/3.2/vendor/bootstrap-4.6.2/js/bootstrap.bundle.min.js +7 -0
- package/demo-site/3.2/vendor/jquery-3.6.4/LICENSE.txt +20 -0
- package/demo-site/3.2/vendor/jquery-3.6.4/jquery.min.js +2 -0
- package/demo-site/cards.html +98 -0
- package/demo-site/chat.html +93 -0
- package/demo-site/components.html +88 -0
- package/demo-site/css/adminlte-no-banner.min.css +1 -0
- package/demo-site/css/demo.css +5 -0
- package/demo-site/index.html +102 -0
- package/demo-site/no-banner.html +101 -0
- package/demo-site/no-sidebar.html +2 -0
- package/demo-site/settings.html +83 -0
- package/demo-site/vendor/admin-lte-4.10.4/LICENSE +20 -0
- package/demo-site/vendor/admin-lte-4.10.4/css/adminlte.min.css +7 -0
- package/demo-site/vendor/admin-lte-4.10.4/js/adminlte.min.js +7 -0
- package/demo-site/vendor/bootstrap-5.3.9/LICENSE +21 -0
- package/demo-site/vendor/bootstrap-5.3.9/js/bootstrap.bundle.min.js +7 -0
- package/package.json +70 -4
- package/tests/features/README.md +20 -0
- package/tests/features/bootstrap-inside.feature +22 -0
- package/tests/features/cards.feature +34 -0
- package/tests/features/chat.feature +13 -0
- package/tests/features/menu.feature +24 -0
- package/tests/features/page.feature +37 -0
- package/tests/features/sidebar.feature +24 -0
- package/tests/step-definitions/README.md +1 -0
- package/tests/step-definitions/adminlte-cards.steps.js +211 -0
- package/tests/step-definitions/adminlte-helpers.js +385 -0
- package/tests/step-definitions/adminlte-layout.steps.js +231 -0
- package/tests/step-definitions/adminlte-menu.steps.js +98 -0
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// The cards of a dashboard and their tools (AdminLTE's card widget: collapse, maximize, remove), and a chat's
|
|
4
|
+
// contacts pane (its direct chat). AdminLTE's script slides a card's body shut and open, fills the window with a
|
|
5
|
+
// maximized card, and fades a removed card away: AdminLTE 4 takes it off the page, 3.2 hides it. The steps wait for
|
|
6
|
+
// what a person sees on both, at rest.
|
|
7
|
+
|
|
8
|
+
const { When, Then } = require('@cucumber/cucumber');
|
|
9
|
+
const h = require('./adminlte-helpers');
|
|
10
|
+
|
|
11
|
+
const CARD_HINT = 'a card is a Bootstrap .card, named by the text of its .card-title.';
|
|
12
|
+
const TOOL_OF = { collapse: 'collapse', expand: 'collapse', maximize: 'maximize', restore: 'maximize', remove: 'remove' };
|
|
13
|
+
|
|
14
|
+
async function card(world, name) {
|
|
15
|
+
return h.resolve(world, 'cards', name, 'card', CARD_HINT);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function describe(s) {
|
|
19
|
+
if (!s || s.gone) return 'it is gone from the page';
|
|
20
|
+
if (!s.shown) return `it is not shown (classes "${s.classes}")`;
|
|
21
|
+
return `it is ${s.collapsed ? 'collapsed' : 'expanded'}${s.maximized ? ' and maximized' : ''} (classes "${s.classes}")`;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const STATES = {
|
|
25
|
+
collapse: (s) => s.shown && s.collapsed && !s.bodyShown,
|
|
26
|
+
expand: (s) => s.shown && !s.collapsed && s.bodyShown,
|
|
27
|
+
maximize: (s) => s.shown && s.maximized,
|
|
28
|
+
restore: (s) => s.shown && !s.maximized,
|
|
29
|
+
remove: (s) => !s.shown,
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Collapse, expand, maximize, restore or remove a card, named by its title, with its own tool in its header (the
|
|
34
|
+
* collapse, maximize or remove button AdminLTE's card widget answers to), and wait until the card has got there and
|
|
35
|
+
* stopped moving. What a person sees is the same on both versions: collapsed, only the card's header is shown;
|
|
36
|
+
* expanded, its body too; maximized, the card fills the window; restored, it is back in its place; removed, it is
|
|
37
|
+
* gone from view (AdminLTE 4 takes it off the page, 3.2 hides it). A card already in that state is left as it is; a
|
|
38
|
+
* card without that tool fails, saying so.
|
|
39
|
+
*
|
|
40
|
+
* Example #1: When I collapse the "Sales" card
|
|
41
|
+
* Example #2: When I expand the "Visitors" card
|
|
42
|
+
* Example #3: When I maximize the "Sales" card
|
|
43
|
+
* Example #4: When I restore the "Sales" card
|
|
44
|
+
* Example #5: When we remove the "Rainfall" card
|
|
45
|
+
*/
|
|
46
|
+
When(/^(?:I |we )?(collapse|expand|maximize|restore|remove) the "([^"]*)" card$/, async function (action, name) {
|
|
47
|
+
const ref = await card(this, name);
|
|
48
|
+
const first = await h.poll(this, (lib, a) => { const s = lib.cardState(a.ref); return { done: s.gone || !s.moving, state: s }; }, { ref }, h.budget(this));
|
|
49
|
+
const s = first.state || {};
|
|
50
|
+
if (s.gone || !s.shown) {
|
|
51
|
+
if (action === 'remove') return;
|
|
52
|
+
throw h.failure(this.page, `Could not ${action} the "${name}" card: ${describe(s)}.`, { hint: 'a removed card cannot come back without a reload of the page.' });
|
|
53
|
+
}
|
|
54
|
+
const done = (await h.call(this, 'cardState', ref));
|
|
55
|
+
if (STATES[action](done)) return;
|
|
56
|
+
const tool = await h.call(this, 'cardTool', ref, TOOL_OF[action]);
|
|
57
|
+
if (!tool) {
|
|
58
|
+
throw h.failure(this.page, `Could not ${action} the "${name}" card: it has no ${TOOL_OF[action]} tool.`, {
|
|
59
|
+
found: `its tools: ${h.listed(s.tools)}`,
|
|
60
|
+
hint: 'a card\'s tools are buttons in its .card-tools with data-lte-toggle="card-collapse|card-maximize|card-remove" (4) or data-card-widget="collapse|maximize|remove" (3.2).',
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
await h.clickRef(this, tool, `the ${TOOL_OF[action]} tool of the "${name}" card`);
|
|
64
|
+
const result = await h.poll(this, (lib, a) => {
|
|
65
|
+
const t = lib.cardState(a.ref);
|
|
66
|
+
const ok = { collapse: t.shown && t.collapsed && !t.bodyShown, expand: t.shown && !t.collapsed && t.bodyShown, maximize: t.shown && t.maximized, restore: t.shown && !t.maximized, remove: !t.shown }[a.action];
|
|
67
|
+
return { done: ok && (t.gone || !t.moving), state: t };
|
|
68
|
+
}, { ref, action }, h.budget(this));
|
|
69
|
+
if (result.done) return;
|
|
70
|
+
throw h.failure(this.page, `Expected the "${name}" card to be ${{ collapse: 'collapsed', expand: 'expanded', maximize: 'maximized', restore: 'restored', remove: 'gone from view' }[action]} after its ${TOOL_OF[action]} tool was clicked, but ${describe(result.state)}.`, {
|
|
71
|
+
hint: 'check that the page loads AdminLTE\'s script; a page may cancel a removal (remove.lte.card-widget) to ask first.',
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* A card, named by its title, is collapsed (only its header shown), expanded (its body shown) or maximized (filling
|
|
77
|
+
* the window), at rest. The same on both versions.
|
|
78
|
+
*
|
|
79
|
+
* Example #1: Then the "Sales" card should be collapsed
|
|
80
|
+
* Example #2: Then the "Sales" card should be expanded
|
|
81
|
+
* Example #3: Then the "Sales" card should be maximized
|
|
82
|
+
* Example #4: And the "Visitors" card should be collapsed
|
|
83
|
+
* Example #5: But the "Wind" card should be expanded
|
|
84
|
+
*/
|
|
85
|
+
Then(/^the "([^"]*)" card should be (collapsed|expanded|maximized)$/, async function (name, wanted) {
|
|
86
|
+
const ref = await card(this, name);
|
|
87
|
+
const action = { collapsed: 'collapse', expanded: 'expand', maximized: 'maximize' }[wanted];
|
|
88
|
+
const result = await h.poll(this, (lib, a) => {
|
|
89
|
+
const t = lib.cardState(a.ref);
|
|
90
|
+
const ok = { collapse: t.shown && t.collapsed && !t.bodyShown, expand: t.shown && !t.collapsed && t.bodyShown, maximize: t.shown && t.maximized }[a.action];
|
|
91
|
+
return { done: ok && !t.moving, state: t };
|
|
92
|
+
}, { ref, action }, h.budget(this));
|
|
93
|
+
if (result.done) return;
|
|
94
|
+
throw h.failure(this.page, `Expected the "${name}" card to be ${wanted}, but ${describe(result.state)}.`, {
|
|
95
|
+
hint: `${action} it first: When I ${action} the "${name}" card`,
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* A card, named by its title, is shown, or with "not" is gone from view: taken off the page (AdminLTE 4's removal)
|
|
101
|
+
* or hidden (3.2's), at rest. A card that is not on the page at all is "not shown" too.
|
|
102
|
+
*
|
|
103
|
+
* Example #1: Then the "Rainfall" card should be shown
|
|
104
|
+
* Example #2: Then the "Rainfall" card should not be shown
|
|
105
|
+
* Example #3: Then the "Sales" card should be shown
|
|
106
|
+
* Example #4: And the "Wind" card should not be shown
|
|
107
|
+
* Example #5: But the "Notes" card should be shown
|
|
108
|
+
*/
|
|
109
|
+
Then(/^the "([^"]*)" card should( not)? be shown$/, async function (name, not) {
|
|
110
|
+
if (!not) {
|
|
111
|
+
const result = await h.poll(this, (lib, a) => {
|
|
112
|
+
const items = lib.cards();
|
|
113
|
+
const hit = lib.pick(items, a.name);
|
|
114
|
+
if (!hit) return { done: false, missing: true, names: items.map((i) => i.names[0]) };
|
|
115
|
+
const t = lib.cardState(hit.ref);
|
|
116
|
+
return { done: t.shown && !t.moving, state: t };
|
|
117
|
+
}, { name }, h.budget(this));
|
|
118
|
+
if (result.done) return;
|
|
119
|
+
if (result.missing) {
|
|
120
|
+
throw h.failure(this.page, `Expected the "${name}" card to be shown, but it is not on the page.`, {
|
|
121
|
+
found: `the cards on the page: ${h.listed(result.names)}`,
|
|
122
|
+
hint: `a removed card is taken off the page (AdminLTE 4) or hidden (3.2); ${CARD_HINT}`,
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
throw h.failure(this.page, `Expected the "${name}" card to be shown, but ${describe(result.state)}.`, { hint: CARD_HINT });
|
|
126
|
+
}
|
|
127
|
+
const result = await h.poll(this, (lib, a) => {
|
|
128
|
+
const hit = lib.pick(lib.cards(), a.name);
|
|
129
|
+
if (!hit) return { done: true };
|
|
130
|
+
const t = lib.cardState(hit.ref);
|
|
131
|
+
return { done: !t.shown, state: t };
|
|
132
|
+
}, { name }, h.budget(this));
|
|
133
|
+
if (result.done) return;
|
|
134
|
+
throw h.failure(this.page, `Expected the "${name}" card not to be shown, but ${describe(result.state)}.`, {
|
|
135
|
+
hint: `remove it first: When I remove the "${name}" card`,
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* A card, named by its title, shows a text (a part of its visible text, compared without regard to letter case,
|
|
141
|
+
* white space and the kind of quote marks), or with "not" does not: a collapsed card shows only its header.
|
|
142
|
+
*
|
|
143
|
+
* Example #1: Then the "Sales" card should show "1,284 orders"
|
|
144
|
+
* Example #2: Then the "Sales" card should not show "1,284 orders"
|
|
145
|
+
* Example #3: Then the "Rainfall" card should show "31 mm"
|
|
146
|
+
* Example #4: And the "Visitors" card should not show "9,310"
|
|
147
|
+
* Example #5: Then the "Team chat" card should show "The tide gauge is back online."
|
|
148
|
+
*/
|
|
149
|
+
Then(/^the "([^"]*)" card should( not)? show "([^"]*)"$/, async function (name, not, wanted) {
|
|
150
|
+
const ref = await card(this, name);
|
|
151
|
+
const result = await h.poll(this, (lib, a) => {
|
|
152
|
+
const t = lib.cardState(a.ref);
|
|
153
|
+
const has = t.shown && lib.lower(t.text || '').includes(lib.lower(a.wanted));
|
|
154
|
+
return { done: (a.not ? !has : has) && !t.moving, state: t };
|
|
155
|
+
}, { ref, wanted, not: !!not }, h.budget(this));
|
|
156
|
+
if (result.done) return;
|
|
157
|
+
const s = result.state || {};
|
|
158
|
+
throw h.failure(this.page, `Expected the "${name}" card ${not ? 'not ' : ''}to show "${wanted}", but ${not ? 'it does' : (s.shown ? 'it does not' : describe(s))}.`, {
|
|
159
|
+
found: s.shown ? `it reads "${h.excerpt(s.text, 140)}"` : undefined,
|
|
160
|
+
hint: not ? `collapse it first: When I collapse the "${name}" card` : 'a collapsed card shows only its header: expand it first.',
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
const CHAT_HINT = 'a chat is AdminLTE\'s direct chat (a .card with the class direct-chat), named by its card\'s title.';
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Open or close the contacts of a chat (AdminLTE's direct chat), named by its card's title, with its contacts button
|
|
168
|
+
* (data-lte-toggle="chat-pane" on 4, data-widget="chat-pane-toggle" on 3.2), and wait until the contacts pane has
|
|
169
|
+
* slid over the messages (or away) and stopped. The same on both versions.
|
|
170
|
+
*
|
|
171
|
+
* Example #1: When I open the contacts of the "Team chat" chat
|
|
172
|
+
* Example #2: When I close the contacts of the "Team chat" chat
|
|
173
|
+
* Example #3: When we open the contacts of the "Team chat" chat
|
|
174
|
+
* Example #4: When we close the contacts of the "Team chat" chat
|
|
175
|
+
* Example #5: When I open the contacts of the "team chat" chat
|
|
176
|
+
*/
|
|
177
|
+
When(/^(?:I |we )?(open|close) the contacts of the "([^"]*)" chat$/, async function (action, name) {
|
|
178
|
+
const want = action === 'open';
|
|
179
|
+
const ref = await h.resolve(this, 'chats', name, 'chat', CHAT_HINT);
|
|
180
|
+
const s = await h.call(this, 'chatState', ref);
|
|
181
|
+
if (s.open === want && s.paneVisible === want) return;
|
|
182
|
+
if (!s.toggle) {
|
|
183
|
+
throw h.failure(this.page, `Could not ${action} the contacts of the "${name}" chat: it has no contacts button.`, { hint: 'the button has data-lte-toggle="chat-pane" (4) or data-widget="chat-pane-toggle" (3.2).' });
|
|
184
|
+
}
|
|
185
|
+
await h.clickRef(this, s.toggle, `the contacts button of the "${name}" chat`);
|
|
186
|
+
const result = await h.poll(this, (lib, a) => { const t = lib.chatState(a.ref); return { done: t.open === a.want && t.paneVisible === a.want && !t.moving, state: t }; }, { ref, want }, h.budget(this));
|
|
187
|
+
if (result.done) return;
|
|
188
|
+
throw h.failure(this.page, `Expected the contacts of the "${name}" chat to be ${want ? 'shown' : 'hidden'} after its contacts button was clicked, but they are ${result.state.paneVisible ? 'shown' : 'hidden'} (classes "${result.state.classes}").`, {
|
|
189
|
+
hint: 'check that the page loads AdminLTE\'s script.',
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* The contacts of a chat (AdminLTE's direct chat), named by its card's title, are shown (the pane over the messages)
|
|
195
|
+
* or hidden, at rest. The same on both versions.
|
|
196
|
+
*
|
|
197
|
+
* Example #1: Then the contacts of the "Team chat" chat should be shown
|
|
198
|
+
* Example #2: Then the contacts of the "Team chat" chat should be hidden
|
|
199
|
+
* Example #3: And the contacts of the "Team chat" chat should be shown
|
|
200
|
+
* Example #4: But the contacts of the "Team chat" chat should be hidden
|
|
201
|
+
* Example #5: Then the contacts of the "team chat" chat should be hidden
|
|
202
|
+
*/
|
|
203
|
+
Then(/^the contacts of the "([^"]*)" chat should be (shown|hidden)$/, async function (name, wanted) {
|
|
204
|
+
const ref = await h.resolve(this, 'chats', name, 'chat', CHAT_HINT);
|
|
205
|
+
const want = wanted === 'shown';
|
|
206
|
+
const result = await h.poll(this, (lib, a) => { const t = lib.chatState(a.ref); return { done: t.open === a.want && t.paneVisible === a.want && !t.moving, state: t }; }, { ref, want }, h.budget(this));
|
|
207
|
+
if (result.done) return;
|
|
208
|
+
throw h.failure(this.page, `Expected the contacts of the "${name}" chat to be ${wanted}, but they are ${result.state.paneVisible ? 'shown' : 'hidden'} (classes "${result.state.classes}").`, {
|
|
209
|
+
hint: `${want ? 'open' : 'close'} them first: When I ${want ? 'open' : 'close'} the contacts of the "${name}" chat`,
|
|
210
|
+
});
|
|
211
|
+
});
|
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Shared helpers for the step files of this pack.
|
|
4
|
+
//
|
|
5
|
+
// Not a step file: it registers no step definitions and no hooks. It holds the plumbing the steps use, and that a
|
|
6
|
+
// project can require for its own steps: the wait budgets, the poll on the page, the page side's library (finding a
|
|
7
|
+
// component of the dashboard by the name a person reads and reading its state), clicks, and the failure messages.
|
|
8
|
+
//
|
|
9
|
+
// AdminLTE adds a few components to Bootstrap: the sidebar and its toggle, the tree menu of the sidebar, a card's
|
|
10
|
+
// tools, a chat's contacts pane, full screen and (on 4) the color mode. Its script moves them: the sidebar slides,
|
|
11
|
+
// a tree menu's submenu slides open, a card's body slides shut, a card fades away when removed. The steps wait for
|
|
12
|
+
// the component's end state (its classes, its ARIA, what is drawn) AND for the movement to be over: no CSS
|
|
13
|
+
// transition or animation running in it, no jQuery animation (AdminLTE 3.2 slides with jQuery), no slide still
|
|
14
|
+
// written in its inline style, its box the same at two looks in a row. Never for a fixed time.
|
|
15
|
+
//
|
|
16
|
+
// The Bootstrap components inside a dashboard (dialogs, drop-down menus, tabs, alerts ...) are the Bootstrap pack's
|
|
17
|
+
// (@webship-js/bootstrap-steps), loaded beside this one.
|
|
18
|
+
//
|
|
19
|
+
// Everything builds on webship-js's own helpers (`friendly`, `failure`) so the steps fail the way the core webship-js
|
|
20
|
+
// steps do.
|
|
21
|
+
|
|
22
|
+
const webship = require('webship-js/tests/step-definitions/webship');
|
|
23
|
+
|
|
24
|
+
const { failure } = webship;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The page side: a function that builds the library the probes of `poll` and `call` receive. It runs in the page, a
|
|
28
|
+
* fresh copy at every look, so it needs no script put into the page and holds no state between looks but the boxes
|
|
29
|
+
* it compares (kept on `window.__webshipLteBoxes`). An element is passed back and forth as a reference, a number it
|
|
30
|
+
* keeps in a `data-webship-ref` attribute.
|
|
31
|
+
*/
|
|
32
|
+
function pageLibrary() {
|
|
33
|
+
const norm = (t) => String(t === null || t === undefined ? '' : t)
|
|
34
|
+
.replace(/[‘’‚‛′]/g, "'")
|
|
35
|
+
.replace(/[“”„″]/g, '"')
|
|
36
|
+
.replace(/[ ]/g, ' ')
|
|
37
|
+
.replace(/\s+/g, ' ')
|
|
38
|
+
.trim();
|
|
39
|
+
const lower = (t) => norm(t).toLowerCase();
|
|
40
|
+
const all = (sel, root) => Array.from((root || document).querySelectorAll(sel));
|
|
41
|
+
|
|
42
|
+
function ref(el) {
|
|
43
|
+
if (!el) return 0;
|
|
44
|
+
let id = el.getAttribute('data-webship-ref');
|
|
45
|
+
if (!id) {
|
|
46
|
+
window.__webshipLteRef = (window.__webshipLteRef || 0) + 1;
|
|
47
|
+
id = String(window.__webshipLteRef);
|
|
48
|
+
el.setAttribute('data-webship-ref', id);
|
|
49
|
+
}
|
|
50
|
+
return Number(id);
|
|
51
|
+
}
|
|
52
|
+
function deref(r) {
|
|
53
|
+
return r ? document.querySelector(`[data-webship-ref="${r}"]`) : null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Drawn on the page: connected, not display none or visibility hidden, with a box. */
|
|
57
|
+
function rendered(el) {
|
|
58
|
+
if (!el || !el.isConnected) return false;
|
|
59
|
+
const cs = getComputedStyle(el);
|
|
60
|
+
if (cs.display === 'none' || cs.visibility === 'hidden') return false;
|
|
61
|
+
return el.getClientRects().length > 0;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** The visible text of an element. */
|
|
65
|
+
function text(el) {
|
|
66
|
+
return el ? norm(el.innerText !== undefined ? el.innerText : el.textContent) : '';
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Pick the item named `name` among `items` ({ names: [...] }): exactly, without regard to case, then by its start. */
|
|
70
|
+
function pick(items, name) {
|
|
71
|
+
const want = norm(name);
|
|
72
|
+
const wantLow = want.toLowerCase();
|
|
73
|
+
return items.find((i) => i.names.some((n) => norm(n) === want))
|
|
74
|
+
|| items.find((i) => i.names.some((n) => lower(n) === wantLow))
|
|
75
|
+
|| items.find((i) => i.names.some((n) => lower(n).startsWith(wantLow)))
|
|
76
|
+
|| null;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const version = () => (document.querySelector('.app-wrapper, .app-sidebar') ? 4 : (document.querySelector('.wrapper, .main-sidebar') ? 3 : 0));
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Is the element still moving? A CSS transition or animation running in it (or in what it holds), a jQuery
|
|
83
|
+
* animation (3.2 slides with jQuery), a slide written in its inline style (4's tree menu animates height in the
|
|
84
|
+
* style attribute), or its box not the same as at the last look.
|
|
85
|
+
*/
|
|
86
|
+
function moving(el, key) {
|
|
87
|
+
if (!el || !el.isConnected) return false;
|
|
88
|
+
try {
|
|
89
|
+
if (el.getAnimations({ subtree: true }).some((a) => a.playState === 'running')) return true;
|
|
90
|
+
} catch (e) { /* an old browser: the other signs remain */ }
|
|
91
|
+
const $ = window.jQuery;
|
|
92
|
+
if ($ && $.fn && ($(el).is(':animated') || $(el).find(':animated').length)) return true;
|
|
93
|
+
for (const n of [el, ...all('.nav-treeview, .card-body, .card-footer', el)]) {
|
|
94
|
+
const st = n.getAttribute && n.getAttribute('style');
|
|
95
|
+
if (st && /overflow:\s*hidden/.test(st) && /height:/.test(st)) return true;
|
|
96
|
+
}
|
|
97
|
+
if (/\b(collapsing-card|expanding-card|hold-transition)\b/.test(el.className)) return true;
|
|
98
|
+
const boxes = window.__webshipLteBoxes || (window.__webshipLteBoxes = {});
|
|
99
|
+
const r = el.getBoundingClientRect();
|
|
100
|
+
const box = [r.left, r.top, r.width, r.height].map((v) => Math.round(v)).join(',');
|
|
101
|
+
const k = key || String(ref(el));
|
|
102
|
+
const was = boxes[k];
|
|
103
|
+
boxes[k] = box;
|
|
104
|
+
return was !== box;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// The sidebar: AdminLTE 4's .app-sidebar, 3.2's .main-sidebar; its toggle is [data-lte-toggle="sidebar"] (4) or
|
|
108
|
+
// [data-widget="pushmenu"] (3.2).
|
|
109
|
+
function sidebar() {
|
|
110
|
+
return document.querySelector('.app-sidebar, .main-sidebar');
|
|
111
|
+
}
|
|
112
|
+
function sidebarToggle() {
|
|
113
|
+
return all('[data-lte-toggle="sidebar"], [data-widget="pushmenu"]').find(rendered) || null;
|
|
114
|
+
}
|
|
115
|
+
/** Is the element the one a click at its centre reaches (not covered by another, such as an open sidebar)? */
|
|
116
|
+
function reachable(el) {
|
|
117
|
+
if (!el || !rendered(el)) return false;
|
|
118
|
+
const r = el.getBoundingClientRect();
|
|
119
|
+
const hit = document.elementFromPoint(r.left + r.width / 2, r.top + r.height / 2);
|
|
120
|
+
return !!hit && (hit === el || el.contains(hit));
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** A point beside the open sidebar a person clicks to close it on a narrow screen (its overlay), or null. */
|
|
124
|
+
function besideSidebar() {
|
|
125
|
+
const el = sidebar();
|
|
126
|
+
if (!el) return null;
|
|
127
|
+
const r = el.getBoundingClientRect();
|
|
128
|
+
if (r.right >= window.innerWidth - 20) return null;
|
|
129
|
+
const x = Math.round(r.right + (window.innerWidth - r.right) / 2);
|
|
130
|
+
const y = Math.round(window.innerHeight / 2);
|
|
131
|
+
const hit = document.elementFromPoint(x, y);
|
|
132
|
+
return hit && !el.contains(hit) ? { x, y } : null;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function sidebarState() {
|
|
136
|
+
const el = sidebar();
|
|
137
|
+
if (!el) return { missing: true };
|
|
138
|
+
const r = el.getBoundingClientRect();
|
|
139
|
+
const body = document.body;
|
|
140
|
+
const toggle = sidebarToggle();
|
|
141
|
+
// Expanded: the sidebar is drawn at its full width inside the window (collapsed, it is out of the window, or
|
|
142
|
+
// 3.2's mini sidebar of icons, under 120 pixels).
|
|
143
|
+
const expanded = rendered(el) && r.right > 120 && r.width > 120 && r.left > -10;
|
|
144
|
+
return {
|
|
145
|
+
expanded, width: Math.round(r.width), left: Math.round(r.left), windowWidth: window.innerWidth,
|
|
146
|
+
classes: norm(body.className), toggle: toggle ? ref(toggle) : 0, toggleReachable: reachable(toggle), moving: moving(el, 'sidebar'),
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// The tree menu: the items of the sidebar's menu ([data-lte-toggle="treeview"] on 4, [data-widget="treeview"] on
|
|
151
|
+
// 3.2), named by the text of their link (its <p>, without the arrow). An item with a submenu (.nav-treeview) opens
|
|
152
|
+
// and closes; any item can be active (its link's class active).
|
|
153
|
+
function menuItems() {
|
|
154
|
+
return all('[data-lte-toggle="treeview"] .nav-item, [data-widget="treeview"] .nav-item, .sidebar-menu .nav-item, .nav-sidebar .nav-item').map((li) => {
|
|
155
|
+
const a = li.querySelector(':scope > .nav-link');
|
|
156
|
+
const p = a && a.querySelector('p');
|
|
157
|
+
let name = '';
|
|
158
|
+
if (p) name = Array.from(p.childNodes).filter((n) => n.nodeType === 3 || (n.nodeType === 1 && !/\b(nav-arrow|right|badge)\b/.test(n.className))).map((n) => n.textContent).join(' ');
|
|
159
|
+
else if (a) name = a.textContent;
|
|
160
|
+
return { ref: ref(li), names: [norm(name), a ? norm(a.getAttribute('aria-label') || '') : ''].filter(Boolean) };
|
|
161
|
+
}).filter((x, i, arr) => arr.findIndex((y) => y.ref === x.ref) === i);
|
|
162
|
+
}
|
|
163
|
+
function menuItemState(r) {
|
|
164
|
+
const li = deref(r);
|
|
165
|
+
if (!li) return { gone: true };
|
|
166
|
+
const a = li.querySelector(':scope > .nav-link');
|
|
167
|
+
const sub = li.querySelector(':scope > .nav-treeview');
|
|
168
|
+
const aria = a ? a.getAttribute('aria-expanded') : null;
|
|
169
|
+
return {
|
|
170
|
+
hasSub: !!sub, open: li.classList.contains('menu-open'), subShown: rendered(sub), active: !!a && a.classList.contains('active'),
|
|
171
|
+
aria: aria === null ? '' : aria, ariaAgrees: aria === null || !sub || (aria === 'true') === li.classList.contains('menu-open'),
|
|
172
|
+
link: a ? ref(a) : 0, classes: norm(li.className), moving: moving(li), drawn: rendered(li),
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Cards: .card, named by its .card-title; its tools are [data-lte-toggle="card-collapse|card-maximize|card-remove"]
|
|
177
|
+
// (4) or [data-card-widget="collapse|maximize|remove"] (3.2).
|
|
178
|
+
function cards() {
|
|
179
|
+
return all('.card').map((c) => {
|
|
180
|
+
const t = c.querySelector(':scope > .card-header .card-title');
|
|
181
|
+
return { ref: ref(c), names: [t ? text(t) || norm(t.textContent) : ''].filter(Boolean) };
|
|
182
|
+
}).filter((c) => c.names.length);
|
|
183
|
+
}
|
|
184
|
+
function cardTool(r, tool) {
|
|
185
|
+
const c = deref(r);
|
|
186
|
+
if (!c) return 0;
|
|
187
|
+
const t = c.querySelector(`:scope > .card-header [data-lte-toggle="card-${tool}"], :scope > .card-header [data-card-widget="${tool}"]`);
|
|
188
|
+
return t ? ref(t) : 0;
|
|
189
|
+
}
|
|
190
|
+
function cardState(r) {
|
|
191
|
+
const c = deref(r);
|
|
192
|
+
if (!c || !c.isConnected) return { gone: true, shown: false };
|
|
193
|
+
const bodyEl = c.querySelector(':scope > .card-body');
|
|
194
|
+
return {
|
|
195
|
+
shown: rendered(c), collapsed: c.classList.contains('collapsed-card') && !rendered(bodyEl), maximized: c.classList.contains('maximized-card'),
|
|
196
|
+
bodyShown: rendered(bodyEl), classes: norm(c.className), moving: moving(c), text: text(c),
|
|
197
|
+
tools: ['collapse', 'maximize', 'remove'].filter((t) => cardTool(r, t)),
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// A chat: .direct-chat, named by its card's title; its contacts pane opens with direct-chat-contacts-open, from its
|
|
202
|
+
// [data-lte-toggle="chat-pane"] (4) or [data-widget="chat-pane-toggle"] (3.2).
|
|
203
|
+
function chats() {
|
|
204
|
+
return all('.direct-chat').map((c) => {
|
|
205
|
+
const t = c.querySelector('.card-title');
|
|
206
|
+
return { ref: ref(c), names: [t ? norm(t.textContent) : ''].filter(Boolean) };
|
|
207
|
+
}).filter((c) => c.names.length);
|
|
208
|
+
}
|
|
209
|
+
function chatState(r) {
|
|
210
|
+
const c = deref(r);
|
|
211
|
+
if (!c) return { gone: true };
|
|
212
|
+
const pane = c.querySelector('.direct-chat-contacts');
|
|
213
|
+
const toggle = c.querySelector('[data-lte-toggle="chat-pane"], [data-widget="chat-pane-toggle"]');
|
|
214
|
+
let paneVisible = false;
|
|
215
|
+
if (pane && rendered(pane)) {
|
|
216
|
+
const pr = pane.getBoundingClientRect();
|
|
217
|
+
const cr = (pane.parentElement || c).getBoundingClientRect();
|
|
218
|
+
paneVisible = pr.left < cr.right - 10 && pr.right > cr.left + 10;
|
|
219
|
+
}
|
|
220
|
+
return { open: c.classList.contains('direct-chat-contacts-open'), paneVisible, toggle: toggle ? ref(toggle) : 0, classes: norm(c.className), moving: moving(pane || c) };
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Full screen: the page's own toggle, [data-lte-toggle="fullscreen"] (4) or [data-widget="fullscreen"] (3.2).
|
|
224
|
+
function fullscreenToggle() {
|
|
225
|
+
return all('[data-lte-toggle="fullscreen"], [data-widget="fullscreen"]').find(rendered) || null;
|
|
226
|
+
}
|
|
227
|
+
function fullscreenState() {
|
|
228
|
+
const t = fullscreenToggle();
|
|
229
|
+
return { on: !!document.fullscreenElement, toggle: t ? ref(t) : 0 };
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// The color mode (AdminLTE 4): the buttons [data-bs-theme-value], the mode applied as data-bs-theme on <html>.
|
|
233
|
+
function colorButtons() {
|
|
234
|
+
return all('[data-bs-theme-value]').map((b) => ({ ref: ref(b), value: b.getAttribute('data-bs-theme-value'), names: [norm(b.textContent), b.getAttribute('data-bs-theme-value'), b.getAttribute('aria-label') || ''].filter(Boolean), pressed: b.getAttribute('aria-pressed') === 'true' || b.classList.contains('active') }));
|
|
235
|
+
}
|
|
236
|
+
function colorState() {
|
|
237
|
+
let settled = true;
|
|
238
|
+
try { settled = document.getAnimations().every((x) => x.playState !== 'running'); } catch (e) { /* an old browser */ }
|
|
239
|
+
return { theme: document.documentElement.getAttribute('data-bs-theme') || '', settled, buttons: colorButtons().map((b) => ({ value: b.value, name: b.names[0], pressed: b.pressed })) };
|
|
240
|
+
}
|
|
241
|
+
/** The control that shows a hidden color button: the toggle of the drop-down menu it sits in. */
|
|
242
|
+
function dropdownToggleOf(r) {
|
|
243
|
+
const b = deref(r);
|
|
244
|
+
if (!b) return 0;
|
|
245
|
+
const menu = b.closest('.dropdown-menu');
|
|
246
|
+
const holder = menu && menu.parentElement;
|
|
247
|
+
const t = holder && holder.querySelector('[data-bs-toggle="dropdown"], [data-toggle="dropdown"]');
|
|
248
|
+
return t && rendered(t) ? ref(t) : 0;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** The style sheets the page loads, by their addresses. */
|
|
252
|
+
function styleSheets() {
|
|
253
|
+
return all('link[rel~="stylesheet"][href]').map((l) => l.href);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
return {
|
|
257
|
+
norm, lower, ref, deref, rendered, text, pick, version, moving, reachable, besideSidebar, sidebar, sidebarToggle, sidebarState, menuItems,
|
|
258
|
+
menuItemState, cards, cardTool, cardState, chats, chatState, fullscreenToggle, fullscreenState, colorButtons,
|
|
259
|
+
colorState, dropdownToggleOf, styleSheets,
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
const LIBRARY = pageLibrary.toString();
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* The per-assertion wait budget: `minWaitTime.page` from cucumber.js, at least 10 seconds. Kept below webship-js's
|
|
267
|
+
* 45 second step timeout. COMPONENT_WAIT_MS sets it outright (the run of the assertions that must fail uses a short
|
|
268
|
+
* one: each of them waits its whole budget).
|
|
269
|
+
*
|
|
270
|
+
* @param {object} world
|
|
271
|
+
* @returns {number}
|
|
272
|
+
*/
|
|
273
|
+
function budget(world) {
|
|
274
|
+
const fixed = Number(process.env.COMPONENT_WAIT_MS);
|
|
275
|
+
if (Number.isFinite(fixed) && fixed > 0) return Math.min(fixed, 40000);
|
|
276
|
+
const page = world && world.minWaitTime && world.minWaitTime.page;
|
|
277
|
+
return Math.min(Math.max(page || 0, 10000), 40000);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/** White space collapsed, typographic quotes made plain: the way the page side compares texts. */
|
|
281
|
+
function norm(text) {
|
|
282
|
+
return String(text === null || text === undefined ? '' : text)
|
|
283
|
+
.replace(/[‘’‚‛′]/g, "'")
|
|
284
|
+
.replace(/[“”„″]/g, '"')
|
|
285
|
+
.replace(/[ ]/g, ' ')
|
|
286
|
+
.replace(/\s+/g, ' ')
|
|
287
|
+
.trim();
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/** A list for a failure message: `"a", "b"` or `(none)`. */
|
|
291
|
+
function listed(values, max) {
|
|
292
|
+
const items = [...new Set((values || []).map(norm).filter(Boolean))];
|
|
293
|
+
const limit = max || 12;
|
|
294
|
+
if (!items.length) return '(none)';
|
|
295
|
+
const shown = items.slice(0, limit).map((v) => `"${v}"`).join(', ');
|
|
296
|
+
return items.length > limit ? `${shown} and ${items.length - limit} more` : shown;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** Shorten a text for a message. */
|
|
300
|
+
function excerpt(text, max) {
|
|
301
|
+
const clean = norm(text);
|
|
302
|
+
const limit = max || 160;
|
|
303
|
+
return clean.length > limit ? `${clean.slice(0, limit)}…` : clean;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Run a function of the page side's library: `call(world, 'sidebarState')`.
|
|
310
|
+
*
|
|
311
|
+
* @returns {Promise<*>}
|
|
312
|
+
*/
|
|
313
|
+
async function call(world, name, ...args) {
|
|
314
|
+
return world.page.evaluate(([lib, fn, list]) => {
|
|
315
|
+
// eslint-disable-next-line no-new-func
|
|
316
|
+
const L = (new Function(`return (${lib})`))()();
|
|
317
|
+
return L[fn](...list);
|
|
318
|
+
}, [LIBRARY, name, args]);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Look at the page every 50 ms, from Node (a timer in the page would be counted by webship-js as pending work), until
|
|
323
|
+
* `probe` returns a value with `done: true` or the time is up. The probe runs in the page with `lib` (the page side's
|
|
324
|
+
* library) and `arg`. Returns the last value it gave.
|
|
325
|
+
*
|
|
326
|
+
* @param {object} world
|
|
327
|
+
* @param {Function} probe - (lib, arg) => ({ done, ... }), run in the page.
|
|
328
|
+
* @param {*} arg
|
|
329
|
+
* @param {number} timeout
|
|
330
|
+
* @returns {Promise<object>}
|
|
331
|
+
*/
|
|
332
|
+
async function poll(world, probe, arg, timeout) {
|
|
333
|
+
const deadline = Date.now() + timeout;
|
|
334
|
+
const source = probe.toString();
|
|
335
|
+
let last = { done: false };
|
|
336
|
+
for (;;) {
|
|
337
|
+
let value;
|
|
338
|
+
try {
|
|
339
|
+
value = await world.page.evaluate(([lib, src, a]) => {
|
|
340
|
+
// eslint-disable-next-line no-new-func
|
|
341
|
+
const L = (new Function(`return (${lib})`))()();
|
|
342
|
+
// eslint-disable-next-line no-new-func
|
|
343
|
+
return (new Function(`return (${src})`))()(L, a);
|
|
344
|
+
}, [LIBRARY, source, arg]);
|
|
345
|
+
} catch (e) {
|
|
346
|
+
value = { done: false, error: e.message };
|
|
347
|
+
}
|
|
348
|
+
if (value && typeof value === 'object') last = value;
|
|
349
|
+
if (last.done || Date.now() >= deadline) return last;
|
|
350
|
+
await sleep(50);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/** A locator of an element the page side holds a reference to. */
|
|
355
|
+
function locatorOf(world, ref) {
|
|
356
|
+
return world.page.locator(`[data-webship-ref="${ref}"]`);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/** Click an element by its reference, as a person would. */
|
|
360
|
+
async function clickRef(world, ref, what) {
|
|
361
|
+
try {
|
|
362
|
+
await locatorOf(world, ref).click({ timeout: budget(world) });
|
|
363
|
+
} catch (e) {
|
|
364
|
+
throw failure(world.page, `Could not click ${what}.`, { found: excerpt(e.message.split('\n')[0], 200), hint: 'it must be visible and enabled, and not covered by another element.' });
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
* Find a component by its name, waiting for it to be on the page; fails with the names that are there.
|
|
370
|
+
* resolve(world, 'cards', 'Sales', 'card')
|
|
371
|
+
*
|
|
372
|
+
* @returns {Promise<number>} the reference of the component.
|
|
373
|
+
*/
|
|
374
|
+
async function resolve(world, list, name, noun, hint) {
|
|
375
|
+
const result = await poll(world, (lib, a) => { const items = lib[a.list](); const hit = lib.pick(items, a.name); return { done: !!hit, ref: hit ? hit.ref : 0, names: items.map((i) => i.names[0]) }; }, { list, name }, budget(world));
|
|
376
|
+
if (result.ref) return result.ref;
|
|
377
|
+
throw failure(world.page, `Expected a ${noun} "${name}" on the page, but there is none.`, {
|
|
378
|
+
found: `the ${noun}s on the page: ${listed(result.names)}`,
|
|
379
|
+
hint,
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
module.exports = {
|
|
384
|
+
budget, norm, listed, excerpt, sleep, call, poll, locatorOf, clickRef, resolve, failure, pageLibrary,
|
|
385
|
+
};
|