#!/usr/local/monitoring/bin/python

import calendar, time, sys
sys.path.insert (1, '/usr/local/monitoring/princeton/lib')
from puselenium import init_selenium

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common import exceptions as EXCEP

if len (sys.argv) != 4:
	print "Usage: %s <URL> <username> <password>" % sys.argv[0]
	sys.exit (3)

url = sys.argv[1]
usr = sys.argv[2]
usrpass = sys.argv[3]

#ff, fp = init_selenium (['mon300w', 'mon301w'])
#ff, fp = init_selenium (['mon300w'])
ff, fp = init_selenium ()

try:
	ff.implicitly_wait (30)
	#ff.allowNativeXpath("false")

	t_start = time.time()

	# Initial page
	try:
		ff.get (url)

		WebDriverWait (ff, 10).until (EC.title_contains("Thesis Central"))

		t_inipage = time.time()

		ff.find_element_by_link_text ('Login').click()

	except:
		print "Error accessing the initial Thesis Central URL"
		sys.exit (2)

	t_puauth = 0

	# Selection page between Shibboleth auth and something else.... only works in Dev/QA
	try:
		ff.implicitly_wait (10)
		WebDriverWait (ff, 10).until (EC.presence_of_element_located((By.LINK_TEXT, "Princeton Authentication")))
		ff.implicitly_wait (30)

		t_puauth = time.time()

		try:
			ff.find_element_by_link_text ('Princeton Authentication').click()
		except:
			print "Error reaching the authentication options page"
			sys.exit (2)
	# Not Dev/QA... proceed
	except:
		pass

	# Shibboleth auth page
	try:
		WebDriverWait (ff, 10).until (EC.presence_of_element_located((By.XPATH, "//label[text()='NetID']")))

		t_shibauth = time.time()

		puusr = ff.find_element_by_id ("username")
		puusr.send_keys (usr)

		pupass = ff.find_element_by_id ("password")
		pupass.send_keys (usrpass)

		ff.find_element_by_xpath ("//input[@value='LOGIN']").click()
	except:
		print "Error reaching the Shibboleth authentication page"
		sys.exit (2)
		
	# Check if we are in fact logged in to the main site. If so, log out
	try:
		WebDriverWait (ff, 30).until (EC.presence_of_element_located((By.LINK_TEXT, "Logout")))

		t_login = time.time()

		ff.find_element_by_link_text ("Logout").click()
	except:
		print "Unable to locate known webpage elements after login, such as the \"Logout\" link. Maybe the page loaded incorrectly"
		sys.exit (2)

	exit_stat = 0
	# Check if we logged out successfully
	#try:
	#	WebDriverWait (ff, 10).until (EC.presence_of_element_located((By.LINK_TEXT, "Login")))

	#	t_logout = time.time()

	print "Successfully logged on to %s and logged out | 'time_inipage'=%.3fs;;; " % (url, t_inipage - t_start) + ("'time_puauth'=%.3fs;;; 'time_shibauth'=%.3fs;;; " % (t_puauth - t_inipage, t_shibauth - t_puauth) if t_puauth > 0 else "'time_shibauth'=%.3fs;;; " % (t_shibauth - t_inipage)) + "'time_login'=%.3fs;;; 'total'=%.3fs;;;" % (t_login - t_shibauth, t_login - t_start)
	#except:
	#	print "Successfully logged on to %s but failed to log out | 'time_inipage'=%.3fs;;; " % (url, t_inipage - t_start) + ("'time_puauth'=%.3fs;;; 'time_shibauth'=%.3fs;;; " % (t_puauth - t_inipage, t_shibauth - t_puauth) if t_puauth > 0 else "'time_shibauth'=%.3fs;;; " % (t_shibauth - t_inipage)) + "'time_login'=%.3fs;;; 'total'=%.3fs;;;" % (t_login - t_shibauth, t_login - t_start)
	#	exit_stat = 1

	time.sleep(3)

except EXCEP.ErrorInResponseException, (resp, msg):
	
	print "ERROR: An error occured communicating with the browser server: Response: %s; Message: %s" % (resp, msg)
	sys.exit (2)

except EXCEP.NoSuchElementException, msg:

	print "ERROR: Unable to locate webpage element. Probably the page loaded incompletely or incorrectly: %s" % msg
	sys.exit (2)

except EXCEP.TimeoutException, msg:

	print "ERROR: Request timed out. Response: %s" % msg
	sys.exit (2)

except EXCEP.WebDriverException, msg:

	print "ERROR: I don't know what happened. Read this - %s" % msg
	sys.exit (2)

finally:
	ff.quit()

sys.exit (exit_stat)
