t.browser Property
Returns information about the active browser, its user agent string, and the current automation mode.
t.browser → Object
import { Selector } from 'testcafe';
fixture`TestController.browser`
.page`https://example.com`;
test('Check browser', async t => {
if (t.browser.name !== 'Chrome')
await t.expect(Selector('div').withText('Browser not supported').visible).ok();
});
t.browser exposes the following properties:
| Property | Type | Description | Example |
|---|---|---|---|
| alias | String | The browser alias string from launch settings | firefox:headless |
| name | String | Browser name | Chrome |
| version | String | Browser version | 77.0.3865.120 |
| platform | String | Browser platform | desktop |
| headless | Boolean | true if the browser is headless |
false |
| os | Object | Name and version of the OS | { name: 'macOS', version: '10.15.1' } |
| engine | Object | Name and version of the browser engine | { name: 'Gecko', version: '20100101' } |
| userAgent | String | User agent string | Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/77.0.3865.120 Safari/537.36 |
| prettyUserAgent | String | Formatted string with the name and version of the browser and operating system. | Chrome 77.0.3865.75 / macOS 10.14.0 |
| nativeAutomation | Boolean | true if TestCafe uses native automation |
false |
Properties
alias
The browser alias string specified on launch.
For local browsers, the property contains the short browser name:
{
alias: 'chrome'
}
For portable browsers, the property contains the path to the browser executable:
{
alias: 'path:C:\Program Files (x86)\Firefox Portable\firefox.exe'
}
The string includes browser mode suffixes (:headless, :emulation, etc.) and CLI parameters:
{
alias: 'chrome:headless --no-sandbox'
}
{
alias: 'firefox:headless:disableMultiprocessing=true'
}
If you use a browser provider plugin to access a cloud browser, the alias property includes browser settings:
{
alias: 'saucelabs:Samsung Galaxy S9 Plus WQHD GoogleAPI Emulator@8.1'
}
name
The short browser name.
{
name: 'Safari'
}
{
name: 'Google Chrome'
}
If TestCafe did not detect the browser on launch, the property value is 'Other'.
version
The browser version.
{
name: 'Chrome',
version: '77.0.3865.120'
}
{
name: 'Firefox',
version: '69.0'
}
If TestCafe cannot determine the browser version, the property value is '0.0'.
platform
The platform property indicates the kind of device that runs the browser. It has one of the following values:
desktopmobiletabletother(indicates other devices, or a failure to detect the device)
{
name: 'Firefox',
platform: 'mobile'
}
headless
The headless property indicates whether the browser is headless.
{
alias: 'chrome:headless',
name: 'Chrome',
headless: true
}
os
Contains the operating system’s name and version.
{
os: {
name: 'Windows',
version: '10'
}
}
If TestCafe cannot detect the operating system, the property’s value is 'Other'. The version parameter defaults to '0.0'.
engine
Contains the browser engine’s name and version.
{
engine: {
name: 'WebKit',
version: '605.1.15'
}
}
If TestCafe cannot detect the browser engine, the value of the engine.name property is 'Other'. The engine.version property defaults to '0.0'.
userAgent
The user agent string.
{
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/77.0.3865.120 Safari/537.36'
}
prettyUserAgent
Contains both the name and version of both the browser and the OS. TestCafe displays this string on the loading screen and in the status bar.
{
prettyUserAgent: 'Firefox 69.0 / Windows 10'
}
{
prettyUserAgent: 'Chrome 77.0.3865.120 / macOS 10.15.1'
}
If TestCafe cannot parse the browser’s user agent string, the property is empty.
nativeAutomation
The nativeAutomation property indicates whether TestCafe uses Native Automation to control the browser.
{
prettyUserAgent: 'Chrome 77.0.3865.120 / macOS 10.15.7',
nativeAutomation: true
}
To make sure that you always use your preferred automation method, check the browser’s native automation status before you start the test:
import { Selector } from 'testcafe';
fixture`TestController.browser`
.page`https://example.com`;
test('Native automation check', async t => {
await t.expect(t.browser.nativeAutomation).ok();
//the test continues only if you use native automation
});