Skip to content

Commit 2a3f95e

Browse files
committed
playwright: add a few platform-specific tests
The Git home page displays some platform-dependent behavior. With this commit, we test that by pretending to run on the three major operating systems: Windows, macOS and Linux. Specifically, we verify that: - the correct download is suggested, based on the current browser - a subset of the GUIs are shown based on the platform indicated by the current browser Pretending to be running on a given platform is a bit tricky because the `navigator.userAgent`/`navigator.platform` attributes are used by the Git home page (via `session.min.js`) to determine what operating system the user is running. These can be easily overridden on most browser, using the `Object.defineProperty()` method. However, in my tests, Chromium on Linux (and only on that operating system) simply ignored those calls and still reported that it is running on Linux. To work around this, we need to use the Chrome DevTools Protocol, via the `BrowserContext.newCDPSession()` method; For more details see https://playwright.dev/docs/api/class-browsercontext#browser-context-new-cdp-session Obviously, this can _only_ be used with Chromium/Chrome and won't work on, say, Firefox or Safari. That is the reason for the somewhat ugly `pretendPlatform()` function we need to use here. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 659dcf5 commit 2a3f95e

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

tests/git-scm.spec.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,80 @@ const { test, expect, selectors } = require('@playwright/test')
22

33
const url = 'https://git.github.io/git-scm.com/'
44

5+
async function pretendPlatform(page, browserName, userAgent, platform) {
6+
if (browserName !== 'chromium') {
7+
await page.context().addInitScript({
8+
content: `Object.defineProperty(navigator, 'platform', { get: () => '${platform}' })`
9+
})
10+
} else {
11+
// As of time of writing, Chromium on Linux (but not on Windows or macOS)
12+
// refuses to let the `navigator.platform` attribute to be overridden via
13+
// `Object.defineProperty()`, therefore we use the Chrome DevTools Protocol
14+
// with that browser (and only with that browser because it is proprietary
15+
// to that browser).
16+
const cdpSession = await page.context().newCDPSession(page);
17+
await cdpSession.send('Emulation.setUserAgentOverride', { platform, userAgent });
18+
}
19+
}
20+
21+
test.describe('Windows', () => {
22+
const userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36'
23+
test.use({ userAgent })
24+
25+
test('download/GUI links', async ({ page, browserName }) => {
26+
await pretendPlatform(page, browserName, userAgent, 'Windows')
27+
await page.goto(url)
28+
await expect(page.getByRole('link', { name: 'Download for Windows' })).toBeVisible()
29+
30+
await expect(page.getByRole('link', { name: 'Graphical UIs' })).toBeHidden()
31+
const windowsGUIs = page.getByRole('link', { name: 'Windows GUIs' })
32+
await expect(windowsGUIs).toBeVisible()
33+
await expect(windowsGUIs).toHaveAttribute('href', /\/download\/gui\/windows$/)
34+
35+
// navigate to Windows GUIs
36+
await windowsGUIs.click()
37+
const windowsButton = page.getByRole('link', { name: 'Windows' })
38+
await expect(windowsButton).toBeVisible()
39+
await expect(windowsButton).toHaveClass(/selected/)
40+
41+
const allButton = page.getByRole('link', { name: 'All' })
42+
await expect(allButton).not.toHaveClass(/selected/)
43+
44+
const thumbnails = page.locator('.gui-thumbnails li:visible')
45+
const count = await thumbnails.count()
46+
await allButton.click()
47+
await expect.poll(() => thumbnails.count()).toBeGreaterThan(count)
48+
})
49+
})
50+
51+
test.describe('macOS', () => {
52+
const userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 14_6_1) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.6 Safari/605.1.15'
53+
test.use({ userAgent })
54+
55+
test('download/GUI links', async ({ page, browserName }) => {
56+
await pretendPlatform(page, browserName, userAgent, 'Mac OS X')
57+
await page.goto(url)
58+
await expect(page.getByRole('link', { name: 'Download for Mac' })).toBeVisible()
59+
60+
await expect(page.getByRole('link', { name: 'Graphical UIs' })).toBeHidden()
61+
await expect(page.getByRole('link', { name: 'Mac GUIs' })).toBeVisible()
62+
})
63+
})
64+
65+
test.describe('Linux', () => {
66+
const userAgent = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:130.0) Gecko/20100101 Firefox/130.0'
67+
test.use({ userAgent })
68+
69+
test('download/GUI links', async ({ page, browserName }) => {
70+
await pretendPlatform(page, browserName, userAgent, 'Linux')
71+
await page.goto(url)
72+
await expect(page.getByRole('link', { name: 'Download for Linux' })).toBeVisible()
73+
74+
await expect(page.getByRole('link', { name: 'Graphical UIs' })).toBeHidden()
75+
await expect(page.getByRole('link', { name: 'Linux GUIs' })).toBeVisible()
76+
})
77+
})
78+
579
test('search', async ({ page }) => {
680
await page.goto(url)
781

0 commit comments

Comments
 (0)