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

import calendar, time, sys
sys.path.insert (1, '/usr/local/monitoring/princeton/lib')
#sys.path.insert (2, '/usr/local/monitoring/python/lib/python2.7/site-packages')
#sys.path.insert (3, '/usr/local/monitoring/python/lib/python2.7')
#sys.path.insert (4, '/usr/local/monitoring/python/lib64/python2.6/site-packages')
#sys.path.insert (5, '/usr/local/monitoring/python/lib/python2.6/site-packages')  
#sys.path.insert (6, '/usr/local/monitoring/python/lib/python2.6/site-packages/selenium-2.48.0-py2.6.egg')
from puselenium import init_selenium

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

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 = puselenium.init_selenium (['ims316', 'ims317'])
ff, fp = init_selenium ()

try:
	t_ini = time.time()
	ff.implicitly_wait (30)
	ff.get (url)

	try:
		#WebDriverWait (ff, 10).until (EC.title_contains("Sign On"))
		#if not ff.find_element_by_xpath ("//body[contains(.,'Logon is required. If you have forgotten your logon information, contact your help desk or administrator')]"):
	
		ff.find_element_by_xpath ("//body[contains(.,'If you have forgotten your logon information')]")
	except:
		print "Error retrieving the initial page"
		ff.quit()
		sys.exit (2)

	t_username = time.time()

	puusr = ff.find_element_by_name ("userID");
	puusr.send_keys (usr)
	ff.find_element_by_name ("nextUserID").click();

	#WebDriverWait (ff, 10).until (EC.presence_of_element_located((By.ID, "Bharosa_Password_PadDataField")))
	#if not ff.find_element_by_xpath ("//body[contains(.,'Select your preferred authentication method and log on')]"):
	#	print "Error retrieving the authentication method page"
	#	sys.exit (1)

	#start_authmethod = time.time()

	#authm = Select (ff.find_element_by_name ("authMethod"))
	#authm.select_by_value ("RSA_Password")
	#ff.find_element_by_name ("loginWithMethod").click();

	#WebDriverWait (ff, 10).until (EC.presence_of_element_located((By.XPATH, "//a[text()='Log out']")))

	try:
		ff.find_element_by_xpath ("//body[contains(.,'" + usr + "')]")
	except:
		print "Error retrieving the password entry page"
		ff.quit()
		sys.exit (2)

	t_pass = time.time()

	pupass = ff.find_element_by_name ("password");
	pupass.send_keys (usrpass)
	ff.find_element_by_name ("loginWithPwd").click();

	try:
		#WebDriverWait (ff, 10).until (EC.presence_of_element_located((By.XPATH, "//a[text()='Log out']")))
		ff.find_element_by_xpath ("//body[contains(.,'Logged on as:')]")
	except:
		print "Error logging in"
		ff.quit()
		sys.exit(2)

	t_login = time.time()

	ff.find_element_by_xpath ("//a[text()='Log Off']").click()

	exit_stat = 0
	try:
		ff.find_element_by_xpath ("//body[contains(.,'Successful Logoff')]")
		print "Successfully logged on to %s and logged off | 'loginpage_get'=%ss;;; 'passpage_get'=%ss;;; 'fulllogin'=%ss;;; 'total'=%ss;;;" % (url, (t_username - t_ini), (t_pass - t_username), (t_login - t_pass), (t_login - t_ini))
	except:
		print "Successfully logged on to %s but could not logoff | 'loginpage_get'=%ss;;; 'passpage_get'=%ss;;; 'fulllogin'=%ss;;; 'total'=%ss;;;" % (url, (t_username - t_ini), (t_pass - t_username), (t_login - t_pass), (t_login - t_ini))
		exit_stat = 2

	time.sleep(1)

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

except EXCEP.NoSuchElementException, msg:

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

except EXCEP.TimeoutException, msg:

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

except EXCEP.WebDriverException, msg:

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

finally:
	ff.quit()

sys.exit (exit_stat)
