Created
September 26, 2015 00:35
-
-
Save fletch3555/13c12a014837d192e106 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import httplib | |
import json | |
import os | |
import time | |
import sys | |
import unittest | |
import socket | |
import __main__ as main | |
from selenium import webdriver | |
from selenium.common import exceptions | |
from selenium.common.exceptions import NoSuchElementException | |
""" | |
class Timeout | |
Prints execution times for blocks of code. | |
Raises a timeout exception if the block took longer than the | |
specified timeout in seconds to complete. | |
""" | |
class Timeout(): | |
""" | |
Takes a timeout in seconds and name to indentify the block | |
""" | |
def __init__(self, timeout, name): | |
self.timeout = timeout | |
self.name = name | |
def __enter__(self): | |
self.start = time.time() | |
""" | |
On block exit check that the block executed within the timeout. | |
If it did, print the time the block took to execute. | |
If it did not, raise a timeout exception. | |
""" | |
def __exit__(self, *args): | |
runtime = time.time() - self.start | |
host = 'monitoring.siteworx.com' | |
port = 2003 | |
try: | |
sock = socket.socket() | |
sock.connect((host,port)) | |
except (host): | |
pass | |
if (runtime > self.timeout): | |
msg = '"{0}" exceeded timeout of {1} seconds'.format(self.name , self.timeout) | |
data = '{0}.{1}.Latency {2} {3}\n'.format("_".join( main.__file__.split() ).split('.')[0] , "_".join( self.name.split() ) , self.timeout , int(time.time())) | |
data = data + '{0}.{1}.Uptime 0 {2}\n'.format("_".join( main.__file__.split() ).split('.')[0] , "_".join( self.name.split() ) , int(time.time())) | |
sock.send(data) | |
print data | |
raise Exception(msg) | |
else: | |
print '"{0}" took {1} seconds'.format(self.name , runtime) | |
#data = '{0}.{1} {2} {3}\n'.format("_".join( main.__file__.split() ).split('.')[0] , "_".join( self.name.split() ) , int(runtime*1000) , int(time.time())) | |
data = '{0}.{1}.Latency {2} {3}\n'.format("_".join( main.__file__.split() ).split('.')[0] , "_".join( self.name.split() ) , runtime , int(time.time())) | |
print sys.exc_info() | |
if (sys.exc_info()[0]): | |
data = data + '{0}.{1}.Uptime 0 {2}\n'.format("_".join( main.__file__.split() ).split('.')[0] , "_".join( self.name.split() ) , int(time.time())) | |
else: | |
data = data + '{0}.{1}.Uptime 1 {2}\n'.format("_".join( main.__file__.split() ).split('.')[0] , "_".join( self.name.split() ) , int(time.time())) | |
sock.send(data) | |
print data | |
sock.close() | |
class TimeoutTests(unittest.TestCase): | |
""" | |
TODO : Description of test | |
""" | |
def log_errors(func): | |
def wrapper(*arg): | |
that = arg[0] | |
that.e = None | |
try: | |
return func(*arg) | |
except: | |
that.e = sys.exc_info() | |
that.driver.save_screenshot('error.png') | |
raise | |
return wrapper | |
@log_errors | |
def testPages(self): | |
with Timeout(20, "Contact Us"): | |
self.driver.get("http://www.mandarinoriental.com/") | |
find_languageEN = lambda: self.driver.find_element_by_link_text("ENGLISH") | |
verify_languageEN = self.wait_for(find_languageEN, 10) | |
verify_languageEN.click() | |
find_languageDE = lambda: self.driver.find_element_by_link_text("DEUTSCH") | |
verify_languageDE = self.wait_for(find_languageDE, 10) | |
verify_languageDE.click() | |
#time.sleep(2.0) | |
with Timeout(20, "Contact Us"): | |
find_contact = lambda: self.driver.find_element_by_link_text("KONTAKT") | |
verify_contact = self.wait_for(find_contact, 10) | |
verify_contact.click() | |
#time.sleep(2.0) | |
find_button = lambda: self.driver.find_element_by_id("uniform-toll-free-input") | |
verify_button = self.wait_for(find_button, 10) | |
verify_button.click() | |
with Timeout(15, "Contact Us"): | |
html_source = self.driver.page_source | |
if 'FREIE RESERVIERUNG' not in html_source: | |
sys.exit("Toll Free Header Not Found") | |
def setUp(self): | |
#self.driver = webdriver.Firefox() | |
# self.driver = webdriver.PhantomJS("/opt/node/lib/node_modules/phantomjs/bin/phantomjs") | |
self.driver = webdriver.PhantomJS() | |
self.driver.set_window_size(1280,720) | |
def tearDown(self): | |
self.driver.quit() | |
""" | |
Wait for an element to exist. Takes a lamdba function that finds the element | |
and a timeout value. | |
""" | |
def wait_for(self, func, timeout): | |
start_time = time.time() | |
cur_time = start_time | |
while (cur_time - start_time < timeout): | |
try: | |
return func() | |
except(exceptions.NoSuchElementException): | |
pass | |
cur_time = time.time() | |
time.sleep(0.2) | |
raise Exception("Timed out looking for element after " + timeout + " seconds") | |
def check_exists_by_class_name(self, classname): | |
try: | |
class_visible = self.driver.find_element_by_class_name(classname) | |
if class_visible.is_displayed(): | |
return True | |
else: | |
return False | |
except NoSuchElementException: | |
return False | |
return True | |
def _testname(self): | |
return self.id().split(".").pop() | |
if __name__ == "__main__": | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment