Using Spectron for Electron Apps
Published: 2021-01-20 11:31:46

---
title: Using Spectron for Electron Apps
published: true
description: The Speed at which Technologies and companies are growing today are quite unfathomable. We have come a long way from the digital age of the 1970s to the info
tags: 
//cover_image:
---
<img src="https://dev-to-uploads.s3.amazonaws.com/i/1x0svmom4sariyts8okl.png">
The Speed at which Technologies and companies are growing today are quite unfathomable. We have come a long way from the digital age of the 1970s to the information age of the 21st century in such a short span of time. The rate at which technologies around the world are evolving things that we thought impossible is starting to take shape in reality. <a href="https://www.pcloudy.com/the-role-of-artificial-intelligence-in-transforming-devops/">Artificial Intelligence in DevOps</a>, Omnichannel communication through <a href="https://www.pcloudy.com/blogs/digital-testing-for-multi-experience-development-apps/">Multi Experience Development Platform</a> and interaction between devices to make lives easy is progressing daily.

Besides all the progress, building a set of applications that work on a desktop has been a challenge as it needs a different skill set. And honestly enough why would someone go through the pain of learning another language, frameworks and scale-up another learning curve to cater to applications for the browser environment? That’s where <a href="https://www.electronjs.org/">Electron Application</a> comes into play. With Electron, developers are able to bank on the skill sets they’ve already acquired to build applications that use the capabilities of the native desktop application.

<b>What is electron application?</b>
If you have used your desktop to send messages over WhatsApp, sent files on Slack or written code on Atom or Visual Studio Code, chances are you are already using an electron application. Applications designed to work on desktops that run on Windows, MacOS or Linux using the open source framework Electron is what can be defined as Electron Applications.

Now, what is <a href="https://www.electronjs.org/">Electron</a>, must be a question that you are dying to ask. Electron is a framework for creating native applications with web technologies like JavaScript, HTML, and CSS. The open-source project started by an engineer at Github has got the conversation going for many as developers are now able to focus on the core of the application instead of spending time on learning another new language or framework. Electron combines Node.js runtimes and Chromium content module to help you build desktop applications using Javascript.

Eg: Appium Desktop, WhatsApp Desktop, Slack Desktop, etc.

<b>What is the challenge we faced in terms of automating this application?</b>
Electron may have eased up the process of building the desktop application. However, testing these applications is a tedious and time consuming job if done manually. The solution to test such desktop applications is to automate the testing process. Now the traditional approach for automation of such applications would be the job for the Selenium Webdriver.

Unfortunately Selenium doesn’t support the applications that are built on the open source electron platform. Hence, there arose a need for another platform to automate tests for the applications built on Electron. Since the Electron community supports the Spectron Project, web developers can write integration tests for testing Electron applications.

<b>What is spectron:</b>
<a href="https://www.electronjs.org/spectron">Spectron</a> is an open source framework that is used to easily write integration and end-to-end tests for your Electron applications. Simulating User Input, navigating web pages are some of the capabilities that Spectron provides. It also sets up and tears down the applications and allows it to be test-driven remotely with full support for the Electron APIs. Since it is built on top of ChromeDriver and WebDriverIO, you can test the Electron applications on the same as well.

<b>Installation:</b>
npm install –save-dev spectron

The example test below verifies a few validations that are specific to the electron app:

Let’s use the Electron API demos app, which can be downloaded <a href="https://github.com/electron/electron-api-demos/releases">here</a>.

The first thing we notice is that the <a href="https://developers.google.com/web/tools/chrome-devtools/">Chrome developer tools</a> are already there.

<img src="https://lh4.googleusercontent.com/p0UVErX0AhPJpsRyBAmKx6Su7SWQ6bjf2CzvkkkpxrgDx5hBzxMAnWdmrp5HrLrO_wbfQIbWSY5wKz63ulcpVeRs4jJ2RydTXRpwBwNBO2Nyn0VcCvfzbrMKvTo_4vJz7GXTRRMe">
<b>Commands to Open Dev Tools on different OS</b>
Use alt+cmd+i for mac,
F12 on windows and
ctrl + shift + i on linux OS
Once you have opened the Dev Tools you can inspect elements and prepare the locators.

