-
Notifications
You must be signed in to change notification settings - Fork 3.5k
[testdriver] Add testdriver extension for prefetch event in speculation module #55274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
srwei
wants to merge
19
commits into
web-platform-tests:master
Choose a base branch
from
srwei:SpeculationRulesPrefetchWebDriverBidi
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
c367423
add testdriver extension for speculation module and event, and add wp…
srwei 27aaa43
Merge branch 'master' into SpeculationRulesPrefetchWebDriverBidi
srwei 386f50d
remove console logs
srwei 41fd8ad
Merge branch 'SpeculationRulesPrefetchWebDriverBidi' of https://githu…
srwei ef4e35a
update tests, remove subscribe_global to inline instead
srwei a9fe604
use pytest timeout exception when not expecting events
srwei df37262
keep prefetch status subscription to specific context
srwei 0503a27
remove unnecessary tests and methods
srwei c33e41e
remove unused fixture
srwei 05867cb
subscribe per test with cleanup
srwei e9e1ab2
expect multiple repeated events, change to sets
srwei 2b1741a
add 30 sec timeout to failure test
srwei 7b480fe
fail prefetch at network level
srwei d493553
add debugging to failure test
srwei a103beb
typo
srwei 05d3b75
subscribe and unsubscribe per each test to avoid contamination in CI
srwei 1450301
remove unncessary logging
srwei 15f8a4b
reverted back to array from set() due to serialization issue in macOS…
srwei 4a4adfb
fix lint errors
srwei File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
156 changes: 156 additions & 0 deletions
156
infrastructure/testdriver/bidi/speculation/prefetch_status_updated.https.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| <!DOCTYPE html> | ||
| <meta charset="utf-8"/> | ||
| <script src="/resources/testharness.js"></script> | ||
| <script src="/resources/testharnessreport.js"></script> | ||
| <script src="/common/dispatcher/dispatcher.js"></script> | ||
| <script src="/common/utils.js"></script> | ||
| <script src="/resources/testdriver.js?feature=bidi"></script> | ||
| <script src="/resources/testdriver-vendor.js"></script> | ||
| <script src="resources/bidi-speculation-helper.js"></script> | ||
|
|
||
| <script> | ||
| promise_setup(async () => { | ||
| await waitForDocumentReady(); | ||
| await test_driver.bidi.speculation.prefetch_status_updated.subscribe(); | ||
| }); | ||
|
|
||
| promise_test(async t => { | ||
|
|
||
| const receivedEvents = []; | ||
| const expectedEvents = ['pending', 'ready']; | ||
|
|
||
| const targetUrl = window.location.origin + "/infrastructure/testdriver/bidi/speculation/resources/target.html"; | ||
|
|
||
| // Create a promise that resolves when we receive the 'ready' event | ||
| const readyEventPromise = new Promise((resolve, reject) => { | ||
| const removeHandler = test_driver.bidi.speculation.prefetch_status_updated.on((event) => { | ||
| receivedEvents.push(event); | ||
|
|
||
| // When we receive the ready event, clean up and resolve | ||
| if (event.status === 'ready') { | ||
| removeHandler(); | ||
| resolve(); | ||
| } | ||
| }); | ||
|
|
||
| }); | ||
|
|
||
| // Create prefetch rules for our target page in resources | ||
| const speculationRules = { | ||
| prefetch: [{ | ||
| source: "list", | ||
| urls: [targetUrl] | ||
| }] | ||
| }; | ||
|
|
||
| // Use helper function to add both speculation rules and link | ||
| const { script, link } = addSpeculationRulesAndLink(speculationRules, targetUrl); | ||
|
|
||
| // Await the ready event | ||
| await readyEventPromise; | ||
|
|
||
| // Assert that we received the expected events | ||
| assert_array_equals( | ||
| receivedEvents.map(e => e.status), | ||
| expectedEvents, | ||
| 'Should have received pending and ready events in order' | ||
| ); | ||
|
|
||
| }, "prefetch_status_updated event subscription and structure validation"); | ||
|
|
||
| promise_test(async t => { | ||
|
|
||
| const receivedEvents = []; | ||
| const expectedEvents = ['pending', 'ready', 'success']; | ||
| let newWindow = null; | ||
|
|
||
| // Create prefetch rules for our target page in resources (different URL to avoid caching) | ||
| const targetUrl = window.location.origin + "/infrastructure/testdriver/bidi/speculation/resources/target.html?test=2"; | ||
|
|
||
| // Create a promise that resolves when we receive the 'success' event | ||
| const successEventPromise = new Promise((resolve, reject) => { | ||
| const removeHandler = test_driver.bidi.speculation.prefetch_status_updated.on((event) => { | ||
| receivedEvents.push(event); | ||
|
|
||
| // When we receive the ready event, navigate to trigger success | ||
| if (event.status === 'ready') { | ||
| // Open the prefetched page in a new window to trigger success | ||
| newWindow = window.open(event.url, '_blank'); | ||
|
|
||
| } else if (event.status === 'success') { | ||
| removeHandler(); | ||
| resolve(); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| const speculationRules = { | ||
| prefetch: [{ | ||
| source: "list", | ||
| urls: [targetUrl] | ||
| }] | ||
| }; | ||
|
|
||
| // Use helper function to add both speculation rules and link | ||
| const { script, link } = addSpeculationRulesAndLink(speculationRules, targetUrl); | ||
|
|
||
| // Await the success event | ||
| await successEventPromise; | ||
|
|
||
| // Assert that we received the expected events | ||
| assert_array_equals( | ||
| receivedEvents.map(e => e.status), | ||
| expectedEvents, | ||
| 'Should have received pending, ready, and success events in order' | ||
| ); | ||
|
|
||
| t.add_cleanup(() => { | ||
| if (newWindow && !newWindow.closed) { | ||
| newWindow.close(); | ||
| } | ||
| }); | ||
|
|
||
| }, "prefetch_status_updated event with navigation to success"); | ||
|
|
||
| promise_test(async t => { | ||
| const receivedEvents = []; | ||
| const expectedEvents = ['pending', 'failure']; | ||
|
|
||
| const errorUrl = window.location.origin + "/infrastructure/testdriver/bidi/speculation/resources/nonexistent-404-page.html"; | ||
|
|
||
| // Create a promise that resolves when we receive the 'failure' event | ||
| const failureEventPromise = new Promise((resolve, reject) => { | ||
| const removeHandler = test_driver.bidi.speculation.prefetch_status_updated.on((event) => { | ||
| receivedEvents.push(event); | ||
|
|
||
| // When we receive the failure event, we're done | ||
| if (event.status === 'failure') { | ||
| removeHandler(); | ||
| resolve(); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| const speculationRules = { | ||
| prefetch: [{ | ||
| source: "list", | ||
| urls: [errorUrl] | ||
| }] | ||
| }; | ||
|
|
||
| // Use helper function to add both speculation rules and link | ||
| const { script, link } = addSpeculationRulesAndLink(speculationRules, errorUrl); | ||
|
|
||
| // Await the failure event | ||
| await failureEventPromise; | ||
|
|
||
| // Assert that we received the expected events | ||
| assert_array_equals( | ||
| receivedEvents.map(e => e.status), | ||
| expectedEvents, | ||
| 'Should have received pending and failure events in order' | ||
| ); | ||
|
|
||
| }, "prefetch_status_updated event with prefetch failure"); | ||
|
|
||
| </script> | ||
45 changes: 45 additions & 0 deletions
45
infrastructure/testdriver/bidi/speculation/resources/bidi-speculation-helper.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| 'use strict'; | ||
|
|
||
| /** | ||
| * Helper functions for speculation rules BiDi testdriver tests | ||
| */ | ||
|
|
||
| /** | ||
| * Waits until the document has finished loading. | ||
| * @returns {Promise<void>} Resolves if the document is already completely | ||
| * loaded or when the 'onload' event is fired. | ||
| */ | ||
| function waitForDocumentReady() { | ||
| return new Promise(resolve => { | ||
| if (document.readyState === 'complete') { | ||
| resolve(); | ||
| } | ||
|
|
||
| window.addEventListener('load', () => { | ||
| resolve(); | ||
| }, {once: true}); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Adds speculation rules and a corresponding link to the page. | ||
| * @param {Object} speculationRules - The speculation rules object to add | ||
| * @param {string} targetUrl - The URL to add as a link | ||
| * @param {string} linkText - The text content of the link (optional) | ||
| * @returns {Object} Object containing the created script and link elements | ||
| */ | ||
| function addSpeculationRulesAndLink(speculationRules, targetUrl, linkText = 'Test Link') { | ||
| // Add speculation rules script exactly like the working test | ||
| const script = document.createElement('script'); | ||
| script.type = 'speculationrules'; | ||
| script.textContent = JSON.stringify(speculationRules); | ||
| document.head.appendChild(script); | ||
|
|
||
| // Also add a link to the page (some implementations might need this) | ||
| const link = document.createElement('a'); | ||
| link.href = targetUrl; | ||
| link.textContent = linkText; | ||
| document.body.appendChild(link); | ||
|
|
||
| return { script, link }; | ||
| } |
10 changes: 10 additions & 0 deletions
10
infrastructure/testdriver/bidi/speculation/resources/target.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <title>Prefetch Target Page</title> | ||
| </head> | ||
| <body> | ||
| <h1>This is a prefetch target page</h1> | ||
| <p>This page is used as a target for prefetch testing.</p> | ||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.