Skip to content
This repository was archived by the owner on Jun 7, 2023. It is now read-only.

Commit 519f86f

Browse files
committed
✅ Adding tests to hparsons directive
1 parent 37975b9 commit 519f86f

File tree

1 file changed

+73
-39
lines changed

1 file changed

+73
-39
lines changed

runestone/hparsons/test/test_hparsons.py

Lines changed: 73 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@
55
__author__ = "zihan wu"
66

77
import time
8-
from selenium.webdriver import ActionChains
8+
from selenium.webdriver.support import expected_conditions as EC
99
from selenium.webdriver.common.by import By
1010

1111
def find_hp_question(selenium_utils, div_id):
1212
selenium_utils.wait_until_ready(div_id)
1313
return selenium_utils.driver.find_element(By.ID, div_id)
1414

15+
def click_control(hp_question, button_name):
16+
css = '.btn-success.run-button' if button_name == 'Run' else '.btn-warning.run-button'
17+
btn = hp_question.find_element(By.CSS_SELECTOR, css)
18+
btn.click()
1519

1620
'''
1721
Test if the blocks are properly randomized.
@@ -33,46 +37,76 @@ def test_randomize_block(selenium_utils_get):
3337
assert not is_same_order
3438

3539

36-
# # Test adding and removing blocks by clicking in non-duplicating blocks
37-
# """
38-
# 1. Click on the first block and make sure it is moved to the bottom
39-
# 2. Click on the first block in the bottom and make sure it is put back
40-
# """
41-
# def test_add_and_remove_blocks(selenium_utils_get):
42-
# div_id = "test_hparsons_sql_1"
43-
# hp = find_hp_question(selenium_utils_get, div_id).find_element(By.CSS_SELECTOR, 'horizontal-parsons')
44-
# drag_area = hp.shadow_root.find_element(By.CSS_SELECTOR, '.drag-area')
45-
# drop_area = hp.shadow_root.find_element(By.CSS_SELECTOR, '.drop-area')
40+
"""
41+
Test adding and removing blocks by clicking in non-duplicating blocks
42+
For not reused blocks:
43+
1. Click on the first block and make sure it is moved to the bottom
44+
2. Click on the first block in the bottom and make sure it is put back
45+
For reuseable blocks:
46+
1. Click on the first block and make sure it is copied to the bottom
47+
2. Click on the first block in the bottom and make sure it disappears
48+
"""
49+
def test_add_and_remove_blocks(selenium_utils_get):
50+
div_id = "test_hparsons_sql_1"
51+
hp = find_hp_question(selenium_utils_get, div_id).find_element(By.CSS_SELECTOR, 'horizontal-parsons')
52+
drag_area = hp.shadow_root.find_element(By.CSS_SELECTOR, '.drag-area')
53+
drop_area = hp.shadow_root.find_element(By.CSS_SELECTOR, '.drop-area')
4654

47-
# # 1. Click on the first block and make sure it is added to the bottom
48-
# block1 = drag_area.find_elements(By.CSS_SELECTOR, '.parsons-block')[0]
49-
# block1.click()
50-
# assert block1 not in drag_area.find_elements(By.CSS_SELECTOR, '.parsons-block')
51-
# assert block1 == drop_area.find_elements(By.CSS_SELECTOR, '.parsons-block')[-1]
52-
55+
# 1. Click on the first block and make sure it is added to the bottom
56+
block1 = drag_area.find_elements(By.CSS_SELECTOR, '.parsons-block')[0]
57+
block1.click()
58+
assert block1 not in drag_area.find_elements(By.CSS_SELECTOR, '.parsons-block')
59+
assert block1 == drop_area.find_elements(By.CSS_SELECTOR, '.parsons-block')[-1]
5360

54-
# # 2. Drag the remaining first block and make sure it is added to the bottom
55-
# drag = [x.text for x in drag_area.find_elements(By.CSS_SELECTOR, '.parsons-block')]
56-
# print(drag)
57-
# block2 = drag_area.find_elements(By.CSS_SELECTOR, '.parsons-block')[0]
58-
# # ActionChains(selenium_utils_get.driver).drag_and_drop(
59-
# # block2, drop_area.find_elements(By.CSS_SELECTOR, '.parsons-block')[-1]
60-
# # ).perform()
61-
# ActionChains(selenium_utils_get.driver).drag_and_drop(
62-
# block2, drag_area.find_elements(By.CSS_SELECTOR, '.parsons-block')[-1]
63-
# ).perform()
64-
# time.sleep(2)
61+
# 2. Click on the moved block and make sure it is returned to the top
62+
# block2 and block1 are the same object
63+
block2 = drop_area.find_elements(By.CSS_SELECTOR, '.parsons-block')[0]
64+
block2.click()
65+
assert block2 not in drop_area.find_elements(By.CSS_SELECTOR, '.parsons-block')
66+
assert block2 == drag_area.find_elements(By.CSS_SELECTOR, '.parsons-block')[-1]
67+
68+
# For reusable blocks
69+
div_id = "test_hparsons_sql_2"
70+
hp = find_hp_question(selenium_utils_get, div_id).find_element(By.CSS_SELECTOR, 'horizontal-parsons')
71+
drag_area = hp.shadow_root.find_element(By.CSS_SELECTOR, '.drag-area')
72+
drop_area = hp.shadow_root.find_element(By.CSS_SELECTOR, '.drop-area')
6573

66-
# drag = [x.text for x in drag_area.find_elements(By.CSS_SELECTOR, '.parsons-block')]
67-
# print(drag)
74+
# 1. Click on the first block and make sure it is copied to the bottom
75+
block1 = drag_area.find_elements(By.CSS_SELECTOR, '.parsons-block')[0]
76+
block1.click()
77+
assert len(drag_area.find_elements(By.CSS_SELECTOR, '.parsons-block')) == 4
78+
assert block1.text == drop_area.find_elements(By.CSS_SELECTOR, '.parsons-block')[-1].text
6879

69-
# # assert block2 not in drag_area.find_elements(By.CSS_SELECTOR, '.parsons-block')
70-
# # assert block2 == drop_area.find_elements(By.CSS_SELECTOR, '.parsons-block')[-1]
71-
# assert block2 in drop_area.find_elements(By.CSS_SELECTOR, '.parsons-block')
80+
# 2. Click on the moved block and make sure it is removed
81+
block2 = drop_area.find_elements(By.CSS_SELECTOR, '.parsons-block')[0]
82+
block2.click()
83+
assert block2 not in drop_area.find_elements(By.CSS_SELECTOR, '.parsons-block')
84+
assert len(drag_area.find_elements(By.CSS_SELECTOR, '.parsons-block')) == 4
85+
86+
87+
"""
88+
Test SQL feedback is correct
89+
1. Click on each of the four blocks in second problem to form a solution
90+
2. Click on run button to run code and check the result
91+
"""
92+
def test_run_SQL(selenium_utils_get):
93+
# For reusable blocks
94+
div_id = "test_hparsons_sql_2"
95+
hp_question = find_hp_question(selenium_utils_get, div_id)
96+
time.sleep(2)
97+
hp = hp_question.find_element(By.CSS_SELECTOR, 'horizontal-parsons')
98+
drag_area = hp.shadow_root.find_element(By.CSS_SELECTOR, '.drag-area')
99+
drop_area = hp.shadow_root.find_element(By.CSS_SELECTOR, '.drop-area')
72100

73-
# # 2. Click on the moved block and make sure it is returned to the top
74-
# # block2 and block1 are the same object
75-
# block2 = drop_area.find_elements(By.CSS_SELECTOR, '.parsons-block')[0]
76-
# block2.click()
77-
# assert block2 not in drop_area.find_elements(By.CSS_SELECTOR, '.parsons-block')
78-
# assert block2 == drag_area.find_elements(By.CSS_SELECTOR, '.parsons-block')[-1]
101+
# 1. Click on each of the four blocks in second problem to form a solution
102+
blocks = drag_area.find_elements(By.CSS_SELECTOR, '.parsons-block')
103+
for block in blocks:
104+
block.click()
105+
106+
# 2. Click on run button to run code and check the result
107+
click_control(hp_question, 'Run')
108+
selenium_utils_get.wait.until(EC.text_to_be_present_in_element((By.ID, f"{div_id}_stdout"), "You"))
109+
res = selenium_utils_get.driver.find_element(By.ID, f"{div_id}_sql_out")
110+
assert res
111+
out = selenium_utils_get.driver.find_element(By.ID, f"{div_id}_stdout")
112+
assert "You passed 2 out of 3 tests" in out.text

0 commit comments

Comments
 (0)