-
Notifications
You must be signed in to change notification settings - Fork 0
/
take_screenshot.py
63 lines (53 loc) · 2.5 KB
/
take_screenshot.py
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
import os
import shutil
import imagecompare
'''module for taking a screenshot and comparing it'''
def take_screenshot(browser, filename):
'''Takes a screenshot of the requested webpage
:param browser: an instance of a selenium webdriver
:param filename: the filename to save as
:return: true if image is the same, false if its different
'''
# if it doesn't exist make the outputs directory
if os.path.exists("outputs") is not True:
os.mkdir("outputs")
if os.path.exists("outputs/screenshots") is not True:
os.mkdir("outputs/screenshots")
browser.save_screenshot("outputs/screenshots/" + filename)
is_different = False
# compare with the previous screenshot (if it exists)
if os.path.isfile("outputs/old_screenshots/" + filename) is True:
print("old image exists, comparing ", "outputs/screenshots/" +
filename, "with outputs/old_screenshots/" + filename)
is_different = imagecompare.compare_image("outputs/screenshots/"
+ filename,
"outputs/old_screenshots/"
+ filename,
"outputs/comparison-"
+ filename,
"outputs/mask-" + filename)
if is_different is True:
print(filename, "has changed")
status_file = open("outputs/" + filename + ".status", "w")
status_file.write("changed")
status_file.close()
else:
print(filename, "hasn't changed")
# delete the mask and comparison image if they exist
status_file = open("outputs/" + filename + ".status", "w")
status_file.write("unchanged")
status_file.close()
# copy the image for the report
print("copying outputs/old_screenshots/" + filename,
"to outputs/report-" + filename)
shutil.copyfile("outputs/old_screenshots/" + filename,
"outputs/report-" + filename)
else:
print("old file doesn't exist")
# if it doesn't exist make a directory for screenshots
if os.path.exists("outputs/old_screenshots") is not True:
os.mkdir("outputs/old_screenshots")
# copy the image
shutil.copyfile("outputs/screenshots/"+filename,
"outputs/old_screenshots/"+filename)
return is_different