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
5import ap_spec
6import netgear_WNDR_dual_band_configurator
7from netgear_WNDR_dual_band_configurator import *
8
9
10class Netgear3700APConfigurator(netgear_WNDR_dual_band_configurator.
11                                NetgearDualBandAPConfigurator):
12    """Derived class to control Netgear 3700 router."""
13
14
15    def _alert_handler(self, alert):
16        """Checks for any modal dialogs which popup to alert the user and
17        either raises a RuntimeError or ignores the alert.
18
19        @param alert: the modal dialog's contents.
20        """
21        text = alert.text
22        #  We ignore warnings that we get when we disable visibility or security
23        #  changed to WEP, WPA Personal or WPA Enterprise.
24        if 'The WEP security can only be supported on one SSID' in text:
25            alert.accept()
26        else:
27            super(Netgear3700APConfigurator, self)._alert_handler(alert)
28
29
30    def _get_settings_page(self):
31        frame1 = self.driver.find_element_by_xpath('//frame[@name="contents"]')
32        frame2 = self.driver.switch_to_frame(frame1)
33        xpath = '//a[text()="Wireless Settings"]'
34        self.click_button_by_xpath(xpath)
35        default = self.driver.switch_to_default_content()
36        setframe = self.driver.find_element_by_xpath(
37                   '//frame[@name="formframe"]')
38        settings = self.driver.switch_to_frame(setframe)
39
40
41    def save_page(self, page_number):
42        """Save the currently opened page.
43
44        @param page_number: the page to save.
45        """
46        self.click_button_by_xpath('//input[@name="Apply"]',
47                                   alert_handler=self._alert_handler)
48
49
50    def get_supported_bands(self):
51        return [{'band': ap_spec.BAND_2GHZ,
52                 'channels': ['Auto', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]},
53                {'band': ap_spec.BAND_5GHZ,
54                 'channels': [36, 40, 44, 48, 149, 153, 157, 161]}]
55
56
57    def get_supported_modes(self):
58        return [{'band': ap_spec.BAND_5GHZ,
59                 'modes': [ap_spec.MODE_A, ap_spec.MODE_N]},
60                {'band': ap_spec.BAND_2GHZ,
61                 'modes': [ap_spec.MODE_G, ap_spec.MODE_N]}]
62
63
64    def navigate_to_page(self, page_number):
65        """Navigate to the given page.
66
67        @param page_number: the page to navigate to.
68        """
69        self.get_url(self.admin_interface_url, page_title='WNDR3700')
70        self._get_settings_page()
71
72
73    def _switch_to_default(self):
74        self.driver.switch_to_default_content()
75        self._get_settings_page()
76
77
78    def set_channel(self, channel):
79        self.add_item_to_command_list(self._set_channel, (channel,), 1, 800)
80
81
82    def _set_channel(self, channel):
83        position = self._get_channel_popup_position(channel)
84        channel_choices = ['Auto', '01', '02', '03', '04', '05', '06', '07',
85                           '08', '09', '10', '11']
86        xpath = '//select[@name="w_channel"]'
87        if self.current_band == ap_spec.BAND_5GHZ:
88            xpath = '//select[@name="w_channel_an"]'
89            channel_choices = ['36', '40', '44', '48', '149', '153',
90                               '157', '161', '165']
91        self.select_item_from_popup_by_xpath(channel_choices[position], xpath)
92
93
94    def set_security_wep(self, key_value, authentication):
95        # The button name seems to differ in various Netgear routers
96        self.add_item_to_command_list(self._set_security_wep,
97                                      (key_value, authentication), 1, 1000)
98
99
100    def _set_security_wep(self, value, authentication):
101        self._switch_to_default()
102        xpath = ('//input[@name="security_type" and @value="WEP"]')
103        text = '//input[@name="passphraseStr"]'
104        button = '//input[@name="Generate"]'
105        if self.current_band == ap_spec.BAND_5GHZ:
106            xpath = ('//input[@name="security_type_an" and @value="WEP"]')
107            text = '//input[@name="passphraseStr_an"]'
108            button = '//input[@name="Generate_an"]'
109        try:
110            self.click_button_by_xpath(xpath, alert_handler=self._alert_handler)
111        except Exception, e:
112            raise RuntimeError('For WEP the mode should be 54Mbps. %s' % e)
113        self.set_content_of_text_field_by_xpath(value, text, abort_check=True)
114        self.click_button_by_xpath(button, alert_handler=self._alert_handler)
115