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

browser-stack does not stop running after local has stopped #36

Closed
peter-mouland opened this issue Jun 19, 2017 · 7 comments
Closed

browser-stack does not stop running after local has stopped #36

peter-mouland opened this issue Jun 19, 2017 · 7 comments

Comments

@peter-mouland
Copy link

Hi,

It seem's I am not able to stop the connection to browserstack, after i call the stop() command, i can see the browserstack task is still running in the automate page.

My test scripts are setup to start the app server, connect to browser stack, then nightwatch and then close them all down again after the tests have finished. This seems like it all works (according to the logging) and the process with the bs connection has been killed.

To be complete, here is my setup...

//start-tests.js
#!/usr/bin/env node
const httpServer = require('http-server');
const path = require('path');

const browserstack = require('./setup/browserstack');

// check to see if multiple process have been started by nightwatch i.e. multiple envs/browsers
const isChildProcess = process.argv.find(arg => arg === '--parallel-mode');
let server;

const startAppServer = () => new Promise((resolve) => {
  server = httpServer.createServer({
    root: path.join(process.cwd(), 'public')
  });
  server.listen(3210);
  resolve()
});

const stopAppServer = () => new Promise((resolve) => {
  server.close();
  resolve();
});

const teardown = (e) => {
  if (isChildProcess) return Promise.resolve();
  return Promise.resolve()
    .then(() => stopAppServer())
    .then(() => browserstack.disconnect())
    .then(() => e && console.log(e));
};

const setup = () => {
  if (isChildProcess) return Promise.resolve();
  return Promise.resolve()
    .then(() => startAppServer())
    .then(() => browserstack.connect())
};

Promise.resolve()
  .then(setup)
  .then(() => browserstack.nightwatch())
  .then(teardown)
.catch(teardown);
// browserstack.js
const browserstack = require('browserstack-local');
const Nightwatch = require('nightwatch');

const bs_local = new browserstack.Local();;

const nightwatch = () => new Promise((resolve) => {
  console.log('Starting Nightwatch...')
  Nightwatch.bs_local = bs_local;
  Nightwatch.cli(function(argv) {
    Nightwatch.CliRunner(argv)
      .setup(null, function(){ resolve(); })
      .runTests(function(){ resolve(); });
  });
})

const connect = () => new Promise((resolve, reject) => {
  console.log('connect to browserStack ...')
  bs_local.start({ key: process.env.BROWSERSTACK_ACCESS_KEY }, (e) => {
    e && reject(e);
    console.log('browserStack connected')
    !e && resolve();
  });
});

const disconnect = () => new Promise((resolve, reject) => {
  console.log('stopping browserStack ...')
  bs_local.stop((e) => {
    e && reject(e);
    console.log('browserStack stopped');
    !e && resolve();
  });
});

module.exports = {
  connect,
  disconnect,
  nightwatch
};
 // nightwtch.conf
/*
 * File Purpose: To configure NightWatch to start up dependencies before running tests
 * */

const argv = require('yargs')
  .usage('Usage: $0 --target=[string]')
  .argv;

const TARGET_PATH = argv.target || `http://localhost:3210/iframe.html`;
const branch = process.env.CIRCLE_BRANCH || 'local';

module.exports = (function (settings) {
  settings.test_settings.default.globals = {
    TARGET_PATH,
    before(done){ /* before tests run */ done() },
    after(done){ /* after tests run */  done() }
  };
  console.log('set nighwtach settings')
  settings.test_settings.default.launch_url = "http://hub.browserstack.com";
  settings.test_settings.default.selenium_host = "hub.browserstack.com";
  settings.test_settings.default.desiredCapabilities['browserstack.local'] = TARGET_PATH.indexOf('localhost') > -1;
  settings.test_settings.default.desiredCapabilities['browserstack.user'] = argv.bsuser || process.env.BROWSERSTACK_USERNAME;
  settings.test_settings.default.desiredCapabilities['browserstack.key'] = argv.bskey || process.env.BROWSERSTACK_ACCESS_KEY;
  settings.test_settings.default.desiredCapabilities['build'] = branch;
  return settings;
})(require('./nightwatch.json'));
@peter-mouland peter-mouland changed the title browser-stack does not stop browser-stack does not stop running after local has stopped Jun 19, 2017
@peter-mouland
Copy link
Author

