Skip to content

Commit

Permalink
Merge "Add _save_screenshot functionality to helpers."
Browse files Browse the repository at this point in the history
  • Loading branch information
Jenkins authored and openstack-gerrit committed Sep 28, 2015
2 parents 223f2eb + bc109d3 commit 0f40f7c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ openstack_dashboard/wsgi/horizon.wsgi
doc/build/
doc/source/sourcecode
/static/
integration_tests_screenshots/
.venv
.tox
node_modules
Expand Down
4 changes: 4 additions & 0 deletions doc/source/topics/testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,16 @@ Pros:
* Catches *many* bugs that unit and functional tests will not.
* Doesn't rely on assumptions about the inputs and outputs.
* Will warn you when changes in external components break your code.
* Will take screenshot of the current page on test fail for easy debug

Cons:

* Difficult and time-consuming to create a repeatable test environment.
* Did I mention that setting it up is a pain?

Screenshot directory could be set through horizon.conf file, default value:
"./integration_tests_screenshots"

So what should I write?
-----------------------

Expand Down
3 changes: 3 additions & 0 deletions openstack_dashboard/test/integration_tests/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@
cfg.IntOpt('page_timeout',
default=30,
help="Page load timeout in seconds"),
cfg.StrOpt('screenshots_directory',
default="integration_tests_screenshots",
help="Output screenshot directory"),
]

ScenarioGroup = [
Expand Down
23 changes: 23 additions & 0 deletions openstack_dashboard/test/integration_tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
# License for the specific language governing permissions and limitations
# under the License.

import datetime
import os
import sys
import time
import traceback
import uuid
Expand All @@ -22,6 +24,11 @@
from openstack_dashboard.test.integration_tests.pages import loginpage
from openstack_dashboard.test.integration_tests import webdriver

ROOT_PATH = os.path.dirname(os.path.abspath(__file__))

if ROOT_PATH not in sys.path:
sys.path.append(ROOT_PATH)


def gen_random_resource_name(resource="", timestamp=True):
"""Generate random resource name using uuid and timestamp.
Expand Down Expand Up @@ -64,6 +71,7 @@ def setUp(self):
self.driver.set_page_load_timeout(
self.CONFIG.selenium.page_timeout)
self.addOnException(self._dump_page_html_source)
self.addOnException(self._save_screenshot)
else:
msg = "The INTEGRATION_TESTS env variable is not set."
raise self.skipException(msg)
Expand All @@ -82,6 +90,21 @@ def _dump_page_html_source(self, exc_info):
finally:
self.addDetail("PageHTMLSource.html", content)

def _save_screenshot(self, exc_info):
screenshot_dir = os.path.join(
ROOT_PATH,
self.CONFIG.selenium.screenshots_directory)
if not os.path.exists(screenshot_dir):
os.makedirs(screenshot_dir)
date_string = datetime.datetime.now().strftime(
'%Y.%m.%d-%H%M%S')
test_name = self._testMethodName
name = '%s_%s.png' % (test_name, date_string)
filename = os.path.join(screenshot_dir, name)
self.driver.get_screenshot_as_file(filename)
content = testtools.content.text_content(filename)
self.addDetail("Screenshot", content)

def _get_page_html_source(self):
"""Gets html page source.
Expand Down
4 changes: 4 additions & 0 deletions openstack_dashboard/test/integration_tests/horizon.conf
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ help_url=http://docs.openstack.org/
# (integer value)
page_timeout=30

# Output directory for screenshots.
# (string value)
screenshots_directory=integration_tests_screenshots

# Implicit timeout to wait until element become available,
# this timeout is used for every find_element, find_elements call.
# (integer value)
Expand Down

0 comments on commit 0f40f7c

Please sign in to comment.