Skip to content

Commit b0f846e

Browse files
committed
use element screenshot to capture about
1 parent 5abcfe8 commit b0f846e

File tree

5 files changed

+65
-9
lines changed

5 files changed

+65
-9
lines changed

atest/acceptance/10_lab/00_shell.robot

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Get Help
1212
Open JupyterLab
1313
Click JupyterLab Menu Help
1414
Click JupyterLab Menu Item About JupyterLab
15+
Capture Element Screenshot css:.jp-Dialog-content ${OUTPUT_DIR}${/}about.png
1516
Click Element css:${JLAB CSS ACCEPT}
1617

1718
*** Keywords ***

atest/run.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
import subprocess
33
import os
44

5+
# import for PATH side-effect. yuck.
6+
import chromedriver_binary # noqa
7+
58
here = os.path.dirname(__file__)
69
out = os.path.join(here, "..", "_artifacts", "test_output")
710
tests = os.path.join(here, "acceptance")

src/JupyterLibrary/core.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
from SeleniumLibrary import SeleniumLibrary
88
from SeleniumLibrary.utils.librarylistener import LibraryListener
99

10-
from .keywords import server
10+
from .keywords import screenshots, server
1111

1212

1313
RESOURCES = join(dirname(__file__), "resources")
14+
CLIENTS = ["jupyterlab"]
1415

1516

1617
class JupyterLibrary(SeleniumLibrary):
@@ -40,7 +41,9 @@ def __init__(
4041
run_on_failure="Capture Page Screenshot",
4142
screenshot_root_directory=None,
4243
)
43-
self.add_library_components([server.ServerKeywords(self)])
44+
self.add_library_components(
45+
[server.ServerKeywords(self), screenshots.ScreenshotKeywords(self)]
46+
)
4447
self.ROBOT_LIBRARY_LISTENER = JupyterLibraryListener()
4548

4649

@@ -52,6 +55,8 @@ class JupyterLibraryListener(LibraryListener):
5255

5356
def start_suite(self, name, attrs):
5457
super(JupyterLibraryListener, self).start_suite(name, attrs)
55-
for path in glob(join(RESOURCES, "jupyterlab", "*.robot")):
56-
resource = "JupyterLibrary/resources/jupyterlab/{}".format(basename(path))
57-
BuiltIn().import_resource(resource)
58+
for client in CLIENTS:
59+
for path in glob(join(RESOURCES, client, "*.robot")):
60+
BuiltIn().import_resource(
61+
"JupyterLibrary/resources/{}/{}".format(client, basename(path))
62+
)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from robot.libraries.BuiltIn import BuiltIn
2+
from SeleniumLibrary.base import LibraryComponent, keyword
3+
4+
5+
try:
6+
import cv2
7+
except ImportError:
8+
cv2 = None
9+
10+
try:
11+
from PIL import Image
12+
except ImportError:
13+
Image = None
14+
15+
16+
class ScreenshotKeywords(LibraryComponent):
17+
@keyword
18+
def capture_element_screenshot(self, locator, filename, embed=True):
19+
el = self.find_element(locator)
20+
BuiltIn().run_keyword("Capture Page Screenshot", filename)
21+
rect = {**el.location, **el.size}
22+
self.crop_image(filename, **self.round_dict(rect))
23+
24+
def round_dict(self, dict):
25+
return {
26+
k: int(round(v)) if isinstance(v, float) else v for k, v in dict.items()
27+
}
28+
29+
@keyword
30+
def crop_image(self, in_file, x, y, width, height, out_file=None):
31+
if cv2:
32+
return self.crop_with_opencv(in_file, x, y, width, height, out_file)
33+
elif Image:
34+
return self.crop_with_pillow(in_file, x, y, width, height, out_file)
35+
36+
def crop_with_opencv(self, in_file, x, y, width, height, out_file=None):
37+
out_file = out_file or in_file
38+
im = cv2.imread(in_file)
39+
im = im[int(y) : int(y + height), int(x) : int(x + width)]
40+
cv2.imwrite(out_file, im)
41+
return out_file
42+
43+
def crop_with_pillow(self, in_file, x, y, width, height, out_file=None):
44+
out_file = out_file or in_file
45+
img = Image.open(in_file)
46+
area = img.crop((int(x), int(y), int(x + width), int(y + height)))
47+
48+
with open(out_file, "wb") as output:
49+
area.save(output, "png")
50+
51+
return out_file

src/JupyterLibrary/keywords/server.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77
from tornado.escape import json_decode
88

99

10-
class NBServer(object):
11-
process = None
12-
13-
1410
class ServerKeywords(LibraryComponent):
1511
_nbserver_handles = []
1612

0 commit comments

Comments
 (0)