forked from umihico/pythonista-chromeless
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.py
More file actions
72 lines (56 loc) · 2.02 KB
/
Copy pathexample.py
File metadata and controls
72 lines (56 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from chromeless import Chromeless
import os
demo_url, supposed_title = "https://example.com/", "example domain"
def example(self, url):
self.url = url
title = self.second_method()
png = self.get_screenshot_as_png()
divcnt = len(self.find_elements_by_xpath("//div"))
return title, png, divcnt
def second_method(self):
self.get(self.url)
return self.title
def test_example():
chrome = Chromeless()
chrome.attach(example)
chrome.attach(second_method)
title, png, divcnt = chrome.example(demo_url)
assert_response(title, png, divcnt)
def test_api():
chrome = Chromeless(os.getenv('API_URL', None), os.getenv('API_KEY', None))
chrome.attach(example)
chrome.attach(second_method)
title, png, divcnt = chrome.example(demo_url)
print(title, demo_url)
assert_response(title, png, divcnt)
def assert_response(title, png, divcnt):
assert supposed_title in title.lower()
with open('./img.png', 'wb') as f:
f.write(png)
from PIL import Image
width, height = Image.open('./img.png').size
assert width > 0 and height > 0
assert divcnt > 0
def test_error(chrome=None):
def method(self, url):
self.get(url)
self.find_element_by_xpath("//invalid")
chrome = Chromeless() if chrome is None else chrome
chrome.attach(method)
try:
chrome.method(demo_url)
except Exception:
import traceback
detail = traceback.format_exc()
REQUIRED_SERVER_VERSION = chrome.REQUIRED_SERVER_VERSION if hasattr(
chrome, "REQUIRED_SERVER_VERSION") else None
if REQUIRED_SERVER_VERSION == 1 or REQUIRED_SERVER_VERSION is None:
assert "return pickle.loads(zlib.decompress(base64.b64decode(obj.encode())))" in detail
else:
assert "CHROMELESS TRACEBACK IN LAMBDA START" in detail
assert "NoSuchElementException" in detail
assert "CHROMELESS TRACEBACK IN LAMBDA END" in detail
else:
assert False
if __name__ == '__main__':
test_example()