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

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

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

// serve static files so they are available for selenium
var http = require('http')
var path = require('path')
var st = require('st')

filepath = path.join(__dirname, '..', '..', 'examples')
http.createServer(st({
  path: filepath
})).listen(1337)

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'
  //}
]

var driver
after(function (done) {
  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)
})

capabilities.forEach(function (capability) {
  const description = capability.platform + ': ' + capability.browserName + ' ' + capability.version + ': '
  describe(description, function () {
    var id

    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()
      })
    })

    describe('Button', function () {
      afterEach(function (done) {
        var title = this.currentTest.fullTitle()
        var passed = this.currentTest.state === 'passed'
        saucelabs.updateJob(id, {
          name: title,
          passed: passed,
          // we have to set a build id to make the badges work
          build: 1
        }, done)
      })

      it('should be clickable', function () {
        driver.get('http://localhost:1337/index.html#/button')
        driver.findElement(By.className('Button')).click()
      })
    })
  })
})

@@ -388,19 +388,21 @@
 elenium%0A
-var
+const
  http = 
@@ -417,19 +417,21 @@
 'http')%0A
-var
+const
  path = 
@@ -446,19 +446,21 @@
 'path')%0A
-var
+const
  st = re
@@ -472,16 +472,22 @@
 ('st')%0A%0A
+const 
 filepath
@@ -1030,20 +1030,15 @@
 %0A  %7D
+,
 %0A 
- //
  %7B%0A 
- //
    p
@@ -1061,19 +1061,16 @@
 s 10',%0A 
- //
    brows
@@ -1099,19 +1099,16 @@
 orer',%0A 
- //
    versi
@@ -1126,23 +1126,17 @@
 '%0A  
-// 
 %7D
+,
 %0A  
-//
 %7B%0A  
-//
   pl
@@ -1159,18 +1159,16 @@
 .11',%0A  
-//
   browse
@@ -1186,18 +1186,16 @@
 ari',%0A  
-//
   versio
@@ -1203,18 +1203,16 @@
 : '9'%0A  
-//
 %7D%0A%5D%0A%0Avar

add windows 10 ie and os x 10.11 safari back to selenium tests