BrowserProvider Interface

To create a browser provider, implement the following methods.

Note

While implementing the methods, you can utilize the testcafe-browser-tools library that contains methods for performing platform-dependent actions on browser windows. To do this, add this library as a dependency into your browser provider project.

Required Methods

openBrowser

Runs the specified browser and opens the specified page in it.

async openBrowser ( id, pageUrl, browserName )
Parameter Type Description
id String A unique identifier generated by TestCafe that you can use to identify a browser window. The identifier is included to the window title exposed using the document.title property.
pageUrl String The url of the page to open.
browserName String The browser name.

See Example.

closeBrowser

Closes the specified browser.

async closeBrowser ( id )
Parameter Type Description
id String A unique identifier generated by TestCafe that you can use to identify a browser window.

See Example.

Optional Methods

init

Performs initialization actions the provider may require. This method is called once before other provider methods are called.

async init ()

For example, the Nightmare browser provider uses this method to create a Nightmare instance.

dispose

Performs cleanup actions for the provider data. This method is called before the testcafe process is finished.

async dispose ()

getBrowserList

Returns the array of names used to identify the browsers. This method is used if the provider supports multiple browsers.

async getBrowserList ()

See Example.

isValidBrowserName

Checks if the specified browser name exists. This method is used if the provider supports multiple browsers.

async isValidBrowserName ( browserName )
Parameter Type Description
browserName String The browser name to validate.

Returns true if the specified name exists, or false otherwise.

See Example.

resizeWindow

Resizes the browser window’s client area to the specified width and height.

async resizeWindow ( id, width, height, currentWidth, currentHeight )
Parameter Type Description
id String A unique identifier generated by TestCafe that you can use to identify a browser window.
width Number The width to resize the browser window’s client area to, in pixels.
height Number The height to resize the browser window’s client area to, in pixels.
currentWidth Number The actual width of the browser window’s client area, in pixels. For example, you can use this parameter to calculate the current width of the browser’s utility area that contains menus, toolbars, etc. This can be useful when you need to resize the whole browser window.
currentHeight Number The actual height of the browser window’s client area, in pixels. For example, you can use this parameter to calculate the current height of the browser’s utility area that contains menus, toolbars, etc. This can be useful when you need to resize the whole browser window.

See Example.

maximizeWindow

Maximizes the browser window.

async maximizeWindow ( id )
Parameter Type Description
id String A unique identifier generated by TestCafe that you can use to identify a browser window.

See Example.

canResizeWindowToDimensions

Checks if the browser window can be resized to the specified dimensions.

async canResizeWindowToDimensions ( id, width, height )
Parameter Type Description
id String A unique identifier generated by TestCafe that you can use to identify a browser window.
width Number The desired width of the browser window’s client area, in pixels.
height Number The desired height of the browser window’s client area, in pixels.

Returns true if the window can be resized to the specified width and height, or false otherwise.

See Example.

takeScreenshot

Takes a screenshot of the browser window’s client area.

async takeScreenshot ( id, screenshotPath, pageWidth, pageHeight )
Parameter Type Description
id String A unique identifier generated by TestCafe that you can use to identify a browser window.
screenshotPath String The directory path to save the screenshots to.
pageWidth Number The actual width of the web page opened in the browser, in pixels.
pageHeight Number The actual height of the web page opened in the browser, in pixels.

See Example.