Add _save_screenshot functionality to helpers.

Having screenshot of failed test might increase speed of
understanding the failure reason.

partially-implements blueprint: selenium-integration-testing

Depends-On: I0fd89ede7a3d04ce9b4bd4c8fba0789770048d40
Change-Id: Ibc4a6235423736e5938fb348bbe83211917b08d1
This commit is contained in:
Serhii Vasheka 2015-06-23 15:56:33 +03:00
parent bd71ffe34b
commit bc109d33ed
5 changed files with 35 additions and 0 deletions

1
.gitignore vendored
View File

@ -22,6 +22,7 @@ openstack_dashboard/wsgi/horizon.wsgi
doc/build/
doc/source/sourcecode
/static/
integration_tests_screenshots/
.venv
.tox
node_modules

View File

@ -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?
-----------------------

View File

@ -68,6 +68,9 @@ SeleniumGroup = [
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 = [

View File

@ -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
@ -22,6 +24,11 @@ from openstack_dashboard.test.integration_tests import config
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.
@ -64,6 +71,7 @@ class BaseTestCase(testtools.TestCase):
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)
@ -82,6 +90,21 @@ class BaseTestCase(testtools.TestCase):
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.

View File

@ -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)