#add the location of this file to Ruby's search path
#so that we can find all our files
runfromdir = "C:/build/webmip/tests/"
#requires
require 'watir'

#require our constants
require 'constants'
#require our useful helper methods
require runfromdir+'default_methods.rb'
#email stuff
require 'net/smtp'
#require the test unit library
require 'test/unit'
require 'test/unit/ui/console/testrunner'
require 'test/unit/testsuite'
#include the integration test cases
require runfromdir+'login_tests'
require runfromdir+'enquirytests'
require runfromdir+'housingtests'
require runfromdir+'drawingtests'
require runfromdir+'basestests'
require runfromdir+'addontests'
require runfromdir+'exportdatatests'
require runfromdir+'moduletests'
require runfromdir+'user_management'
#includes
include Watir


@@ie = IE.new
@@ie.maximize
#set the speed of watir, we'd also set the speed of light but that's supposed to be a constant
@@ie.speed = :fast


#
# Setup default config
#
# Remove the test data
#
#dosql('begin cleanup_testdata; commit; end;')
#
# Set the system submission deadline
#
#dosql('begin UPDATE system_configuration SET value = \'20:00\' 
 #      WHERE parameter = \'G_SUBMISSION_DEADLINE\'; commit; end; ')

#
#Set up our contracts before we start serious testing
#
#require 'usefulscriptthatmightsetupdata'

#
#Get our individual integration tests
#
class WebMipIntegrationTests
  def self.suite
     suite = Test::Unit::TestSuite.new('webMIP Integration tests')
     suite << Test_01_login_screen.suite
     suite << Test_02_enquiry.suite
     suite << Test_03_housings_screen.suite
     suite << Test_04_drawings_screen.suite
     suite << Test_05_base_screen.suite
     suite << Test_06_addon_screen.suite     
     suite << Test_07_export_data.suite
     suite << Test_08_modules_screen.suite
     suite << Test_09_MIPADMIN_party_management.suite
     suite << Test_10_SUPPADMIN_party_management.suite
     suite << Test_11_edit_parties.suite
     #copy the line above, inserting your test to add
     #more testcases into the integration test suite
     return suite
  end
end

#set output from test runner to be verbose and output to a iostring
sio = StringIO.new
tr = Test::Unit::UI::Console::TestRunner.new(
WebMipIntegrationTests, Test::Unit::UI::VERBOSE, sio )

#Set the test runner off
passed = tr.start()



#if we have some test failures or errors then send the email
if (passed.failure_count() > 0 or  passed.error_count() > 0)
#build the email
from_addr = 'jamie.priest@advantica.com'
to_addr = %w[jamie.priest@advanticagroup.com matthew.mullen@advanticagroup.com andrew.hardy@advanticagroup.com]
project = 'webMIP'
errors = sio.string
emailtext = <<END_EMAIL
From: webMIP <#{from_addr}>
To: <Webmip Build Team>
Subject: #{project} automated test failure

An automated assertion failed for the project
#{project}

#{errors}

END_EMAIL
  Net::SMTP.start('LOMAIL01') do |smtp|
    smtp.sendmail emailtext, from_addr, to_addr
  end
end



@@ie.wait
@@ie.close