<s> from functools import wraps <EOL> from selenium import webdriver <EOL> from selenium . common . exceptions import WebDriverException <EOL> import time <EOL> import os <EOL> from django . db import transaction <EOL> from django . core . urlresolvers import reverse <EOL> from django . test import TransactionTestCase <EOL> from django . utils . html import strip_tags <EOL> from django_selenium import settings , selenium_server <EOL> def wait ( func ) : <EOL> @ wraps ( func ) <EOL> def wrapper ( self , * args , ** kwargs ) : <EOL> i = settings . SELENIUM_DRIVER_TIMEOUT <EOL> if '<STR_LIT>' in kwargs : <EOL> i = kwargs . pop ( '<STR_LIT>' ) <EOL> res = func ( self , * args , ** kwargs ) <EOL> while not res and i : <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> res = func ( self , * args , ** kwargs ) <EOL> i -= <NUM_LIT:1> <EOL> return res <EOL> return wrapper <EOL> class SeleniumElement ( object ) : <EOL> def __init__ ( self , elements , selector ) : <EOL> """<STR_LIT>""" <EOL> self . elements = elements <EOL> self . selector = selector <EOL> def __getattribute__ ( self , name ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> attr = object . __getattribute__ ( self , '<STR_LIT>' ) [ <NUM_LIT:0> ] . __getattribute__ ( name ) <EOL> except IndexError : <EOL> raise NoElementException ( u'<STR_LIT>' . format ( object . __getattribute__ ( self , '<STR_LIT>' ) ) ) <EOL> return attr <EOL> def __getitem__ ( self , key ) : <EOL> """<STR_LIT>""" <EOL> return object . __getattribute__ ( self , '<STR_LIT>' ) [ key ] <EOL> class NoElementException ( Exception ) : <EOL> pass <EOL> class MyDriver ( object ) : <EOL> def __init__ ( self ) : <EOL> driver = getattr ( webdriver , settings . SELENIUM_DRIVER , None ) <EOL> assert driver , "<STR_LIT>" <EOL> driver_opts = getattr ( settings , "<STR_LIT>" , dict ( ) ) <EOL> if driver is webdriver . Remote : <EOL> if isinstance ( settings . SELENIUM_CAPABILITY , dict ) : <EOL> capability = settings . SELENIUM_CAPABILITY <EOL> else : <EOL> capability = getattr ( webdriver . DesiredCapabilities , settings . SELENIUM_CAPABILITY , None ) <EOL> assert capability , '<STR_LIT>' <EOL> self . driver = driver ( '<STR_LIT>' % ( settings . SELENIUM_HOST , settings . SELENIUM_PORT ) , capability , ** driver_opts ) <EOL> else : <EOL> self . driver = driver ( ** driver_opts ) <EOL> self . live_server_url = '<STR_LIT>' % ( settings . SELENIUM_TESTSERVER_HOST , str ( settings . SELENIUM_TESTSERVER_PORT ) ) <EOL> self . text = '<STR_LIT>' <EOL> def __getattribute__ ( self , name ) : <EOL> try : <EOL> attr = object . __getattribute__ ( self , name ) <EOL> except AttributeError : <EOL> attr = self . driver . __getattribute__ ( name ) <EOL> return attr <EOL> def _wait_for_page_source ( self ) : <EOL> try : <EOL> page_source = self . page_source <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> while page_source != self . page_source : <EOL> page_source = self . page_source <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> self . update_text ( ) <EOL> except WebDriverException : <EOL> pass <EOL> def authorize ( self , username , password ) : <EOL> self . open_url ( reverse ( '<STR_LIT>' ) ) <EOL> self . type_in ( "<STR_LIT>" , username ) <EOL> self . type_in ( "<STR_LIT>" , password ) <EOL> self . click ( "<STR_LIT>" ) <EOL> def update_text ( self ) : <EOL> """<STR_LIT>""" <EOL> self . text = strip_tags ( unicode ( self . page_source ) ) <EOL> def open_url ( self , url ) : <EOL> """<STR_LIT>""" <EOL> self . get ( '<STR_LIT>' % ( self . live_server_url , url ) ) <EOL> self . _wait_for_page_source ( ) <EOL> def click ( self , selector ) : <EOL> """<STR_LIT>""" <EOL> self . find ( selector ) . click ( ) <EOL> self . _wait_for_page_source ( ) <EOL> def click_and_wait ( self , selector , newselector ) : <EOL> """<STR_LIT>""" <EOL> self . click ( selector ) <EOL> return self . wait_element_present ( newselector ) <EOL> def is_element_present ( self , selector ) : <EOL> """<STR_LIT>""" <EOL> return len ( self . find_elements_by_css_selector ( selector ) ) > <NUM_LIT:0> <EOL> def is_text_present ( self , text ) : <EOL> """<STR_LIT>""" <EOL> return text in self . text <EOL> def get_alert_text ( self ) : <EOL> """<STR_LIT>""" <EOL> alert = self . switch_to_alert ( ) <EOL> try : <EOL> text = alert . text [ '<STR_LIT:text>' ] <EOL> except TypeError : <EOL> text = alert . text <EOL> alert . dismiss ( ) <EOL> self . switch_to_default_content ( ) <EOL> self . update_text ( ) <EOL> return text <EOL> def get_text ( self , selector ) : <EOL> return self . find ( selector ) . text <EOL> def drop_image ( self , file_path , droparea_selector , append_to ) : <EOL> """<STR_LIT>""" <EOL> self . execute_script ( "<STR_LIT>" + append_to + "<STR_LIT>" ) <EOL> self . find ( '<STR_LIT>' ) . send_keys ( os . path . join ( os . getcwd ( ) , file_path ) ) <EOL> self . execute_script ( '<STR_LIT>' ) <EOL> self . execute_script ( "<STR_LIT>" + droparea_selector + "<STR_LIT>" ) <EOL> self . execute_script ( "<STR_LIT>" ) <EOL> @ wait <EOL> def wait_for_text ( self , selector , text ) : <EOL> return text in self . find ( selector ) . text <EOL> @ wait <EOL> def wait_for_visible ( self , selector , visible = True ) : <EOL> return self . find ( selector ) . is_displayed ( ) == visible <EOL> @ wait <EOL> def wait_element_present ( self , selector , present = True ) : <EOL> return self . is_element_present ( selector ) == present <EOL> def get_title ( self ) : <EOL> return self . title <EOL> def get_value ( self , selector ) : <EOL> return self . find ( selector ) . get_attribute ( '<STR_LIT:value>' ) <EOL> def find ( self , cssselector ) : <EOL> """<STR_LIT>""" <EOL> return SeleniumElement ( self . find_elements_by_css_selector ( cssselector ) , cssselector ) <EOL> def select ( self , selector , value ) : <EOL> self . click ( selector + ( "<STR_LIT>" % value ) ) <EOL> def type_in ( self , selector , text ) : <EOL> elem = self . find ( selector ) <EOL> elem . clear ( ) <EOL> elem . send_keys ( text ) <EOL> class SeleniumTestCase ( TransactionTestCase ) : <EOL> def __getattribute__ ( self , name ) : <EOL> try : <EOL> attr = object . __getattribute__ ( self , name ) <EOL> except AttributeError : <EOL> attr = object . __getattribute__ ( self , '<STR_LIT>' ) . __getattribute__ ( name ) <EOL> return attr <EOL> def _fixture_setup ( self ) : <EOL> test_server = selenium_server . get_test_server ( ) <EOL> test_server . deactivate ( ) <EOL> transaction . commit_unless_managed ( ) <EOL> transaction . enter_transaction_management ( ) <EOL> transaction . managed ( True ) <EOL> super ( SeleniumTestCase , self ) . _fixture_setup ( ) <EOL> transaction . commit ( ) <EOL> transaction . leave_transaction_management ( ) <EOL> test_server . activate ( ) <EOL> def setUp ( self ) : <EOL> import socket <EOL> socket . setdefaulttimeout ( settings . SELENIUM_TIMEOUT ) <EOL> self . driver = MyDriver ( ) <EOL> def tearDown ( self ) : <EOL> self . driver . quit ( ) </s>
