blob: 1a5ad968a14a625504ba1c97b12b86e929306632 [file] [log] [blame]
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Class to control the Linksys WRT54G2 router."""
import logging
import urlparse
import ap_configurator
class LinksysAPConfigurator(ap_configurator.APConfigurator):
"""Derived class to control Linksys WRT54G2 router."""
def __init__(self, router_dict):
super(LinksysAPConfigurator, self).__init__(router_dict)
# Overrides
self.security_disabled = 'Disabled'
self.security_wep = 'WEP'
self.security_wpapsk = 'WPA Personal'
self.security_wpa2psk = 'WPA2 Personal'
self.security_wpa8021x = 'WPA Enterprise'
self.security_wpa28021x = 'WPA2 Enterprise'
self.mode_disabled = 'Disabled'
def get_number_of_pages(self):
return 2
def get_supported_bands(self):
return [{'band': self.band_2ghz,
'channels': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]}]
def get_supported_modes(self):
return [{'band': self.band_2ghz,
'modes': [self.mode_b, self.mode_g, self.mode_b |
self.mode_g]}]
def is_security_mode_supported(self, security_mode):
return security_mode in (self.security_type_disabled,
self.security_type_wpapsk,
self.security_type_wpa2psk,
self.security_type_wep)
def navigate_to_page(self, page_number):
if page_number == 1:
url = urlparse.urljoin(self.admin_interface_url, 'wireless.htm')
self.driver.get(url)
xpath = '//input[@name="wsc_smode" and @value=1]'
button = self.driver.find_element_by_xpath(xpath)
if not button.is_selected():
self.click_button_by_xpath(xpath)
elif page_number == 2:
url = urlparse.urljoin(self.admin_interface_url, 'WSecurity.htm')
self.driver.get(url)
else:
raise RuntimeError('Invalid page number passed. Number of pages '
'%d, page value sent was %d' %
(self.get_number_of_pages(), page_number))
def save_page(self, page_number):
self.wait_for_object_by_id('divBT1')
self.click_button_by_xpath('id("divBT1")')
# Wait for the continue button
continue_xpath = '//input[@value="Continue" and @type="button"]'
self.wait_for_object_by_xpath(continue_xpath)
self.click_button_by_xpath(continue_xpath)
def set_mode(self, mode, band=None):
self.add_item_to_command_list(self._set_mode, (mode,), 1, 900)
def _set_mode(self, mode):
# Different bands are not supported so we ignore.
# Create the mode to popup item mapping
mode_mapping = {self.mode_b: 'B-Only', self.mode_g: 'G-Only',
self.mode_b | self.mode_g: 'Mixed',
self.mode_disabled: 'Disabled'}
mode_name = mode_mapping.get(mode)
if not mode_name:
raise RuntimeError('The mode selected %d is not supported by router'
' %s.', hex(mode), self.get_router_name())
xpath = ('//select[@onchange="SelWL()" and @name="Mode"]')
self.select_item_from_popup_by_xpath(mode_name, xpath)
def set_radio(self, enabled=True):
# If we are enabling we are activating all other UI components, do it
# first. Otherwise we are turning everything off so do it last.
weight = 1 if enabled else 1000
self.add_item_to_command_list(self._set_radio, (enabled,), 1, weight)
def _set_radio(self, enabled=True):
xpath = ('//select[@onchange="SelWL()" and @name="Mode"]')
# To turn off we pick disabled, to turn on we set to G
if not enabled:
self._set_mode(self.mode_disabled)
else:
self._set_mode(self.mode_g)
def set_ssid(self, ssid):
self.add_item_to_command_list(self._set_ssid, (ssid,), 1, 900)
def _set_ssid(self, ssid):
self._set_radio(enabled=True)
xpath = ('//input[@maxlength="32" and @name="SSID"]')
self.set_content_of_text_field_by_xpath(ssid, xpath)
def set_channel(self, channel):
self.add_item_to_command_list(self._set_channel, (channel,), 1, 900)
def _set_channel(self, channel):
position = self._get_channel_popup_position(channel)
self._set_radio(enabled=True)
channel_choices = ['1 - 2.412GHz', '2 - 2.417GHz', '3 - 2.422GHz',
'4 - 2.427GHz', '5 - 2.432GHz', '6 - 2.437GHz',
'7 - 2.442GHz', '8 - 2.447GHz', '9 - 2.452GHz',
'10 - 2.457GHz', '11 - 2.462GHz']
xpath = ('//select[@onfocus="check_action(this,0)" and @name="Freq"]')
self.select_item_from_popup_by_xpath(channel_choices[position],
xpath)
def set_band(self, band):
return None
def set_security_disabled(self):
self.add_item_to_command_list(self._set_security_disabled, (), 2, 1000)
def _set_security_disabled(self):
xpath = ('//select[@name="SecurityMode"]')
self.select_item_from_popup_by_xpath(self.security_disabled, xpath)
def set_security_wep(self, key_value, authentication):
self.add_item_to_command_list(self._set_security_wep,
(key_value, authentication), 2, 1000)
def _set_security_wep(self, key_value, authentication):
logging.info('This router %s does not support WEP authentication type:'
' %s', self.get_router_name(), authentication)
popup = '//select[@name="SecurityMode"]'
self.wait_for_object_by_xpath(popup)
text_field = ('//input[@name="wl_passphrase"]')
self.select_item_from_popup_by_xpath(self.security_wep, popup,
wait_for_xpath=text_field)
self.set_content_of_text_field_by_xpath(key_value, text_field,
abort_check=True)
self.click_button_by_xpath('//input[@value="Generate"]')
def set_security_wpapsk(self, shared_key, update_interval=1800):
self.add_item_to_command_list(self._set_security_wpapsk,
(shared_key, update_interval), 2, 900)
def _set_security_wpapsk(self, shared_key, update_interval=1800):
if update_interval < 600:
logging.info('The minimum update interval is 600, overriding.')
update_interval = 600
elif update_interval > 7200:
logging.info('The maximum update interval is 7200, overriding.')
update_interval = 7200
popup = '//select[@name="SecurityMode"]'
self.wait_for_object_by_xpath(popup)
key_field = '//input[@name="PassPhrase"]'
self.select_item_from_popup_by_xpath(self.security_wpapsk, popup,
wait_for_xpath=key_field)
self.set_content_of_text_field_by_xpath(shared_key, key_field,
abort_check=True)
interval_field = ('//input[@name="GkuInterval"]')
self.set_content_of_text_field_by_xpath(str(update_interval),
interval_field)
def set_visibility(self, visible=True):
self.add_item_to_command_list(self._set_visibility, (visible,), 1, 900)
def _set_visibility(self, visible=True):
self._set_radio(enabled=True)
# value=1 is visible; value=0 is invisible
int_value = int(visible)
xpath = ('//input[@value="%d" and @name="wl_closed"]' % int_value)
self.click_button_by_xpath(xpath)