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

Commit 3eabd2d

Browse files
committed
More waiting improvements
1 parent 22e8a49 commit 3eabd2d

2 files changed

Lines changed: 24 additions & 29 deletions

File tree

runestone/activecode/test/test_activecode.py

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ def test_hello(self):
1919
:return:
2020
"""
2121
self.driver.get(self.host + "/index.html")
22-
wait = WebDriverWait(self.driver, 10)
2322
try:
24-
wait.until(EC.presence_of_element_located((By.ID, "test1")))
23+
self.wait.until(EC.presence_of_element_located((By.ID, "test1")))
2524
except:
2625
text = self.driver.page_source
2726
print(text[:300])
@@ -30,10 +29,9 @@ def test_hello(self):
3029
rb = t1.find_element_by_class_name("run-button")
3130
self.assertIsNotNone(rb)
3231
rb.click()
33-
time.sleep(0.5)
34-
wait = WebDriverWait(self.driver, 10)
35-
wait.until(
36-
EC.text_to_be_present_in_element((By.ID, "test1_stdout"), "Hello")
32+
self.wait.until(
33+
EC.text_to_be_present_in_element((By.ID, "test1_stdout"), "Hello World"),
34+
message="Did not find expected text",
3735
)
3836
output = t1.find_element_by_class_name("ac_output")
3937
self.assertEqual(output.text.strip(), "Hello World")
@@ -47,9 +45,8 @@ def test_hidden(self):
4745
:return:
4846
"""
4947
self.driver.get(self.host + "/index.html")
50-
wait = WebDriverWait(self.driver, 10)
5148
try:
52-
wait.until(EC.presence_of_element_located((By.ID, "testprefixcode")))
49+
self.wait.until(EC.presence_of_element_located((By.ID, "testprefixcode")))
5350
except:
5451
text = self.driver.page_source
5552
print(text[:300])
@@ -58,11 +55,12 @@ def test_hidden(self):
5855
rb = t1.find_element_by_class_name("run-button")
5956
self.assertIsNotNone(rb)
6057
rb.click()
61-
wait.until(
62-
EC.text_to_be_present_in_element((By.ID, "testprefixcode_stdout"), "My")
58+
self.wait.until(
59+
EC.text_to_be_present_in_element(
60+
(By.ID, "testprefixcode_stdout"), "My Code"
61+
)
6362
)
6463
output = t1.find_element_by_class_name("ac_output")
65-
self.assertIn("My Code", output.text.strip())
6664
self.assertIn("hidden code", output.text.strip())
6765
self.assertIn("i\nx", output.text.strip())
6866

@@ -98,9 +96,7 @@ def test_history(self):
9896
self.driver.execute_script(
9997
"""window.edList['test1'].editor.setValue("print('GoodBye')")"""
10098
)
101-
WebDriverWait(self.driver, 10).until(
102-
EC.element_to_be_clickable((By.CLASS_NAME, "run-button"))
103-
)
99+
self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "run-button")))
104100
try:
105101
rb.click()
106102
except ElementClickInterceptedException:
@@ -179,10 +175,7 @@ def test_sql_activecode(self):
179175
rb = t2.find_element_by_class_name("run-button")
180176
self.assertIsNotNone(rb)
181177
rb.click()
182-
wait = WebDriverWait(self.driver, 10)
183-
wait.until(
184-
EC.text_to_be_present_in_element((By.ID, "sql1_stdout"), "You")
185-
)
178+
self.wait.until(EC.text_to_be_present_in_element((By.ID, "sql1_stdout"), "You"))
186179
res = self.driver.find_element_by_id("sql1_sql_out")
187180
self.assertIsNotNone(res)
188181
out = self.driver.find_element_by_id("sql1_stdout")
@@ -204,8 +197,7 @@ def test_readfiles(self):
204197
rb = t2.find_element_by_class_name("run-button")
205198
self.assertIsNotNone(rb)
206199
rb.click()
207-
wait = WebDriverWait(self.driver, 10)
208-
wait.until(
200+
self.wait.until(
209201
EC.text_to_be_present_in_element((By.ID, "ac9_13_1_stdout"), "Lind")
210202
)
211203
out = t2.find_element_by_id("ac9_13_1_stdout")
@@ -221,11 +213,11 @@ def test_altair(self):
221213
rb.click()
222214
out = t2.find_element_by_id("alt_kiva_bar1_stdout")
223215
self.assertIsNotNone(out)
224-
wait = WebDriverWait(self.driver, 10)
225-
wait.until(
226-
EC.text_to_be_present_in_element((By.ID, "alt_kiva_bar1_stdout"), "mark")
216+
self.wait.until(
217+
EC.text_to_be_present_in_element(
218+
(By.ID, "alt_kiva_bar1_stdout"), "mark = bar"
219+
)
227220
)
228-
self.assertIn("mark = bar", out.text)
229221
self.assertIn("{'field': 'customer', 'type': 'nominal'}", out.text)
230222
can = t2.find_element_by_tag_name("canvas")
231223
self.assertIsNotNone(can)
@@ -237,13 +229,11 @@ def test_image(self):
237229
rb = t2.find_element_by_class_name("run-button")
238230
self.assertIsNotNone(rb)
239231
rb.click()
240-
wait = WebDriverWait(self.driver, 10)
241-
wait.until(
232+
self.wait.until(
242233
EC.text_to_be_present_in_element((By.ID, "ac14_7_2_stdout"), "400")
243234
)
244235
out = t2.find_element_by_id("ac14_7_2_stdout")
245236
self.assertIsNotNone(out)
246-
self.assertTrue("400" in out.text)
247237
self.assertTrue("244" in out.text)
248238
self.assertTrue(("165 161 158" in out.text) or ("165 162 157" in out.text))
249239
can = t2.find_element_by_tag_name("canvas")

runestone/unittest_base.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
# -------------------
2525
from selenium import webdriver
2626
from selenium.webdriver.chrome.options import Options
27+
from selenium.webdriver.support.ui import WebDriverWait
2728
import pytest
2829
from pyvirtualdisplay import Display
2930

@@ -70,14 +71,15 @@ def setUpModule(self):
7071
# Change to this directory for running Runestone.
7172
self.old_cwd = os.getcwd()
7273
os.chdir(self.base_path)
73-
# use call so that npm run build completes before we move on to runestone build
74+
# use `run` so that `npm run build` completes before we move on to `runestone build`
7475
# otherwise the runestone build may fail due to lack of a runestone.js file!
75-
p = subprocess.call(
76+
p = subprocess.run(
7677
["npm.cmd" if IS_WINDOWS else "npm", "run", "build"],
7778
stdout=subprocess.PIPE,
7879
stderr=subprocess.PIPE,
7980
universal_newlines=True,
8081
)
82+
self.assertFalse(p.returncode)
8183
# Compile the docs. Save the stdout and stderr for examination.
8284
p = subprocess.Popen(
8385
["runestone", "build", "--all"],
@@ -230,6 +232,9 @@ def setUp(self):
230232
self.host = HOST_URL
231233
# Add an `implicit wait <https://selenium-python.readthedocs.io/waits.html#implicit-waits>`_.
232234
self.driver.implicitly_wait(5)
235+
# For cases where an implicit wait does not help. For example waiting for text to appear
236+
# after running an activecode. We create an explicit wait object.
237+
self.wait = WebDriverWait(self.driver, 5)
233238

234239
def tearDown(self):
235240
# Clear as much as possible, to present an almost-fresh instance of a browser for the next test. (Shutting down then starting up a browswer is very slow.)

0 commit comments

Comments
 (0)