<commit_before>mr = require 'Mr.Async'
_ = require 'underscore'
Canvas = require 'canvas'

webdriver = require 'selenium-webdriver'
Comparison = require './comparison'


class Viff
  constructor: (seleniumHost) ->
    @builder = new webdriver.Builder().usingServer(seleniumHost)
    @drivers = {}

  takeScreenshot: (browserName, envHost, url, callback) -> 
    that = @
    defer = mr.Deferred()
    defer.done(callback)
    
    unless driver = @drivers[browserName]
      @builder = @builder.withCapabilities { browserName: browserName }
      driver = @builder.build()
      @drivers[browserName] = driver

    envName = _.first(envName for envName of envHost)
    [url, selector, preHandle] = Viff.parseUrl url

    driver.get envHost[envName] + url
    preHandle driver, webdriver if _.isFunction preHandle
    driver.takeScreenshot().then (base64Img) -> 
      if _.isString selector
        Viff.dealWithPartial base64Img, driver, selector, (partialBase64Img) ->
          defer.resolve partialBase64Img
      else <commit_msg>add unhandled exception handling. Qiu<commit_after>mr = require 'Mr.Async'
_ = require 'underscore'
Canvas = require 'canvas'

webdriver = require 'selenium-webdriver'
Comparison = require './comparison'

webdriver.promise.controlFlow().on 'uncaughtException', (e) -> 
  console.error 'Unhandled error: ' + e

class Viff
  constructor: (seleniumHost) ->
    @builder = new webdriver.Builder().usingServer(seleniumHost)
    @drivers = {}

  takeScreenshot: (browserName, envHost, url, callback) -> 
    that = @
    defer = mr.Deferred()
    defer.done(callback)
    
    unless driver = @drivers[browserName]
      @builder = @builder.withCapabilities { browserName: browserName }
      driver = @builder.build()
      @drivers[browserName] = driver

    envName = _.first(envName for envName of envHost)
    [url, selector, preHandle] = Viff.parseUrl url

    driver.get envHost[envName] + url
    preHandle driver, webdriver if _.isFunction preHandle
    driver.takeScreenshot().then (base64Img) -> 
      if _.isString selector
        Viff.dealWithPartial base64Img, driver, selector, (partialBase64Img) ->
          defer.resolve partialBase64Img
      else 
{'fraction_non_alphanumeric': 0.10828025477707007, 'fraction_numerical': 0.007279344858962694, 'mean_word_length': 3.5061475409836067, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2306383', 'n_tokens_mistral': 701, 'n_tokens_neox': 652, 'n_words': 161}