Page -> Emulation.setOrientationOverride

This commit is contained in:
Max Schmitt 2024-06-11 21:25:41 +02:00
parent af34968d24
commit 9db325d790
2 changed files with 15 additions and 1 deletions

View file

@ -16,6 +16,7 @@
*/
import path from 'path';
import os from 'os';
import { PNG, jpegjs } from '../../utilsBundle';
import { splitErrorMessage } from '../../utils/stackTrace';
import { assert, createGuid, debugAssert, headersArrayToObject } from '../../utils';
@ -713,7 +714,9 @@ export class WKPage implements PageDelegate {
];
if (options.isMobile) {
const angle = viewportSize.width > viewportSize.height ? 90 : 0;
promises.push(this._session.send('Page.setOrientationOverride', { angle }));
// Special handling for macOS 12.
const useLegacySetOrientationOverrideMethod = parseInt(os.release().split('.')[0], 10) <= 21;
promises.push(this._pageProxySession.send(useLegacySetOrientationOverrideMethod ? 'Page.setOrientationOverride' as any : 'Emulation.setOrientationOverride', { angle }));
}
await Promise.all(promises);
}

View file

@ -15,6 +15,7 @@
* limitations under the License.
*/
import { devices } from '@playwright/test';
import { contextTest as it, expect } from '../config/browserTest';
import { browserTest } from '../config/browserTest';
import { verifyViewport } from '../config/utils';
@ -175,3 +176,13 @@ browserTest('should be able to get correct orientation angle on non-mobile devic
expect(await page.evaluate(() => window.screen.orientation.angle)).toBe(0);
await context.close();
});
it('should set window.screen.orientation.type for mobile devices', async ({ contextFactory, browserName, server }) => {
it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/31151' });
it.skip(browserName === 'firefox', 'Firefox does not support mobile emulation');
const context = await contextFactory(devices['iPhone 14']);
const page = await context.newPage();
await page.goto(server.PREFIX + '/index.html');
expect(await page.evaluate(() => window.screen.orientation.type)).toBe('portrait-primary');
await context.close();
});