Add html source code on test failure for debugging

Sometimes it is not possible to reproduce failure in integration test
locally. Adding html source code of page where failure happened should
increase our ability to debug properly errors on the gates. For this
purpose the addDetail method from testtools package is used.

Following changes were done:

* TestCase and AdminTestCase classes were modified in order to share
  common functionality and remove code repetition.

* Testcases have been modified in order to use newly introduce class
  variables(TEST_USER_NAME, TEST_PASSWORD, CONFIG).

* Methods _get_page_html_source and _dump_page_html_source have been created
  in the BaseTestCase class. _get_page_html_source uses javascript innerHTML
  attribute in order to make javascript modification to the page visible
  in the output. (javascript modificaton would not have been visible if
  webdriver's property page_source was used)

Partially implements blueprint: selenium-integration-testing

Change-Id: I064405337fb68c966da007f2a5bf91d5c0ec2b3d
This commit is contained in:
Tomáš Nováčik 2015-03-11 16:24:38 +01:00 committed by Luigi Toscano
parent bffd06910f
commit e74296fb20
6 changed files with 48 additions and 28 deletions

View File

@ -12,6 +12,7 @@
import os
import time
import traceback
import uuid
import testtools
@ -43,6 +44,8 @@ def gen_random_resource_name(resource="", timestamp=True):
class BaseTestCase(testtools.TestCase):
CONFIG = config.get_config()
def setUp(self):
if os.environ.get('INTEGRATION_TESTS', False):
# Start a virtual display server for running the tests headless.
@ -57,14 +60,38 @@ class BaseTestCase(testtools.TestCase):
# Start the Selenium webdriver and setup configuration.
self.driver = webdriver.WebDriverWrapper()
self.driver.maximize_window()
self.conf = config.get_config()
self.driver.implicitly_wait(self.conf.selenium.implicit_wait)
self.driver.set_page_load_timeout(self.conf.selenium.page_timeout)
self.driver.implicitly_wait(self.CONFIG.selenium.implicit_wait)
self.driver.set_page_load_timeout(
self.CONFIG.selenium.page_timeout)
self.addOnException(self._dump_page_html_source)
else:
msg = "The INTEGRATION_TESTS env variable is not set."
raise self.skipException(msg)
super(BaseTestCase, self).setUp()
def _dump_page_html_source(self, exc_info):
content = None
try:
pg_source = self._get_page_html_source()
content = testtools.content.Content(
testtools.content_type.ContentType('text', 'html'),
lambda: pg_source)
except Exception:
exc_traceback = traceback.format_exc()
content = testtools.content.text_content(exc_traceback)
finally:
self.addDetail("PageHTMLSource.html", content)
def _get_page_html_source(self):
"""Gets html page source.
self.driver.page_source is not used on purpose because it does not
display html code generated/changed by javascript.
"""
html_elem = self.driver.find_element_by_tag_name("html")
return html_elem.get_attribute("innerHTML").encode("UTF-8")
def tearDown(self):
if os.environ.get('INTEGRATION_TESTS', False):
self.driver.quit()
@ -75,11 +102,15 @@ class BaseTestCase(testtools.TestCase):
class TestCase(BaseTestCase):
TEST_USER_NAME = BaseTestCase.CONFIG.identity.username
TEST_PASSWORD = BaseTestCase.CONFIG.identity.password
def setUp(self):
super(TestCase, self).setUp()
self.login_pg = loginpage.LoginPage(self.driver, self.conf)
self.login_pg = loginpage.LoginPage(self.driver, self.CONFIG)
self.login_pg.go_to_login_page()
self.home_pg = self.login_pg.login()
self.home_pg = self.login_pg.login(self.TEST_USER_NAME,
self.TEST_PASSWORD)
def tearDown(self):
try:
@ -90,18 +121,7 @@ class TestCase(BaseTestCase):
super(TestCase, self).tearDown()
class AdminTestCase(BaseTestCase):
def setUp(self):
super(AdminTestCase, self).setUp()
self.login_pg = loginpage.LoginPage(self.driver, self.conf)
self.login_pg.go_to_login_page()
self.home_pg = self.login_pg.login(
user=self.conf.identity.admin_username,
password=self.conf.identity.admin_password)
class AdminTestCase(TestCase):
def tearDown(self):
try:
if self.home_pg.is_logged_in:
self.home_pg.log_out()
finally:
super(AdminTestCase, self).tearDown()
TEST_USER_NAME = TestCase.CONFIG.identity.admin_username
TEST_PASSWORD = TestCase.CONFIG.identity.admin_password

View File

@ -21,7 +21,7 @@ class TestDashboardHelp(helpers.TestCase):
self.home_pg.go_to_help_page()
self.home_pg.switch_window()
self.assertEqual(self.conf.dashboard.help_url,
self.assertEqual(self.CONFIG.dashboard.help_url,
self.home_pg.get_url_current_page(),
"help link did not redirect to the right URL")

View File

@ -21,7 +21,7 @@ class TestLogin(helpers.BaseTestCase):
* checks that the user home page loads without error
"""
def test_login(self):
login_pg = loginpage.LoginPage(self.driver, self.conf)
login_pg = loginpage.LoginPage(self.driver, self.CONFIG)
login_pg.go_to_login_page()
home_pg = login_pg.login()
if not home_pg.is_logged_in:

View File

@ -33,11 +33,11 @@ class TestPasswordChange(helpers.TestCase):
passwordchange_page = self.home_pg.go_to_settings_changepasswordpage()
try:
passwordchange_page.change_password(self.conf.identity.password,
passwordchange_page.change_password(self.TEST_PASSWORD,
NEW_PASSWORD)
self.home_pg = self.login_pg.login(
user=self.conf.identity.username, password=NEW_PASSWORD)
self.home_pg = self.login_pg.login(user=self.TEST_USER_NAME,
password=NEW_PASSWORD)
self.assertTrue(self.home_pg.is_logged_in,
"Failed to login with new password")
finally:
@ -51,13 +51,13 @@ class TestPasswordChange(helpers.TestCase):
passwordchange_page = self.home_pg.go_to_settings_changepasswordpage()
try:
passwordchange_page.change_password(self.conf.identity.password,
passwordchange_page.change_password(self.TEST_PASSWORD,
NEW_PASSWORD)
self.assertTrue(
self.login_pg.is_logout_reason_displayed(),
"The logout reason message was not found on the login page")
finally:
self.login_pg.login(user=self.conf.identity.username,
self.login_pg.login(user=self.TEST_USER_NAME,
password=NEW_PASSWORD)
self._reset_password()
self._login()

View File

@ -28,7 +28,7 @@ class TestSaharaImageRegistry(helpers.TestCase):
def test_image_register_unregister(self):
"""Test the image registration in Sahara."""
image_reg_pg = self.home_pg.go_to_dataprocessing_imageregistrypage()
image_reg_pg.register_image(IMAGE_NAME, self.conf.scenario.ssh_user,
image_reg_pg.register_image(IMAGE_NAME, self.CONFIG.scenario.ssh_user,
"Test description")
self.assertTrue(image_reg_pg.is_image_registered(IMAGE_NAME),
"Image was not registered.")

View File

@ -20,7 +20,7 @@ class TestUser(helpers.AdminTestCase):
def test_create_delete_user(self):
users_page = self.home_pg.go_to_identity_userspage()
password = self.conf.identity.password
password = self.TEST_PASSWORD
users_page.create_user(self.USER_NAME, password=password,
project='admin', role='admin')
self.assertTrue(users_page.is_user_present(self.USER_NAME))