1#    Licensed under the Apache License, Version 2.0 (the "License"); you may
2#    not use this file except in compliance with the License. You may obtain
3#    a copy of the License at
4#
5#         http://www.apache.org/licenses/LICENSE-2.0
6#
7#    Unless required by applicable law or agreed to in writing, software
8#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10#    License for the specific language governing permissions and limitations
11#    under the License.
12
13from urllib import parse
14
15from openstack_dashboard.test.integration_tests import basewebobject
16
17
18class PageObject(basewebobject.BaseWebObject):
19    """Base class for page objects."""
20
21    PARTIAL_LOGIN_URL = 'auth/login'
22
23    def __init__(self, driver, conf):
24        """Constructor."""
25        super().__init__(driver, conf)
26        self._page_title = None
27
28    @property
29    def page_title(self):
30        return self.driver.title
31
32    def is_the_current_page(self, do_assert=False):
33        found_expected_title = self.page_title.startswith(self._page_title)
34        if do_assert:
35            self.assertTrue(
36                found_expected_title,
37                "Expected to find %s in page title, instead found: %s"
38                % (self._page_title, self.page_title))
39        return found_expected_title
40
41    @property
42    def login_url(self):
43        base_url = self.conf.dashboard.dashboard_url
44        if not base_url.endswith('/'):
45            base_url += '/'
46        return parse.urljoin(base_url, self.PARTIAL_LOGIN_URL)
47
48    def get_url_current_page(self):
49        return self.driver.current_url
50
51    def close_window(self):
52        return self.driver.close()
53
54    def is_nth_window_opened(self, n):
55        return len(self.driver.window_handles) == n
56
57    def switch_window(self, window_name=None, window_index=None):
58        """Switches focus between the webdriver windows.
59
60        Args:
61        - window_name: The name of the window to switch to.
62        - window_index: The index of the window handle to switch to.
63        If the method is called without arguments it switches to the
64         last window in the driver window_handles list.
65        In case only one window exists nothing effectively happens.
66        Usage:
67        page.switch_window('_new')
68        page.switch_window(2)
69        page.switch_window()
70        """
71
72        if window_name is not None and window_index is not None:
73            raise ValueError("switch_window receives the window's name or "
74                             "the window's index, not both.")
75        if window_name is not None:
76            self.driver.switch_to.window(window_name)
77        elif window_index is not None:
78            self.driver.switch_to.window(
79                self.driver.window_handles[window_index])
80        else:
81            self.driver.switch_to.window(self.driver.window_handles[-1])
82
83    def go_to_previous_page(self):
84        self.driver.back()
85
86    def go_to_next_page(self):
87        self.driver.forward()
88
89    def refresh_page(self):
90        self.driver.refresh()
91
92    def go_to_login_page(self):
93        self.driver.get(self.login_url)
94        self.is_the_current_page(do_assert=True)
95