Commit 17c1e77e by Xavier Antoviaque

Add tests for answer block & factorize scenarios loading from directory

parent f0c75daa
......@@ -3,12 +3,12 @@
# Imports ###########################################################
import logging
import os
from xblock.core import XBlock
from xblock.fields import Boolean, Scope, String
from .utils import load_resource, render_template, XBlockWithChildrenFragmentsMixin
from .utils import get_scenarios_from_path, load_resource, render_template, \
XBlockWithChildrenFragmentsMixin
# Globals ###########################################################
......@@ -138,16 +138,4 @@ class MentoringBlock(XBlock, XBlockWithChildrenFragmentsMixin):
"""
Scenarios displayed by the workbench. Load them from external (private) repository
"""
templates_path = 'templates/xml'
base_fullpath = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
templates_fullpath = os.path.join(base_fullpath, templates_path)
scenarios = []
for template in os.listdir(templates_fullpath):
if not template.endswith('.xml'):
continue
title = template[:-4].replace('_', ' ').title()
template_path = os.path.join(templates_path, template)
scenarios.append((title, load_resource(template_path)))
return scenarios
return get_scenarios_from_path('templates/xml')
"""Tests for the mentoring module"""
# Imports ###########################################################
from workbench import scenarios
from workbench.test.selenium_test import SeleniumTest
from .utils import load_scenarios_from_path
# Classes ###########################################################
class MentoringBlockTest(SeleniumTest):
class MentoringBaseTest(SeleniumTest):
def setUp(self):
super(MentoringBlockTest, self).setUp()
super(MentoringBaseTest, self).setUp()
# Use test scenarios
self.browser.get(self.live_server_url) # Needed to load tests once
scenarios.SCENARIOS.clear()
scenarios_list = load_scenarios_from_path('tests/xml')
for identifier, title, xml in scenarios_list:
self.addCleanup(scenarios.remove_scenario, identifier)
# Suzy opens the browser to visit the workbench
self.browser.get(self.live_server_url)
......@@ -19,6 +28,3 @@ class MentoringBlockTest(SeleniumTest):
header1 = self.browser.find_element_by_css_selector('h1')
self.assertEqual(header1.text, 'XBlock scenarios')
def test_TODO_NAME(self):
link = self.browser.find_element_by_link_text('001) Pre-goal brainstom')
link.click()
......@@ -11,6 +11,7 @@ import unicodecsv
from cStringIO import StringIO
from django.template import Context, Template
from xblock.fragment import Fragment
from workbench.scenarios import add_xml_scenario
# Globals ###########################################################
......@@ -46,6 +47,37 @@ def list2csv(row):
f.seek(0)
return f.read()
def get_scenarios_from_path(scenarios_path, 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
"""
base_fullpath = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
scenarios_fullpath = os.path.join(base_fullpath, scenarios_path)
scenarios = []
for template in os.listdir(scenarios_fullpath):
if not template.endswith('.xml'):
continue
identifier = template[:-4]
title = identifier.replace('_', ' ').title()
template_path = os.path.join(scenarios_path, template)
if not include_identifier:
scenarios.append((title, load_resource(template_path)))
else:
scenarios.append((identifier, title, load_resource(template_path)))
return scenarios
def load_scenarios_from_path(scenarios_path):
"""
Load all xml files contained in a specified directory, as workbench scenarios
"""
scenarios = get_scenarios_from_path(scenarios_path, include_identifier=True)
for identifier, title, xml_template in scenarios:
add_xml_scenario(identifier, title, xml_template)
return scenarios
# Classes ###########################################################
......
# Imports ###########################################################
from mentoring.test_base import MentoringBaseTest
# Classes ###########################################################
class AnswerBlockTest(MentoringBaseTest):
def test_answer_edit(self):
# Answer should initially be blank on all instances with the same answer name
link = self.browser.find_element_by_link_text('Answer Edit 2')
link.click()
mentoring = self.browser.find_element_by_css_selector('div.mentoring')
answer1_bis = mentoring.find_element_by_css_selector('.xblock textarea')
answer1_readonly = mentoring.find_element_by_css_selector('blockquote.answer.read_only')
self.assertEqual(answer1_bis.get_attribute('value'), '')
self.assertEqual(answer1_readonly.text, '')
# Another answer with the same name
self.browser.get(self.live_server_url)
link = self.browser.find_element_by_link_text('Answer Edit 1')
link.click()
header1 = self.browser.find_element_by_css_selector('h1')
self.assertEqual(header1.text, 'XBlock: Answer Edit 1')
# Mentoring block
vertical = self.browser.find_element_by_css_selector('div.vertical')
mentoring = vertical.find_element_by_css_selector('div.mentoring')
# Check <html> child
p = mentoring.find_element_by_css_selector('div.xblock > p')
self.assertEqual(p.text, 'This should be displayed in the answer_edit scenario')
# Initial unsubmitted text
answer1 = mentoring.find_element_by_css_selector('textarea')
self.assertEqual(answer1.text, '')
progress = mentoring.find_element_by_css_selector('.progress > .indicator')
self.assertEqual(progress.text, '(Not completed)')
self.assertFalse(progress.find_elements_by_xpath('./*'))
# Submit without answer
submit = mentoring.find_element_by_css_selector('input.submit')
submit.click()
self.assertEqual(answer1.get_attribute('value'), '')
self.assertEqual(progress.text, '(Not completed)')
self.assertFalse(progress.find_elements_by_xpath('./*'))
# Submit an answer
answer1.send_keys('This is the answer')
submit.click()
self.assertEqual(answer1.get_attribute('value'), 'This is the answer')
self.assertEqual(progress.text, '')
self.assertTrue(progress.find_elements_by_css_selector('img'))
# Answer content should show on a different instance with the same name
self.browser.get(self.live_server_url)
link = self.browser.find_element_by_link_text('Answer Edit 2')
link.click()
mentoring = self.browser.find_element_by_css_selector('div.mentoring')
answer1_bis = mentoring.find_element_by_css_selector('.xblock textarea')
answer1_readonly = mentoring.find_element_by_css_selector('blockquote.answer.read_only')
self.assertEqual(answer1_bis.get_attribute('value'), 'This is the answer')
self.assertEqual(answer1_readonly.text, 'This is the answer')
<vertical>
<mentoring url_name="answer_edit_1" enforce_dependency="false">
<html>
<p>This should be displayed in the answer_edit scenario</p>
</html>
<answer name="answer_1" />
</mentoring>
</vertical>
<vertical>
<mentoring url_name="answer_edit_2" enforce_dependency="false">
<answer name="answer_1" read_only="true" />
<answer name="answer_1" />
</mentoring>
</vertical>
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