<s> from selenium . common . exceptions import NoAlertPresentException <EOL> from waiting import wait <EOL> import webium . settings <EOL> from webium . driver import get_driver <EOL> class WindowsHandler ( object ) : <EOL> @ property <EOL> def _driver ( self ) : <EOL> if self . __driver : <EOL> return self . __driver <EOL> return get_driver ( ) <EOL> def __init__ ( self , driver = None ) : <EOL> self . __driver = driver <EOL> self . _snapshot_of_handles = set ( ) <EOL> self . _active_window = None <EOL> self . _parent_windows = [ ] <EOL> def save_window_set ( self ) : <EOL> self . _snapshot_of_handles = set ( self . _driver . window_handles ) <EOL> @ property <EOL> def new_window ( self ) : <EOL> new_handles = set ( self . _driver . window_handles ) <EOL> new_handles = new_handles - self . _snapshot_of_handles <EOL> return iter ( new_handles ) . next ( ) <EOL> @ property <EOL> def active_window ( self ) : <EOL> self . _active_window = self . _driver . current_window_handle <EOL> return self . _active_window <EOL> @ property <EOL> def handles ( self ) : <EOL> return self . _driver . window_handles <EOL> def switch_to_window ( self , window_handle ) : <EOL> self . _driver . switch_to_window ( window_handle ) <EOL> def switch_to_new_window ( self ) : <EOL> self . _parent_windows . append ( self . active_window ) <EOL> self . switch_to_window ( self . new_window ) <EOL> self . _active_window = self . _driver . current_window_handle <EOL> def switch_to_parent_window ( self ) : <EOL> self . switch_to_window ( self . _parent_windows . pop ( ) ) <EOL> self . _active_window = self . _driver . current_window_handle <EOL> def close_active_window ( self ) : <EOL> self . _driver . close ( ) <EOL> self . drop_active_window ( ) <EOL> def drop_active_window ( self ) : <EOL> self . switch_to_window ( self . _parent_windows . pop ( ) ) <EOL> self . _active_window = self . _driver . current_window_handle <EOL> def does_active_window_exist ( self ) : <EOL> temp_handles = set ( self . _driver . window_handles ) <EOL> return self . _active_window in temp_handles <EOL> def wait_for_active_window_is_closed ( self ) : <EOL> wait ( lambda : self . does_active_window_exist ( ) , waiting_for = '<STR_LIT>' , <EOL> timeout_seconds = webium . settings . wait_timeout ) <EOL> def create_window ( self ) : <EOL> self . save_window_set ( ) <EOL> self . _driver . execute_script ( "<STR_LIT>" ) <EOL> return self . new_window <EOL> def is_alert_present ( self ) : <EOL> try : <EOL> self . get_alert_text ( ) <EOL> return True <EOL> except NoAlertPresentException : <EOL> return False <EOL> def is_new_window_present ( self ) : <EOL> return len ( self . _driver . window_handles ) - len ( self . _snapshot_of_handles ) > <NUM_LIT:0> <EOL> def accept_alert ( self ) : <EOL> self . _driver . switch_to_alert ( ) . accept ( ) <EOL> def get_alert_text ( self ) : <EOL> return self . _driver . switch_to_alert ( ) . text </s>
