Skip to content

Instantly share code, notes, and snippets.

@DinisCruz
Created May 19, 2016 17:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DinisCruz/060b427139bc2dd747a214d77dd1f970 to your computer and use it in GitHub Desktop.
Save DinisCruz/060b427139bc2dd747a214d77dd1f970 to your computer and use it in GitHub Desktop.
Testing wallbyjs electron integration, see https://twitter.com/DinisCruz/status/733343716195336193
chai = require 'chai'
expect = chai.expect
$ = require 'jquery'
#require 'electron-prebuilt'
describe 'testing in coffee', ->
# it 'Check nodeIntegration value', ()->
# # this works
# code = "BrowserWindow = require('electron').remote;
# BrowserWindow.shell.openItem('.');
# BrowserWindow.shell.openItem('/Applications/Calculator.app');"
#
# eval(code)
#
# # but this doesn't
# require('electron').remote
it 'open test', (done)->
new_Window = open('https://www.google.co.uk')
checkResponse = ->
expect(new_Window.location).to.contain 'https://www.google.co.uk'
expect(new_Window.eval).to.be.an('function')
done()
setTimeout checkResponse, 500
#@.timeout 900000
# code = "mainWindow = require('remote').getCurrentWindow();
# mainWindow.toggleDevTools();
# //mainWindow.setPosition(12,12);"
# eval(code)
# this only runs ok once
xit 'X-Frame-Options bypass changes', (done)->
# set the headers hook
code = "
mainWindow = require('remote').getCurrentWindow();
var onHeadersReceived = function(d, c)
{
if(d.responseHeaders['x-frame-options'])
{
d.responseHeaders['x-frame-options'] = null;
}
c({cancel: false, responseHeaders: d.responseHeaders});
};
mainWindow.webContents.session.webRequest.onHeadersReceived({}, onHeadersReceived);
/*mainWindow.on('closed', function() {
mainWindow.removeAllListeners();
mainWindow = null;
});*/
"
eval(code)
# add the iframe which would fail without the header's change
iframe = $("<iframe id='my_iframe' src='https://www.google.co.uk/'>abc</iframe>")
$(document.body).append(iframe)
checkResponse = ->
console.log url = document.querySelector('#my_iframe').contentDocument.location.href
expect(url).to.equal 'https://www.google.co.uk/'
done()
setTimeout checkResponse, 600
it 'global object', ()->
keys = (key for key of global).sort()
expect(keys.length).to.equal 223
interresting_Ones = (list)->
for item in list
expect(keys).to.contain item
list = ['WebView', 'caches', 'clientInformation', 'console', 'alert',
'applicationCache','close','closed','document','fetch','frames'
'history','location','moveTo','navigator','onclick','onerror','oninput','onkeydown'
'onload','onmessage','onpageshow','onprogress','onshow','onsubmit','open','opener'
'run','screen','screenX','self','status','window' ]
interresting_Ones list
it 'document.location.href ', ->
expect(document.location.href).to.contain 'wallaby_sandbox0.html' # this is usually at http://localhost:49957/wallaby_sandbox0.html
# console.log document.location.href
it 'document.body.innerHtml', ->
body = $(document.body)
expect(document.body.innerHTML).to.equal body.html() # confirm that jQuery is working ok
it 'window', () ->
expect(window).to.be.defined
window.localStorage.setItem('test', '123')
expect(window.localStorage.getItem('test')).to.equal '123'
xit 'document load', (done)->
document.onload = ->
console.log 'here'
done()
document.url = 'http:/www.google.com'
it 'jQuery get file from cdn', (done)->
$.get "https://code.jquery.com/jquery-2.2.3.js", (data)->
expect(data).to.contain('jQuery JavaScript Library v2.2.3');
expect(data.length).to.equal 258648
.done -> done()
# now works (due to webSecurity: false )
# it 'jQuery failed to get file', (done)->
# $.get "https://www.google.com", ()-> {}
# .fail (data)->
# expect(data.statusText).to.equal('error')
# expect(this.url ).to.equal 'https://www.google.com'
# expect(this.type ).to.equal 'GET'
# expect(this.contentType).to.equal 'application/x-www-form-urlencoded; charset=UTF-8'
# done()
it 'Adding h1 element to dom using jQuery',->
h1 = $("<h1 id='my-h1'>inside h1</h1>")
expect(h1.html() ).to.equal 'inside h1'
expect($('h1').length ).to.equal 0
$(document.body).append(h1)
expect($('h1' ).length).to.equal 1
expect($('h1' ).html()).to.equal 'inside h1'
expect($('#my-h1').html()).to.equal 'inside h1'
h1.html('value dynamically changed')
expect($('#my-h1').html()).to.equal 'value dynamically changed'
it 'Adding webview element to dom using jQuery (doesnt work)', (done)->
web_View = $("<webview id='my-webview' src='http://coffeescript.org/'>abc</webview>")
$(document.body).append(web_View)
checkResponse = ->
expect($('#my-webview').html()).to.equal 'abc' # I wanted this to be coffeescript.org
#console.log document.body.innerHTML
done()
setTimeout checkResponse , 1000
it 'Adding iframe element to dom using jQuery (works due to webSecurity: false)', (done)->
iframe = $("<iframe id='my-iframe' src='http://coffeescript.org/'>abc</iframe>")
$(document.body).append(iframe)
checkResponse = ->
html = iframe.contents().find('body').html()
expect(html).to.contain('coffee-script')
done()
setTimeout checkResponse , 201
# this now doesn't work
# it.only 'Adding iframe element to dom using jQuery (work, but throws security error)', (done)->
# iframe = $("<iframe id='my-iframe' src='http://coffeescript.org/'>abc</iframe>")
# $(document.body).append(iframe)
#
# checkResponse = ->
# expect($('#my-iframe').html()).to.equal 'abc' # I wanted this to be google
# #console.log document.body.innerHTML
# try
# console.log iframe.contents().find('body').html()
# catch ex
# expect(ex.message).to.contain "Failed to read the 'contentDocument' property from 'HTMLIFrameElement': Blocked a frame with origin"
# done()
#
# setTimeout checkResponse , 201
# these don't work
# require 'child_process'
# require 'app'
# require 'browser-window'
# require 'electron'
# alert(42) will open an electron popup
'use strict';
var wallabyWebpack = require('wallaby-webpack');
var webpackPostprocessor = wallabyWebpack({});
module.exports = function (wallaby) {
//var preload_File = wallaby.localProjectDir + 'preload.js' // not working
return {
files: [
{ pattern: 'src/**/*.js', load: false }
],
tests: [
{ pattern: 'test/**/*Spec.js', load: false },
{ pattern: 'test/**/*.coffee', load: false }
],
env: {
kind: 'electron',
options: {
show: false,
skipTaskbar: false,
autoHideMenuBar: false,
webPreferences: {
nodeIntegration: true,
webSecurity: false,
}
}},
compilers: {
'**/*.coffee': wallaby.compilers.coffeeScript({})
},
testFramework: 'mocha',
postprocessor: webpackPostprocessor,
bootstrap: function () {
window.__moduleBundler.loadTests();
}
};
};
@ArtemGovorov
Copy link

Nice!
One thing that may make your life easier: you don't have to eval the code. In your case Webpack require is shadowing Electron node require, so doing require('electron') is an attempt to resolve the module via webpack (and it fails), The eval just prevents replacing the require by Webpack, so it works. An easier way to make webpack not touch the require would be:

screen shot 2016-05-20 at 1 47 45 pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment