#!/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.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
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, fp = init_selenium (['mon200w', 'mon201w', 'mon202w', 'mon203w'])
#ff, fp = init_selenium (['mon203w'])
ff, fp = init_selenium ()

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

	try:
		WebDriverWait (ff, 10).until (EC.presence_of_element_located((By.LINK_TEXT, "log in")))
		t_inipage = time.time()

		ff.find_element_by_link_text ("log in").click ()
	except:
		print "Error retrieving the initial page having a login link"
		sys.exit (1)

	try:
		WebDriverWait (ff, 10).until (EC.presence_of_element_located((By.ID, "Email Address")))
		t_username = time.time()

		puusr = ff.find_element_by_id ("Email Address")
		puusr.send_keys (usr)

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

		ff.find_element_by_name ("e").click();

	except:
		print "Error retrieving the login page"
		sys.exit (1)

	try:
		WebDriverWait (ff, 10).until (EC.presence_of_element_located((By.XPATH, "//label[@for='Lists per Page']")))
		t_logon = time.time()
	except:
		print "Error logging in or retrieving the lists management page"
		sys.exit(1)

	lgout = ff.find_element_by_id ("logout.cell")
	ActionChains(ff).move_to_element(lgout).perform()
	lgout.click()

	exit_stat = 0
	try:
		WebDriverWait (ff, 10).until (EC.text_to_be_present_in_element((By.TAG_NAME, "h2"), 'Logged Out'))
		t_end = time.time()

		print "Successfully logged on to %s, found 'Lists per Page' and logged off | 'initial_page'=%.3fs;;; 'login_page'=%.3fs;;; 'management_page'=%.3fs;;; 'total'=%.3fs;;;" % (url, (t_inipage - t_ini), (t_username - t_inipage), (t_logon - t_username), (t_end - t_logon))
	except:
		exit_stat = 1
		t_end = time.time()
		print "Successfully logged on to %s, found 'Lists per Page' but could not log off | 'initial_page'=%.3fs;;; 'login_page'=%.3fs;;; 'management_page'=%.3fs;;; 'total'=%.3fs;;;" % (url, (t_inipage - t_ini), (t_username - t_inipage), (t_logon - t_username), (t_end - t_ini))

	time.sleep(5)

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)
