-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from HDE/phantomjs
Added a lot of new APIs
- Loading branch information
Showing
13 changed files
with
757 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import abc | ||
from enum import Enum | ||
from typing import List, Dict, Any, Union | ||
|
||
import attr | ||
|
||
from arsenic.connection import WEB_ELEMENT | ||
from arsenic.session import Element | ||
|
||
|
||
TOrigin = Union[Element, 'Origins'] | ||
|
||
|
||
class Origins(Enum): | ||
viewport = 'viewport' | ||
pointer = 'pointer' | ||
|
||
|
||
class Button(Enum): | ||
left = 0 | ||
middle = 1 | ||
right = 2 | ||
|
||
|
||
class Device(metaclass=abc.ABCMeta): | ||
id: str = abc.abstractproperty() | ||
type: str = abc.abstractproperty() | ||
parameters: Dict[str, str] = abc.abstractproperty() | ||
|
||
def __init__(self): | ||
self.actions: List[Action] = [] | ||
|
||
@abc.abstractmethod | ||
def encode_actions(self) -> List[Dict[str, Any]]: | ||
raise NotImplementedError() | ||
|
||
|
||
@attr.s | ||
class Action: | ||
type = attr.ib() | ||
duration = attr.ib() | ||
data = attr.ib() | ||
|
||
def encode(self): | ||
return { | ||
'type': self.type, | ||
'duration': self.duration, | ||
**self.data | ||
} | ||
|
||
|
||
def encode_origin(origin): | ||
if isinstance(origin, Element): | ||
return {WEB_ELEMENT: origin.id} | ||
elif isinstance(origin, Origins): | ||
return origin.value | ||
else: | ||
raise TypeError() | ||
|
||
|
||
class Mouse(Device): | ||
id = 'mouse' | ||
type = 'pointer' | ||
parameters = { | ||
'pointerType': 'mouse' | ||
} | ||
|
||
def move(self, origin: TOrigin, x: int, y: int, duration: int=250): | ||
self.actions.append(Action( | ||
type='pointerMove', | ||
duration=duration, | ||
data={ | ||
'origin': encode_origin(origin), | ||
'x': x, | ||
'y': y | ||
} | ||
)) | ||
|
||
def button_down(self, button: Button=Button.left): | ||
self.actions.append(Action( | ||
type='pointerDown', | ||
duration=0, | ||
data={ | ||
'button': button.value | ||
} | ||
)) | ||
|
||
def button_up(self, button: Button=Button.left): | ||
self.actions.append(Action( | ||
type='pointerUp', | ||
duration=0, | ||
data={ | ||
'button': button.value | ||
} | ||
)) | ||
|
||
def encode_actions(self): | ||
return [action.encode() for action in self.actions] | ||
|
||
|
||
class Actions: | ||
def __init__(self): | ||
self.mouse = Mouse() | ||
self.devices: List[Device] = [self.mouse] | ||
|
||
def move_to(self, element: Element) -> 'Actions': | ||
self.mouse.move(element, 0, 0) | ||
return self | ||
|
||
def move_by(self, x: int, y: int) -> 'Actions': | ||
self.mouse.move(Origins.pointer, x, y) | ||
return self | ||
|
||
def mouse_down(self) -> 'Actions': | ||
self.mouse.button_down() | ||
return self | ||
|
||
def mouse_up(self) -> 'Actions': | ||
self.mouse.button_up() | ||
return self | ||
|
||
def encode(self): | ||
return { | ||
'actions': [ | ||
{ | ||
'id': device.id, | ||
'type': device.type, | ||
'parameters': device.parameters, | ||
'actions': device.encode_actions(), | ||
} for device in self.devices | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
NULL = '\ue000' | ||
CANCEL = '\ue001' | ||
HELP = '\ue002' | ||
BACKSPACE = '\ue003' | ||
TAB = '\ue004' | ||
CLEAR = '\ue005' | ||
RETURN = '\ue006' | ||
ENTER = '\ue007' | ||
SHIFT = '\ue008' | ||
CONTROL = '\ue009' | ||
ALT = '\ue00a' | ||
PAUSE = '\ue00b' | ||
ESCAPE = '\ue00c' | ||
SPACE = '\ue00d' | ||
PAGE_UP = '\ue00e' | ||
PAGE_DOWN = '\ue00f' | ||
END = '\ue010' | ||
HOME = '\ue011' | ||
LEFT = '\ue012' | ||
UP = '\ue013' | ||
RIGHT = '\ue014' | ||
DOWN = '\ue015' | ||
INSERT = '\ue016' | ||
DELETE = '\ue017' | ||
SEMICOLON = '\ue018' | ||
EQUALS = '\ue019' | ||
|
||
NUMPAD0 = '\ue01a' | ||
NUMPAD1 = '\ue01b' | ||
NUMPAD2 = '\ue01c' | ||
NUMPAD3 = '\ue01d' | ||
NUMPAD4 = '\ue01e' | ||
NUMPAD5 = '\ue01f' | ||
NUMPAD6 = '\ue020' | ||
NUMPAD7 = '\ue021' | ||
NUMPAD8 = '\ue022' | ||
NUMPAD9 = '\ue023' | ||
MULTIPLY = '\ue024' | ||
ADD = '\ue025' | ||
SEPARATOR = '\ue026' | ||
SUBTRACT = '\ue027' | ||
DECIMAL = '\ue028' | ||
DIVIDE = '\ue029' | ||
|
||
F1 = '\ue031' | ||
F2 = '\ue032' | ||
F3 = '\ue033' | ||
F4 = '\ue034' | ||
F5 = '\ue035' | ||
F6 = '\ue036' | ||
F7 = '\ue037' | ||
F8 = '\ue038' | ||
F9 = '\ue039' | ||
F10 = '\ue03a' | ||
F11 = '\ue03b' | ||
F12 = '\ue03c' | ||
|
||
META = '\ue03d' | ||
COMMAND = '\ue03d' |
Oops, something went wrong.