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
5import urlparse
6
7import belkinF5D8236_ap_configurator
8from selenium.common.exceptions import WebDriverException
9from selenium.common.exceptions import TimeoutException
10
11
12class BelkinF9K1001APConfigurator(
13        belkinF5D8236_ap_configurator.BelkinF5D8236APConfigurator):
14    """Class to configure Belkin F9K1001v5 (01B) router."""
15
16    def __init__(self, ap_config):
17        super(BelkinF9K1001APConfigurator, self).__init__(ap_config)
18        self._dhcp_delay = 0
19
20
21    def _security_alert(self, alert):
22        text = alert.text
23        if 'Selecting WEP Encryption will disable the WPS' in text:
24            alert.accept()
25        elif 'Selecting WPA/TKIP Encryption will disable the WPS' in text:
26            alert.accept()
27        elif 'Invalid character' in text:
28            alert.accept()
29        elif 'It is recommended to use WPA/WPA2 when WPS is enabled' in text:
30            alert.accept()
31        elif 'Are you sure to configure WPS in Open security?' in text:
32            alert.accept()
33        else:
34            alert.accept()
35            raise RuntimeError('Unhandeled modal dialog. %s' % text)
36
37
38    def navigate_to_page(self, page_number):
39        """
40        Navigates to the page corresponding to the given page number.
41
42        This method performs the translation between a page number and a url to
43        load. This is used internally by apply_settings.
44
45        @param page_number: page number of the page to load
46
47        """
48        page_title='Network Name'
49        element_xpath='//input[@name="ssid"]'
50        page_url = None
51
52        if page_number == 1:
53            page_url = urlparse.urljoin(self.admin_interface_url,
54                                        'wifi_id.stm')
55        elif page_number == 2:
56            page_url = urlparse.urljoin(self.admin_interface_url,
57                                        'wifi_sc.stm')
58            page_title='Security'
59            element_xpath=self.security_popup
60        else:
61            raise RuntimeError('Invalid page number passed. Number of pages '
62                               '%d, page value sent was %d' %
63                               (self.get_number_of_pages(), page_number))
64        try:
65            self.get_url(page_url, page_title=page_title,
66                         element_xpath=element_xpath)
67        except WebDriverException, e:
68            if 'Welcome to your Belkin router dashboard!' in self.driver.title:
69                self._login_to_dashboard(element_xpath)
70        self.wait_for_object_by_xpath(element_xpath)
71
72
73    def _login_to_dashboard(self, obj_xpath):
74        """
75        Login and wait for the object with obj_xpath to show up.
76
77        @param obj_xpath: The object that should be searched to confirm that we
78                          logged in.
79
80        """
81        self.set_content_of_text_field_by_id('password', 'p1210Password',
82                                             abort_check=True)
83        self.click_button_by_id('p1210a005')
84        self.wait_for_object_by_xpath(obj_xpath, wait_time=10)
85
86
87    def save_page(self, page_number):
88        """
89        Save changes.
90
91        @param page_number: the page number to save as an integer.
92
93        """
94        self.set_wait_time(30)
95        if page_number == 1:
96            xpath = '//a[text()="Save" and @href="#save" and @id="dnsapply"]'
97            self.click_button_by_xpath(xpath,
98                                       alert_handler=self._security_alert)
99        elif page_number == 2:
100            button = self.driver.find_element_by_link_text('Save')
101            button.click()
102            self._handle_alert(None, alert_handler=self._security_alert)
103        dashboard_title = 'Welcome to your Belkin router dashboard!'
104        network_title = 'Network Name'
105        security_title = 'Security'
106        try:
107            # This is a dummy wait. We just need to make sure that we give the
108            # router enough time to save the changes.
109            self.wait.until(lambda _: dashboard_title in self.driver.title)
110        except TimeoutException, e:
111            if not self.driver.title in [dashboard_title, network_title,
112                                    security_title]:
113                raise RuntimeError('Error while saving the page. ' + str(e))
114        finally:
115            self.restore_default_wait_time()
116
117
118    def set_security_wep(self, key_value, authentication):
119        self.add_item_to_command_list(self._set_security_wep,
120                                      (key_value, authentication), 2, 1000)
121
122
123    def _set_security_wep(self, key_value, authentication):
124        text_field = '//input[@name="passphrase"]'
125        try:
126            self.select_item_from_popup_by_xpath('64bit WEP',
127                    self.security_popup, wait_for_xpath=text_field,
128                    alert_handler=self._security_alert)
129        except WebDriverException, e:
130            message = str(e)
131            if message.find('An open modal dialog blocked') == -1:
132               raise RuntimeError(message)
133            self._security_alert(self.driver.switch_to_alert())
134        self.set_content_of_text_field_by_xpath(key_value, text_field,
135                                                abort_check=True)
136        self.click_button_by_id('btngen', alert_handler=self._security_alert)
137