You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Feature] Allow keyboard methods to be OS agnostic #12168
// on Windows and Linux
await page.keyboard.press('Control+A');
// on macOS
await page.keyboard.press('Meta+A');
The script should not have to implement special handling to ensure that these methods work in the expected manner across each OS, the method should allow something like
await page.keyboard.press('ControlOrMeta+A');
and the method should handle the OS detection transparently.
Reacted by Andy Delso, Timothee Guerin, canopy-js-user, Jamie Birch, Jason Axelson, htbkoo, Michael Render, Yusuf Canbaz, Nicolas Olivares, Matthieu Izoulet and 29 more
I just ran into something that should be taken into account when implementing this change - you can't just look at process.platform to figure out which key to use - we're now executing remotely to BrowserStack, running from linux on our CICD agent but the browser might be running on Mac or Windows - you need to query the browser -
async getBrowserOs(): Promise<string> {
let OSName: string;
let response: string = String(
await this._page.evaluate(() => {
return navigator.userAgent;
})
);
if (response.indexOf('Win') != -1) OSName = 'Windows';
if (response.indexOf('Mac') != -1) OSName = 'MacOS';
if (response.indexOf('X11') != -1) OSName = 'Unix';
if (response.indexOf('Linux') != -1) OSName = 'Linux';
return OSName;
}
First of all, yes this would make the testing more seamless so +1 for this addition or similar.
As to your followup, I'm not sure if the browser check is correct as the commands might be coming from the OS. For instance I'm trying to copy some text then paste them back. To do the paste I use keyboard.press('${modifier}+KeyV') which depending on the userAgent will result in either Meta or Control.
Running this test on the 3 browsers (FF, Chromium, Webkit) the user agent returns Windows for FF and Chromium, and Mac for Webkit. So for FF and Chrome it will pick Control as the modifier, however running these tests locally on my Mac that is incorrect. It requires it to be Meta for all 3 cases as this is a System clipboard not a browser one.
So this may be further complicating how this could be implemented.
// before the tests
const os = require('os');
let platform = os.platform();
let systemNewTabKey;
if (platform === 'darwin') {
systemNewTabKey = 'Meta';
} else {
systemNewTabKey = 'Control';
}
// in the test
page.keyboard.press(`${systemNewTabKey}+Enter`)
Activity
MartinP-Autopilot commentedon Mar 3, 2022
I just ran into something that should be taken into account when implementing this change - you can't just look at process.platform to figure out which key to use - we're now executing remotely to BrowserStack, running from linux on our CICD agent but the browser might be running on Mac or Windows - you need to query the browser -
Nithos commentedon Jul 27, 2022
First of all, yes this would make the testing more seamless so +1 for this addition or similar.
As to your followup, I'm not sure if the browser check is correct as the commands might be coming from the OS. For instance I'm trying to copy some text then paste them back. To do the paste I use
keyboard.press('${modifier}+KeyV')which depending on the userAgent will result in eitherMetaorControl.Running this test on the 3 browsers (FF, Chromium, Webkit) the user agent returns Windows for FF and Chromium, and Mac for Webkit. So for FF and Chrome it will pick
Controlas the modifier, however running these tests locally on my Mac that is incorrect. It requires it to beMetafor all 3 cases as this is a System clipboard not a browser one.So this may be further complicating how this could be implemented.
canopy-js-user commentedon Aug 11, 2022
Did anyone find a workaround for this in the meantime? Thanks for making the original suggestion.
nico-olivares commentedon Sep 9, 2022
This was my solution. It worked for me.
canopy-js-user commentedon Sep 9, 2022
Similar solution:
canbax commentedon Feb 20, 2023
Nicolas Olivares (@nico-olivares) canopy-js-user aren't your answers fall into the trap MartinP-Autopilot mentioned? His solution seems more robust IMHO. BTW MartinP-Autopilot 's answer give false results on MacOS m1 chips in Playwright.
feat(ctrl_or_meta): add a universal ctrl-meta modifier (#30572)