<s> import re <EOL> import time <EOL> from urlparse import urljoin <EOL> from selenium . webdriver . support . ui import WebDriverWait <EOL> from selenium . common . exceptions import NoSuchElementException <EOL> from selenium . common . exceptions import ElementNotVisibleException <EOL> http_regex = re . compile ( '<STR_LIT>' ) <EOL> class Page ( object ) : <EOL> URL_TEMPLATE = None <EOL> def __init__ ( self , base_url , selenium , timeout = <NUM_LIT:10> , ** url_kwargs ) : <EOL> self . base_url = base_url <EOL> self . selenium = selenium <EOL> self . timeout = timeout <EOL> self . url_kwargs = url_kwargs <EOL> self . wait = WebDriverWait ( self . selenium , self . timeout ) <EOL> @ property <EOL> def canonical_url ( self ) : <EOL> if self . URL_TEMPLATE is not None : <EOL> return urljoin ( self . base_url , <EOL> self . URL_TEMPLATE . format ( ** self . url_kwargs ) ) <EOL> return self . base_url <EOL> def open ( self ) : <EOL> self . selenium . get ( self . canonical_url ) <EOL> self . wait_for_page_to_load ( ) <EOL> return self <EOL> def wait_for_page_to_load ( self ) : <EOL> self . wait . until ( lambda s : self . canonical_url in s . current_url ) <EOL> return self <EOL> @ property <EOL> def is_the_current_page ( self ) : <EOL> if self . _page_title : <EOL> assert self . _page_title == self . page_title <EOL> @ property <EOL> def url_current_page ( self ) : <EOL> return ( self . selenium . current_url ) <EOL> @ property <EOL> def page_title ( self ) : <EOL> WebDriverWait ( self . selenium , self . timeout ) . until ( lambda s : s . title ) <EOL> return self . selenium . title <EOL> def refresh ( self ) : <EOL> self . selenium . refresh ( ) <EOL> def is_element_present ( self , * locator ) : <EOL> self . selenium . implicitly_wait ( <NUM_LIT:0> ) <EOL> try : <EOL> self . selenium . find_element ( * locator ) <EOL> return True <EOL> except NoSuchElementException : <EOL> return False <EOL> finally : <EOL> self . selenium . implicitly_wait ( <NUM_LIT:10> ) <EOL> def is_element_visible ( self , * locator ) : <EOL> try : <EOL> return self . selenium . find_element ( * locator ) . is_displayed ( ) <EOL> except ( NoSuchElementException , ElementNotVisibleException ) : <EOL> return False <EOL> def wait_for_element_present ( self , * locator ) : <EOL> count = <NUM_LIT:0> <EOL> while not self . is_element_present ( * locator ) : <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> count += <NUM_LIT:1> <EOL> if count == self . timeout : <EOL> raise Exception ( * locator + '<STR_LIT>' ) <EOL> def wait_for_element_visible ( self , * locator ) : <EOL> count = <NUM_LIT:0> <EOL> while not self . is_element_visible ( * locator ) : <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> count += <NUM_LIT:1> <EOL> if count == self . timeout : <EOL> raise Exception ( * locator + "<STR_LIT>" ) <EOL> def wait_for_ajax ( self ) : <EOL> count = <NUM_LIT:0> <EOL> while count < self . timeout : <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> count += <NUM_LIT:1> <EOL> if self . selenium . execute_script ( "<STR_LIT>" ) : <EOL> return <EOL> raise Exception ( "<STR_LIT>" % count ) </s>
