var webdriver = require('selenium-webdriver')
var remote = require('selenium-webdriver/remote')

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

// var platform = { browser: 'firefox', version: '34.0', os: 'Windows 7' }
var platform = { browser: 'Safari', version: '9.0', os: 'OS X 10.11' }

function buildDriver (platform) {
  var driver = new webdriver
    .Builder()
    .withCapabilities({
      'browserName': platform.browser,
      'platform': platform.os,
      'version': platform.version,
      'username': username,
      'accessKey': accessKey
    })
    .usingServer('http://' + username + ':' + accessKey +
                '@ondemand.saucelabs.com:80/wd/hub')
    .build()
  driver.setFileDetector(new remote.FileDetector())
  return driver
}

function getBtnValue () {
  return document.querySelector('.button__text').textContent
}

function runTest (driver, platform) {
  console.log('Running dummy test in ' + platform.browser + ' on ' + platform.os)
  driver.executeScript('sauce:job-name=dummy test')

  driver.get('http://ya.ru')
  driver.executeScript(getBtnValue).then(function (val) {
    console.log(val)
    driver.getTitle().then(function (title) {
      console.log('title is: ' + title)
      console.log('Finnished running dummy test, I quit!')
      driver
        .executeScript('sauce:job-result=passed')
      driver.quit()
    })
  })
}

var driver = buildDriver(platform)
runTest(driver, platform)
