| OLD | NEW |
| (Empty) |
| 1 # Copyright (c) 2012 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 logging | |
| 6 import os | |
| 7 | |
| 8 import ap_configurator | |
| 9 import selenium.common.exceptions | |
| 10 | |
| 11 | |
| 12 class DLinkAPConfigurator(ap_configurator.APConfigurator): | |
| 13 """Derived class to control the DLink DAP-1522.""" | |
| 14 | |
| 15 def __init__(self, pyauto_instance, admin_interface_url): | |
| 16 super(DLinkAPConfigurator, self).__init__(pyauto_instance) | |
| 17 # Override constants | |
| 18 self.security_disabled = 'Disable Wireless Security (not recommended)' | |
| 19 self.security_wep = 'WEP' | |
| 20 self.security_wpapsk = 'WPA-Personal' | |
| 21 self.security_wpa2psk = 'WPA-Personal' | |
| 22 self.security_wpa8021x = 'WPA-Enterprise' | |
| 23 self.security_wpa28021x = 'WPA2-Enterprise' | |
| 24 | |
| 25 self.admin_interface_url = admin_interface_url | |
| 26 | |
| 27 def _OpenLandingPage(self): | |
| 28 self.pyauto_instance.NavigateToURL('http://%s/index.php' % | |
| 29 self.admin_interface_url) | |
| 30 page_name = os.path.basename(self.pyauto_instance.GetActiveTabURL().spec()) | |
| 31 if page_name == 'login.php' or page_name == 'index.php': | |
| 32 try: | |
| 33 self._wait.until(lambda _: self._driver.find_element_by_xpath( | |
| 34 '//*[@name="login"]')) | |
| 35 except selenium.common.exceptions.TimeoutException, e: | |
| 36 # Maybe we were re-routes to the configuration page | |
| 37 if (os.path.basename(self.pyauto_instance.GetActiveTabURL().spec()) == | |
| 38 'bsc_wizard.php'): | |
| 39 return | |
| 40 logging.exception('WebDriver exception: %s', str(e)) | |
| 41 login_button = self._driver.find_element_by_xpath('//*[@name="login"]') | |
| 42 login_button.click() | |
| 43 | |
| 44 def _OpenConfigurationPage(self): | |
| 45 self._OpenLandingPage() | |
| 46 if (os.path.basename(self.pyauto_instance.GetActiveTabURL().spec()) != | |
| 47 'bsc_wizard.php'): | |
| 48 self.fail(msg='Taken to an unknown page %s' % | |
| 49 self.pyauto_instance.GetActiveTabURL().spec()) | |
| 50 | |
| 51 # Else we are being logged in automatically to the landing page | |
| 52 wlan = '//*[@name="wlan_wireless"]' | |
| 53 try: | |
| 54 self._wait.until(lambda _: self._driver.find_element_by_xpath(wlan)) | |
| 55 except selenium.common.exceptions.TimeoutException, e: | |
| 56 logging.exception('WebDriver exception: %s', str(e)) | |
| 57 | |
| 58 wlan_button = self._driver.find_element_by_xpath(wlan) | |
| 59 wlan_button.click() | |
| 60 # Wait for the main configuration page, look for the radio button | |
| 61 try: | |
| 62 self._wait.until(lambda _: self._driver.find_element_by_xpath( | |
| 63 'id("enable")')) | |
| 64 except selenium.common.exceptions.TimeoutException, e: | |
| 65 logging.exception('Unable to find the radio button on the main landing ' | |
| 66 'page.\nWebDriver exception: %s', str(e)) | |
| 67 | |
| 68 def GetRouterName(self): | |
| 69 return 'Router Name: DAP-1522; Class: DLinkAPConfigurator' | |
| 70 | |
| 71 def GetRouterShortName(self): | |
| 72 return 'DAP-1522' | |
| 73 | |
| 74 def GetNumberOfPages(self): | |
| 75 return 1 | |
| 76 | |
| 77 def GetSupportedBands(self): | |
| 78 return [{'band': self.band_2ghz, | |
| 79 'channels': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]}, | |
| 80 {'band': self.band_5ghz, | |
| 81 'channels': [26, 40, 44, 48, 149, 153, 157, 161, 165]}] | |
| 82 | |
| 83 def GetSupportedModes(self): | |
| 84 return [{'band': self.band_2ghz, | |
| 85 'modes': [self.mode_b, self.mode_g, self.mode_n, | |
| 86 self.mode_b | self.mode_g, self.mode_g | self.mode_n]}, | |
| 87 {'band': self.band_5ghz, | |
| 88 'modes': [self.mode_a, self.mode_n, self.mode_a | self.mode_n]}] | |
| 89 | |
| 90 def NavigateToPage(self, page_number): | |
| 91 # All settings are on the same page, so we always open the config page | |
| 92 self._OpenConfigurationPage() | |
| 93 return True | |
| 94 | |
| 95 def SavePage(self, page_number): | |
| 96 # All settings are on the same page, we can ignore page_number | |
| 97 button = self._driver.find_element_by_xpath('//input[@name="apply"]') | |
| 98 button.click() | |
| 99 # If we did not make changes so we are sent to the continue screen. | |
| 100 continue_screen = True | |
| 101 button_xpath = '//input[@name="bt"]' | |
| 102 try: | |
| 103 self._wait.until(lambda _: | |
| 104 self._driver.find_element_by_xpath(button_xpath)) | |
| 105 except selenium.common.exceptions.TimeoutException, e: | |
| 106 continue_screen = False | |
| 107 if continue_screen: | |
| 108 button = self._driver.find_element_by_xpath(button_xpath) | |
| 109 button.click() | |
| 110 # We will be returned to the landing page when complete | |
| 111 try: | |
| 112 self._wait.until(lambda _: | |
| 113 self._driver.find_element_by_xpath('id("enable")')) | |
| 114 except selenium.common.exceptions.TimeoutException, e: | |
| 115 logging.exception('Unable to find the radio button on the main landing ' | |
| 116 'page.\nWebDriver exception: %s', str(e)) | |
| 117 return False | |
| 118 return True | |
| 119 | |
| 120 def SetMode(self, mode, band=None): | |
| 121 # Mode overrides the band. So if a band change is made after a mode change | |
| 122 # it may make an incompatible pairing. | |
| 123 self.AddItemToCommandList(self._SetMode, (mode, band), 1, 800) | |
| 124 | |
| 125 def _SetMode(self, mode, band=None): | |
| 126 # Create the mode to popup item mapping | |
| 127 mode_mapping = {self.mode_b: '802.11b Only', self.mode_g: '802.11g Only', | |
| 128 self.mode_n: '802.11n Only', | |
| 129 self.mode_b | self.mode_g: 'Mixed 802.11g and 802.11b', | |
| 130 self.mode_n | self.mode_g: 'Mixed 802.11n and 802.11g', | |
| 131 self.mode_n | self.mode_g | self.mode_b: | |
| 132 'Mixed 802.11n, 802.11g, and 802.11b', | |
| 133 self.mode_n | self.mode_g | self.mode_b: | |
| 134 'Mixed 802.11n, 802.11g, and 802.11b', | |
| 135 self.mode_a: '802.11a Only', | |
| 136 self.mode_n | self.mode_a: 'Mixed 802.11n and 802.11a'} | |
| 137 band_value = self.band_2ghz | |
| 138 if mode in mode_mapping.keys(): | |
| 139 popup_value = mode_mapping[mode] | |
| 140 # If the mode contains 802.11a we use 5Ghz | |
| 141 if mode & self.mode_a == self.mode_a: | |
| 142 band_value = self.band_5ghz | |
| 143 # If the mode is 802.11n mixed with 802.11a it must be 5Ghz | |
| 144 elif mode & (self.mode_n | self.mode_a) == (self.mode_n | self.mode_a): | |
| 145 band_value = self.band_5ghz | |
| 146 # If the mode is 802.11n mixed with something other than 802.11a its 2Ghz | |
| 147 elif mode & self.mode_n == self.mode_n and mode ^ self.mode_n > 0: | |
| 148 band_value = self.band_2ghz | |
| 149 # If the mode is 802.11n then we default to 5Ghz unless there is a band | |
| 150 elif mode == self.mode_n: | |
| 151 band_value = self.band_5ghz | |
| 152 if band: | |
| 153 band_value = band | |
| 154 else: | |
| 155 logging.exception('The mode selected %d is not supported by router %s.', | |
| 156 hex(mode), self.getRouterName()) | |
| 157 # Set the band first | |
| 158 self._SetBand(band_value) | |
| 159 popup_id = 'mode_80211_11g' | |
| 160 if band_value == self.band_5ghz: | |
| 161 popup_id = 'mode_80211_11a' | |
| 162 self.SelectItemFromPopupByID(popup_value, popup_id) | |
| 163 | |
| 164 def SetRadio(self, enabled=True): | |
| 165 # If we are enabling we are activating all other UI components, do it first. | |
| 166 # Otherwise we are turning everything off so do it last. | |
| 167 if enabled: | |
| 168 weight = 1 | |
| 169 else: | |
| 170 weight = 1000 | |
| 171 # This disables all UI so it should be the last item to be changed | |
| 172 self.AddItemToCommandList(self._SetRadio, (enabled,), 1, weight) | |
| 173 | |
| 174 def _SetRadio(self, enabled=True): | |
| 175 # The radio checkbox for this router always has a value of 1. So we need to | |
| 176 # use other methods to determine if the radio is on or not. Check if the | |
| 177 # ssid textfield is disabled. | |
| 178 ssid = self._driver.find_element_by_xpath('//input[@name="ssid"]') | |
| 179 if ssid.get_attribute('disabled') == 'true': | |
| 180 radio_enabled = False | |
| 181 else: | |
| 182 radio_enabled = True | |
| 183 if radio_enabled == enabled: | |
| 184 # Nothing to do | |
| 185 return | |
| 186 self.SetCheckBoxSelectedByID('enable', selected=False, | |
| 187 wait_for_xpath='id("security_type_ap")') | |
| 188 | |
| 189 def SetSSID(self, ssid): | |
| 190 # Can be done as long as it is enabled | |
| 191 self.AddItemToCommandList(self._SetSSID, (ssid,), 1, 900) | |
| 192 | |
| 193 def _SetSSID(self, ssid): | |
| 194 self._SetRadio(enabled=True) | |
| 195 self.SetContentOfTextFieldByID(ssid, 'ssid') | |
| 196 | |
| 197 def SetChannel(self, channel): | |
| 198 self.AddItemToCommandList(self._SetChannel, (channel,), 1, 900) | |
| 199 | |
| 200 def _SetChannel(self, channel): | |
| 201 self._SetRadio(enabled=True) | |
| 202 self.SetCheckBoxSelectedByID('autochann', selected=False) | |
| 203 self.SelectItemFromPopupByID(str(channel), 'channel_g') | |
| 204 | |
| 205 # Experimental | |
| 206 def GetBand(self): | |
| 207 # The radio buttons do more than run a script that adjusts the possible | |
| 208 # channels. We will just check the channel to popup. | |
| 209 self.setRadioSetting(enabled=True) | |
| 210 xpath = ('id("channel_g")') | |
| 211 self._OpenConfigurationPage() | |
| 212 try: | |
| 213 self._wait.until(lambda _: self._driver.find_element_by_xpath(xpath)) | |
| 214 except selenium.common.exceptions.TimeoutException, e: | |
| 215 logging.exception('WebDriver exception: %s', str(e)) | |
| 216 element = self._driver.find_element_by_xpath(xpath) | |
| 217 if element.find_elements_by_tag_name('option')[0].text == '1': | |
| 218 return self.band_2ghz | |
| 219 return self.band_5ghz | |
| 220 | |
| 221 def SetBand(self, band): | |
| 222 if band != self.band_2GHz or band != self.band_5ghz: | |
| 223 self.fail(msg='Invalid band sent %s' % band) | |
| 224 self.AddItemToCommandList(self._SetBand, (band,), 1, 900) | |
| 225 | |
| 226 def _SetBand(self, band): | |
| 227 self._SetRadio(enabled=True) | |
| 228 if band == self.band_2ghz: | |
| 229 int_value = 0 | |
| 230 wait_for_xpath = 'id("mode_80211_11g")' | |
| 231 elif band == self.band_5ghz: | |
| 232 int_value = 1 | |
| 233 wait_for_xpath = 'id("mode_80211_11a")' | |
| 234 xpath = ('//*[contains(@class, "l_tb")]/input[@value="%d" and @name="band"]' | |
| 235 % int_value) | |
| 236 element = self._driver.find_element_by_xpath(xpath) | |
| 237 element.click() | |
| 238 try: | |
| 239 self._wait.until(lambda _: | |
| 240 self._driver.find_element_by_xpath(wait_for_xpath)) | |
| 241 except selenium.common.exceptions.TimeoutException, e: | |
| 242 logging.exception('The appropriate mode popup could not be found after ' | |
| 243 'adjusting the band. WebDriver exception: %s', str(e)) | |
| 244 | |
| 245 def SetSecurityDisabled(self): | |
| 246 self.AddItemToCommandList(self._SetSecurityDisabled, (), 1, 900) | |
| 247 | |
| 248 def _SetSecurityDisabled(self): | |
| 249 self._SetRadio(enabled=True) | |
| 250 self.SelectItemFromPopupByID(self.security_disabled, 'security_type_ap') | |
| 251 | |
| 252 def SetSecurityWEP(self, key_value, authentication): | |
| 253 self.AddItemToCommandList(self._SetSecurityWEP, (key_value, authentication), | |
| 254 1, 900) | |
| 255 | |
| 256 def _SetSecurityWEP(self, key_value, authentication): | |
| 257 self._SetRadio(enabled=True) | |
| 258 self.SelectItemFromPopupByID(self.security_wep, 'security_type_ap', | |
| 259 wait_for_xpath='id("auth_type")') | |
| 260 self.SelectItemFromPopupByID(authentication, 'auth_type', | |
| 261 wait_for_xpath='id("wep_key_value")') | |
| 262 self.SetContentOfTextFieldByID(key_value, 'wep_key_value') | |
| 263 self.SetContentOfTextFieldByID(key_value, 'verify_wep_key_value') | |
| 264 | |
| 265 def SetSecurityWPAPSK(self, shared_key, update_interval=1800): | |
| 266 self.AddItemToCommandList(self._SetSecurityWPAPSK, | |
| 267 (shared_key, update_interval), 1, 900) | |
| 268 | |
| 269 def _SetSecurityWPAPSK(self, shared_key, update_interval=1800): | |
| 270 self._SetRadio(enabled=True) | |
| 271 self.SelectItemFromPopupByID(self.security_wpapsk, 'security_type_ap', | |
| 272 wait_for_xpath='id("wpa_mode")') | |
| 273 self.SelectItemFromPopupByID('WPA Only', 'wpa_mode', | |
| 274 wait_for_xpath='id("grp_key_interval")') | |
| 275 self.SetContentOfTextFieldByID(str(update_interval), 'grp_key_interval') | |
| 276 self.SetContentOfTextFieldByID(shared_key, 'wpapsk1') | |
| 277 | |
| 278 def SetVisibility(self, visible=True): | |
| 279 self.AddItemToCommandList(self._SetVisibility, (visible,), 1, 900) | |
| 280 | |
| 281 def _SetVisibility(self, visible=True): | |
| 282 self._SetRadio(enabled=True) | |
| 283 # value=0 is visible; value=1 is invisible | |
| 284 int_value = 0 | |
| 285 if not visible: | |
| 286 int_value = 1 | |
| 287 xpath = ('//*[contains(@class, "l_tb")]/input[@value="%d" ' | |
| 288 'and @name="visibility_status"]' % int_value) | |
| 289 element = self._driver.find_element_by_xpath(xpath) | |
| 290 element.click() | |
| 291 | |
| OLD | NEW |