Integration tests - page objects pattern

First pages:
* pageobject - the base page for all page objects
* loginpage - the login page object
* basepage - base class for all dashboard page objects
* adminpage
* projectpage

First test:
test_login - a basic scenario test:
* checks that the login page is available
* logs in as a regular user
* checks that the user home page loads without error

https://wiki.openstack.org/wiki/Horizon/Testing/UI

Partially implements blueprint: selenium-integration-testing

Change-Id: Icad82f5f7810c348ddc30cd767f1d3bf5ecb926e
This commit is contained in:
Daniel Korn 2014-03-02 12:43:01 +02:00
parent 1c2d3d7374
commit 5001bca975
8 changed files with 290 additions and 32 deletions

View File

@ -25,4 +25,4 @@ password=pass
admin_username=admin
# API key to use when authenticating as admin. (string value)
admin_password=pass
admin_password=pass

View File

@ -0,0 +1,19 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from openstack_dashboard.test.integration_tests.pages import basepage
class AdminPage(basepage.BasePage):
def __init__(self, driver, conf):
super(AdminPage, self).__init__(driver, conf)
self._page_title = "Usage Overview"

View File

@ -0,0 +1,69 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from selenium.webdriver.common import by
from openstack_dashboard.test.integration_tests.pages import pageobject
class BasePage(pageobject.PageObject):
"""Base class for all dashboard page objects."""
@property
def top_bar(self):
return BasePage.TopBarRegion(self.driver, self.conf)
@property
def is_logged_in(self):
return self.top_bar.is_logged_in
def go_to_login_page(self):
self.driver.get(self.login_url)
def log_out(self):
self.top_bar.logout_link.click()
return self.go_to_login_page()
class TopBarRegion(pageobject.PageObject):
_user_indicator_locator = (by.By.CSS_SELECTOR, "#user_info")
_user_dropdown_menu_locator = (by.By.CSS_SELECTOR,
"#profile_editor_switcher >"
" a.dropdown-toggle")
_settings_link_locator = (by.By.CSS_SELECTOR,
"a[href*='/settings/']")
_help_link_locator = (by.By.CSS_SELECTOR,
"ul#editor_list li:nth-of-type(3) > a")
_logout_link_locator = (by.By.CSS_SELECTOR,
"a[href*='/auth/logout/']")
@property
def logout_link(self):
return self.get_element(*self._logout_link_locator)
@property
def user_dropdown_menu(self):
return self.get_element(*self._user_dropdown_menu_locator)
@property
def settings_link(self):
return self.get_element(*self._settings_link_locator)
@property
def help_link(self):
return self.get_element(*self._help_link_locator)
@property
def is_logout_visible(self):
return self.is_element_visible(*self._logout_link_locator)
@property
def is_logged_in(self):
return self.is_element_visible(*self._user_indicator_locator)

View File

@ -0,0 +1,80 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from selenium.webdriver.common import by
from selenium.webdriver.common import keys
from openstack_dashboard.test.integration_tests.pages import adminpage
from openstack_dashboard.test.integration_tests.pages import pageobject
from openstack_dashboard.test.integration_tests.pages import projectpage
class LoginPage(pageobject.PageObject):
_login_username_field_locator = (by.By.CSS_SELECTOR, '#id_username')
_login_password_field_locator = (by.By.CSS_SELECTOR, '#id_password')
_login_submit_button_locator = (by.By.CSS_SELECTOR,
'div.modal-footer button.btn')
def __init__(self, driver, conf):
super(LoginPage, self).__init__(driver, conf)
self._page_title = "Login"
def is_login_page(self):
return self.is_the_current_page and \
self.is_element_visible(*self._login_submit_button_locator)
@property
def username(self):
return self.get_element(*self._login_username_field_locator)
@property
def password(self):
return self.get_element(*self._login_password_field_locator)
@property
def login_button(self):
return self.get_element(*self._login_submit_button_locator)
def _click_on_login_button(self):
self.login_button.click()
def _press_enter_on_login_button(self):
self.login_button.send_keys(keys.Keys.RETURN)
def login(self, *args, **kwargs):
return self.login_with_mouse_click(*args, **kwargs)
def login_with_mouse_click(self, *args, **kwargs):
return self._do_login(self._click_on_login_button, *args, **kwargs)
def login_with_enter_key(self, *args, **kwargs):
return self._do_login(self._press_enter_on_login_button,
*args, **kwargs)
def _do_login(self, login_method, user='user'):
if user != 'user':
return self.login_as_admin(login_method)
else:
return self.login_as_user(login_method)
def login_as_admin(self, login_method):
self.username.send_keys(self.conf.identity.admin_username)
self.password.send_keys(self.conf.identity.admin_password)
login_method()
return adminpage.AdminPage(self.driver, self.conf)
def login_as_user(self, login_method):
self.username.send_keys(self.conf.identity.username)
self.password.send_keys(self.conf.identity.password)
login_method()
return projectpage.ProjectPage(self.driver, self.conf)