<b>How To Test an Electron app ?</b>
We can use mocha, standard, chai modules using spectron and javascript.

It’s required to pass application binary path in Application object.

<b>setup.js:</b> This JS file is the base file where “removeStoredPreferences” function will help you clear the cache and start the app freshly, setupApp function will help you to click on “Get Started” button when the element is visible.

<img src="https://lh6.googleusercontent.com/YmfLhivEqp5rbvL0u9YQLKrJCuoSt7uh0fFs0JAVdgVfc-qWgAZikGze89nD8rAbX9_ztOsXgXnhR_TkaoliOf1fdIDayTASKwtLAhUx6BXfe9rXVttlM-yn88uP35RlBVaUeaZG">
<img src="https://lh3.googleusercontent.com/Dn3-_F5l68W36WB4EH1xGf7ppbwnLb83XpT9_GNQKP6HYZB7RvcE_P59vcvD6yBB70_ccJMjkpofrVSbg_Kzqv7p4cv3TWbkJwbYwrqp9hJJMoPt_f5urH0mCZOvI2s1DRqeK2o4">

<b>index.js:</b> This is an actual test file where some of the hooks like “before” and “after” have been used to start & close the app along with the “it” block where the actual test cases has been written.

<img src="https://lh5.googleusercontent.com/cyIS0w91skAw8IBeYvWHnVHNTgD8YkFXPXiB2zFyNb8enrxR6S8qfwcIjla5dDSakVfwmwqptykFmXf-YNQ_zkYRI0N4MNSPQTFJAkwurZvOhwpeKO9GbGVYsAx_7kUHRM2jRdoD">
<img src="https://lh3.googleusercontent.com/ZfpwqzhVZSEg1k1lpHI5WAUyJuSvF-Zu9zP6yGRrRIQenEF4YFjF7i93sbZ1kYSz7N8vdWYfztCqouV4lpUaVzu4z1XrrXt1DYx-Tkpz6HR8n3IoE7-BNuhVbPEUGRJ9HCzn9XWr">

You can use <b>mocha && standard</b> command to run the above sample script

Once you have executed the command we can see the test running faster than other tools. You can also view that we have performed some validations like window count, isVisible, isFocused etc, which are specific to the Electron App. We have also performed a wait statement by using “waitForVisible” in the script to wait for an element to be visible. Some actions like click to click on the “Get Started” button have also been performed. The page below will show us what happens when we click on the “Get Started” button.
<a href="https://lh5.googleusercontent.com/LwI5vIL03_dwpRm6e9H7MpdPzQtNIxjUo0AlzAieGvfX06XuvdCIR2nx2VOz6iQ5mEj09aPvHw5Br8pDyyYJICIz3c0uZXJMfdC66NP4YzXS-ts88i1utIexoLP0ba1kn260_vsL">

We have attached a link below to access the Spectron APIs – <a href="https://github.com/electron-userland/spectron#clientwindowbyindexindex">https://github.com/electron-userland/spectron#clientwindowbyindexindex</a>

We were also able to provide the link to a Sample Spectron Project that you can access and learn more about –<a href="https://github.com/electron/electron-api-demos">https://github.com/electron/electron-api-demos</a> (electron community official project)

With the sample project handy and the APIs so readily available, running projects is simple by using the steps below –

<b>Steps to Run the projects:</b>
<li>git clone <a href="https://github.com/electron/electron-api-demos">https://github.com/electron/electron-api-demos</a></li>
<li>cd electron-api-demos</li>
<li>npm install</li>
<li>npm test</li>

<b>Summary</b>
Spectron’s unique proposition to provide node modules to test Electron apps is going to be a game-changer to all those that depend on the open-source tools. And since there is a large community of developers that are already sharing ideas and best practices, the dearth of knowledge that we can use to build Electron Apps is never going to deplete and instead evolve from time to time. While the commercial tools have their set of advantages, open-source tools such as Spectron are worth exploring.