Socket
Socket
Sign inDemoInstall

spectron

Package Overview
Dependencies
Maintainers
1
Versions
66
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

spectron

Easy ChromeDriver tests for Electron apps


Version published
Weekly downloads
6.3K
decreased by-5.61%
Maintainers
1
Weekly downloads
 
Created
Source

spectron

js-standard-style Build Status devDependencies:?
license:mit npm: dependencies:?

Easily test your Electron apps using ChromeDriver and WebdriverIO.

This minor version of this library tracks the minor version of the Electron versions released. So if you are using Electron 0.33.x you would want to use a spectron dependency of ^0.33 in your package.json file.

Using

npm --save-dev spectron

Spectron works with any testing framework but the following example uses mocha:

var Application = require('spectron').Application
var assert = require('assert')

describe('application launch', function () {
  this.timeout(10000)

  beforeEach(function () {
    this.app = new Application({
      path: '/Applications/MyApp.app/Contents/MacOS/MyApp'
    })
    return this.app.start()
  })

  afterEach(function () {
    return this.app.stop()
  })

  it('shows an initial window', function () {
    return this.app.client.getWindowCount().then(function (count) {
      assert.equal(count, 1)
    })
  })
})

With Chai As Promised

WebdriverIO is promise-based and so it pairs really well with the Chai as Promised library that builds on top of Chai.

Using these together allows you to chain assertions together and have fewer callback blocks. See below for a simple example:

npm install --save-dev chai
npm install --save-dev chai-as-promised
var Application = require('spectron').Application
var chai = require('chai')
var chaiAsPromised = require('chai-as-promised')
var path = require('path')

chai.should()
chai.use(chaiAsPromised)

describe('application launch', function () {
  beforeEach(function () {
    this.app = new Application({
      path: path: '/Applications/MyApp.app/Contents/MacOS/MyApp'
    })
    return this.app.start()
  })

  beforeEach(function () {
    chaiAsPromised.transferPromiseness = this.app.client.transferPromiseness
  })

  afterEach(function () {
    return this.app.stop()
  })

  it('opens a window', function () {
    return this.app.client.waitUntilWindowLoaded()
      .getWindowCount().should.eventually.equal(1)
      .isWindowMinimized().should.eventually.be.false
      .isWindowVisible().should.eventually.be.true
      .isWindowFocused().should.eventually.be.true
      .getWindowWidth().should.eventually.be.above(0)
      .getWindowHeight().should.eventually.be.above(0)
  })
})

Application

new Application(options)

Create a new application with the following options:

  • path - String path to the application executable to launch. Required
  • args - Array of Chrome arguments to pass to the executable. See here for more details.
  • env - Object of additional environment variables to set in the launched application.
  • host - String host name of the launched chromedriver process. Defaults to 'localhost'.
  • port - Number port of the launched chromedriver process. Defaults to 9515.
  • quitTimeout - Number in milliseconds to wait for application quitting. Defaults to 1000 milliseconds.
start()

Starts the application. Returns a Promise that will be resolved when the application is ready to use. You should always wait for start to complete before running any commands.

stop()

Stops the application. Returns a Promise that will be resolved once the application has stopped.

Client Commands

Spectron uses WebdriverIO and exposes the managed client property on the created Application instances.

The full client API provided by WebdriverIO can be found here.

Several additional commands are provided specific to Electron.

getWindowCount()

Gets the number of open windows.

app.client.getWindowCount().then(function (count) {
  console.log(count)
})
getWindowDimensions()

Gets the dimensions of the current window. Object returned has x, y, width, and height properties.

app.client.getWindowDimensions().then(function (dimensions) {
  console.log(dimensions.x, dimensions.y, dimensions.width, dimensions.height)
})
getWindowHeight()

Get the height of the current window.

app.client.getWindowHeight().then(function (height) {
  console.log(height)
})
getWindowWidth()

Get the width of the current window.

app.client.getWindowWidth().then(function (width) {
  console.log(width)
})
isWindowDevToolsOpened()

Returns whether the current window's dev tools are opened.

app.client.isWindowDevToolsOpened().then(function (devToolsOpened) {
  console.log(devToolsOpened)
})
isWindowFocused()

Returns whether the current window has focus.

app.client.isWindowFocused().then(function (focused) {
  console.log(focused)
})
isWindowFullScreen()

Returns whether the current window is in full screen mode.

app.client.isWindowFullScreen().then(function (fullScreen) {
  console.log(fullScreen)
})
isWindowLoading()

Returns whether the current window is loading.

app.client.isWindowLoading().then(function (loading) {
  console.log(loading)
})
isWindowMaximized()

Returns whether the current window is maximized.

app.client.isWindowMaximized().then(function (maximized) {
  console.log(maximized)
})
isWindowMinimized()

Returns whether the current window is minimized.

app.client.isWindowMinimized().then(function (minimized) {
  console.log(minimized)
})
isWindowVisible()

Returns whether the current window is visible.

app.client.isWindowVisible().then(function (visible) {
  console.log(visible)
})
setWindowDimensions(x, y, width, height)

Sets the window position and size.

app.client.setWindowDimensions(100, 200, 50, 75)
waitUntilTextExists(selector, text, [timeout])

Waits until the element matching the given selector contains the given text. Takes an optional timeout in milliseconds that defaults to 5000.

app.client.waitUntilTextExists('#message', 'Success', 10000)
waitUntilWindowLoaded([timeout])

Wait until the window is no longer loading. Takes an optional timeout in milliseconds that defaults to 5000.

app.client.waitUntilWindowLoaded(10000)

Keywords

FAQs

Package last updated on 15 Oct 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc