/
selenium_canvas.py
145 lines (123 loc) · 6.22 KB
/
selenium_canvas.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import math
from selenium.common.exceptions import NoSuchElementException
from js_classes import JSCanvas, JSContext
from vector_2d import Vector2D
class SeleniumCanvas:
def __init__(self, driver):
self.driver = driver
# self.canvases = []
self.contexts = {}
def create_canvas_context(self, canvas, context):
script = "window.{0} = document.createElement('canvas');" \
"{0}.id = {0};" \
"{0}.width = {1};" \
"{0}.height = {2};" \
"{0}.style.width = {3};" \
"{0}.style.height = {4};" \
"{0}.style.position = {5};" \
"{0}.style.left = {6};" \
"{0}.style.top = {7};" \
"{0}.style.zIndex = {8};" \
"{0}.style.pointerEvents = {9};" \
"document.body.appendChild({0});" \
"window.{10} = {0}.getContext('2d');" \
"{10}.id = {10};" \
"{10}.globalAlpha = {11};" \
"{10}.font = {12};".format(canvas.name,
canvas.width,
canvas.height,
canvas.style_width,
canvas.style_height,
canvas.style_position,
canvas.style_left,
canvas.style_top,
canvas.style_zIndex,
canvas.style_pointerEvents,
context.name,
context.globalAlpha,
context.font)
self.driver.execute_script(script)
def add_canvas_context(self, canvas_name, context_name):
new_canvas = JSCanvas(canvas_name)
new_context = JSContext(context_name, new_canvas)
self.create_canvas_context(new_canvas, new_context)
# self.canvases.append(new_canvas)
# self.contexts.append(new_context)
self.contexts[context_name] = new_context
def check_missing_contexts(self):
missing = []
for context in self.contexts:
try:
self.driver.find_element_by_id(context.name)
except NoSuchElementException:
missing.append(context)
for context in missing:
self.create_canvas_context(context.canvas, context)
def clear_context(self, context_name):
context = self.contexts[context_name]
script = "{0}.clearRect(0, 0, {1}.width, {1}.height);".format(context.name, context.canvas.name)
self.driver.execute_script(script)
def set_styles(self, context_name, visibility=None, stroke_style=None, fill_style=None, global_alpha=None, font=None):
context = self.contexts[context_name]
canvas_name = context.canvas.name
script = ""
if visibility is not None:
script += "{0}.style.visibility = {1};".format(canvas_name, visibility)
if stroke_style is not None:
script += "{0}.strokeStyle = {1};".format(canvas_name, stroke_style)
if fill_style is not None:
script += "{0}.fillStyle = {1};".format(context.name, fill_style)
if global_alpha is not None:
script += "{0}.globalAlpha = {1};".format(context.name, global_alpha)
if font is not None:
script += "{0}.font = {1};".format(context.name, font)
self.driver.execute_script(script)
def draw_text(self, context_name, text, pos):
context = self.contexts[context_name]
script = "{0}.fillText({1}, {2}, {3});".format(context.name, text, pos.x, pos.y)
self.driver.execute_script(script)
def draw_centered_text(self, context_name, text, pos, stroke_style="'black'", fill_style=None):
context = self.contexts[context_name]
script = ""
script += "{0}.textAlign = 'center';".format(context.name)
if stroke_style is not None:
self.set_styles(context.name, stroke_style=stroke_style, global_alpha='1.0')
script += "{0}.strokeText('{1}', {2}, {3});".format(context.name, text, pos.x, pos.y)
if fill_style is not None:
self.set_styles(context.name, fill_style=fill_style)
script += "{0}.fillText('{1}', {2}, {3});".format(context.name, text, pos.x, pos.y)
self.driver.execute_script(script)
def draw_filled_rect(self, context_name, pos, dim):
context = self.contexts[context_name]
script = "{0}.fillRect({1}, {2}, {3}, {4});".format(context.name, pos.x, pos.y, dim.x, dim.y)
self.driver.execute_script(script)
def draw_arrow(self, context_name, from_pos, to_pos):
context = self.contexts[context_name]
head_length = 20
line_width = 5
angle = math.atan2(to_pos.y - from_pos.y, to_pos.x - from_pos.x)
off_from = Vector2D(to_pos.x - head_length * math.cos(angle - math.pi / 7),
to_pos.y - head_length * math.sin(angle - math.pi / 7))
off_to = Vector2D(to_pos.x - head_length * math.cos(angle + math.pi / 7),
to_pos.y - head_length * math.sin(angle + math.pi / 7))
script = "{0}.beginPath();" \
"{0}.lineWidth = {1};" \
"{0}.moveTo({2}, {3});" \
"{0}.lineTo({4}, {5});" \
"{0}.moveTo({4}, {5});" \
"{0}.lineTo({6}, {7});" \
"{0}.lineTo({8}, {9});" \
"{0}.lineTo({4}, {5});" \
"{0}.lineTo({6}, {7});" \
"{0}.stroke();" \
"{0}.fill();".format(context.name,
line_width,
from_pos.x,
from_pos.y,
to_pos.x,
to_pos.y,
off_from.x,
off_from.y,
off_to.x,
off_to.y)
self.driver.execute_script(script)