View File

@ -0,0 +1,85 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#TODO(dkorn): add handle_popup method
import selenium.common.exceptions as Exceptions
import selenium.webdriver.support.ui as Support
class PageObject(object):
"""Base class for page objects."""
def __init__(self, driver, conf):
"""Constructor"""
self.driver = driver
self.conf = conf
self.login_url = self.conf.dashboard.login_url
self._page_title = None
@property
def page_title(self):
return self.driver.title
def is_the_current_page(self):
if self._page_title not in self.page_title:
raise AssertionError(
"Expected page title: %s. Actual page title: %s"
% (self._page_title, self.page_title))
return True
def get_url_current_page(self):
return self.driver.current_url()
def close_window(self):
return self.driver.close()
def go_to_login_page(self):
self.driver.get(self.login_url)
self.is_the_current_page()
def is_element_present(self, *locator):
try:
self.driver.find_element(*locator)
return True
except Exceptions.NoSuchElementException:
return False
def is_element_visible(self, *locator):
try:
return self.driver.find_element(*locator).is_displayed()
except (Exceptions.NoSuchElementException,
Exceptions.ElementNotVisibleException):
return False
def return_to_previous_page(self):
self.driver.back()
def get_element(self, *element):
return self.driver.find_element(*element)
def fill_field_element(self, data, field_element):
field_element.clear()
field_element.send_keys(data)
return field_element
def fill_field_by_locator(self, data, *locator):
field_element = self.get_element(*locator)
self.fill_field_element(data, field_element)
return field_element
def select_dropdown(self, value, *element):
select = Support.Select(self.driver.find_element(*element))
select.select_by_visible_text(value)
def select_dropdown_by_value(self, value, *element):
select = Support.Select(self.driver.find_element(*element))
select.select_by_value(value)

View File

@ -0,0 +1,19 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from openstack_dashboard.test.integration_tests.pages import basepage
class ProjectPage(basepage.BasePage):
def __init__(self, driver, conf):
super(ProjectPage, self).__init__(driver, conf)
self._page_title = 'Instance Overview'

View File

@ -1,43 +1,29 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import selenium.webdriver.common.keys as keys
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from openstack_dashboard.test.integration_tests import helpers
from openstack_dashboard.test.integration_tests.pages import loginpage
class TestLoginPage(helpers.BaseTestCase):
class TestLogin(helpers.BaseTestCase):
"""This is a basic scenario test:
* checks that the login page is available
* logs in as a regular user
* checks that the user home page loads without error
FIXME(jpichon): This test will be rewritten using the Page Objects
pattern, which is much more maintainable.
"""
def test_login(self):
self.driver.get(self.conf.dashboard.login_url)
self.assertIn("Login", self.driver.title)
username = self.driver.find_element_by_name("username")
password = self.driver.find_element_by_name("password")
username.send_keys(self.conf.identity.username)
password.send_keys(self.conf.identity.password)
username.send_keys(keys.Keys.RETURN)
self.wait_for_title()
self.assertIn("Instance Overview", self.driver.title)
login_pg = loginpage.LoginPage(self.driver, self.conf)
login_pg.go_to_login_page()
home_pg = login_pg.login()
if not home_pg.is_logged_in:
self.fail("Could not determine if logged in")
home_pg.log_out()