| @@ -179,12 +179,41 @@ | ||
| 179 | 179 | await saveSettings(page); |
| 180 | 180 | await expect(page.locator('[name="simpleanalytics_manual_collect"]')).toBeChecked(); |
| 181 | 181 | |
| 182 | 182 | const guest = await visitAsGuest(browser); |
| 183 | - expect(await guest.content()).toContain('data-auto-collect="true"'); | |
| 183 | + await expect(guest.locator(DEFAULT_SCRIPT_SELECTOR)).toHaveAttribute('data-auto-collect', 'false'); | |
| 184 | 184 | await guest.context().close(); |
| 185 | + | |
| 186 | + await page.locator('[name="simpleanalytics_manual_collect"]').uncheck(); | |
| 187 | + await saveSettings(page); | |
| 188 | + const automaticGuest = await visitAsGuest(browser); | |
| 189 | + await expect(automaticGuest.locator(DEFAULT_SCRIPT_SELECTOR)).not.toHaveAttribute('data-auto-collect'); | |
| 190 | + await automaticGuest.context().close(); | |
| 185 | 191 | }); |
| 186 | 192 | |
| 193 | +test('executes the saved onload callback when the analytics script loads', async ({ page, browser }) => { | |
| 194 | + await asAdmin(page); | |
| 195 | + await page.goto('/wp-admin/options-general.php?page=simpleanalytics&tab=advanced'); | |
| 196 | + const callback = `document.documentElement.setAttribute('data-sa-callback', 'loaded "quoted"');`; | |
| 197 | + await page.locator('[name="simpleanalytics_onload_callback"]').fill(callback); | |
| 198 | + await saveSettings(page); | |
| 199 | + | |
| 200 | + const context = await browser.newContext(); | |
| 201 | + await context.route('https://scripts.simpleanalyticscdn.com/latest.js', route => | |
| 202 | + route.fulfill({ contentType: 'application/javascript', body: '/* successful script load */' })); | |
| 203 | + const guest = await context.newPage(); | |
| 204 | + try { | |
| 205 | + await guest.goto('/'); | |
| 206 | + await expect(guest.locator('html')).toHaveAttribute('data-sa-callback', 'loaded "quoted"'); | |
| 207 | + await expect(guest.locator(DEFAULT_SCRIPT_SELECTOR)).toHaveAttribute('onload', callback); | |
| 208 | + await expect(guest.locator(DEFAULT_SCRIPT_SELECTOR)).not.toHaveAttribute('data-onload'); | |
| 209 | + } finally { | |
| 210 | + await page.locator('[name="simpleanalytics_onload_callback"]').fill(''); | |
| 211 | + await saveSettings(page); | |
| 212 | + await context.close(); | |
| 213 | + } | |
| 214 | +}); | |
| 215 | + | |
| 187 | 216 | test('adds a script with overwrite domain name', async ({ page, browser }) => { |
| 188 | 217 | await asAdmin(page); |
| 189 | 218 | await page.goto('/wp-admin/options-general.php?page=simpleanalytics&tab=advanced'); |
| 190 | 219 | await page.fill('[name="simpleanalytics_hostname"]', 'example.com'); |
| @@ -257,9 +286,14 @@ | ||
| 257 | 286 | await expect(page.locator('[name="simpleanalytics_automated_events"]')).toBeChecked(); |
| 258 | 287 | await expect(page.locator('[name="simpleanalytics_event_use_title"]')).toBeChecked(); |
| 259 | 288 | |
| 260 | 289 | const guest = await visitAsGuest(browser); |
| 261 | - expect(await guest.content()).toContain('data-use-title'); | |
| 290 | + const eventsScript = guest.locator('script[src="https://scripts.simpleanalyticscdn.com/auto-events.js"]'); | |
| 291 | + await expect(eventsScript).toHaveAttribute('data-use-title', 'true'); | |
| 292 | + await page.locator('[name="simpleanalytics_event_use_title"]').uncheck(); | |
| 293 | + await saveSettings(page); | |
| 294 | + await guest.reload(); | |
| 295 | + await expect(eventsScript).toHaveAttribute('data-use-title', 'false'); | |
| 262 | 296 | await guest.context().close(); |
| 263 | 297 | }); |
| 264 | 298 | |
| 265 | 299 | test('adds automated events script with use full urls enabled', async ({ page, browser }) => { |
| @@ -271,9 +305,14 @@ | ||
| 271 | 305 | await expect(page.locator('[name="simpleanalytics_automated_events"]')).toBeChecked(); |
| 272 | 306 | await expect(page.locator('[name="simpleanalytics_event_full_urls"]')).toBeChecked(); |
| 273 | 307 | |
| 274 | 308 | const guest = await visitAsGuest(browser); |
| 275 | - expect(await guest.content()).toContain('data-full-urls'); | |
| 309 | + const eventsScript = guest.locator('script[src="https://scripts.simpleanalyticscdn.com/auto-events.js"]'); | |
| 310 | + await expect(eventsScript).toHaveAttribute('data-full-urls', 'true'); | |
| 311 | + await page.locator('[name="simpleanalytics_event_full_urls"]').uncheck(); | |
| 312 | + await saveSettings(page); | |
| 313 | + await guest.reload(); | |
| 314 | + await expect(eventsScript).toHaveAttribute('data-full-urls', 'false'); | |
| 276 | 315 | await guest.context().close(); |
| 277 | 316 | }); |
| 278 | 317 | |
| 279 | 318 | test('adds automated events script with override global', async ({ page, browser }) => { |