Test an Electron app
Electron is the Browser library plus keywords to start, switch and close Electron applications. It contains the Browser keywords that apply to applications and takes the same import arguments. The windows of an application are ordinary Browser pages.
Installation
Section titled “Installation”robotframework-electron needs Python 3.11 or later. It brings the Browser library, 21.0.0 or later, and with it Robot Framework, 7.1.1 or later.
With Node.js, rfbrowser init completes the installation of the Browser library:
pip install robotframework-electronrfbrowser init --skip-browsersWithout Node.js, Browser’s BrowserBatteries take its place:
pip install robotframework-electron "robotframework-browser[bb]"An application brings its own Electron, so this is all that testing an application needs. Two things need more:
| For | With Node.js | Without Node.js |
|---|---|---|
| Recording videos | rfbrowser init chromium-headless-shell |
rfbrowser install --only-shell chromium |
| A visible web browser beside an application | rfbrowser init chromium |
rfbrowser install chromium |
First test
Section titled “First test”Import Electron instead of Browser, start the application, and use Browser keywords on its window:
*** Settings ***Library Electron
*** Test Cases ***Greeting Is Shown Start Electron Application /opt/my-app/my-app Click text=Say hello Get Text id=greeting == Hello! Close Electron ApplicationAn app from source
Section titled “An app from source”An app that runs on the plain Electron binary gets its app folder as an argument. The npm package electron provides the binary in the version that the app needs, and in Node, require('electron') returns its executable:
npm install --save-dev electron@44.7.0node -p "require('electron')"The package needs Node.js 22.12 or later and downloads the binary on first use, into the user’s cache directory, or into the folder that the environment variable electron_config_cache names. The library downloads nothing itself. A variable in robot.toml holds the executable’s path, on Linux node_modules/electron/dist/electron:
[variables]ELECTRON_EXECUTABLE = "node_modules/electron/dist/electron"*** Settings ***Library Electron
*** Test Cases ***App From Source Start Electron Application ${ELECTRON_EXECUTABLE} args=${{ [$EXECDIR + "/app"] }}Windows and closing
Section titled “Windows and closing”Start Electron Applicationreturns the application id and the details of the first window’s page:${app} ${page} = Start Electron Application ….Switch Window title=<title>switches to a window that the app opens, once the window shows the title.Get Window Titleslists the titles of the windows.Set Window Size,Maximize Window,Minimize WindowandRestore Windowsize a window and change its state, andGet Window SizeandGet Window Statereturn them. Maximising and minimising need a window manager, see CI and the Linux display.Close Electron Applicationand Browser’s automatic closing end the application.- Several applications can be open at once.
Switch Electron Applicationswitches between them. - The library hides Browser’s keywords for browsers and contexts, such as
New PageandClose Browser. Browser keywords in applications compares the two libraries.
The main process
Section titled “The main process”Browser keywords reach the windows, but not the application’s main process. Evaluate In Main Process runs a JavaScript function there. The function gets the electron module, the keyword’s arg, and the BrowserWindow that shows the active page.
Native dialogs, such as file dialogs and message boxes, are outside the page, so no Browser keyword can click them. Replace them before the application opens them:
Evaluate In Main Process ({ dialog }, path) => {... dialog.showOpenDialog = async () => ({ canceled: false, filePaths: [path] });... }... arg=${EXECDIR}/data/report.csvClick text=Open reportshowSaveDialog and showMessageBox are replaced the same way. The replacement takes effect when the application calls the dialog through the module, as in dialog.showOpenDialog(…), and lasts until the application ends.
The function can also read the application’s state:
${version} = Evaluate In Main Process ({ app }) => app.getVersion()Windows have keywords of their own for their size and state, see windows and closing.
Launch options
Section titled “Launch options”Start Electron Application takes some of the options that the Browser library’s New Context has for a web browser. They apply from the start, so another value means starting the application again.
Start Electron Application /opt/my-app/my-app color_scheme=dark locale=pt-BR timezone_id=Asia/Tokyocolor_scheme: by default, the application decides its color scheme, as it does outside of tests. Withlightordark, its windows report that color scheme forprefers-color-scheme. Set it for tests that compare screenshots, so that the application looks the same on every machine.localeandtimezone_id: they set the language that the windows report, their number and date formats, and their time zone. By default, they follow the system, so a test run on your machine can differ from CI. They do not reach the main process: there,--langinargssets the locale andTZinenvthe time zone.bypass_csp: the windows ignore their Content Security Policy, including Trusted Types. Browser features that add a script to the page need it in applications with a strict policy, for exampleRecord Selectorin VS Code. An application can load its first window before the option takes effect, soStart Electron Applicationloads the windows that are open once more. It is off by default, because it also switches off the policy of webviews and frames, and a test would then miss policy problems of the application.
Next steps
Section titled “Next steps”- Browser keywords in applications for what works on an application’s windows and what does not.
- Traces and HAR files and Videos and slow motion for recording what a test did.
- CI and the Linux display for running without a desktop.
- Logs and troubleshooting if an app does not start.
- Keyword reference for every keyword of the library, including those that come from the Browser library.