Commit 66185084 by dragonfi

Add scenario loading from path

parent d24be50d
<explanation>This is an even simpler xml template.</explanation>
<example>
<title>This is a simple xml template.</title>
<arguments>
<url_name>{{url_name}}</url_name>
</arguments>
</example>
...@@ -45,6 +45,30 @@ expected_filled_js_template = u"""\ ...@@ -45,6 +45,30 @@ expected_filled_js_template = u"""\
""".format(expected_filled_template) """.format(expected_filled_template)
another_template = u"""\
<explanation>This is an even simpler xml template.</explanation>
"""
simple_template = u"""\
<example>
<title>This is a simple xml template.</title>
<arguments>
<url_name>simple_template</url_name>
</arguments>
</example>
"""
expected_scenarios_with_identifiers = [
("another_template", "Another Template", another_template),
("simple_template", "Simple Template", simple_template),
]
expected_scenarios = [(t, c) for (i, t, c) in expected_scenarios_with_identifiers]
class TestResourceLoader(unittest.TestCase): class TestResourceLoader(unittest.TestCase):
def test_load_unicode(self): def test_load_unicode(self):
s = ResourceLoader(__name__).load_unicode("data/simple_django_template.txt") s = ResourceLoader(__name__).load_unicode("data/simple_django_template.txt")
...@@ -55,11 +79,21 @@ class TestResourceLoader(unittest.TestCase): ...@@ -55,11 +79,21 @@ class TestResourceLoader(unittest.TestCase):
self.assertEquals(s, expected_string) self.assertEquals(s, expected_string)
def test_render_template(self): def test_render_template(self):
loader = ResourceLoader("tests.unit.data") loader = ResourceLoader(__name__)
s = loader.render_template("simple_django_template.txt", example_context) s = loader.render_template("data/simple_django_template.txt", example_context)
self.assertEquals(s, expected_filled_template) self.assertEquals(s, expected_filled_template)
def test_render_js_template(self): def test_render_js_template(self):
loader = ResourceLoader("tests.unit.data") loader = ResourceLoader(__name__)
s = loader.render_js_template("simple_django_template.txt", example_id, example_context) s = loader.render_js_template("data/simple_django_template.txt", example_id, example_context)
self.assertEquals(s, expected_filled_js_template) self.assertEquals(s, expected_filled_js_template)
def test_load_scenarios(self):
loader = ResourceLoader(__name__)
scenarios = loader.load_scenarios_from_path("data")
self.assertEquals(scenarios, expected_scenarios)
def test_load_scenarios_with_identifiers(self):
loader = ResourceLoader(__name__)
scenarios = loader.load_scenarios_from_path("data", include_identifier=True)
self.assertEquals(scenarios, expected_scenarios_with_identifiers)
...@@ -11,6 +11,8 @@ from selenium.webdriver.support.ui import WebDriverWait ...@@ -11,6 +11,8 @@ from selenium.webdriver.support.ui import WebDriverWait
from workbench import scenarios from workbench import scenarios
from workbench.test.selenium_test import SeleniumTest from workbench.test.selenium_test import SeleniumTest
from .resources import ResourceLoader
class SeleniumBaseTest(SeleniumTest): class SeleniumBaseTest(SeleniumTest):
module_name = None module_name = None
...@@ -30,18 +32,14 @@ class SeleniumBaseTest(SeleniumTest): ...@@ -30,18 +32,14 @@ class SeleniumBaseTest(SeleniumTest):
raise NotImplementedError("Overwrite cls.default_css_selector in your derived class.") raise NotImplementedError("Overwrite cls.default_css_selector in your derived class.")
return self.default_css_selector return self.default_css_selector
@property
def scenario_path(self):
base_dir = os.path.dirname(os.path.realpath(sys.modules[self._module_name].__file__))
return os.path.join(base_dir, self.relative_scenario_path)
def setUp(self): def setUp(self):
super(SeleniumBaseTest, self).setUp() super(SeleniumBaseTest, self).setUp()
# Use test scenarios # Use test scenarios
self.browser.get(self.live_server_url) # Needed to load tests once self.browser.get(self.live_server_url) # Needed to load tests once
scenarios.SCENARIOS.clear() scenarios.SCENARIOS.clear()
scenarios_list = self._load_scenarios_from_path(self.scenario_path) loader = ResourceLoader(self._module_name)
scenarios_list = loader.load_scenarios_from_path(self.relative_scenario_path, include_identifier=True)
for identifier, title, xml in scenarios_list: for identifier, title, xml in scenarios_list:
scenarios.add_xml_scenario(identifier, title, xml) scenarios.add_xml_scenario(identifier, title, xml)
self.addCleanup(scenarios.remove_scenario, identifier) self.addCleanup(scenarios.remove_scenario, identifier)
...@@ -82,31 +80,3 @@ class SeleniumBaseTest(SeleniumTest): ...@@ -82,31 +80,3 @@ class SeleniumBaseTest(SeleniumTest):
time.sleep(1) time.sleep(1)
block = self.browser.find_element_by_css_selector(css_selector) block = self.browser.find_element_by_css_selector(css_selector)
return block return block
def _load_resource(self, resource_path):
"""
Gets the content of a resource
"""
resource_content = pkg_resources.resource_string(self._module_name, resource_path)
return unicode(resource_content)
def _render_template(self, template_path, context={}):
"""
Evaluate a template by resource path, applying the provided context
"""
template_str = self._load_resource(template_path)
template = Template(template_str)
return template.render(Context(context))
def _load_scenarios_from_path(self, xml_path):
list_of_scenarios = []
if os.path.isdir(xml_path):
for template in os.listdir(xml_path):
if not template.endswith('.xml'):
continue
identifier = template[:-4]
title = identifier.replace('_', ' ').title()
template_path = os.path.join(self.relative_scenario_path, template)
scenario = unicode(self._render_template(template_path, {"url_name": identifier}))
list_of_scenarios.append((identifier, title, scenario))
return list_of_scenarios
import os
import sys
import pkg_resources import pkg_resources
from django.template import Context, Template from django.template import Context, Template
...@@ -29,3 +32,29 @@ class ResourceLoader(object): ...@@ -29,3 +32,29 @@ class ResourceLoader(object):
element_id, element_id,
self.render_template(template_path, context) self.render_template(template_path, context)
) )
def load_scenarios_from_path(self, relative_scenario_dir, include_identifier=False):
"""
Returns an array of (title, xmlcontent) from files contained in a specified directory,
formatted as expected for the return value of the workbench_scenarios() method.
If `include_identifier` is True, returns an array of (identifier, title, xmlcontent).
"""
base_dir = os.path.dirname(os.path.realpath(sys.modules[self.module_name].__file__))
scenario_dir = os.path.join(base_dir, relative_scenario_dir)
scenarios = []
if os.path.isdir(scenario_dir):
for template in os.listdir(scenario_dir):
if not template.endswith('.xml'):
continue
identifier = template[:-4]
title = identifier.replace('_', ' ').title()
template_path = os.path.join(relative_scenario_dir, template)
scenario = unicode(self.render_template(template_path, {"url_name": identifier}))
if not include_identifier:
scenarios.append((title, scenario))
else:
scenarios.append((identifier, title, scenario))
return scenarios
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment