This is the electron browser provider plugin for TestCafe.
npm install testcafe-browser-provider-electron
First, create a .testcafe-electron-rc
file in the root directory of your Electron app. For more info, see the Config section.
{
"mainWindowUrl": "./index.html"
}
When you run tests from the command line, specify the path to the Electron app prefixed with "electron:" -
testcafe "electron:/home/user/electron-app" "path/to/test/file.js"
When you use API, pass the app path to the browsers()
method:
testCafe
.createRunner()
.src('path/to/test/file.js')
.browsers('electron:/home/user/electron-app')
.run();
Below is a list of configuration options that you can specify in .testcafe-electron-rc
.
mainWindowUrl
(required) - specifies URL used for the main window page of the appplication. If you usefile://
urls, you can also specify a relative (to the application directory) or an absolute path to the file of the page.appPath
(optional) - alters path to the application. By default, the application path is specified after theelectron:
prefix in the string passed to the command line or API. You can override it by specifying an absolute path, or append a relative path fromappPath
.appArgs
(optional) - overrides application command line arguments with the values specified in this option. It should be an array or an object with numeric keys.disableNavigateEvents
(optional) - if you usedid-navigate
orwill-navigate
webContent events to prevent navigation, you should disable it by setting this option totrue
.openDevTools
(optional) - iftrue
, DevTools will be opened just before tests start.
You can use helper functions from the provider in your test files. Use ES6 import statement to access them.
import { getMainMenu, clickOnMenuItem } from 'testcafe-browser-provider-electron';
-
async function getMenuItem (menuItemSelector)
- gets a snapshot of the specified menu item.menuItemSelector
is a string that consists of the menu type and menu item labels, separated by the>
sign, e.g.Main Menu > File > Open
orContext Menu > Undo
. TheMain Menu
menu type can be skipped. If there are several menu items with the same label on the same level, you can specify a one-based index in square brackets, e.g.Main Menu > Window > My Window [2]
selects the second menu item with labelMy Window
in theWindow
menu. Check the properties available in the snapshot here. -
async function getMainMenu ()
- gets a snapshot of the application main menu. You can check properties available in the snapshot here. -
async function getContextMenu ()
- gets a snapshot of the context menu. You can check properties available in the snapshot here, -
async function clickOnMenuItem (menuItem, modifiers)
- performs a click on the givenmenuItem
. It can be a string, in this case it will be passed to thegetMenuItem
function and the returned value will be used; or a value retrieved withgetMenuItem
,getMainMenu
,getContextMenu
functions. Also, you can pass the state of the control keys (Shift
,Ctrl
,Alt
,Meta
) in themodifiers
argument, e.g. the default is{ shift: false, ctrl: false, alt: false, meta: false}
. Examples:clickOnMenuItem('Main Menu > File > Open')
,clickOnMenuItem('File > Open')
,clickOnMenuItem((await getMainMenu()).items[0].submenu.items[0])
, -
async function setElectronDialogHandler (handler, dependencies)
- sets a functionfunction handler (type, ...args)
that will handle native Electron dialogs. Specify the function's global variables in thedependencies
argument. The handler function must be synchronous and will be invoked with the dialog typetype
, and the argumentsargs
from the original dialog function.
Developer Express Inc. (https://devexpress.com)