/* global describe, before, after, it, afterEach */

var username = process.env.SAUCE_USERNAME
var accessKey = process.env.SAUCE_ACCESS_KEY

var webdriver = require('selenium-webdriver')
var Saucelabs = require('saucelabs')
var saucelabs = new Saucelabs({
  username: username,
  password: accessKey
})

var capabilities = [
    {
      platform: 'Linux',
      browserName: 'Chrome',
      version: '48'
    },
    {
      platform: 'Linux',
      browserName: 'Firefox',
      version: '45'
    },
    {
      platform: 'Windows 10',
      browserName: 'Chrome',
      version: '51'
    },
    {
      platform: 'Windows 10',
      browserName: 'Firefox',
      version: '47'
    },
    {
      platform: 'Windows 10',
      browserName: 'MicrosoftEdge',
      version: '13.10586'
    },
    {
      platform: 'Windows 10',
      browserName: 'Internet Explorer',
      version: '11.103'
    },
    {
      platform: 'OS X 10.11',
      browserName: 'Safari',
      version: '9'
    }
]

capabilities.forEach(function (capability) {

  describe(capability.platform + ': ' + capability.browserName, function () {
    var id
    var driver

    before(function (done) {
      driver = new webdriver.Builder()
        .withCapabilities({
          platform: capability.platform,
          browserName: capability.browserName,
          version: capability.version,
          username: username,
          accessKey: accessKey
        })
        .usingServer('http://' + username + ':' + accessKey + '@ondemand.saucelabs.com:80/wd/hub')
        // .usingServer('http://localhost:4445/wd/hub')
        .build()

      driver.getSession().then(function (sessionID) {
        id = sessionID.id_
        done()
      })
    })

    afterEach(function (done) {
      var title = this.currentTest.title
      var passed = this.currentTest.state === 'passed'
      saucelabs.updateJob(id, {
        name: title,
        passed: passed
      }, done)
    })

    after(function (done) {
      // get sauce labs job id
      driver.quit()
      // without timeout sauce labs tests do not end properly and
      // report "Your test errored. Test did not see a new command for 90 seconds. Timing out."
      setTimeout(function () {
        done()
      }, 1000)
    })

    it('should work', function (done) {
      driver.get('http://www.google.com')
      driver.getTitle().then(function (title) {
        console.log(title)
        done()
      })
    })
  })

})


@@ -320,22 +320,18 @@
 s = %5B%0A  
-  %7B%0A  
+%7B%0A
     plat
@@ -341,34 +341,32 @@
 m: 'Linux',%0A    
- 
- 
 browserName: 'Ch
@@ -368,34 +368,32 @@
 : 'Chrome',%0A    
-  
 version: '48'%0A  
@@ -388,37 +388,31 @@
 ion: '48'%0A  
- 
- 
 %7D,%0A
-  
   %7B%0A
-  
     platform
@@ -422,26 +422,24 @@
 Linux',%0A    
-  
 browserName:
@@ -442,34 +442,32 @@
 ame: 'Firefox',%0A
-  
     version: '45
@@ -468,35 +468,29 @@
 n: '45'%0A
-  
   %7D,%0A  
- 
- 
 %7B%0A    
- 
- 
 platform
@@ -501,34 +501,32 @@
 indows 10',%0A    
-  
 browserName: 'Ch
@@ -528,34 +528,32 @@
 : 'Chrome',%0A    
- 
- 
 version: '51'%0A  
@@ -550,33 +550,29 @@
 n: '51'%0A
-  
   %7D,%0A
-  
   %7B%0A    
   platfo
@@ -555,34 +555,32 @@
 1'%0A  %7D,%0A  %7B%0A    
-  
 platform: 'Windo
@@ -579,34 +579,32 @@
 : 'Windows 10',%0A
-  
     browserName:
@@ -615,26 +615,24 @@
 refox',%0A    
-  
 version: '47
@@ -633,31 +633,25 @@
 n: '47'%0A
-  
   %7D,%0A  
- 
- 
 %7B%0A
-  
     plat
@@ -666,34 +666,32 @@
 indows 10',%0A    
- 
- 
 browserName: 'Mi
@@ -704,26 +704,24 @@
 tEdge',%0A    
-  
 version: '13
@@ -728,31 +728,25 @@
 .10586'%0A
-  
   %7D,%0A  
-  %7B%0A  
+%7B%0A
     plat
@@ -765,26 +765,24 @@
 ws 10',%0A    
-  
 browserName:
@@ -803,26 +803,24 @@
 lorer',%0A    
-  
 version: '11
@@ -829,27 +829,21 @@
 03'%0A
-  
   %7D,%0A  
- 
- 
 %7B%0A    
- 
- 
 plat
@@ -862,26 +862,24 @@
 10.11',%0A    
-  
 browserName:
@@ -889,26 +889,24 @@
 afari',%0A    
- 
- 
 version: '9'
@@ -906,18 +906,16 @@
 on: '9'%0A
-  
   %7D%0A%5D%0A%0Ac
@@ -958,17 +958,16 @@
 lity) %7B%0A
-%0A
   descri
@@ -2374,13 +2374,12 @@
 %7D)%0A  %7D)%0A
-%0A
 %7D)%0A%0A

fix standard formatting in selenium