Giter Site home page Giter Site logo

Comments (4)

kevinsawicki avatar kevinsawicki commented on July 24, 2024 1

Since Spectron is a framework to help with testing, then it would make sense to show a test on its main page :)

Spectron is test framework agnostic so I'd like to keep an example on that page that doesn't assume any specific library like mocha, jasmine, etc.

The current example shows the basic start/stop flow, promise chaining, and node's built-in asserts so it should be able to get you up and going testing your app without worrying about which framework to pick and how to integrate it.

I'm going to close this out. I think it makes sense to have lots of examples in the readme for different framework, but for the main marketing page I think it is better to show an example using only node built-ins.

from spectron.

DinisCruz avatar DinisCruz commented on July 24, 2024

For example here is an simple API wrapper for spectron

Spectron_API

require 'fluentnode'

spectron = require 'spectron'

class Spectron_API

  constructor: (options)->
    @.options     = options || {}
    @.Application = spectron.Application
    @.app         = null
    @.root_Path   = wallaby?.localProjectDir || __dirname.path_Combine '../'        

  is_Running: =>
    @.app?.isRunning() || false

  setup: =>
    @.options.path = @.root_Path.path_Combine 'node_modules/.bin/electron'
    @.options.args = [ __dirname.path_Combine '../electron-apps/web-view' ]
    @.app          = new @.Application @.options
    @

  start: =>
    @.app.start()

  stop: =>
    @.app.stop()

module.exports =   Spectron_API

Spectron-API.test

Spectron_API = require '../../src/Spectron-API'

describe 'constructor',->
    it 'constructor', ->
      using new Spectron_API(), ->
        @.options    .assert_Is {}
        @.Application.assert_Is_Function()
        @.root_Path  .assert_Folder_Exists()
                     .path_Combine('node_modules').assert_Folder_Exists()
        assert_Is_Null @.app

    it 'isRunning', ->
      using new Spectron_API(), ->
        @.is_Running().assert_Is_False()

    it 'setup', ->
      using new Spectron_API(), ->
        @.setup().assert_Is @
        using @.app, ->
          @.host                .assert_Is '127.0.0.1'
          @.port                .assert_Is 9515
          @.quitTimeout         .assert_Is 1000
          @.startTimeout        .assert_Is 5000
          @.waitTimeout         .assert_Is 5000
          @.connectionRetryCount.assert_Is 10
          @.nodePath            .assert_Is process.execPath
          @.env                 .assert_Is {}
          @.workingDirectory    .assert_Is __dirname.path_Combine('../..')
          @.api.app             .assert_Is @
          @.api.requireName     .assert_Is 'require'
          @.transferPromiseness .assert_Is_Function()

          @.path.assert_File_Exists()
                .assert_Contains '.bin/electron'
          @.args.assert_Size_Is(1).first()
                .assert_Folder_Exists()
                .assert_Contains 'electron-apps/web-view'


    it 'start and stop', ()->
      @.timeout 4000
      using new Spectron_API().setup(), ->
        @.start().then =>
          @.is_Running().assert_Is_True()
          @.stop()

    # other tests

    it 'expected files inside electron-apps/web-view folder', ->
      using new Spectron_API().setup(), ->
        app_Folder = @.options.args.first()
        app_Folder.assert_Folder_Exists().assert_Contains '/electron-apps/web-view'
                  .files().file_Names()  .assert_Contains ['index.html', 'main.js', 'package.json', 'web-view.html']

    it 'check title', ->
      @.timeout 4000
      using new Spectron_API().setup(), ->
        @.start().then =>
          @.app.client.getTitle().then (title)=>
            title.assert_Is 'Electron App - with WebView'
            @.stop()    

index.html

<!DOCTYPE html>
<html>
  <head>
    <title>Electron App - with WebView</title>
    <style>
      html, body {
        width: 100%;  height: 100%;  margin: 0;  padding: 0;
      },
      webview {
        display: block;  border: none;  height: 80vh;   width: 95vw;
      }
    </style>
    <script>
    </script>
  </head>
  <body style="background-color:lightslategray">
    <webview id='google' src="http://www.google.com"  style='height:100vh; width:110vw' autosize="on" ></webview>
  </body>
</html>

main.js

var app = require('electron').app
var BrowserWindow = require('electron').BrowserWindow

var mainWindow = null

app.on('ready', function () {
  mainWindow = new BrowserWindow({
    show      : false,
    center    : true,
    width     : 800,
    height    : 400,
    minHeight : 100,
    minWidth  : 100
  })
  mainWindow.loadURL('file://' + __dirname + '/index.html')
  mainWindow.on('closed', function () { mainWindow = null })
})

from spectron.

DinisCruz avatar DinisCruz commented on July 24, 2024

For reference here are the tests above running in travis
image

from spectron.

DinisCruz avatar DinisCruz commented on July 24, 2024

ok @kevinsawicki fair enough

from spectron.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.