Skip to content

Commit 3d2853c

Browse files
DavertMikclaude
andcommitted
fix(playwright): keep resource blocker when stopMockingRoute('**/*') is called
Register the blockResources route with a predicate so unroute by URL pattern cannot remove it. Regenerate helper docs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent e029096 commit 3d2853c

3 files changed

Lines changed: 12 additions & 2 deletions

File tree

‎docs/helpers/Playwright.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ Type: [object][8]
7979
* `bypassCSP` **[boolean][26]?** bypass Content Security Policy or CSP
8080
* `highlightElement` **[boolean][26]?** highlight the interacting elements. Default: false. Note: only activate under verbose mode (--verbose).
8181
* `visibleLocator` **[boolean][26]?** append [`visible()`][49] to locators, so only visible elements are matched. Requires Playwright 1.63 or newer. Switch it off for a single step with `stepOpts({ visibleLocator: false })`. Not applied to `dragAndDrop`, which passes selectors to Playwright directly, nor to steps that must reach hidden elements: `grab*` methods, `scrollTo`, `seeElementInDOM`, `dontSeeElementInDOM` and `seeNumberOfElements`. When enabled, a locator matching only hidden elements fails as "element not found" instead of timing out on actionability, `strict` mode ignores hidden duplicates, and elements hidden by CSS (like a custom checkbox built on a visually hidden `input`) are no longer found.
82+
* `instantAnimations` **[boolean][26]?** run CSS transitions, CSS animations and Web Animations 1000 times faster, so a click waits for a moving element for one frame instead of the whole animation. `transitionend` and `animationend` still fire. Chromium only. Timers and JS-driven animations keep their speed. Not applied inside cross-origin iframes that Chromium runs in a separate process.
83+
* `blockResources` **[Array][9]<[string][7]>?** resource types to skip loading: `'image'`, `'media'`, `'font'`, `'stylesheet'`. Tests no longer exercise those resources. Enabling it turns on request routing, which disables Playwright's HTTP cache.
8284
* `recordHar` **[object][8]?** record HAR and will be saved to `output/har`. See more of [HAR options][3].
8385
* `testIdAttribute` **[string][7]?** locate elements based on the testIdAttribute. See more of [locate by test id][50].
8486
* `storageState` **([string][7] | [object][8])?** Playwright storage state (path to JSON file or object)

‎lib/helper/Playwright.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1244,7 +1244,7 @@ class Playwright extends Helper {
12441244

12451245
async _setupContext(context) {
12461246
if (this.options.blockResources.length) {
1247-
await context.route('**/*', route => {
1247+
await context.route(() => true, route => {
12481248
if (this.options.blockResources.includes(route.request().resourceType())) return route.abort('blockedbyclient')
12491249
return route.fallback()
12501250
})

‎test/helper/Playwright_test.js‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1792,7 +1792,7 @@ describe('Playwright - speed options', function () {
17921792

17931793
it('should not load blocked resource types', async () => {
17941794
await I.amOnPage('/image')
1795-
expect(await I.executeScript(() => document.getElementById('logo').complete)).to.equal(true)
1795+
await I.waitForFunction(() => document.getElementById('logo').complete, 2)
17961796
expect(await I.executeScript(() => document.getElementById('logo').naturalWidth)).to.equal(0)
17971797
})
17981798

@@ -1804,6 +1804,14 @@ describe('Playwright - speed options', function () {
18041804
await I.stopMockingRoute('**/codeceptjs.png')
18051805
})
18061806

1807+
it('should keep blocking after stopMockingRoute for all urls', async () => {
1808+
await I.mockRoute('**/*', route => route.fallback())
1809+
await I.stopMockingRoute('**/*')
1810+
await I.amOnPage('/image')
1811+
await I.waitForFunction(() => document.getElementById('logo').complete, 2)
1812+
expect(await I.executeScript(() => document.getElementById('logo').naturalWidth)).to.equal(0)
1813+
})
1814+
18071815
it('should reject resource types that are not allowed', () => {
18081816
expect(() => new Playwright({ blockResources: ['script'] })).to.throw('image, media, font, stylesheet')
18091817
})

0 commit comments

Comments
 (0)