spectron icon indicating copy to clipboard operation
spectron copied to clipboard

webContents is missing part of its API

Open dcposch opened this issue 9 years ago • 7 comments

Here, it says that app.webContents should give a handle to an Electron WebContents instance: https://github.com/electron/spectron#webcontents

That section also links to the full webContents API, documented here: http://electron.atom.io/docs/api/web-contents/

However, when I use Spectron, I'm seeing that only a subset of the webContents API works.

For example, app.webContents.getTitle() works, but app.webContents.executeJavascript() doesn't work. session is also missing.

  1. Why is only a subset of the WebContents API available?
  2. Is there any easy way to run Javascript in the context of the renderer? In other words, an alternative to executeJavascript

dcposch avatar Sep 09 '16 06:09 dcposch

  1. executeJavascript is a typo. The API is actually executeJavaScript
  2. See the answer to question 1 👍

MarshallOfSound avatar Sep 09 '16 06:09 MarshallOfSound

@MarshallOfSound thanks. Wow I'm a tool

What about webContents.session ?

dcposch avatar Sep 09 '16 07:09 dcposch

@dcposch Currently we are only mapping methods on the webContents object to Spectron methods.

See https://github.com/electron/spectron/blob/master/lib/api.js#L150

@kevinsawicki Should we add something like getProperty('session') to return the session object?

MarshallOfSound avatar Sep 09 '16 08:09 MarshallOfSound

Should we add something like getProperty('session') to return the session object?

Yeah, would be nice to support important properties

kevinsawicki avatar Sep 09 '16 16:09 kevinsawicki

Is there an appropriate work around for accessing properties of the webContents object?

It might also be sensible at some-point to document the fact that it only maps the methods and not the properties.

strideynet avatar Aug 28 '18 12:08 strideynet

I am also in need for access to webContents.session. Are there any news on that?

webD97 avatar Jan 11 '19 07:01 webD97

If you want to clear localStorage data this works:

await app.webContents.executeJavaScript('localStorage.clear()');
app.webContents.reload();

sytolk avatar Dec 14 '20 12:12 sytolk