|
| 1 | +import { test, expect } from '@playwright/test'; |
| 2 | +import { ResultsCollector } from './page-objects/results-collector.js'; |
| 3 | + |
| 4 | +test.describe('Element Hiding Integration Tests', () => { |
| 5 | + test('Basic element hiding with simple hide rules', async ({ page }, testInfo) => { |
| 6 | + const collector = ResultsCollector.create(page, testInfo.project.use); |
| 7 | + await collector.load( |
| 8 | + '/element-hiding/pages/basic-hiding.html', |
| 9 | + './integration-test/test-pages/element-hiding/config/basic-hiding.json', |
| 10 | + ); |
| 11 | + const results = await collector.results(); |
| 12 | + |
| 13 | + expect(results).toMatchObject({ |
| 14 | + 'Basic Element Hiding': [ |
| 15 | + { |
| 16 | + name: 'ad-banner hidden', |
| 17 | + result: true, |
| 18 | + expected: true, |
| 19 | + }, |
| 20 | + { |
| 21 | + name: 'sidebar-ad hidden', |
| 22 | + result: true, |
| 23 | + expected: true, |
| 24 | + }, |
| 25 | + { |
| 26 | + name: 'data-ad element hidden', |
| 27 | + result: true, |
| 28 | + expected: true, |
| 29 | + }, |
| 30 | + { |
| 31 | + name: 'content visible', |
| 32 | + result: true, |
| 33 | + expected: true, |
| 34 | + }, |
| 35 | + { |
| 36 | + name: 'main-content visible', |
| 37 | + result: true, |
| 38 | + expected: true, |
| 39 | + }, |
| 40 | + { |
| 41 | + name: 'data-content element visible', |
| 42 | + result: true, |
| 43 | + expected: true, |
| 44 | + }, |
| 45 | + ], |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + test('Hide empty elements only', async ({ page }, testInfo) => { |
| 50 | + const collector = ResultsCollector.create(page, testInfo.project.use); |
| 51 | + await collector.load( |
| 52 | + '/element-hiding/pages/empty-elements.html', |
| 53 | + './integration-test/test-pages/element-hiding/config/empty-elements.json', |
| 54 | + ); |
| 55 | + const results = await collector.results(); |
| 56 | + |
| 57 | + expect(results).toMatchObject({ |
| 58 | + 'Hide Empty Elements': [ |
| 59 | + { |
| 60 | + name: 'empty-container-1 hidden', |
| 61 | + result: true, |
| 62 | + expected: true, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: 'empty-container-2 (whitespace) hidden', |
| 66 | + result: true, |
| 67 | + expected: true, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: 'maybe-empty-3 hidden', |
| 71 | + result: true, |
| 72 | + expected: true, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: 'non-empty-1 visible', |
| 76 | + result: true, |
| 77 | + expected: true, |
| 78 | + }, |
| 79 | + { |
| 80 | + name: 'non-empty-2 (with children) visible', |
| 81 | + result: true, |
| 82 | + expected: true, |
| 83 | + }, |
| 84 | + { |
| 85 | + name: 'non-empty-3 (with text) visible', |
| 86 | + result: true, |
| 87 | + expected: true, |
| 88 | + }, |
| 89 | + ], |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + test('Modify element attributes', async ({ page }, testInfo) => { |
| 94 | + const collector = ResultsCollector.create(page, testInfo.project.use); |
| 95 | + await collector.load( |
| 96 | + '/element-hiding/pages/modify-attributes.html', |
| 97 | + './integration-test/test-pages/element-hiding/config/modify-attributes.json', |
| 98 | + ); |
| 99 | + const results = await collector.results(); |
| 100 | + |
| 101 | + expect(results).toMatchObject({ |
| 102 | + 'Modify Attributes': [ |
| 103 | + { |
| 104 | + name: 'ad-image src modified', |
| 105 | + result: 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7', |
| 106 | + expected: 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7', |
| 107 | + }, |
| 108 | + { |
| 109 | + name: 'tracking-link href modified', |
| 110 | + result: expect.stringContaining('#blocked'), |
| 111 | + expected: expect.stringContaining('#blocked'), |
| 112 | + }, |
| 113 | + { |
| 114 | + name: 'content-image src unchanged', |
| 115 | + result: true, |
| 116 | + expected: true, |
| 117 | + }, |
| 118 | + { |
| 119 | + name: 'regular-link href unchanged', |
| 120 | + result: true, |
| 121 | + expected: true, |
| 122 | + }, |
| 123 | + ], |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + test('Modify element styles', async ({ page }, testInfo) => { |
| 128 | + const collector = ResultsCollector.create(page, testInfo.project.use); |
| 129 | + await collector.load( |
| 130 | + '/element-hiding/pages/modify-styles.html', |
| 131 | + './integration-test/test-pages/element-hiding/config/modify-styles.json', |
| 132 | + ); |
| 133 | + const results = await collector.results(); |
| 134 | + |
| 135 | + expect(results).toMatchObject({ |
| 136 | + 'Modify Styles': [ |
| 137 | + { |
| 138 | + name: 'ad-container display modified', |
| 139 | + result: 'none', |
| 140 | + expected: 'none', |
| 141 | + }, |
| 142 | + { |
| 143 | + name: 'banner-ad visibility modified', |
| 144 | + result: 'hidden', |
| 145 | + expected: 'hidden', |
| 146 | + }, |
| 147 | + { |
| 148 | + name: 'resize-ad width modified', |
| 149 | + result: '0px', |
| 150 | + expected: '0px', |
| 151 | + }, |
| 152 | + { |
| 153 | + name: 'normal-content display unchanged', |
| 154 | + result: true, |
| 155 | + expected: true, |
| 156 | + }, |
| 157 | + { |
| 158 | + name: 'regular-element width unchanged', |
| 159 | + result: true, |
| 160 | + expected: true, |
| 161 | + }, |
| 162 | + ], |
| 163 | + }); |
| 164 | + }); |
| 165 | + |
| 166 | + test('Element hiding disabled - baseline behavior', async ({ page }, testInfo) => { |
| 167 | + const collector = ResultsCollector.create(page, testInfo.project.use); |
| 168 | + await collector.load( |
| 169 | + '/element-hiding/pages/basic-hiding.html', |
| 170 | + './integration-test/test-pages/element-hiding/config/disabled.json', |
| 171 | + ); |
| 172 | + const results = await collector.results(); |
| 173 | + |
| 174 | + // When element hiding is disabled, all elements should remain visible |
| 175 | + // Note: The test logic checks for elements being hidden, but when disabled they shouldn't be hidden |
| 176 | + const basicResults = results['Basic Element Hiding']; |
| 177 | + expect(basicResults).toBeDefined(); |
| 178 | + |
| 179 | + // Find the hide tests - they should return false when element hiding is disabled |
| 180 | + const adBannerResult = basicResults.find((r) => r.name === 'ad-banner hidden'); |
| 181 | + const sidebarResult = basicResults.find((r) => r.name === 'sidebar-ad hidden'); |
| 182 | + const dataAdResult = basicResults.find((r) => r.name === 'data-ad element hidden'); |
| 183 | + |
| 184 | + expect(adBannerResult.result).toBe(false); // Should NOT be hidden when disabled |
| 185 | + expect(sidebarResult.result).toBe(false); // Should NOT be hidden when disabled |
| 186 | + expect(dataAdResult.result).toBe(false); // Should NOT be hidden when disabled |
| 187 | + |
| 188 | + // Visible elements should remain visible |
| 189 | + const contentResult = basicResults.find((r) => r.name === 'content visible'); |
| 190 | + const mainContentResult = basicResults.find((r) => r.name === 'main-content visible'); |
| 191 | + const dataContentResult = basicResults.find((r) => r.name === 'data-content element visible'); |
| 192 | + |
| 193 | + expect(contentResult.result).toBe(true); |
| 194 | + expect(mainContentResult.result).toBe(true); |
| 195 | + expect(dataContentResult.result).toBe(true); |
| 196 | + }); |
| 197 | + |
| 198 | + test('Performance: Element hiding rules apply quickly', async ({ page }, testInfo) => { |
| 199 | + const collector = ResultsCollector.create(page, testInfo.project.use); |
| 200 | + |
| 201 | + const startTime = Date.now(); |
| 202 | + await collector.load( |
| 203 | + '/element-hiding/pages/basic-hiding.html', |
| 204 | + './integration-test/test-pages/element-hiding/config/basic-hiding.json', |
| 205 | + ); |
| 206 | + |
| 207 | + // Wait a minimal time and check if elements are already hidden |
| 208 | + await page.waitForTimeout(50); |
| 209 | + |
| 210 | + const adBanner = page.locator('.ad-banner').first(); |
| 211 | + const isHidden = await adBanner.evaluate((el) => { |
| 212 | + const style = window.getComputedStyle(el); |
| 213 | + return style.display === 'none' || (el instanceof HTMLElement && el.hidden === true); |
| 214 | + }); |
| 215 | + |
| 216 | + const loadTime = Date.now() - startTime; |
| 217 | + |
| 218 | + expect(isHidden).toBe(true); |
| 219 | + expect(loadTime).toBeLessThan(1000); // Should complete within 1 second |
| 220 | + }); |
| 221 | + |
| 222 | + test('Forgiving selectors handle invalid CSS gracefully', async ({ page }, testInfo) => { |
| 223 | + // Use a simple test page |
| 224 | + const collector = ResultsCollector.create(page, testInfo.project.use); |
| 225 | + await collector.load( |
| 226 | + '/element-hiding/pages/basic-hiding.html', |
| 227 | + './integration-test/test-pages/element-hiding/config/basic-hiding.json', |
| 228 | + ); |
| 229 | + |
| 230 | + // The page should load without errors, even with invalid selectors |
| 231 | + const title = await page.title(); |
| 232 | + expect(title).toBe('Basic Element Hiding Test'); |
| 233 | + |
| 234 | + // Valid selectors should still work |
| 235 | + const results = await collector.results(); |
| 236 | + expect(results).toBeDefined(); |
| 237 | + }); |
| 238 | + |
| 239 | + test('Privacy Test Pages site match - specific element hiding scenarios', async ({ page }, testInfo) => { |
| 240 | + const collector = ResultsCollector.create(page, testInfo.project.use); |
| 241 | + await collector.load( |
| 242 | + '/element-hiding/pages/privacy-test-pages-match.html', |
| 243 | + './integration-test/test-pages/element-hiding/config/privacy-test-pages-match.json', |
| 244 | + ); |
| 245 | + const results = await collector.results(); |
| 246 | + |
| 247 | + expect(results).toMatchObject({ |
| 248 | + 'Privacy Test Pages Match': [ |
| 249 | + { |
| 250 | + name: 'advertisement should be hidden', |
| 251 | + result: true, |
| 252 | + expected: true, |
| 253 | + }, |
| 254 | + { |
| 255 | + name: 'advertisement should not be hidden', |
| 256 | + result: true, |
| 257 | + expected: true, |
| 258 | + }, |
| 259 | + { |
| 260 | + name: 'lorem ipsum content visible', |
| 261 | + result: true, |
| 262 | + expected: true, |
| 263 | + }, |
| 264 | + { |
| 265 | + name: 'content element visible', |
| 266 | + result: true, |
| 267 | + expected: true, |
| 268 | + }, |
| 269 | + ], |
| 270 | + }); |
| 271 | + }); |
| 272 | +}); |
0 commit comments