7

I'm using spectron to run integration tests against my electron app. Everything is working fine apart from attempting to test that app settings are persisted properly between app restarts.

While running tests, my app starts up with new temporary userData directory for every test which ensures that the tests are isolated. This means the the persistence testing needs to ideally occur in a single test and to achieve this I have to restart the app in the middle of the test. There's an app.restart method so this has got to be supported right?

I'm using the following spectron test code:

// save some settings here

await app.restart();
await app.client.waitUntilWindowLoaded()

// do some more checking to ensure the app is fully loaded
// check the settings here

However I'm getting the following error:

Error: waitUntilWindowLoaded Promise was rejected with the following reason: 
Error: A session id is required for this command but wasn't found in the response payload

What's the correct way to do this? I've also tried stopping the Application instance and starting a new one with similar results.

Tim
  • 6,485
  • 2
  • 42
  • 81
  • Where are you persisting userData? In the browserStorage, or in the local file system? – unseen_damage Mar 06 '17 at 19:38
  • `userData` is the electron path where the whole Chromium user app storage is saved. I'm using the electron `app.setPath('userData', x)` API to set this. Everything is stored there indexedDb, GPU cache, etc. I'm setting it to `path.join(os.tmpdir(), 'spectron', randomString)`. – Tim Mar 06 '17 at 19:44
  • Hm.. I haven't used that to persist anything. I have used electron-config, but perhaps you can use the set/get methods from `app.setLoginItemSettings([options])`to draw out your settings in each test in a beforeEach? Or maybe this [issue](https://github.com/electron/spectron/issues/16) has some relevance. – unseen_damage Mar 06 '17 at 19:55
  • The persistence is working fine. It's the spectron test code which is failing. – Tim Mar 06 '17 at 19:56
  • We had problems with app.restart() too. We have similar scenario. We do not use temp folder but rather normal folder with random name. After each test the folder is removed (aka afterAll). Also before all tests all the folders are delete just to be sure. And for app restart we are using simlpe app.stop , app.start , the app.start functions are written in a way that the application starts correctly so it doesn't matter if it was running before or this is the first start of the day. With this we don't have any problems anymore. – pagep Mar 19 '17 at 11:12

2 Answers2

0

This appears to work

// save some settings here

await app.stop();

app = new Application({ ... });
await app.start();
await app.client.waitUntilWindowLoaded();

// do some more checking to ensure the app is fully loaded
// check the settings here
Tim
  • 6,485
  • 2
  • 42
  • 81
0

Following snippsets works,

import test from 'ava'
import util from '../util'

test(async t => {
  // This runs after each test and other test hooks, even if they failed
  let app = util.createApp()
  app = await util.waitForLoad(app, t)
  await app.restart()
  await app.client.waitUntilWindowLoaded()
  // app = await util.waitForLoad(app, t)
})

works with, "spectron": "^3.5.0"

Necmttn
  • 769
  • 6
  • 15