Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly start & stop edge browser #9

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ os:
- windows
language: node_js
node_js:
- 4
- 5
- 6
- 7
- 8
- 9
- 10
install: npm install
script:
- npm test
5 changes: 3 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
environment:
matrix:
- nodejs_version: "4"
- nodejs_version: "5"
- nodejs_version: "6"
- nodejs_version: "7"
- nodejs_version: "8"
- nodejs_version: "9"
- nodejs_version: "10"
install:
- ps: Install-Product node $env:nodejs_version
- npm install
Expand Down
55 changes: 29 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,33 @@
// ------------

var path = require('path')
var spawn = require('child_process').spawn
var exec = require('child_process').exec

var escapeRegex = /\\/g
var escapement = '\\\\'
var startScriptPath = path.join(__dirname, 'scripts/start_edge.ps1').replace(escapeRegex, escapement)
var stopScriptPath = path.join(__dirname, 'scripts/stop_edge.ps1').replace(escapeRegex, escapement)
var backslashRegex = /\\/g
var escapeBackslash = '\\\\'
var spaceRegex = / /g
var escapeSpace = '` '
var startScriptPath = path
.join(__dirname, 'scripts/start_edge.ps1')
.replace(backslashRegex, escapeBackslash)
.replace(spaceRegex, escapeSpace)

// Constructor
function EdgeBrowser (baseBrowserDecorator) {
function EdgeBrowser (baseBrowserDecorator, logger) {
baseBrowserDecorator(this)

var self = this
var log = logger.create('launcher')

function killEdgeProcess (cb) {
exec('taskkill /t /f /im MicrosoftEdge.exe', function (err) {
if (err) {
log.error('Killing Edge process failed. ' + err)
} else {
log.debug('Killed Edge process')
}
cb()
})
}

// Use start_edge script path as powershell argument, and url as script argument
this._getOptions = function (url) {
Expand All @@ -25,24 +40,12 @@ function EdgeBrowser (baseBrowserDecorator) {

// Override onProcessExit to manage edge shutdown
var baseOnProcessExit = this._onProcessExit
this._onProcessExit = function (code, errorOutput) {
// In case of error return immediatly
if (code > 0 || errorOutput.length > 0) {
baseOnProcessExit(code, errorOutput)
} else {
// Start stop process to close edge gracefully
var stopProcess = spawn(self.DEFAULT_CMD.win32, [ stopScriptPath ])

stopProcess.stdout.on('data', self._onStdout)

stopProcess.stderr.on('data', self._onStderr)

stopProcess.on('error', self._onStderr)

stopProcess.on('exit', function (code) {
baseOnProcessExit(code, errorOutput)
})
}
this._onProcessExit = function (code, signal, errorOutput) {
killEdgeProcess(function () {
if (baseOnProcessExit) {
baseOnProcessExit(code, signal, errorOutput)
}
})
}
}

Expand All @@ -54,7 +57,7 @@ EdgeBrowser.prototype = {
ENV_CMD: 'EDGE_BIN'
}

EdgeBrowser.$inject = ['baseBrowserDecorator']
EdgeBrowser.$inject = ['baseBrowserDecorator', 'logger']

// Publish di module
// -----------------
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"Andreas Krummsdorf <[email protected]>",
"Marcos Cáceres <[email protected]>",
"Nikita Khomyakov <[email protected]>",
"Christopher Currie <[email protected]>"
"Christopher Currie <[email protected]>",
"John Slegers <[email protected]>"
]
}
34 changes: 20 additions & 14 deletions scripts/start_edge.ps1
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
<#
@author [Tristan Valcke]{@link https://github.com/Itee}
@license [MIT]{@link https://opensource.org/licenses/MIT}

@description This script will attemp to start Microsoft Edge browser and wait the process to be detach
#>
param([string]$url = "")

# Check if edge is already running and cancel run if one is found.
$MicrosoftEdgeProcess = Get-Process "MicrosoftEdge" -ErrorAction SilentlyContinue
$MicrosoftEdgeCPProcess = Get-Process "MicrosoftEdgeCP" -ErrorAction SilentlyContinue
if( $MicrosoftEdgeProcess -or $MicrosoftEdgeCPProcess ) {
Write-Output "An instance of Windows Edge browser is already running. Please close it and try again."
exit 1
try {
$MicrosoftEdgePath = Join-Path $ENV:APPDATA "..\Local\Packages\Microsoft.MicrosoftEdge_8wekyb3d8bbwe" -Resolve -ErrorAction SilentlyContinue
if ($MicrosoftEdgePath) {
# Delete Edge's data folder starting from the inner most file
Get-ChildItem -Path $MicrosoftEdgePath -Force -Recurse |
Sort-Object -Property FullName -Descending |
Remove-Item -Recurse -Force
}

Get-AppXPackage -Name Microsoft.MicrosoftEdge |
Foreach {
Add-AppxPackage -DisableDevelopmentMode -Register "$( $_.InstallLocation )\AppXManifest.xml" -Verbose
}
}
catch
{
Write-Output "An unexpected error occured while resetting Microsoft Edge."
}

# Start our Microsoft Edge instance
try
{
Start-Process -FilePath "microsoft-edge:$url"
Wait-Process -Name "MicrosoftEdge" -ErrorAction Stop
exit 0
}
catch [ Microsoft.PowerShell.Commands.ProcessCommandException ]
{
Expand All @@ -27,6 +33,6 @@ catch [ Microsoft.PowerShell.Commands.ProcessCommandException ]
}
catch
{
Write-Output "An unexpected error occure."
Write-Output "An unexpected error occured while starting Microsoft Edge."
exit 1
}
}
35 changes: 0 additions & 35 deletions scripts/stop_edge.ps1

This file was deleted.

36 changes: 18 additions & 18 deletions test/launchers/launcher.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,17 @@ describe('launcher', function () {
captureTimeoutLauncherDecorator: ['factory', captureTimeoutDecorator],
retryLauncherDecorator: ['factory', retryDecorator],
processLauncherDecorator: ['factory', processDecorator],
baseBrowserDecorator: ['factory', baseBrowserDecoratorFactory]
baseBrowserDecorator: ['factory', baseBrowserDecoratorFactory],
logger: [
'value', {
create: function () {
return {
error: function () {},
debug: function () {}
}
}
}
]
}
})

Expand Down Expand Up @@ -166,37 +176,27 @@ describe('launcher', function () {
})

describe('_onProcessExit', function () {
var childProcessCmd, childProcessArgs, onProcessExit

var childProcessCmd, onProcessExit
beforeEach(function () {
onProcessExit = function () {
EdgeLauncher = proxyquire('../../index', {
child_process: {
spawn: function (cmd, args) {
exec: function (cmd, cb) {
childProcessCmd = cmd
childProcessArgs = args

return spawn(cmd, args)
cb()
}
}
})
injector = new di.Injector([module, EdgeLauncher])
launcher = injector.get('launcher:Edge')
launcher._onProcessExit(0, '')
launcher._onProcessExit(0, null, '')
}
})

it('should spawn powershell stop_edge.ps1 script', function (done) {
it('should call taskkill', function (done) {
onProcessExit()

var powershellPath = path.normalize(childProcessCmd)
expect(powershellPath).to.be.a.file()
expect(powershellPath).to.include('powershell.exe')

var scriptPath = path.normalize(childProcessArgs[0])
expect(scriptPath).to.be.a.file()
expect(scriptPath).to.include('stop_edge.ps1')

expect(childProcessCmd).to.equal('taskkill /t /f /im MicrosoftEdge.exe')
done()
})
})
Expand Down