var defineSupportCode = require('cucumber').defineSupportCode;

var webdriver = require("selenium-webdriver");
var browserstack = require("browserstack-local");

var createBrowserStackSession = function(config, caps){
  return new webdriver.Builder().
    usingServer("http://"+config.server+"/wd/hub").
    withCapabilities(caps).
    build();
};

var config = {
    server: "hub-cloud.browserstack.com",
    capabilities: [
        {
            browserName: "chrome",
            build: process.env.CIRCLE_BUILD_NUM,
            project: process.env.CIRCLE_PROJECT_REPONAME + "-" + process.env.CIRCLE_BRANCH,
            'browserstack.local': true
        }
    ]
};

var username = process.env.BS_USERNAME || config.user;
var accessKey = process.env.BS_AUTHKEY || config.key;

defineSupportCode(function(context) {
  var Before = context.Before;
  var After = context.After;
  var bs_local = null;

  Before(function (scenario, callback) {
    var world = this;
    var task_id = parseInt(process.env.TASK_ID || 0, 10);
    var caps = config.capabilities[task_id];
    caps['browserstack.user'] = username;
    caps['browserstack.key'] = accessKey;

    if(caps["browserstack.local"]){
      // Code to start browserstack local before start of test and stop browserstack local after end of test
      bs_local = new browserstack.Local();
      bs_local.start({'key': accessKey }, function(error) {
        if (error) return console.log(error.red);

        world.driver = createBrowserStackSession(config, caps);
        callback();
      });
    }
    else {
      world.driver = createBrowserStackSession(config, caps);
      callback();
    }
  });

  After(function(scenario, callback){
    this.driver.quit().then(function(){
      if(bs_local){
        bs_local.stop(callback);
      }
      else callback();
    });
  });
});

@@ -29,17 +29,17 @@
 ire(
-'
+%22
 cucumber
 ').d
@@ -34,17 +34,17 @@
 cucumber
-'
+%22
 ).define
@@ -636,17 +636,17 @@
         
-'
+%22
 browsers
@@ -655,17 +655,17 @@
 ck.local
-'
+%22
 : true%0D%0A
@@ -910,18 +910,17 @@
   var bs
-_l
+L
 ocal = n
@@ -1004,18 +1004,17 @@
 var task
-_i
+I
 d = pars
@@ -1093,10 +1093,9 @@
 task
-_i
+I
 d%5D;%0D
@@ -1334,26 +1334,25 @@
 st%0D%0A      bs
-_l
+L
 ocal = new b
@@ -1377,26 +1377,25 @@
 );%0D%0A      bs
-_l
+L
 ocal.start(%7B
@@ -1398,13 +1398,11 @@
 rt(%7B
-'
 key
-'
 : ac
@@ -1408,17 +1408,16 @@
 ccessKey
- 
 %7D, funct
@@ -1448,16 +1448,29 @@
  (error)
+ %7B%0D%0A         
  return 
@@ -1490,24 +1490,33 @@
 rror.red);%0D%0A
+        %7D
 %0D%0A        wo
@@ -1810,18 +1810,17 @@
    if(bs
-_l
+L
 ocal)%7B%0D%0A
@@ -1833,10 +1833,9 @@
   bs
-_l
+L
 ocal

Update hooks.js