#!${buildout:directory}/bin/${eggs:interpreter}
# BEWARE: This file is operated by slapgrid
# BEWARE: It will be overwritten automatically
"""
  Script to run jIO/renderJS test suite using Nexedi's test node framework.
"""
import argparse, os, re, shutil, subprocess, sys, traceback
from erp5.util import taskdistribution
from time import gmtime, strftime
from lxml import etree
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

os.environ['TMPDIR'] = '$${xvfb-instance:tmp-path}'
os.environ['DISPLAY'] = ':0'

BASE_URL = 'http://[$${nginx-configuration:ip}]:$${nginx-configuration:port}/'

def main():
  parser = argparse.ArgumentParser(description='Run a test suite.')
  parser.add_argument('--test_suite', help='The test suite name')
  parser.add_argument('--test_suite_title', help='The test suite title')
  parser.add_argument('--test_node_title', help='The test node title')
  parser.add_argument('--project_title', help='The project title')
  parser.add_argument('--revision', help='The revision to test',
                      default='dummy_revision')
  parser.add_argument('--node_quantity', help='ignored', type=int)
  parser.add_argument('--master_url',
                      help='The Url of Master controling many suites')
  parser.add_argument('--frontend_url',
                      help='The url of frontend of the test suite')
  parser.add_argument('--target',
                      help='Target OS to run tests on',
                      type=str)
  parser.add_argument('--target_version',
                      help='Target OS version to use',
                      type=str,)
  parser.add_argument('--target_browser',
                      help='The desired browser of the target OS to be used. Example: Firefox if target is Android.',
                      type=str,)
  parser.add_argument('--target_device',
                      help='The desired device running the target OS. Example: iPad Simulator, if target is iOS.',
                      type=str,)
  parser.add_argument('--appium_server_auth',
                      help='Combination of user and token to access SauceLabs service. (i.e. user:token)',
                      type=str)

  args = parser.parse_args()

  import json
  parsed_parameters = json.loads('$${instance-parameter:configuration._}')


  if not getattr(args, 'target', None):
    args.target = parsed_parameters.get('target', 'firefox')
  if not getattr(args, 'test_suite', None):
    args.test_suite = parsed_parameters.get('test-suite')
  if not getattr(args, 'target_version', None):
    args.target_version = parsed_parameters.get('target-version')
  if not getattr(args, 'appium_server_auth', None):
    args.appium_server_auth = parsed_parameters.get('appium-server-auth')
  if not getattr(args, 'target_browser', None):
    args.target_browser = parsed_parameters.get('target-browser')
  if not getattr(args, 'target_device', None):
    args.target_device = parsed_parameters.get('target-device')

  is_browser_running = False
  try:
    test_suite_title = args.test_suite_title or args.test_suite
    test_suite = args.test_suite
    revision = args.revision

    test_line_dict = {}

    if ('jio' in test_suite):
      url = BASE_URL + 'jio/test/tests.html'
    else:
      url = BASE_URL + 'renderjs/test/'

    date = strftime("%Y/%m/%d %H:%M:%S", gmtime())

  
    ##########################
    # Run all tests
    ##########################
    
    is_appium = False
    
    if args.target == 'firefox':
      firefox_capabilities = webdriver.common.desired_capabilities.DesiredCapabilities.FIREFOX
      firefox_capabilities['marionette'] = True
      browser = webdriver.Firefox(capabilities=firefox_capabilities,
                                  firefox_binary='${firefox:location}/firefox-slapos',
                                  executable_path='${firefox:location}/geckodriver')
    elif args.target in ['iOS', 'Android']:
      # parameters for mobile emulators have different names then parameters for
      # desktop OSes
      is_appium = True
      capabilities = {
        'platformName': args.target,
        'platformVersion': args.target_version,
        'deviceName': args.target_device,
        'browserName': args.target_browser

      }
    elif 'Windows' in args.target or 'OS X' in args.target:
      # parameters for mobile emulators have different names then parameters for
      # desktop OSes
      is_appium = True
      capabilities = {
        'browserName': args.target_browser,
        'platform': args.target,
        'version': args.target_version
      }
    if is_appium:
      if not args.appium_server_auth:
        raise RuntimeError('--appium_server_auth is required.')
      appium_url = "http://%s@ondemand.saucelabs.com/wd/hub" % (args.appium_server_auth)
      browser = webdriver.Remote(appium_url, capabilities)

      # adjust path for remote test url
      remote_access_url = parsed_parameters.get('remote-access-url', None)
      if remote_access_url:
        if ('jio' in test_suite):
          url = os.path.join(remote_access_url, 'jio/test/tests.html')
        else:
          url = os.path.join(remote_access_url, 'renderjs/test/')
      else:
        raise ValueError('remote-access-url is not defined in instance parameter')

    is_browser_running = True
    agent = browser.execute_script("return navigator.userAgent")
    print agent
    print url

    browser.get(url)
    WebDriverWait(browser, 300).until(EC.presence_of_element_located((
      By.XPATH, '//p[@id="qunit-testresult" and contains(text(), "completed")]')
    ))

    html_parser = etree.HTMLParser(recover=True)
    body = etree.fromstring(browser.page_source.encode('UTF-8'), html_parser)

    print ' '.join(body.xpath('//*[@id="qunit-testresult"]//text()'))

    for elt in body.xpath('.//ol[@id="qunit-tests"]/li'):
      if (len(elt.xpath('.//span[@class="module-name"]'))):
        test_name = '%s: %s' % (
          elt.xpath('.//span[@class="module-name"]')[0].text,
          elt.xpath('.//span[@class="test-name"]')[0].text
        )
      #global failure, like Uncaught ReferenceError: RSVP is not defined
      else:
        test_name = elt.xpath('.//span[@class="test-name"]')[0].text
         
      print elt.get('class'), ''.join(elt.xpath('.//strong')[0].itertext())
      # print elt.find_element_by_tag_name('ol').get_attribute('innerHTML')

      failure = int(elt.xpath('.//b[@class="failed"]')[0].text)
      success = int(elt.xpath('.//b[@class="passed"]')[0].text)
      test_line_dict[test_name] = {
        'test_count': success + failure,
        'error_count': 0,
        'failure_count': failure,
        'skip_count': 0,
        'duration': int(elt.xpath('.//span[@class="runtime"]')[0].text.split()[0]),
        'command': elt.xpath('.//a[text()="Rerun"]')[0].get('href'),
        'stdout': agent,
        'stderr': '',
        'html_test_result': etree.tostring(elt.xpath('.//ol')[0])
      }

    # do quit browser asap as we have results. this is required in case of timeout of
    # remote appium service which will close test session of no command received within
    # usually 90s and thus fail this script. And it costs processing time as well
    # to keep test session needlessly opened.
    browser.quit()
    is_browser_running = False

    tool = taskdistribution.TaskDistributor(portal_url=args.master_url)
    test_result = tool.createTestResult(revision = revision,
                                        test_name_list = test_line_dict.keys(),
                                        node_title = args.test_node_title,
                                        test_title = test_suite_title,
                                        project_title = args.project_title)
    if test_result is None or not hasattr(args, 'master_url'):
      return
    # report test results
    while 1:
      test_result_line = test_result.start()
      if not test_result_line:
        print 'No test result anymore.'
        break

      print 'Submitting: "%s"' % test_result_line.name
      # report status back to Nexedi ERP5
      test_result_line.stop(**test_line_dict[test_result_line.name])

  except:
    # Catch any exception here, to warn user instead of being silent,
    # by generating fake error result
    print traceback.format_exc()
    result = dict(status_code=-1,
                  command=url,
                  stderr=traceback.format_exc(),
                  stdout='')
    # XXX: inform test node master of error
    raise EnvironmentError(result)
  
  finally:
    if is_browser_running:
      # if by any chance browser is still running due to
      # traceback raised make sure we cleanup
      browser.quit()

if __name__ == "__main__":
    main()
