Home | History | Annotate | Download | only in ap_configurators
      1 # Copyright (c) 2013 The Chromium Authors. All rights reserved.
      2 # Use of this source code is governed by a BSD-style license that can be
      3 # found in the LICENSE file.
      4 
      5 import os
      6 import dynamic_ap_configurator
      7 import ap_spec
      8 
      9 
     10 class Keeboxw150nrAPConfigurator(
     11         dynamic_ap_configurator.DynamicAPConfigurator):
     12     """Derived class to control the Keeboxw150nr AP."""
     13 
     14 
     15     def navigate_to_page(self, page_number):
     16         """
     17         Navigate to the required page.
     18         """
     19         if page_number == 1:
     20             page_url = os.path.join(self.admin_interface_url ,'wlan2_basic.php')
     21         elif page_number == 2:
     22             page_url = os.path.join(self.admin_interface_url ,
     23                                     'wlan2_security.php')
     24         else:
     25             raise RuntimeError('Invalid page number passed.  Number of pages is'
     26                                '%d, page value sent was %d' %
     27                                (self.get_number_of_pages(), page_number))
     28         self.get_url(page_url, page_title='HOME')
     29         # Check if we have to login
     30         if self.admin_login_needed(page_url):
     31             self.ap_login()
     32 
     33 
     34     def admin_login_needed(self, page_url):
     35         """
     36         Check if we are on the admin login page.
     37 
     38         @param page_url: string, the page to open.
     39 
     40         @return True if login needed False otherwise.
     41         """
     42         login_element = '//input[@value="Login"]'
     43         apply_element = '//input[@value="Apply"]'
     44         login_displayed = self.wait_for_objects_by_xpath([login_element,
     45                                                           apply_element])
     46         if login_displayed == login_element:
     47             return True
     48         elif login_displayed == apply_element:
     49             return False
     50         else:
     51             raise Exception('The page %s did not load' % page_url)
     52 
     53 
     54     def ap_login(self):
     55         """
     56         Login as admin before configuring settings.
     57         """
     58         self.set_content_of_text_field_by_id('admin', 'loginusr',
     59                                              abort_check=True)
     60         self.set_content_of_text_field_by_id('password', 'loginpwd',
     61                                              abort_check=True)
     62         self.click_button_by_xpath('//input[@value="Login"]')
     63         self.wait_for_object_by_id('content')
     64 
     65 
     66     def save_page(self,page_number):
     67         xpath_apply = ('//input[@type="button" and @value="Apply"]')
     68         xpath_continue = ('//input[@type="button" and @value="Continue"]')
     69         self.click_button_by_xpath(xpath_apply,
     70                                    alert_handler=self._alert_handler)
     71         try:
     72             element = self.wait_for_object_by_xpath(xpath_continue)
     73             self.click_button_by_xpath(xpath_continue,
     74                                        alert_handler=self._alert_handler)
     75         except:
     76             self._handle_alert(xpath_apply, self._alert_handler)
     77         self.wait_for_object_by_xpath(xpath_apply)
     78 
     79 
     80     def get_number_of_pages(self):
     81         return 2
     82 
     83 
     84     def set_ssid(self, ssid):
     85         self.add_item_to_command_list(self._set_ssid, (ssid,), 1, 1000)
     86 
     87 
     88     def _set_ssid(self, ssid):
     89         self.set_content_of_text_field_by_id(ssid, 'ssid')
     90         self._ssid = ssid
     91 
     92 
     93     def set_radio(self, enabled=True):
     94         self.add_item_to_command_list(self._set_radio, (enabled, ), 1, 1000)
     95 
     96 
     97     def _set_radio(self, enabled=True):
     98         # value=1 is ON; value=0 is OFF
     99         int_value = int(enabled)
    100         xpath = ('//input[@value="%d" and @name="en_wifi"]' %
    101                  int_value)
    102         self.click_button_by_xpath(xpath, alert_handler=self._alert_handler)
    103 
    104 
    105     def is_update_interval_supported(self):
    106         """
    107         Returns True if setting the PSK refresh interval is supported.
    108 
    109         @return True is supported; False otherwise
    110         """
    111         return False
    112 
    113 
    114     def set_band(self, band):
    115         return None
    116 
    117 
    118     def is_security_mode_supported(self, security_mode):
    119         return security_mode in (ap_spec.SECURITY_TYPE_DISABLED,
    120                                  ap_spec.SECURITY_TYPE_WEP,
    121                                  ap_spec.SECURITY_TYPE_WPAPSK,
    122                                  ap_spec.SECURITY_TYPE_WPA2PSK)
    123 
    124 
    125     def get_supported_bands(self):
    126         return [{'band': ap_spec.BAND_2GHZ, 'channels': range(1, 12)}]
    127 
    128 
    129     def set_security_disabled(self):
    130         self.add_item_to_command_list(self._set_security_disabled, (), 2, 1000)
    131 
    132 
    133     def _set_security_disabled(self):
    134         self.wait_for_object_by_id('security_type')
    135         self.select_item_from_popup_by_id('Disable', 'security_type')
    136 
    137     def get_supported_modes(self):
    138         return [{'band': ap_spec.BAND_2GHZ,
    139                  'modes': [ap_spec.MODE_B,
    140                            ap_spec.MODE_G,
    141                            ap_spec.MODE_N,
    142                            ap_spec.MODE_B | ap_spec.MODE_G,
    143                            ap_spec.MODE_B | ap_spec.MODE_G | ap_spec.MODE_N]}]
    144 
    145 
    146     def set_mode(self, mode, band=None):
    147         self.add_item_to_command_list(self._set_mode, (mode,), 1, 1000)
    148 
    149 
    150     def _set_mode(self, mode, band=None):
    151         # Different bands are not supported so we ignore.
    152         # Create the mode to popup item mapping
    153         mode_mapping = {ap_spec.MODE_B | ap_spec.MODE_G | ap_spec.MODE_N:
    154                         '2.4 GHz (802.11b/g/n)',
    155                         ap_spec.MODE_N: '2.4 GHz (802.11n)',
    156                         ap_spec.MODE_B: '2.4 GHz (802.11b)',
    157                         ap_spec.MODE_G: '2.4 GHz (802.11g)',
    158                         ap_spec.MODE_B | ap_spec.MODE_G:
    159                         '2.4 GHz (802.11b/g)'}
    160         mode_name = ''
    161         if mode in mode_mapping.keys():
    162             mode_name = mode_mapping[mode]
    163         else:
    164             raise RuntimeError('The mode selected %d is not supported by router'
    165                                ' %s.', hex(mode), self.name)
    166         self.select_item_from_popup_by_id(mode_name, 'wlan_mode')
    167 
    168 
    169     def get_supported_bands(self):
    170         return [{'band': ap_spec.BAND_2GHZ, 'channels': range(1, 11)}]
    171 
    172 
    173     def set_channel(self, channel):
    174         self.add_item_to_command_list(self._set_channel, (channel,), 1, 900)
    175 
    176 
    177     def _set_channel(self, channel):
    178         position = self._get_channel_popup_position(channel)
    179         channel_choices = ['1', '2', '3', '4', '5',
    180                            '6', '7', '8', '9', '10', '11']
    181         self.select_item_from_popup_by_id(channel_choices[position],
    182                                           'channel')
    183 
    184 
    185     def set_visibility(self, visible=True):
    186         self.add_item_to_command_list(self._set_visibility, (visible,), 2, 1000)
    187 
    188 
    189     def _set_visibility(self, visible=True):
    190         # value=1 is visible; value=0 is invisible
    191         if visible == True:
    192            self.select_item_from_popup_by_id('Enable', 'suppress')
    193         else:
    194            self.select_item_from_popup_by_id('Disable', 'suppress')
    195 
    196 
    197     def set_security_wep(self, key_value, authentication):
    198         self.add_item_to_command_list(self._set_security_wep,
    199                                       (key_value, authentication), 2, 900)
    200 
    201 
    202     def _set_security_wep(self, key_value, authentication):
    203         # This AP does not allow selecting Shared Key. Open System is default.
    204         self.wait_for_object_by_id('security_type')
    205         self.select_item_from_popup_by_id('WEP', 'security_type')
    206         self.select_item_from_popup_by_id('64-bit', 'wep_key_len')
    207         self.select_item_from_popup_by_id('ASCII (5 characters)',
    208                                           'key_str_type')
    209         self.set_content_of_text_field_by_id(key_value, 'wep_key_1')
    210 
    211 
    212     def set_security_wpapsk(self, security, shared_key, update_interval=None):
    213         self.add_item_to_command_list(self._set_security_wpapsk,
    214                                       (security,shared_key, update_interval),
    215                                        2, 900)
    216 
    217 
    218     def _set_security_wpapsk(self, security, shared_key, update_interval=None):
    219         self.wait_for_object_by_id('security_type')
    220         if security == ap_spec.SECURITY_TYPE_WPAPSK:
    221             self.select_item_from_popup_by_id('WPA Only', 'security_type')
    222         else:
    223             self.select_item_from_popup_by_id('WPA2 Only', 'security_type')
    224         self.select_item_from_popup_by_id('Passphrase', 'id_shared_keytype')
    225         self.set_content_of_text_field_by_id(shared_key, 'wpapsk')
    226 
    227 
    228     def _alert_handler(self, alert):
    229         """Checks for any modal dialogs which popup to alert the user and
    230         either raises a RuntimeError or ignores the alert.
    231 
    232         Args:
    233           alert: The modal dialog's contents.
    234         """
    235         text = alert.text
    236         if 'Your wireless settings have changed' in text:
    237             alert.accept()
    238         elif 'Settings have not changed' in text:
    239             alert.accept()
    240         else:
    241             raise RuntimeError('We have an unhandled alert: %s' % text)
    242 
    243