Testing nwjs apps using Cypress?

172 views
Skip to first unread message

Murray Hopkins

unread,
Apr 21, 2019, 12:57:11 AM4/21/19
to nw.js
Hi,

Cypress is an Electron app that is used to test "anything that runs un a browser". The authors use Cypress to test their Electron app, although it does not "just run" to test Electron apps out of the box and it seems to need a fair bit of fiddling to make it work. Cypress does have a node "module API" and says "You can require Cypress as a Node module."

So, before I spend too much time on this, has anyone had experience using Cypress to test their NW.js app? Either just code that would run without node.js and/or to test code that uses node.js modules.

Thanks,
Murray


jap...@gmail.com

unread,
Apr 25, 2019, 7:23:46 PM4/25/19
to nw.js
Hi Murray,

I am also interested in the answer to your question about whether it is possible to use Cypress.io to exercise the DOM inside an NW.js app.

May I ask what framework or set of ecosystem tools you currently use?

Thanks!

Murray Hopkins

unread,
Apr 26, 2019, 7:33:03 PM4/26/19
to nw.js
Hi. Thanks for your reply.

I use nw.js for business applications and am much more interested in testing the business logic than the UI. So, I wanted to test the javascript in the app, rather than the DOM. The app JS has functions that call node.js modules (eg to access the file system). The frameworks vary (ExtJS, Vue, whatever).

I was using the cypress-cucumber-preprocessor to run Gherkin since I am using BDD. (BTW: that preprocessor converts Gherkin to Chai that Cypress then runs. Despite the name it does not actually run cucumber-js)

After some testing I have abandoned this idea. What it comes down to is best expressed in the following code snippet. This function exists in the nw.js app I was  attempting to test using Cypress.

In short, for this to work, Cypress needs an extension to run an nw.js "browser" rather than Electron or Chrome. I have no idea how to do that or even if it could work.

I am now going back to working on a way to run cucumber-js within nw.js which is also not as easy as it would seem (mostly due to my lack of skill, I suspect), although I am making some progress and will post the results here: https://groups.google.com/d/topic/nwjs-general/SKuVu9cKryg/discussion

function runTest() {




    const cypress = require('cypress');




        // Note: This seems to need v37.4 of nw.js.


        // Maybe a few earlier but v35 crashed.




        // Throws: Error [ERR_UNKNOWN_STDIN_TYPE]: Unknown stdin file type


        // in v35.


        // It seems this is an error cause by NW.js stdin being disabled.


        // https://github.com/nwjs/nw.js/issues/586


        // But, seems to work in v37.4




    cypress.run({


            // The issue is here:


            // We need to be able to run the nw.js chromium browser in nw.js


            // inorder to test your app that is written for that environment.




            // The default browser used by Cyress is the built-in Electron browser.


            // You can also specify 'chrome' which launches a window/tab in the normal Chrome browser app.


            // Both these options would probably work if you did not use node.js in your app. But if that


            // is the case then there is no point running Cypress within nw.js. You could just run it as


            // the normal standalone way it was intended.


            // I guess it *could* be possible to customise Cypress to add the option to run the nw.js 'browser'


            // but I have no idea how to go about that, even if it is possible.


        browser: 'electron',


        env : {


            // Pass the nw browser window so the tests


            // can access the global scope and hence your app


            nwWin: nw.Window.get()


        },




            // The spec to run. Yes, it does run but even with the env set it to the nw window


            // it is *executing the test* in an Electron browser, not a nw.js browser. Of course.


        spec: './cypress/integration/nwjs_spec_1.js'


    })


    .then((results) => {


                    // We get here so output the result


        console.log("My Results=" + results.runs[0].tests[0].state)


        console.table(results.runs[0].tests[0])


    })


    .catch((err) => {


        console.error("My Error",err)


    })




}



Reply all
Reply to author
Forward
0 new messages