Skip to content

Commit 099383e

Browse files
authored
Merge pull request #4141 from askerry/selenium_display_image
Migrate image display test to selenium
2 parents 2af7440 + a5a6e4a commit 099383e

File tree

3 files changed

+74
-66
lines changed

3 files changed

+74
-66
lines changed

notebook/tests/notebook/display_image.js

Lines changed: 0 additions & 63 deletions
This file was deleted.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""Test display of images
2+
3+
The effect of shape metadata is validated, using Image(retina=True)
4+
"""
5+
6+
from .utils import wait_for_tag
7+
8+
9+
# 2x2 black square in b64 jpeg and png
10+
b64_image_data = {
11+
"image/png" : b'iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAAC0lEQVR4nGNgQAYAAA4AAamRc7EA\\nAAAASUVORK5CYII',
12+
"image/jpeg" : b'/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0a\nHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIy\nMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAACAAIDASIA\nAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA\nAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3\nODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWm\np6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEA\nAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSEx\nBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElK\nU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3\nuLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD5/ooo\noAoo2Qoo'
13+
}
14+
15+
16+
def imports(notebook):
17+
commands = [
18+
'import base64',
19+
'from IPython.display import display, Image',
20+
]
21+
notebook.edit_cell(index=0, content="\n".join(commands))
22+
notebook.execute_cell(0)
23+
24+
25+
def validate_img(notebook, cell_index, image_fmt, retina):
26+
"""Validate that image renders as expected."""
27+
28+
b64data = b64_image_data[image_fmt]
29+
commands = [
30+
'b64data = %s' % b64data,
31+
'data = base64.decodebytes(b64data)',
32+
'display(Image(data, retina=%s))' % retina
33+
]
34+
notebook.append("\n".join(commands))
35+
notebook.execute_cell(cell_index)
36+
37+
# Find the image element that was just displayed
38+
wait_for_tag(notebook.cells[cell_index], "img", single=True)
39+
img_element = notebook.cells[cell_index].find_element_by_tag_name("img")
40+
41+
src = img_element.get_attribute("src")
42+
prefix = src.split(',')[0]
43+
expected_prefix = "data:%s;base64" % image_fmt
44+
assert prefix == expected_prefix
45+
46+
expected_size = 1 if retina else 2
47+
assert img_element.size["width"] == expected_size
48+
assert img_element.size["height"] == expected_size
49+
assert img_element.get_attribute("width") == str(expected_size)
50+
assert img_element.get_attribute("height") == str(expected_size)
51+
52+
53+
def test_display_image(notebook):
54+
imports(notebook)
55+
# PNG, non-retina
56+
validate_img(notebook, 1, "image/png", False)
57+
58+
# PNG, retina display
59+
validate_img(notebook, 2, "image/png", True)
60+
61+
# JPEG, non-retina
62+
validate_img(notebook, 3, "image/jpeg", False)
63+
64+
# JPEG, retina display
65+
validate_img(notebook, 4, "image/jpeg", True)

notebook/tests/selenium/utils.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,14 @@
1212
pjoin = os.path.join
1313

1414

15-
def wait_for_selector(browser, selector, timeout=10, visible=False, single=False):
16-
wait = WebDriverWait(browser, timeout)
15+
def wait_for_selector(driver, selector, timeout=10, visible=False, single=False):
16+
return _wait_for(driver, By.CSS_SELECTOR, selector, timeout, visible, single)
17+
18+
def wait_for_tag(driver, tag, timeout=10, visible=False, single=False):
19+
return _wait_for(driver, By.TAG_NAME, tag, timeout, visible, single)
20+
21+
def _wait_for(driver, locator_type, locator, timeout=10, visible=False, single=False):
22+
wait = WebDriverWait(driver, timeout)
1723
if single:
1824
if visible:
1925
conditional = EC.visibility_of_element_located
@@ -24,7 +30,7 @@ def wait_for_selector(browser, selector, timeout=10, visible=False, single=False
2430
conditional = EC.visibility_of_all_elements_located
2531
else:
2632
conditional = EC.presence_of_all_elements_located
27-
return wait.until(conditional((By.CSS_SELECTOR, selector)))
33+
return wait.until(conditional((locator_type, locator)))
2834

2935

3036
class CellTypeError(ValueError):

0 commit comments

Comments
 (0)