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 6 import urlparse 7 8 import ap_spec 9 import belkin_ap_configurator 10 11 12 class BelkinWRTRAPConfigurator( 13 belkin_ap_configurator.BelkinAPConfigurator): 14 """Base class for Belkin G EB1389 router.""" 15 16 17 def is_security_mode_supported(self, security_mode): 18 """ 19 Returns if a given security_type is supported. 20 21 @param security_mode: one security modes defined in the APSpec 22 23 @return True if the security mode is supported; False otherwise. 24 25 """ 26 return security_mode in (ap_spec.SECURITY_TYPE_DISABLED, 27 ap_spec.SECURITY_TYPE_WPAPSK, 28 ap_spec.SECURITY_TYPE_WPA2PSK,) 29 30 31 def get_supported_modes(self): 32 return [{'band': ap_spec.BAND_2GHZ, 33 'modes': [ap_spec.MODE_G, ap_spec.MODE_G | ap_spec.MODE_B]}] 34 35 36 def navigate_to_page(self, page_number): 37 """ 38 Navigates to the page corresponding to the given page number. 39 40 This method performs the translation between a page number and a url to 41 load. This is used internally by apply_settings. 42 43 @param page_number: page number of the page to load 44 45 """ 46 if page_number == 1: 47 page_url = urlparse.urljoin(self.admin_interface_url, 48 'wireless_chan.htm') 49 self._load_page(page_url, page_title='SSID') 50 elif page_number == 2: 51 page_url = urlparse.urljoin(self.admin_interface_url, 52 'wireless_encrypt_64.htm') 53 self._load_page(page_url, page_title='Security') 54 else: 55 raise RuntimeError('Invalid page number passed. Number of pages ' 56 '%d, page value sent was %d' % 57 (self.get_number_of_pages(), page_number)) 58 59 60 def _load_page(self, page_url, page_title): 61 """ 62 Load the given page and login if required. 63 64 @param page_url: The complete page url to load. 65 @param page_title: The page title that we expect. 66 """ 67 try: 68 self.get_url(page_url, page_title) 69 except: 70 if 'Login' in self.driver.title: 71 xpath = '//input[@name="login_password"]' 72 self.set_content_of_text_field_by_xpath('password', xpath, 73 abort_check=True) 74 submit = '//input[@class="submitBtn" and @value="Submit"]' 75 self.click_button_by_xpath(submit) 76 finally: 77 self.wait.until(lambda _: page_title in self.driver.title) 78 79 80 def save_page(self, page_number): 81 """ 82 Saves the given page. 83 84 @param page_number: Page number of the page to save. 85 86 """ 87 xpath = '//input[@value="Apply Changes" and @class="submitBtn"]' 88 self.click_button_by_xpath(xpath, alert_handler=self._security_alert) 89 # Give belkin some time to save settings and come to login page. 90 try: 91 self.wait_for_object_by_xpath('//input[@name="login_password"]', 92 wait_time=20) 93 except: 94 raise RuntimeError('Settings were not saved. We did not find the ' 95 'login text field.') 96 97 98 def set_mode(self, mode): 99 self.add_item_to_command_list(self._set_mode, (mode,), 1, 900) 100 101 102 def _set_mode(self, mode): 103 mode_mapping = {ap_spec.MODE_G | ap_spec.MODE_B: '802.11g&802.11b', 104 ap_spec.MODE_G: '802.11g Only'} 105 mode_name = mode_mapping.get(mode) 106 if not mode_name: 107 raise RuntimeError('The mode %d not supported by router %s. ' % 108 hex(mode), self.name) 109 xpath = '//select[@name="wl_gmode"]' 110 self.select_item_from_popup_by_xpath(mode_name, xpath) 111 112 113 def set_security_wep(self, key_value, authentication): 114 self.add_item_to_command_list(self._set_security_wep, 115 (key_value, authentication), 2, 1000) 116 117 118 def _set_security_wep(self, key_value, authentication): 119 # WEP has been removed from the list of supported security options 120 # since we get a Duplicate Administrator page everytime 121 # we generate a key for WEP. 122 popup = '//select[@name="wl_sec_mode"]' 123 self.wait_for_object_by_xpath(popup) 124 text_field = '//input[@name="wep64pp"]' 125 self.select_item_from_popup_by_xpath('64bit WEP', popup, 126 wait_for_xpath=text_field) 127 self.set_content_of_text_field_by_xpath(key_value, text_field, 128 abort_check=True) 129 generate = '//input[@name="wep128a_btn" and @value="generate"]' 130 self.click_button_by_xpath(generate) 131 self.wait_for_object_by_xpath(text_field) 132 133 134 def set_security_wpapsk(self, security, shared_key, update_interval=None): 135 self.add_item_to_command_list(self._set_security_wpapsk, 136 (security, shared_key, update_interval), 137 2, 900) 138 139 140 def _set_security_wpapsk(self, security, shared_key, update_interval=None): 141 popup = '//select[@name="wl_sec_mode"]' 142 self.wait_for_object_by_xpath(popup) 143 key_field = '//input[@name="wl_wpa_psk1"]' 144 self.select_item_from_popup_by_xpath('WPA-PSK(no server)', popup, 145 wait_for_xpath=key_field) 146 self.set_content_of_text_field_by_xpath(shared_key, key_field, 147 abort_check=False) 148 security_popup = 'WPA-PSK' 149 if security == ap_spec.SECURITY_TYPE_WPA2PSK: 150 security_popup = 'WPA2-PSK' 151 self.select_item_from_popup_by_xpath(security_popup, 152 '//select[@name="wl_auth"]') 153 154