Skip to content

Commit e9bd3f8

Browse files
test(scripts): update to handle hash params (#30807)
Issue number: N/A --------- <!-- Please do not submit updates to dependencies unless it fixes an issue. --> <!-- Please try to limit your pull request to one type (bugfix, feature, etc). Submit multiple pull requests if needed. --> ## What is the current behavior? <!-- Please describe the current behavior that you are modifying. --> Playwright's `setContent` cannot handle query params which causes the `scripts.js` to not run effectively. ## What is the new behavior? <!-- Please describe the behavior or changes that are being added by this PR. --> - Update `scripts.js` to accept hash params as well - Update `scripts.js` to accept the dark class to set dark mode if dark query or hash was not passed ## Does this introduce a breaking change? - [ ] Yes - [x] No <!-- If this introduces a breaking change: 1. Describe the impact and migration path for existing applications below. 2. Update the BREAKING.md file with the breaking change. 3. Add "BREAKING CHANGE: [...]" to the commit description when merging. See https://github.com/ionic-team/ionic-framework/blob/main/docs/CONTRIBUTING.md#footer for more information. --> ## Other information <!-- Any other information that is important to this PR such as screenshots of how the component looks before and after the change. --> How to test: 1. Verify that tests are passing --------- Co-authored-by: Brandy Smith <brandyscarney@users.noreply.github.com>
1 parent bf0f1e3 commit e9bd3f8

File tree

2 files changed

+61
-11
lines changed

2 files changed

+61
-11
lines changed

core/scripts/testing/scripts.js

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,73 @@
1+
/**
2+
* This script is loaded in testing environments to set up the
3+
* document based on URL parameters.
4+
*
5+
* Test pages (e.g., `chip/test/basic/index.html`) are set to use
6+
* URL query parameters.
7+
*
8+
* Playwright test environments (e.g., `chip/test/basic/chip.e2e.ts`)
9+
* are set based on whether `setContent` or `goto` has been used:
10+
* - `setContent` uses URL hash parameters. Tests will break if
11+
* query parameters are used.
12+
* - `goto` uses URL query parameters.
13+
*
14+
* The following URL parameters are supported:
15+
* - `rtl`: Set to `true` to enable right-to-left directionality.
16+
* - `ionic:_testing`: Set to `true` to identify testing environments.
17+
* - `ionic:mode`: Set to `ios` or `md` to load a specific mode.
18+
* Defaults to `md`.
19+
* - `palette`: Set to `light`, `dark`, `high-contrast`, or
20+
* `high-contrast-dark` to load a specific palette. Defaults to `light`.
21+
*/
122

223
(function() {
324

4-
if (window.location.search.indexOf('rtl=true') > -1) {
25+
/**
26+
* The `rtl` param is used to set the directionality of the
27+
* document. This can be `true` or `false`.
28+
*/
29+
const isRTL = window.location.search.indexOf('rtl=true') > -1 || window.location.hash.indexOf('rtl=true') > -1;
30+
31+
if (isRTL) {
532
document.documentElement.setAttribute('dir', 'rtl');
633
}
734

8-
if (window.location.search.indexOf('ionic:_testing=true') > -1) {
35+
/**
36+
* The `ionic:_testing` param is used to identify testing
37+
* environments.
38+
*/
39+
const isTestEnv = window.location.search.indexOf('ionic:_testing=true') > -1 || window.location.hash.indexOf('ionic:_testing=true') > -1;
40+
41+
if (isTestEnv) {
942
const style = document.createElement('style');
1043
style.innerHTML = `
11-
* {
12-
caret-color: transparent !important;
13-
}`;
44+
* {
45+
caret-color: transparent !important;
46+
}
47+
`;
1448
document.head.appendChild(style);
1549
}
1650

1751
/**
18-
* The term `palette` is used to as a param to match the
19-
* Ionic docs, plus here is already a `ionic:theme` query being
20-
* used for `md`, `ios`, and `ionic` themes.
52+
* The `palette` param is used to load a specific palette
53+
* for the theme.
54+
* The dark class will load the dark palette automatically
55+
* if no palette is specified through the URL.
56+
*
57+
* Values can be `light`, `dark`, `high-contrast`,
58+
* or `high-contrast-dark`. Default to `light` for tests.
2159
*/
22-
const palette = window.location.search.match(/palette=([a-z]+)/);
23-
if (palette && palette[1] !== 'light') {
60+
const paletteQuery = window.location.search.match(/palette=([a-z]+)/);
61+
const paletteHash = window.location.hash.match(/palette=([a-z]+)/);
62+
const darkClass = document.body?.classList.contains('ion-palette-dark') ? 'dark' : null;
63+
64+
const paletteName = paletteQuery?.[1] || paletteHash?.[1] || darkClass || 'light';
65+
66+
if (paletteName !== 'light') {
2467
const linkTag = document.createElement('link');
2568
linkTag.setAttribute('rel', 'stylesheet');
2669
linkTag.setAttribute('type', 'text/css');
27-
linkTag.setAttribute('href', `/css/palettes/${palette[1]}.always.css`);
70+
linkTag.setAttribute('href', `/css/palettes/${paletteName}.always.css`);
2871
document.head.appendChild(linkTag);
2972
}
3073

core/src/utils/test/playwright/page/utils/set-content.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ export const setContent = async (page: Page, html: string, testInfo: TestInfo, o
102102
}
103103
});
104104

105+
/**
106+
* URL query parameters cause the custom Playwright `page.route`
107+
* interceptor to fail, which is necessary to inject the test HTML.
108+
*
109+
* To avoid this, the final navigation URL is kept simple by using
110+
* hash params to ensure the route interceptor always works.
111+
*/
105112
await page.goto(`${baseUrl}#`, options);
106113
}
107114
};

0 commit comments

Comments
 (0)