detect Browser and OS details using Playwright
Playwright is a Node library to automate Chromium, Firefox, and WebKit with a single API. As Playwright has already reached its stable version, it is a high time to re-think the possibilities of introducing it in your end-to-end test automation journey. Talking about automation, quite often you need to detect the browser and OS settings on the go to handle different logics for different browser settings. Let’s see how you can detect the settings run time.
Basically you need to fetch userAgent
information from the executor machine.
ua-parser-js
is a JavaScript-based User-Agent string parser. You need to add it as a dependency in your project.
npm i --save-dev ua-parser-js
Now, simply you can fetch the user agent information like:
const uaParser = require('ua-parser-js');const getUA = await page.evaluate(() => navigator.userAgent);const userAgentInfo= uaParser(ua);// get browser nameconsole.log(userAgentInfo.browser.name);
You can now parse the userAgentInfo
to fetch browser and OS information. Below script will give you an idea.