<s> import collections <EOL> import six <EOL> from selenium . common import exceptions <EOL> from selenium . webdriver . common import by <EOL> import selenium . webdriver . support . ui as Support <EOL> from openstack_dashboard . test . integration_tests . regions import baseregion <EOL> from openstack_dashboard . test . integration_tests . regions import menus <EOL> class FieldFactory ( baseregion . BaseRegion ) : <EOL> """<STR_LIT>""" <EOL> FORM_FIELDS_TYPES = set ( ) <EOL> _element_locator_str_prefix = '<STR_LIT>' <EOL> def __init__ ( self , driver , conf , src_elem = None ) : <EOL> super ( FieldFactory , self ) . __init__ ( driver , conf , src_elem ) <EOL> def fields ( self ) : <EOL> for field_cls in self . FORM_FIELDS_TYPES : <EOL> locator = ( by . By . CSS_SELECTOR , <EOL> '<STR_LIT>' % ( self . _element_locator_str_prefix , <EOL> field_cls . _element_locator_str_suffix ) ) <EOL> elements = super ( FieldFactory , self ) . _get_elements ( * locator ) <EOL> for element in elements : <EOL> yield field_cls ( self . driver , self . conf , element ) <EOL> @ classmethod <EOL> def register_field_cls ( cls , field_class , base_classes = None ) : <EOL> """<STR_LIT>""" <EOL> cls . FORM_FIELDS_TYPES . add ( field_class ) <EOL> cls . FORM_FIELDS_TYPES -= set ( base_classes ) <EOL> class MetaBaseFormFieldRegion ( type ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( cls , name , bases , dct ) : <EOL> FieldFactory . register_field_cls ( cls , bases ) <EOL> super ( MetaBaseFormFieldRegion , cls ) . __init__ ( name , bases , dct ) <EOL> @ six . add_metaclass ( MetaBaseFormFieldRegion ) <EOL> class BaseFormFieldRegion ( baseregion . BaseRegion ) : <EOL> """<STR_LIT>""" <EOL> _label_locator = None <EOL> _element_locator = None <EOL> @ property <EOL> def label ( self ) : <EOL> return self . _get_element ( * self . _label_locator ) <EOL> @ property <EOL> def element ( self ) : <EOL> return self . src_elem <EOL> @ property <EOL> def name ( self ) : <EOL> return self . element . get_attribute ( '<STR_LIT:name>' ) <EOL> def is_required ( self ) : <EOL> classes = self . driver . get_attribute ( '<STR_LIT:class>' ) <EOL> return '<STR_LIT>' in classes <EOL> def is_displayed ( self ) : <EOL> return self . element . is_displayed ( ) <EOL> class CheckBoxMixin ( object ) : <EOL> @ property <EOL> def label ( self ) : <EOL> id_attribute = self . element . get_attribute ( '<STR_LIT:id>' ) <EOL> return self . element . find_element ( <EOL> by . By . XPATH , '<STR_LIT>' . format ( id_attribute ) ) <EOL> def is_marked ( self ) : <EOL> return self . element . is_selected ( ) <EOL> def mark ( self ) : <EOL> if not self . is_marked ( ) : <EOL> self . label . click ( ) <EOL> def unmark ( self ) : <EOL> if self . is_marked ( ) : <EOL> self . label . click ( ) <EOL> class CheckBoxFormFieldRegion ( CheckBoxMixin , BaseFormFieldRegion ) : <EOL> """<STR_LIT>""" <EOL> _element_locator_str_suffix = '<STR_LIT>' <EOL> class ChooseFileFormFieldRegion ( BaseFormFieldRegion ) : <EOL> """<STR_LIT>""" <EOL> _element_locator_str_suffix = '<STR_LIT>' <EOL> def choose ( self , path ) : <EOL> self . element . send_keys ( path ) <EOL> class BaseTextFormFieldRegion ( BaseFormFieldRegion ) : <EOL> _element_locator = None <EOL> @ property <EOL> def text ( self ) : <EOL> return self . element . text <EOL> @ text . setter <EOL> def text ( self , text ) : <EOL> self . _fill_field_element ( text , self . element ) <EOL> class TextInputFormFieldRegion ( BaseTextFormFieldRegion ) : <EOL> """<STR_LIT>""" <EOL> _element_locator_str_suffix = '<STR_LIT>' <EOL> class PasswordInputFormFieldRegion ( BaseTextFormFieldRegion ) : <EOL> """<STR_LIT>""" <EOL> _element_locator_str_suffix = '<STR_LIT>' <EOL> class EmailInputFormFieldRegion ( BaseTextFormFieldRegion ) : <EOL> """<STR_LIT>""" <EOL> _element_locator_str_suffix = '<STR_LIT>' <EOL> class TextAreaFormFieldRegion ( BaseTextFormFieldRegion ) : <EOL> """<STR_LIT>""" <EOL> _element_locator_str_suffix = '<STR_LIT>' <EOL> class IntegerFormFieldRegion ( BaseFormFieldRegion ) : <EOL> """<STR_LIT>""" <EOL> _element_locator_str_suffix = '<STR_LIT>' <EOL> @ property <EOL> def value ( self ) : <EOL> return self . element . get_attribute ( "<STR_LIT:value>" ) <EOL> @ value . setter <EOL> def value ( self , value ) : <EOL> self . _fill_field_element ( value , self . element ) <EOL> class SelectFormFieldRegion ( BaseFormFieldRegion ) : <EOL> """<STR_LIT>""" <EOL> _element_locator_str_suffix = '<STR_LIT>' <EOL> def is_displayed ( self ) : <EOL> return self . element . _el . is_displayed ( ) <EOL> @ property <EOL> def element ( self ) : <EOL> return Support . Select ( self . src_elem ) <EOL> @ property <EOL> def values ( self ) : <EOL> results = [ ] <EOL> for option in self . element . all_selected_options : <EOL> results . append ( option . get_attribute ( '<STR_LIT:value>' ) ) <EOL> return results <EOL> @ property <EOL> def options ( self ) : <EOL> results = collections . OrderedDict ( ) <EOL> for option in self . element . options : <EOL> results [ option . get_attribute ( '<STR_LIT:value>' ) ] = option . text <EOL> return results <EOL> @ property <EOL> def name ( self ) : <EOL> return self . element . _el . get_attribute ( '<STR_LIT:name>' ) <EOL> @ property <EOL> def text ( self ) : <EOL> return self . element . first_selected_option . text <EOL> @ text . setter <EOL> def text ( self , text ) : <EOL> self . element . select_by_visible_text ( text ) <EOL> @ property <EOL> def value ( self ) : <EOL> return self . element . first_selected_option . get_attribute ( '<STR_LIT:value>' ) <EOL> @ value . setter <EOL> def value ( self , value ) : <EOL> self . element . select_by_value ( value ) <EOL> class BaseFormRegion ( baseregion . BaseRegion ) : <EOL> """<STR_LIT>""" <EOL> _submit_locator = ( by . By . CSS_SELECTOR , '<STR_LIT>' ) <EOL> _cancel_locator = ( by . By . CSS_SELECTOR , '<STR_LIT>' ) <EOL> _default_form_locator = ( by . By . CSS_SELECTOR , '<STR_LIT>' ) <EOL> def __init__ ( self , driver , conf , src_elem = None ) : <EOL> """<STR_LIT>""" <EOL> if src_elem is None : <EOL> self . src_elem = driver <EOL> src_elem = self . _get_elements ( * self . _default_form_locator ) [ - <NUM_LIT:1> ] <EOL> super ( BaseFormRegion , self ) . __init__ ( driver , conf , src_elem ) <EOL> @ property <EOL> def _submit_element ( self ) : <EOL> return self . _get_element ( * self . _submit_locator ) <EOL> def submit ( self ) : <EOL> self . _submit_element . click ( ) <EOL> self . wait_till_spinner_disappears ( ) <EOL> @ property <EOL> def _cancel_element ( self ) : <EOL> return self . _get_element ( * self . _cancel_locator ) <EOL> def cancel ( self ) : <EOL> self . _cancel_element . click ( ) <EOL> class FormRegion ( BaseFormRegion ) : <EOL> """<STR_LIT>""" <EOL> _header_locator = ( by . By . CSS_SELECTOR , '<STR_LIT>' ) <EOL> _side_info_locator = ( by . By . CSS_SELECTOR , '<STR_LIT>' ) <EOL> _fields_locator = ( by . By . CSS_SELECTOR , '<STR_LIT>' ) <EOL> def __init__ ( self , driver , conf , src_elem = None , field_mappings = None ) : <EOL> super ( FormRegion , self ) . __init__ ( driver , conf , src_elem ) <EOL> self . field_mappings = self . _prepare_mappings ( field_mappings ) <EOL> self . wait_till_spinner_disappears ( ) <EOL> self . _init_form_fields ( ) <EOL> def _prepare_mappings ( self , field_mappings ) : <EOL> if isinstance ( field_mappings , tuple ) : <EOL> return { item : item for item in field_mappings } <EOL> else : <EOL> return field_mappings <EOL> def _init_form_fields ( self ) : <EOL> self . fields_src_elem = self . _get_element ( * self . _fields_locator ) <EOL> fields = self . _get_form_fields ( ) <EOL> for accessor_name , accessor_expr in self . field_mappings . items ( ) : <EOL> if isinstance ( accessor_expr , six . string_types ) : <EOL> self . _dynamic_properties [ accessor_name ] = fields [ accessor_expr ] <EOL> else : <EOL> self . _dynamic_properties [ accessor_name ] = accessor_expr ( <EOL> self . driver , self . conf ) <EOL> def _get_form_fields ( self ) : <EOL> factory = FieldFactory ( self . driver , self . conf , self . fields_src_elem ) <EOL> try : <EOL> self . _turn_off_implicit_wait ( ) <EOL> return { field . name : field for field in factory . fields ( ) } <EOL> finally : <EOL> self . _turn_on_implicit_wait ( ) <EOL> def set_field_values ( self , data ) : <EOL> """<STR_LIT>""" <EOL> for field_name in data : <EOL> field = getattr ( self , field_name , None ) <EOL> if field is None : <EOL> raise AttributeError ( "<STR_LIT>" ) <EOL> value = data [ field_name ] <EOL> if value is not None : <EOL> if hasattr ( field , "<STR_LIT:text>" ) : <EOL> field . text = value <EOL> elif hasattr ( field , "<STR_LIT:path>" ) : <EOL> field . path = value <EOL> elif hasattr ( field , "<STR_LIT:value>" ) : <EOL> field . value = value <EOL> @ property <EOL> def header ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _get_element ( * self . _header_locator ) <EOL> @ property <EOL> def sideinfo ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _get_element ( * self . _side_info_locator ) <EOL> @ property <EOL> def fields ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _get_form_fields ( ) <EOL> class TabbedFormRegion ( FormRegion ) : <EOL> """<STR_LIT>""" <EOL> _submit_locator = ( by . By . CSS_SELECTOR , '<STR_LIT>' ) <EOL> _side_info_locator = ( by . By . CSS_SELECTOR , "<STR_LIT>" ) <EOL> def __init__ ( self , driver , conf , field_mappings = None , default_tab = <NUM_LIT:0> ) : <EOL> self . current_tab = default_tab <EOL> super ( TabbedFormRegion , self ) . __init__ ( <EOL> driver , conf , field_mappings = field_mappings ) <EOL> def _prepare_mappings ( self , field_mappings ) : <EOL> return [ super ( TabbedFormRegion , self ) . _prepare_mappings ( tab_mappings ) <EOL> for tab_mappings in field_mappings ] <EOL> def _init_form_fields ( self ) : <EOL> self . switch_to ( self . current_tab ) <EOL> def _init_tab_fields ( self , tab_index ) : <EOL> fieldsets = self . _get_elements ( * self . _fields_locator ) <EOL> self . fields_src_elem = fieldsets [ tab_index ] <EOL> fields = self . _get_form_fields ( ) <EOL> current_tab_mappings = self . field_mappings [ tab_index ] <EOL> for accessor_name , accessor_expr in current_tab_mappings . items ( ) : <EOL> if isinstance ( accessor_expr , six . string_types ) : <EOL> self . _dynamic_properties [ accessor_name ] = fields [ accessor_expr ] <EOL> else : <EOL> self . _dynamic_properties [ accessor_name ] = accessor_expr ( <EOL> self . driver , self . conf ) <EOL> def switch_to ( self , tab_index = <NUM_LIT:0> ) : <EOL> self . tabs . switch_to ( index = tab_index ) <EOL> self . _init_tab_fields ( tab_index ) <EOL> @ property <EOL> def tabs ( self ) : <EOL> return menus . TabbedMenuRegion ( self . driver , self . conf , <EOL> src_elem = self . src_elem ) <EOL> class DateFormRegion ( BaseFormRegion ) : <EOL> """<STR_LIT>""" <EOL> _from_field_locator = ( by . By . CSS_SELECTOR , '<STR_LIT>' ) <EOL> _to_field_locator = ( by . By . CSS_SELECTOR , '<STR_LIT>' ) <EOL> @ property <EOL> def from_date ( self ) : <EOL> return self . _get_element ( * self . _from_field_locator ) <EOL> @ property <EOL> def to_date ( self ) : <EOL> return self . _get_element ( * self . _to_field_locator ) <EOL> def query ( self , start , end ) : <EOL> self . _set_from_field ( start ) <EOL> self . _set_to_field ( end ) <EOL> self . submit ( ) <EOL> def _set_from_field ( self , value ) : <EOL> self . _fill_field_element ( value , self . from_date ) <EOL> def _set_to_field ( self , value ) : <EOL> self . _fill_field_element ( value , self . to_date ) <EOL> class MetadataFormRegion ( BaseFormRegion ) : <EOL> _input_fields = ( by . By . CSS_SELECTOR , '<STR_LIT>' ) <EOL> _custom_input_field = ( by . By . XPATH , "<STR_LIT>" ) <EOL> _custom_input_button = ( by . By . CSS_SELECTOR , '<STR_LIT>' ) <EOL> _submit_locator = ( by . By . CSS_SELECTOR , '<STR_LIT>' ) <EOL> _cancel_locator = ( by . By . CSS_SELECTOR , '<STR_LIT>' ) <EOL> def _form_getter ( self ) : <EOL> return self . driver . find_element ( * self . _default_form_locator ) <EOL> @ property <EOL> def custom_field_value ( self ) : <EOL> return self . _get_element ( * self . _custom_input_field ) <EOL> @ property <EOL> def add_button ( self ) : <EOL> return self . _get_element ( * self . _custom_input_button ) <EOL> def add_custom_field ( self , field_name , field_value ) : <EOL> self . custom_field_value . send_keys ( field_name ) <EOL> self . add_button . click ( ) <EOL> for div in self . _get_elements ( * self . _input_fields ) : <EOL> if div . text in field_name : <EOL> field = div . find_element ( by . By . CSS_SELECTOR , '<STR_LIT:input>' ) <EOL> if not hasattr ( self , field_name ) : <EOL> self . _dynamic_properties [ field_name ] = field <EOL> self . set_field_value ( field_name , field_value ) <EOL> def set_field_value ( self , field_name , field_value ) : <EOL> if hasattr ( self , field_name ) : <EOL> field = getattr ( self , field_name ) <EOL> field . send_keys ( field_value ) <EOL> else : <EOL> raise AttributeError ( "<STR_LIT>" . format ( field_name ) ) <EOL> def wait_till_spinner_disappears ( self ) : <EOL> try : <EOL> self . wait_till_element_disappears ( self . _form_getter ) <EOL> except exceptions . StaleElementReferenceException : <EOL> pass <EOL> class ItemTextDescription ( baseregion . BaseRegion ) : <EOL> _separator_locator = ( by . By . CSS_SELECTOR , '<STR_LIT>' ) <EOL> _key_locator = ( by . By . CSS_SELECTOR , '<STR_LIT>' ) <EOL> _value_locator = ( by . By . CSS_SELECTOR , '<STR_LIT>' ) <EOL> def __init__ ( self , driver , conf , src = None ) : <EOL> super ( ItemTextDescription , self ) . __init__ ( driver , conf , src ) <EOL> def get_content ( self ) : <EOL> keys = [ ] <EOL> values = [ ] <EOL> for section in self . _get_elements ( * self . _separator_locator ) : <EOL> keys . extend ( [ x . text for x in <EOL> section . find_elements ( * self . _key_locator ) ] ) <EOL> values . extend ( [ x . text for x in <EOL> section . find_elements ( * self . _value_locator ) ] ) <EOL> return dict ( zip ( keys , values ) ) </s>
