@@ -23,42 +23,31 @@ test('Sanitization Off', async ({ page }) => {
2323
2424 // The body should have exactly two direct children.
2525 const bodyDirectChildren = page . locator ( 'body > *' ) ;
26- expect ( await bodyDirectChildren . count ( ) ) . toBe ( 2 ) ;
26+ await expect ( bodyDirectChildren ) . toHaveCount ( 2 ) ;
2727
2828 // The body's direct children should be in a specific order.
29- const last = page . locator ( 'img:first-child + .last:last-child' ) ;
30- expect ( await last . count ( ) ) . toBe ( 1 ) ;
31- const lastElement = await last . elementHandle ( ) ;
32- expect ( lastElement ) . toBeTruthy ( ) ;
29+ const last = page . locator ( 'body > img:first-child + .last:last-child' ) ;
30+ await expect ( last ) . toHaveCount ( 1 ) ;
3331
3432 // There should be exactly one img element.
3533 const img = page . locator ( 'img' ) ;
36- expect ( await img . count ( ) ) . toBe ( 1 ) ;
37- const imgElement = await img . elementHandle ( ) ;
38- expect ( imgElement ) . toBeTruthy ( ) ;
34+ await expect ( img ) . toHaveCount ( 1 ) ;
3935
40- // The img should have an `onerror` attribute.
41- const imgAttribute = await imgElement ?. getAttribute ( 'onerror' ) ;
42- expect ( imgAttribute ) . toBeDefined ( ) ;
43- expect ( imgAttribute ?. length ) . toBeGreaterThan ( 10 ) ;
44-
45- // The img should have an `xss` class.
46- const imgClass = await imgElement ?. getAttribute ( 'class' ) ;
47- expect ( imgClass ) . toBe ( 'xss' ) ;
36+ // The img should have an `onerror` attribute, and an `xss` class.
37+ await expect ( img ) . toHaveAttribute ( 'onerror' , / .{ 10 } / ) ;
38+ await expect ( img ) . toHaveClass ( 'xss' ) ;
4839
4940 // There should be exactly one div element.
5041 const div = page . locator ( 'div' ) ;
51- expect ( await div . count ( ) ) . toBe ( 1 ) ;
52- const divElement = await div . elementHandle ( ) ;
53- expect ( divElement ) . toBeTruthy ( ) ;
42+ await expect ( div ) . toHaveCount ( 1 ) ;
5443
5544 // The div should have specific text content.
45+ const divElement = await div . elementHandle ( ) ;
5646 const divContent = await divElement ?. innerHTML ( ) ;
5747 expect ( divContent ) . toBe ( 'A' ) ;
5848
5949 // The div should have an `onclick` attribute.
60- const divAttribute = await divElement ?. getAttribute ( 'onclick' ) ;
61- expect ( divAttribute ) . toBe ( 'console.log(\'foo\')' ) ;
50+ await expect ( div ) . toHaveAttribute ( 'onclick' , 'console.log(\'foo\')' ) ;
6251
6352 // An 'xss' console message should be present.
6453 expect ( consoleMessages . includes ( 'xss' ) ) . toBe ( true ) ;
@@ -82,41 +71,31 @@ test('Sanitize Imports Only', async ({ page }) => {
8271
8372 // The body should have exactly two direct children.
8473 const bodyDirectChildren = page . locator ( 'body > *' ) ;
85- expect ( await bodyDirectChildren . count ( ) ) . toBe ( 2 ) ;
74+ await expect ( bodyDirectChildren ) . toHaveCount ( 2 ) ;
8675
8776 // The body's direct children should be in a specific order.
88- const last = page . locator ( 'img:first-child + .last:last-child' ) ;
89- expect ( await last . count ( ) ) . toBe ( 1 ) ;
90- const lastElement = await last . elementHandle ( ) ;
91- expect ( lastElement ) . toBeTruthy ( ) ;
77+ const last = page . locator ( 'body > img:first-child + .last:last-child' ) ;
78+ await expect ( last ) . toHaveCount ( 1 ) ;
9279
9380 // There should be exactly one img element.
9481 const img = page . locator ( 'img' ) ;
95- expect ( await img . count ( ) ) . toBe ( 1 ) ;
96- const imgElement = await img . elementHandle ( ) ;
97- expect ( imgElement ) . toBeTruthy ( ) ;
82+ await expect ( img ) . toHaveCount ( 1 ) ;
9883
99- // The img should not have an `onerror` attribute.
100- const imgAttribute = await imgElement ?. getAttribute ( 'onerror' ) ;
101- expect ( imgAttribute ) . toBeNull ( ) ;
102-
103- // The img should not have an `xss` class.
104- const imgClass = await imgElement ?. getAttribute ( 'class' ) ;
105- expect ( imgClass ) . toBeNull ( ) ;
84+ // The img should not have an `onerror` attribute, nor an `xss` class.
85+ await expect ( img ) . not . toHaveAttribute ( 'onerror' ) ;
86+ await expect ( img ) . not . toHaveClass ( 'xss' ) ;
10687
10788 // There should be exactly one div element.
10889 const div = page . locator ( 'div' ) ;
109- expect ( await div . count ( ) ) . toBe ( 1 ) ;
110- const divElement = await div . elementHandle ( ) ;
111- expect ( divElement ) . toBeTruthy ( ) ;
90+ await expect ( div ) . toHaveCount ( 1 ) ;
11291
11392 // The div should have specific text content.
93+ const divElement = await div . elementHandle ( ) ;
11494 const divContent = await divElement ?. innerHTML ( ) ;
11595 expect ( divContent ) . toBe ( 'A' ) ;
11696
11797 // The div should have an `onclick` attribute.
118- const divAttribute = await divElement ?. getAttribute ( 'onclick' ) ;
119- expect ( divAttribute ) . toBe ( 'console.log(\'foo\')' ) ;
98+ await expect ( div ) . toHaveAttribute ( 'onclick' , 'console.log(\'foo\')' ) ;
12099
121100 // An 'xss' console message should not be present.
122101 expect ( consoleMessages . includes ( 'xss' ) ) . toBe ( false ) ;
@@ -134,41 +113,31 @@ test('Sanitize Imports Only', async ({ page }) => {
134113async function expectEverythingToBeSanitized ( page : Page ) : Promise < void > {
135114 // The body should have exactly two direct children.
136115 const bodyDirectChildren = page . locator ( 'body > *' ) ;
137- expect ( await bodyDirectChildren . count ( ) ) . toBe ( 2 ) ;
116+ await expect ( bodyDirectChildren ) . toHaveCount ( 2 ) ;
138117
139118 // The body's direct children should be in a specific order.
140- const last = page . locator ( 'img:first-child + .last:last-child' ) ;
141- expect ( await last . count ( ) ) . toBe ( 1 ) ;
142- const lastElement = await last . elementHandle ( ) ;
143- expect ( lastElement ) . toBeTruthy ( ) ;
119+ const last = page . locator ( 'body > img:first-child + .last:last-child' ) ;
120+ await expect ( last ) . toHaveCount ( 1 ) ;
144121
145122 // There should be exactly one img element.
146123 const img = page . locator ( 'img' ) ;
147- expect ( await img . count ( ) ) . toBe ( 1 ) ;
148- const imgElement = await img . elementHandle ( ) ;
149- expect ( imgElement ) . toBeTruthy ( ) ;
150-
151- // The img should not have an `onerror` attribute.
152- const imgAttribute = await imgElement ?. getAttribute ( 'onerror' ) ;
153- expect ( imgAttribute ) . toBeNull ( ) ;
124+ await expect ( img ) . toHaveCount ( 1 ) ;
154125
155- // The img should not have an `xss` class.
156- const imgClass = await imgElement ?. getAttribute ( 'class ') ;
157- expect ( imgClass ) . toBeNull ( ) ;
126+ // The img should not have an `onerror` attribute, nor an ` xss` class.
127+ await expect ( img ) . not . toHaveAttribute ( 'onerror ') ;
128+ await expect ( img ) . not . toHaveClass ( 'xss' ) ;
158129
159130 // There should be exactly one div element.
160131 const div = page . locator ( 'div' ) ;
161- expect ( await div . count ( ) ) . toBe ( 1 ) ;
162- const divElement = await div . elementHandle ( ) ;
163- expect ( divElement ) . toBeTruthy ( ) ;
132+ await expect ( div ) . toHaveCount ( 1 ) ;
164133
165134 // The div should have specific text content.
135+ const divElement = await div . elementHandle ( ) ;
166136 const divContent = await divElement ?. innerHTML ( ) ;
167137 expect ( divContent ) . toBe ( 'A' ) ;
168138
169139 // The div should not have an `onclick` attribute.
170- const divAttribute = await divElement ?. getAttribute ( 'onclick' ) ;
171- expect ( divAttribute ) . toBeNull ( ) ;
140+ await expect ( div ) . not . toHaveAttribute ( 'onclick' ) ;
172141}
173142
174143test ( 'Sanitize Everything' , async ( { page } ) => {
0 commit comments