#!/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("Self-Service Console"))
		ff.find_element_by_name ("textFieldUserID")
	except:
		print "Error retrieving the initial page having a login textbox"
		#ff.quit()
		sys.exit (2)

	t_username = time.time()

	puusr = ff.find_element_by_name ("textFieldUserID");
	puusr.send_keys (usr)
	ff.find_element_by_name ("loginToMyAccount").click();

	try:
		ff.find_element_by_xpath ("//label[contains(.,'Password:')]")
	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(.,'This page allows you to view your user profile and manage your authenticators')]")
	except:
		print "Error logging in"
		#ff.quit()
		sys.exit(2)

	t_logon = time.time()

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

	exit_stat = 0
	try:
		ff.find_element_by_xpath ("//body[contains(.,'Log on with your')]")
		print "Successfully logged on to %s and logged off | 'username'=%ss;;; 'pass'=%ss;;; 'logon'=%ss;;; 'total'=%ss;;;" % (url, (t_username - t_ini), (t_pass - t_username), (t_logon - t_pass), (t_logon - t_ini))
	except:
		exit_stat = 2
		print "Successfully logged on to %s but could not log off | 'username'=%ss;;; 'pass'=%ss;;; 'logon'=%ss;;; 'total'=%ss;;;" % (url, (t_username - t_ini), (t_pass - t_username), (t_logon - t_pass), (t_logon - t_ini))

	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)
