const request = require('request-promise');

class BrowserstackService {
  before() {
    this.sessionId = global.browser.sessionId;
    this.auth = global.browser.requestHandler.auth || {};
  }

  after(failures) {
    this._update(this.sessionId, this._getBody(failures === 0));
  }

  _update(sessionId, body) {
    request({
      method: 'PUT',
      uri: `https://www.browserstack.com/automate/sessions/${this.sessionId}.json`,
      json: true,
      auth: this.auth,
      body
    });
  }

  _getBody(success) {
    return {
      status: success ? 'completed' : 'error'
    };
  }
}

module.exports = BrowserstackService;

@@ -190,92 +190,414 @@
 ;%0A  
-%7D%0A%0A  after(failures) %7B%0A    this._update(this.sessionId, this._getBody(failures === 0
+  this.failures = 0;%0A  %7D%0A%0A  afterSuite (suite) %7B%0A    if (suite.hasOwnProperty('err')) %7B%0A      this.failures++;%0A    %7D%0A  %7D%0A%0A  afterTest (test) %7B%0A    if (!test.passed) %7B%0A      this.failures++;%0A    %7D%0A  %7D%0A%0A  after() %7B%0A    return this._update(this.sessionId, this._getBody());%0A  %7D%0A%0A  onReload(oldSessionId, newSessionId) %7B%0A    this.sessionId = newSessionId;%0A    return this._update(this.sessionId, this._getBody(
 ));%0A
@@ -634,16 +634,23 @@
 ) %7B%0A    
+return 
 request(
@@ -733,21 +733,16 @@
 sions/$%7B
-this.
 sessionI
@@ -827,23 +827,16 @@
 getBody(
-success
 ) %7B%0A    
@@ -862,15 +862,29 @@
 us: 
-success
+(this.failures === 0)
  ? '

Fix failed tests marking. Closes #4