@@ -15,33 +15,32 @@ let assert;
15
15
let jsdom ;
16
16
/** @type {import('../../lib/page-functions.js') } */
17
17
let pageFunctions ;
18
- /** @type {import('../../../report/renderer/dom.js').DOM } */
19
- let DOM ;
20
18
21
19
/* eslint-env jest */
22
20
21
+ /* global document */
22
+
23
23
describe ( 'Page Functions' , ( ) => {
24
24
const url = 'http://www.example.com' ;
25
- let dom ;
26
25
27
26
beforeAll ( async ( ) => {
28
27
assert = ( await import ( 'assert' ) ) . strict ;
29
28
jsdom = await import ( 'jsdom' ) ;
30
29
pageFunctions = ( await import ( '../../lib/page-functions.js' ) ) . default ;
31
- DOM = ( await import ( '../../../report/renderer/dom.js' ) ) . DOM ;
32
30
33
31
const { document, ShadowRoot, Node, HTMLElement} = new jsdom . JSDOM ( '' , { url} ) . window ;
34
32
global . ShadowRoot = ShadowRoot ;
35
33
global . Node = Node ;
36
34
global . HTMLElement = HTMLElement ;
35
+ global . document = document ;
37
36
global . window = { } ;
38
- dom = new DOM ( document ) ;
39
37
} ) ;
40
38
41
39
afterAll ( ( ) => {
42
40
global . ShadowRoot = undefined ;
43
41
global . Node = undefined ;
44
42
global . window = undefined ;
43
+ global . document = undefined ;
45
44
} ) ;
46
45
47
46
describe ( 'wrapRuntimeEvalErrorInBrowser()' , ( ) => {
@@ -85,124 +84,146 @@ describe('Page Functions', () => {
85
84
86
85
describe ( 'get outer HTML snippets' , ( ) => {
87
86
it ( 'gets full HTML snippet' , ( ) => {
88
- assert . equal ( pageFunctions . getOuterHTMLSnippet (
89
- dom . createElement ( 'div' , '' , { id : '1' , style : 'style' } ) ) , '<div id="1" style="style">' ) ;
87
+ const elem = document . createElement ( 'div' ) ;
88
+ elem . id = '1' ;
89
+ elem . style = 'width: 1px;' ;
90
+ assert . equal ( pageFunctions . getOuterHTMLSnippet ( elem ) , '<div id="1" style="width: 1px;">' ) ;
90
91
} ) ;
91
92
92
93
it ( 'replaces img.src with img.currentSrc' , ( ) => {
93
- const el = dom . createElement ( 'img' , '' , { id : '1' , src : 'no' } ) ;
94
+ const el = document . createElement ( 'img' ) ;
95
+ el . id = '1' ;
96
+ el . src = 'no' ;
94
97
Object . defineProperty ( el , 'currentSrc' , { value : 'yes' } ) ;
95
98
assert . equal ( pageFunctions . getOuterHTMLSnippet ( el ) , '<img id="1" src="yes">' ) ;
96
99
} ) ;
97
100
98
101
it ( 'does not replace img.src with img.currentSrc if resolve to same URL' , ( ) => {
99
- const el = dom . createElement ( 'img' , '' , { id : '1' , src : './a.png' } ) ;
102
+ const el = document . createElement ( 'img' ) ;
103
+ el . id = '1' ;
104
+ el . src = './a.png' ;
100
105
Object . defineProperty ( el , 'currentSrc' , { value : `${ url } /a.png` } ) ;
101
106
assert . equal ( pageFunctions . getOuterHTMLSnippet ( el ) , '<img id="1" src="./a.png">' ) ;
102
107
} ) ;
103
108
104
109
it ( 'removes a specific attribute' , ( ) => {
105
- assert . equal ( pageFunctions . getOuterHTMLSnippet (
106
- dom . createElement ( 'div' , '' , { id : '1' , style : 'style' } ) , [ 'style' ] ) , '<div id="1">' ) ;
110
+ const elem = document . createElement ( 'div' ) ;
111
+ elem . id = '1' ;
112
+ elem . style = 'width: 1px;' ;
113
+ assert . equal ( pageFunctions . getOuterHTMLSnippet ( elem , [ 'style' ] ) , '<div id="1">' ) ;
107
114
} ) ;
108
115
109
116
it ( 'removes multiple attributes' , ( ) => {
110
- assert . equal ( pageFunctions . getOuterHTMLSnippet (
111
- dom . createElement ( 'div' , '' , { 'id' : '1' , 'style' : 'style' , 'aria-label' : 'label' } ) ,
112
- [ 'style' , 'aria-label' ]
113
- ) , '<div id="1">' ) ;
117
+ const elem = document . createElement ( 'div' ) ;
118
+ elem . id = '1' ;
119
+ elem . style = 'width: 1px;' ;
120
+ elem . setAttribute ( 'aria-label' , 'label' ) ;
121
+ assert . equal (
122
+ pageFunctions . getOuterHTMLSnippet ( elem , [ 'style' , 'aria-label' ] ) ,
123
+ '<div id="1">' ) ;
114
124
} ) ;
115
125
116
126
it ( 'should handle dom nodes that cannot be cloned' , ( ) => {
117
- const element = dom . createElement ( 'div' ) ;
127
+ const element = document . createElement ( 'div' ) ;
118
128
element . cloneNode = ( ) => {
119
129
throw new Error ( 'oops!' ) ;
120
130
} ;
121
131
assert . equal ( pageFunctions . getOuterHTMLSnippet ( element ) , '<div>' ) ;
122
132
} ) ;
123
133
it ( 'ignores when attribute not found' , ( ) => {
134
+ const elem = document . createElement ( 'div' ) ;
135
+ elem . id = '1' ;
136
+ elem . style = 'width: 1px;' ;
137
+ elem . setAttribute ( 'aria-label' , 'label' ) ;
124
138
assert . equal ( pageFunctions . getOuterHTMLSnippet (
125
- dom . createElement ( 'div' , '' , { 'id' : '1' , 'style' : 'style' , 'aria-label' : 'label' } ) ,
139
+ elem ,
126
140
[ 'style-missing' , 'aria-label-missing' ]
127
- ) , '<div id="1" style="style " aria-label="label">' ) ;
141
+ ) , '<div id="1" style="width: 1px; " aria-label="label">' ) ;
128
142
} ) ;
129
143
130
144
it ( 'works if attribute values contain line breaks' , ( ) => {
131
- assert . equal ( pageFunctions . getOuterHTMLSnippet (
132
- dom . createElement ( 'div' , '' , { style : 'style1\nstyle2' } ) ) , '<div style="style1\nstyle2">' ) ;
145
+ const elem = document . createElement ( 'div' ) ;
146
+ elem . style = 'width: 1px;\nheight: 2px;' ;
147
+ assert . equal ( pageFunctions . getOuterHTMLSnippet ( elem ) ,
148
+ '<div style="width: 1px; height: 2px;">' ) ;
133
149
} ) ;
134
150
135
151
it ( 'truncates attribute values that are too long' , ( ) => {
136
- const longClass = 'a' . repeat ( 200 ) ;
152
+ const elem = document . createElement ( 'div' ) ;
153
+ elem . className = 'a' . repeat ( 200 ) ;
137
154
const truncatedExpectation = 'a' . repeat ( 74 ) + '…' ;
138
- assert . equal ( pageFunctions . getOuterHTMLSnippet (
139
- dom . createElement ( 'div' , '' , { class : longClass } ) ) , `<div class=" ${ truncatedExpectation } ">`
140
- ) ;
155
+ assert . equal (
156
+ pageFunctions . getOuterHTMLSnippet ( elem ) ,
157
+ `<div class=" ${ truncatedExpectation } ">` ) ;
141
158
} ) ;
142
159
143
160
it ( 'removes attributes if the length of the attribute name + value is too long' , ( ) => {
144
161
const longValue = 'a' . repeat ( 200 ) ;
145
162
const truncatedValue = 'a' . repeat ( 74 ) + '…' ;
146
- const element = dom . createElement ( 'div' , '' , {
147
- class : longValue ,
148
- id : longValue ,
149
- att1 : 'shouldn\'t see this' ,
150
- att2 : 'shouldn\'t see this either' ,
151
- } ) ;
152
- const snippet = pageFunctions . getOuterHTMLSnippet ( element , [ ] , 150 ) ;
163
+ const elem = document . createElement ( 'div' ) ;
164
+ elem . className = longValue ;
165
+ elem . id = longValue ;
166
+ elem . setAttribute ( ' att1' , 'shouldn\'t see this' ) ;
167
+ elem . setAttribute ( ' att2' , 'shouldn\'t see this either' ) ;
168
+
169
+ const snippet = pageFunctions . getOuterHTMLSnippet ( elem , [ ] , 150 ) ;
153
170
assert . equal ( snippet , `<div class="${ truncatedValue } " id="${ truncatedValue } " …>`
154
171
) ;
155
172
} ) ;
156
173
} ) ;
157
174
158
175
describe ( 'getNodeSelector' , ( ) => {
159
176
it ( 'Uses IDs where available and otherwise falls back to classes' , ( ) => {
160
- const parentEl = dom . createElement ( 'div' , '' , { id : 'wrapper' , class : 'dont-use-this' } ) ;
161
- const childEl = dom . createElement ( 'div' , '' , { class : 'child' } ) ;
177
+ const parentEl = document . createElement ( 'div' ) ;
178
+ parentEl . id = 'wrapper' ;
179
+ parentEl . className = 'dont-use-this' ;
180
+ const childEl = document . createElement ( 'div' ) ;
181
+ childEl . className = 'child' ;
162
182
parentEl . appendChild ( childEl ) ;
163
183
assert . equal ( pageFunctions . getNodeSelector ( childEl ) , 'div#wrapper > div.child' ) ;
164
184
} ) ;
165
185
} ) ;
166
186
167
187
describe ( 'getNodeLabel' , ( ) => {
168
188
it ( 'Returns innerText if element has visible text' , ( ) => {
169
- const el = dom . createElement ( 'div' ) ;
189
+ const el = document . createElement ( 'div' ) ;
170
190
el . innerText = 'Hello' ;
171
191
assert . equal ( pageFunctions . getNodeLabel ( el ) , 'Hello' ) ;
172
192
} ) ;
173
193
174
194
it ( 'Falls back to children and alt/aria-label if a title can\'t be determined' , ( ) => {
175
- const el = dom . createElement ( 'div' ) ;
176
- const childEl = dom . createElement ( 'div' , '' , { 'aria-label' : 'Something' } ) ;
195
+ const el = document . createElement ( 'div' ) ;
196
+ const childEl = document . createElement ( 'div' ) ;
197
+ childEl . setAttribute ( 'aria-label' , 'Something' ) ;
177
198
el . appendChild ( childEl ) ;
178
199
assert . equal ( pageFunctions . getNodeLabel ( el ) , 'Something' ) ;
179
200
} ) ;
180
201
181
202
it ( 'Truncates long text' , ( ) => {
182
- const el = dom . createElement ( 'div' ) ;
203
+ const el = document . createElement ( 'div' ) ;
183
204
el . setAttribute ( 'alt' , Array ( 100 ) . fill ( 'a' ) . join ( '' ) ) ;
184
205
assert . equal ( pageFunctions . getNodeLabel ( el ) . length , 80 ) ;
185
206
} ) ;
186
207
187
208
it ( 'Truncates long text containing unicode surrogate pairs' , ( ) => {
188
- const el = dom . createElement ( 'div' ) ;
209
+ const el = document . createElement ( 'div' ) ;
189
210
// `getNodeLabel` truncates to 80 characters internally.
190
211
// We want to test a unicode character on the boundary.
191
212
el . innerText = Array ( 78 ) . fill ( 'a' ) . join ( '' ) + '💡💡💡' ;
192
213
assert . equal ( pageFunctions . getNodeLabel ( el ) , Array ( 78 ) . fill ( 'a' ) . join ( '' ) + '💡…' ) ;
193
214
} ) ;
194
215
195
216
it ( 'Returns null if there is no better label' , ( ) => {
196
- const el = dom . createElement ( 'div' ) ;
197
- const childEl = dom . createElement ( 'span' ) ;
217
+ const el = document . createElement ( 'div' ) ;
218
+ const childEl = document . createElement ( 'span' ) ;
198
219
el . appendChild ( childEl ) ;
199
220
assert . equal ( pageFunctions . getNodeLabel ( el ) , null ) ;
200
221
} ) ;
201
222
} ) ;
202
223
203
224
describe ( 'getNodePath' , ( ) => {
204
225
it ( 'returns basic node path' , ( ) => {
205
- const el = dom . createElement ( 'div' ) ;
226
+ const el = document . createElement ( 'div' ) ;
206
227
el . innerHTML = `
207
228
<section>
208
229
<span>Sup</span>
@@ -215,11 +236,11 @@ describe('Page Functions', () => {
215
236
} ) ;
216
237
217
238
it ( 'returns node path through shadow root' , ( ) => {
218
- const el = dom . createElement ( 'div' ) ;
219
- const main = el . appendChild ( dom . createElement ( 'main' ) ) ;
239
+ const el = document . createElement ( 'div' ) ;
240
+ const main = el . appendChild ( document . createElement ( 'main' ) ) ;
220
241
const shadowRoot = main . attachShadow ( { mode : 'open' } ) ;
221
- const sectionEl = dom . createElement ( 'section' ) ;
222
- const img = dom . createElement ( 'img' ) ;
242
+ const sectionEl = document . createElement ( 'section' ) ;
243
+ const img = document . createElement ( 'img' ) ;
223
244
img . src = '#' ;
224
245
sectionEl . append ( img ) ;
225
246
shadowRoot . append ( sectionEl ) ;
@@ -230,8 +251,12 @@ describe('Page Functions', () => {
230
251
231
252
describe ( 'getNodeDetails' , ( ) => {
232
253
it ( 'Returns selector as fallback if nodeLabel equals html tag name' , ( ) => {
233
- const el = dom . createElement ( 'div' , '' , { id : 'parent' , class : 'parent-el' } ) ;
234
- const childEl = dom . createElement ( 'p' , '' , { id : 'child' , class : 'child-el' } ) ;
254
+ const el = document . createElement ( 'div' ) ;
255
+ el . id = 'parent' ;
256
+ el . className = 'parent-el' ;
257
+ const childEl = document . createElement ( 'p' ) ;
258
+ childEl . id = 'child' ;
259
+ childEl . className = 'child-el' ;
235
260
el . appendChild ( childEl ) ;
236
261
const { nodeLabel} = pageFunctions . getNodeDetails ( el ) ;
237
262
assert . equal ( nodeLabel , 'div#parent' ) ;
0 commit comments