1636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal# Copyright (c) 2013 The Chromium Authors. All rights reserved.
2636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal# Use of this source code is governed by a BSD-style license that can be
3636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal# found in the LICENSE file.
4636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal
585b09c5ca28e9cda022519162198a37303bf0ec2Kris Rambishimport os
6c1ae75b6c0f7ab689e50c9fe7f816b7560b50fa5Kris Rambishimport urlparse
785b09c5ca28e9cda022519162198a37303bf0ec2Kris Rambish
8d751a4f5e86c0e5f8a984d7fe21a4f74c7caa761Kris Rambishimport ap_spec
9636b08df603edccf33247c24545844abdc5a5c99Deepak Gopalimport netgear_WNDR_dual_band_configurator
10636b08df603edccf33247c24545844abdc5a5c99Deepak Gopalfrom netgear_WNDR_dual_band_configurator import *
11636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal
12d751a4f5e86c0e5f8a984d7fe21a4f74c7caa761Kris Rambish
13636b08df603edccf33247c24545844abdc5a5c99Deepak Gopalclass Netgear4300APConfigurator(netgear_WNDR_dual_band_configurator.
14636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal                                NetgearDualBandAPConfigurator):
15636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal    """Derived class to control Netgear WNDR4300 router."""
16636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal
17636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal
18636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal    def _alert_handler(self, alert):
19636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal        """Checks for any modal dialogs which popup to alert the user and
20636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal        either raises a RuntimeError or ignores the alert.
21636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal
22d751a4f5e86c0e5f8a984d7fe21a4f74c7caa761Kris Rambish        @param alert: The modal dialog's contents.
23636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal        """
24636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal        text = alert.text
25636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal        #  We ignore warnings that we get when we disable visibility or security
26636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal        #  changed to WEP, WPA Personal or WPA Enterprise.
27636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal        if 'The WEP security can only be supported on one SSID' in text:
28636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal            alert.accept()
29636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal        elif '40 Mhz and 20 Mhz coexistence' in text:
30636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal            alert.accept()
31636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal        elif 'WPS is going to become inaccessible' in text:
32636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal            alert.accept()
332057368d8619d01f7f6fa6ac70a3c0d89b238a0eDeepak Gopal        elif 'Keys length should be 10 Hex' in text:
342057368d8619d01f7f6fa6ac70a3c0d89b238a0eDeepak Gopal            alert.accept()
352057368d8619d01f7f6fa6ac70a3c0d89b238a0eDeepak Gopal            raise RuntimeError('We got a dialog with Invalid Key Error. '
362057368d8619d01f7f6fa6ac70a3c0d89b238a0eDeepak Gopal                               + text)
37636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal        else:
38636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal            super(Netgear4300APConfigurator, self)._alert_handler(alert)
39636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal
40636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal
41636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal    def get_supported_bands(self):
42d751a4f5e86c0e5f8a984d7fe21a4f74c7caa761Kris Rambish        return [{'band': ap_spec.BAND_2GHZ,
43636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal                 'channels': ['Auto', 1, 2, 3, 4, 5, 6, 7, 8, 9 , 10, 11]},
44d751a4f5e86c0e5f8a984d7fe21a4f74c7caa761Kris Rambish                {'band': ap_spec.BAND_5GHZ,
45636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal                 'channels': [36, 40, 44, 48, 149, 153, 157, 161]}]
46636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal
47636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal
48636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal    def get_supported_modes(self):
49d751a4f5e86c0e5f8a984d7fe21a4f74c7caa761Kris Rambish        return [{'band': ap_spec.BAND_5GHZ,
50d751a4f5e86c0e5f8a984d7fe21a4f74c7caa761Kris Rambish                 'modes': [ap_spec.MODE_A, ap_spec.MODE_N]},
51d751a4f5e86c0e5f8a984d7fe21a4f74c7caa761Kris Rambish                {'band': ap_spec.BAND_2GHZ,
52d751a4f5e86c0e5f8a984d7fe21a4f74c7caa761Kris Rambish                 'modes': [ap_spec.MODE_G, ap_spec.MODE_N]}]
53636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal
54636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal
55636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal    def logout_from_previous_netgear(self):
56636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal        """Some netgear routers dislike you being logged into another
57636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal           one of their kind. So make sure that you are not."""
58636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal        self.click_button_by_id('yes')
59636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal
60636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal
61636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal    def navigate_to_page(self, page_number):
62d751a4f5e86c0e5f8a984d7fe21a4f74c7caa761Kris Rambish        """Navigate to the given page.
63d751a4f5e86c0e5f8a984d7fe21a4f74c7caa761Kris Rambish
64d751a4f5e86c0e5f8a984d7fe21a4f74c7caa761Kris Rambish        @param page_number: the page to navigate to.
65d751a4f5e86c0e5f8a984d7fe21a4f74c7caa761Kris Rambish        """
667b0325111d7a7efea187d79f0087c710a9bdc626Deepak Gopal        self.set_wait_time(30)
67d751a4f5e86c0e5f8a984d7fe21a4f74c7caa761Kris Rambish        try:
68d751a4f5e86c0e5f8a984d7fe21a4f74c7caa761Kris Rambish            self.get_url(urlparse.urljoin(self.admin_interface_url,
69d751a4f5e86c0e5f8a984d7fe21a4f74c7caa761Kris Rambish                         'adv_index.htm'), page_title='WNDR4300')
707b0325111d7a7efea187d79f0087c710a9bdc626Deepak Gopal            self.driver.execute_script("click_adv_action('wireless')")
71d751a4f5e86c0e5f8a984d7fe21a4f74c7caa761Kris Rambish        except Exception as e:
72d751a4f5e86c0e5f8a984d7fe21a4f74c7caa761Kris Rambish            if os.path.basename(self.driver.current_url) != 'adv_index.htm':
73d751a4f5e86c0e5f8a984d7fe21a4f74c7caa761Kris Rambish                raise RuntimeError('Invalid url %s' % self.driver.current_url)
74d751a4f5e86c0e5f8a984d7fe21a4f74c7caa761Kris Rambish            elif os.path.basename(
75d751a4f5e86c0e5f8a984d7fe21a4f74c7caa761Kris Rambish                self.driver.current_url) == 'multi_login.html':
76d751a4f5e86c0e5f8a984d7fe21a4f74c7caa761Kris Rambish                self.logout_from_previous_netgear()
777b0325111d7a7efea187d79f0087c710a9bdc626Deepak Gopal        setframe = self.wait_for_object_by_xpath('//iframe[@name="formframe"]')
78d751a4f5e86c0e5f8a984d7fe21a4f74c7caa761Kris Rambish        settings = self.driver.switch_to_frame(setframe)
797b0325111d7a7efea187d79f0087c710a9bdc626Deepak Gopal        xpath = '//input[@name="security_type_an" and @value="WPA-ENTER"]'
807b0325111d7a7efea187d79f0087c710a9bdc626Deepak Gopal        self.wait.until(lambda _:
817b0325111d7a7efea187d79f0087c710a9bdc626Deepak Gopal                        self.wait_for_object_by_xpath(xpath).is_displayed())
827b0325111d7a7efea187d79f0087c710a9bdc626Deepak Gopal        self.driver.execute_script('window.stop()')
837b0325111d7a7efea187d79f0087c710a9bdc626Deepak Gopal        self.restore_default_wait_time()
84636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal
85636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal
86636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal    def save_page(self, page_number):
87d751a4f5e86c0e5f8a984d7fe21a4f74c7caa761Kris Rambish        """Saves the given page.
88d751a4f5e86c0e5f8a984d7fe21a4f74c7caa761Kris Rambish
89d751a4f5e86c0e5f8a984d7fe21a4f74c7caa761Kris Rambish        @param page_number: the page to save.
90d751a4f5e86c0e5f8a984d7fe21a4f74c7caa761Kris Rambish        """
912057368d8619d01f7f6fa6ac70a3c0d89b238a0eDeepak Gopal        # Router throws 3 different alerts when the settings are applied.
922057368d8619d01f7f6fa6ac70a3c0d89b238a0eDeepak Gopal        try:
932057368d8619d01f7f6fa6ac70a3c0d89b238a0eDeepak Gopal            self.click_button_by_xpath('//input[@name="Apply"]',
942057368d8619d01f7f6fa6ac70a3c0d89b238a0eDeepak Gopal                                       alert_handler=self._alert_handler)
952057368d8619d01f7f6fa6ac70a3c0d89b238a0eDeepak Gopal        except Exception, e1:
962057368d8619d01f7f6fa6ac70a3c0d89b238a0eDeepak Gopal            try:
972057368d8619d01f7f6fa6ac70a3c0d89b238a0eDeepak Gopal                self._check_for_alert_in_message(str(e1),
982057368d8619d01f7f6fa6ac70a3c0d89b238a0eDeepak Gopal                        alert_handler=self._alert_handler)
992057368d8619d01f7f6fa6ac70a3c0d89b238a0eDeepak Gopal            except Exception, e2:
1002057368d8619d01f7f6fa6ac70a3c0d89b238a0eDeepak Gopal                self._check_for_alert_in_message(str(e2),
1012057368d8619d01f7f6fa6ac70a3c0d89b238a0eDeepak Gopal                        alert_handler=self._alert_handler)
102636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal
103636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal
104c1ae75b6c0f7ab689e50c9fe7f816b7560b50fa5Kris Rambish    def _set_mode(self, mode, band=None):
105c1ae75b6c0f7ab689e50c9fe7f816b7560b50fa5Kris Rambish        if mode == ap_spec.MODE_G or mode == ap_spec.MODE_A:
106c1ae75b6c0f7ab689e50c9fe7f816b7560b50fa5Kris Rambish            mode_selection = 'Up to 54 Mbps'
107c1ae75b6c0f7ab689e50c9fe7f816b7560b50fa5Kris Rambish        elif mode == ap_spec.MODE_N:
108c1ae75b6c0f7ab689e50c9fe7f816b7560b50fa5Kris Rambish            if self.current_band == ap_spec.BAND_2GHZ:
109c1ae75b6c0f7ab689e50c9fe7f816b7560b50fa5Kris Rambish                mode_selection = 'Up to 300 Mbps'
110c1ae75b6c0f7ab689e50c9fe7f816b7560b50fa5Kris Rambish            else:
111c1ae75b6c0f7ab689e50c9fe7f816b7560b50fa5Kris Rambish                mode_selection = 'Up to 450 Mbps'
112c1ae75b6c0f7ab689e50c9fe7f816b7560b50fa5Kris Rambish        else:
113c1ae75b6c0f7ab689e50c9fe7f816b7560b50fa5Kris Rambish            raise RuntimeError('Unsupported mode passed.')
114c1ae75b6c0f7ab689e50c9fe7f816b7560b50fa5Kris Rambish        xpath = '//select[@name="opmode"]'
115c1ae75b6c0f7ab689e50c9fe7f816b7560b50fa5Kris Rambish        if self.current_band == ap_spec.BAND_5GHZ:
116c1ae75b6c0f7ab689e50c9fe7f816b7560b50fa5Kris Rambish            xpath = '//select[@name="opmode_an"]'
117c1ae75b6c0f7ab689e50c9fe7f816b7560b50fa5Kris Rambish        self.wait_for_object_by_xpath(xpath)
118c1ae75b6c0f7ab689e50c9fe7f816b7560b50fa5Kris Rambish        self.select_item_from_popup_by_xpath(mode_selection, xpath)
119c1ae75b6c0f7ab689e50c9fe7f816b7560b50fa5Kris Rambish
120c1ae75b6c0f7ab689e50c9fe7f816b7560b50fa5Kris Rambish
1218238fdcf89535456a0be158a4f267611ae8b152aKris Rambish    def set_channel(self, channel):
1228238fdcf89535456a0be158a4f267611ae8b152aKris Rambish        self.add_item_to_command_list(self._set_channel, (channel,), 1, 900)
1238238fdcf89535456a0be158a4f267611ae8b152aKris Rambish
1248238fdcf89535456a0be158a4f267611ae8b152aKris Rambish
125636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal    def _set_channel(self, channel):
126636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal        position = self._get_channel_popup_position(channel)
127636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal        channel_choices = ['Auto', '01', '02', '03', '04', '05', '06', '07',
128636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal                           '08', '09', '10', '11']
129636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal        xpath = '//select[@name="w_channel"]'
130d751a4f5e86c0e5f8a984d7fe21a4f74c7caa761Kris Rambish        if self.current_band == ap_spec.BAND_5GHZ:
131636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal            xpath = '//select[@name="w_channel_an"]'
132636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal            channel_choices = ['36', '40', '44', '48', '149', '153',
133636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal                               '157', '161']
134636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal        self.select_item_from_popup_by_xpath(channel_choices[position], xpath)
135636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal
136636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal
1378238fdcf89535456a0be158a4f267611ae8b152aKris Rambish    def set_security_wep(self, key_value, authentication):
1388238fdcf89535456a0be158a4f267611ae8b152aKris Rambish        # The button name seems to differ in various Netgear routers
1398238fdcf89535456a0be158a4f267611ae8b152aKris Rambish        self.add_item_to_command_list(self._set_security_wep,
1408238fdcf89535456a0be158a4f267611ae8b152aKris Rambish                                      (key_value, authentication), 1, 900)
1418238fdcf89535456a0be158a4f267611ae8b152aKris Rambish
1428238fdcf89535456a0be158a4f267611ae8b152aKris Rambish
143636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal    def _set_security_wep(self, key_value, authentication):
1447b0325111d7a7efea187d79f0087c710a9bdc626Deepak Gopal        self.set_wait_time(30)
145636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal        xpath = ('//input[@name="security_type" and @value="WEP" and '
146636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal                 '@type="radio"]')
1472057368d8619d01f7f6fa6ac70a3c0d89b238a0eDeepak Gopal        text_field = '//input[@name="KEY1"]'
148d751a4f5e86c0e5f8a984d7fe21a4f74c7caa761Kris Rambish        if self.current_band == ap_spec.BAND_5GHZ:
149636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal            xpath = '//input[@name="security_type_an" and @value="WEP" and\
150636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal                     @type="radio"]'
1512057368d8619d01f7f6fa6ac70a3c0d89b238a0eDeepak Gopal            text_field = '//input[@name="KEY1_an"]'
152636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal        try:
153636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal            self.wait_for_object_by_xpath(xpath)
154636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal            self.click_button_by_xpath(xpath, alert_handler=self._alert_handler)
155636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal        except Exception, e:
156636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal            raise RuntimeError('We got an exception: "%s". Check the mode. '
157636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal                               'It should be \'Up to 54 Mbps\'.' % str(e))
158636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal        self.wait_for_object_by_xpath(text_field)
159636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal        self.set_content_of_text_field_by_xpath(key_value, text_field,
160636b08df603edccf33247c24545844abdc5a5c99Deepak Gopal                                                abort_check=True)
1617b0325111d7a7efea187d79f0087c710a9bdc626Deepak Gopal        self.restore_default_wait_time()
162