going to close this for now as building a reduced test-case did not reproduce the problem. hopefully building up the test-case will highlight where i'm going wrong, if not i'll reopen.

@oles
Copy link

oles commented Sep 19, 2017

Had the same problem.
bs_local.stop(function() {}) did not work for me no matter what I did.

Found out that bs_local exposes its process id, so instead I did process.kill(bs_local.pid) - that shotgun of a solution worked.

@9av1n-j0hn50n
Copy link

I'm having the same problem, but I'm struggling to the implement the process.kill(bs_local.pid) method mentioned above in my local.runner.js file (see below).

Could someone advise me how to resolve this please?

#!/usr/bin/env node

var Nightwatch = require('nightwatch')
var browserstack = require('browserstack-local')
var bs_local

try {
  process.mainModule.filename = './node_modules/.bin/nightwatch'
  console.log('\nconnecting to browserstack\n')
  Nightwatch.bs_local = bs_local = new browserstack.Local()
  bs_local.start({ key: 'KEY HERE' }, function(error) {
    if (error) throw error
    console.log('connected to browserstack\n\ntests are now running against the following browsers / devices:')
    Nightwatch.cli(function(argv) {
      Nightwatch.CliRunner(argv)
        .setup(null, function() {
          bs_local.stop(function() {})
        })
        .runTests(function() {
          bs_local.stop(function() {})
        })
    })
  })
} catch (ex) {
  console.log('there was an error whilst starting the test runner:\n\n')
  process.stderr.write(ex.stack + '\n')
  process.exit(2)
}

Thanks!!!

@raghuhit
Copy link
Contributor

Hi @9av1n-j0hn50n , this issue has been fixed in version v1.3.4, please update and check.

@9av1n-j0hn50n
Copy link

Hey @raghuhit,

As soon as I posted I saw where I was going wrong.

Many thanks!

@reykjavikingur
Copy link

reykjavikingur commented Nov 16, 2018

I would argue that this was not fixed. I'm using version 1.3.5 and am having the same problem.

I started from the instructions on https://www.browserstack.com/automate/webdriverio in the section "Testing on Internal Networks".

The "stop" method does not stop the process, so my subsequent test run crashes due to the port being in use. I'm on MacOS 10.13.6. The process.kill workaround was successful for me, but I would prefer to do it the normal way.

Here is my wdio.conf.js file:

// Browserstack

const Promise = require('promise');
const Browserstack = require('browserstack-local');
var bs_local;

exports.config = {

	user: '########',
	key: '#################',

	specs: [
		'./test/specs/**/*.js'
	],
	exclude: [
		// 'path/to/excluded/files'
	],

	maxInstances: 10,
	
	sync: true,
	logLevel: 'silent',
	coloredLogs: true,
	deprecationWarnings: true,
	bail: 0,
	screenshotPath: './errorShots/',
	baseUrl: '###########################',
	waitforTimeout: 10000,
	connectionRetryTimeout: 90000,
	connectionRetryCount: 3,

	framework: 'mocha',
	reporters: ['dot'],

	mochaOpts: {
		ui: 'bdd',
		timeout: 30000,
	},

	capabilities: [
		{
			browser: 'chrome',
			'browserstack.local': true,
		}
	],

	services: ['browserstack'],

	onPrepare(config, capabilities) {
		console.log('Connecting local');
		return new Promise((resolve, reject) => {
			bs_local = new Browserstack.Local();
			bs_local.start({key: config.key}, (error) => {
				if (error) {
					return reject(error);
				}
				console.log('Connected. Now testing...');
				resolve();
			})
		});
	},

	onComplete(capabilities, specs) {
		 bs_local.stop(function () {
		 });
	},

};

@akoliadiuk
Copy link

Using the Promise for stop() worked for me:

onComplete: function (exitCode, config, capabilities, results) {
   console.log('Stopping Browserstack Local');
   return new Promise(function (resolve, reject) {
     exports.browserstackLocal.stop(function (error) {
       if (error) return reject(error);
       console.log('Stopped');
       resolve();
     });
   });
 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants