Skip to content
This repository has been archived by the owner on Mar 9, 2024. It is now read-only.

Commit

Permalink
fix(input): clicks and send key methods
Browse files Browse the repository at this point in the history
Click methods weren't functioning correctly.
sendKey* were using sendKeys. 😮 

Add tests using more of the keyboard and mouse methods.
Also changed to print dictionary when dumping on failure.
In hope that it will reduce UnicodeErrors in python 2
  • Loading branch information
daveenguyen committed Mar 20, 2019
1 parent e9fbeea commit 8ed61d9
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 30 deletions.
51 changes: 26 additions & 25 deletions atomacos/mixin/_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def clickMouseButtonLeftWithMods(self, coord, modifiers, interval=None, clicks=1
"""
kb = Keyboard()
kb.pressModifiers(modifiers)
self.clickMouseButtonLeft(interval=interval, clicks=clicks)
self.clickMouseButtonLeft(coord, interval=interval, clicks=clicks)
kb.releaseModifiers(modifiers)

def clickMouseButtonRightWithMods(self, coord, modifiers, interval=None):
Expand All @@ -65,7 +65,7 @@ def clickMouseButtonRightWithMods(self, coord, modifiers, interval=None):
"""
kb = Keyboard()
kb.pressModifiers(modifiers)
self.clickMouseButtonRight(interval=interval)
self.clickMouseButtonRight(coord, interval=interval)
kb.releaseModifiers(modifiers)

def leftMouseDragged(self, stopCoord, strCoord=(0, 0), speed=1):
Expand All @@ -77,7 +77,6 @@ def leftMouseDragged(self, stopCoord, strCoord=(0, 0), speed=1):
speed is mouse moving speed, 0 to unlimited
Returns: None
"""
# Get current position as start point if strCoord not given
if strCoord == (0, 0):
strCoord = pyautogui.position()
self.dragMouseButtonLeft(coord=strCoord, dest_coord=stopCoord, interval=speed)
Expand All @@ -88,31 +87,44 @@ def doubleClickMouse(self, coord):
Parameters: coordinates to click (assume primary is left button)
Returns: None
"""
self.clickMouseButtonLeft(coord=coord, clicks=2)
pyautogui.doubleClick(*coord, button="left")

def doubleMouseButtonLeftWithMods(self, coord, modifiers):
"""Click the left mouse button with modifiers pressed.
Parameters: coordinates to click; modifiers (list)
Returns: None
"""
self.clickMouseButtonLeftWithMods(coord=coord, modifiers=modifiers, clicks=2)
kb = Keyboard()
kb.pressModifiers(modifiers)
pyautogui.doubleClick(*coord, button="left")
kb.releaseModifiers(modifiers)

def tripleClickMouse(self, coord):
"""Triple-click primary mouse button.
Parameters: coordinates to click (assume primary is left button)
Returns: None
"""
# Note above re: double-clicks applies to triple-clicks
self.clickMouseButtonLeft(coord=coord, clicks=3)
pyautogui.tripleClick(*coord, button="left")


class Keyboard(object):
def sendKey(self, keychr):
"""Send one character with no modifiers."""
pyautogui.press(keychr)

def sendKeyWithModifiers(self, keychr, modifiers):
"""Send one character with modifiers pressed
Parameters: key character, modifiers (list) (e.g. [SHIFT] or
[COMMAND, SHIFT] (assuming you've first used
from pyatom.AXKeyCodeConstants import *))
"""
self.pressModifiers(modifiers)
self.sendKey(keychr)
self.releaseModifiers(modifiers)

def sendGlobalKey(self, keychr):
"""Send one character without modifiers to the system.
Expand All @@ -123,6 +135,13 @@ def sendGlobalKey(self, keychr):
"""
self.sendKey(keychr)

def sendGlobalKeyWithModifiers(self, keychr, modifiers):
"""Global send one character with modifiers pressed.
See sendKeyWithModifiers
"""
self.sendKeyWithModifiers(keychr, modifiers)

def sendKeys(self, keystr):
"""Send a series of characters with no modifiers."""
pyautogui.typewrite(keystr)
Expand All @@ -137,24 +156,6 @@ def releaseModifiers(self, modifiers):
for modifier in modifiers:
pyautogui.keyUp(modifier_map[modifier])

def sendKeyWithModifiers(self, keychr, modifiers):
"""Send one character with modifiers pressed
Parameters: key character, modifiers (list) (e.g. [SHIFT] or
[COMMAND, SHIFT] (assuming you've first used
from pyatom.AXKeyCodeConstants import *))
"""
self.pressModifiers(modifiers)
self.sendKeys(keychr)
self.releaseModifiers(modifiers)

def sendGlobalKeyWithModifiers(self, keychr, modifiers):
"""Global send one character with modifiers pressed.
See sendKeyWithModifiers
"""
self.sendKeyWithModifiers(keychr, modifiers)


class KeyboardMouseMixin(Mouse, Keyboard):
pass
6 changes: 2 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@ def pytest_exception_interact(node, call, report):
for app in atomacos.NativeUIElement.getRunningApps():
pid = app.processIdentifier()
app_ref = atomacos.NativeUIElement.from_pid(pid)
print("%s" % app_ref)
print("\n%s" % app_ref)
for child in app_ref._generateChildren(recursive=True):
print("%s" % child)
for attribute in child.ax_attributes:
if attribute == "AXChildren":
continue
try:
print(
"%s: %s" % (attribute, child._get_ax_attribute(attribute))
)
print({attribute: child._get_ax_attribute(attribute)})
except atomacos.errors.AXError as e:
print("Issue getting %s: %s" % (attribute, e))

Expand Down
24 changes: 23 additions & 1 deletion tests/test_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,36 @@ def calculate_center(size, position):
return center


def test_click_and_enter(finder_app):
def test_types(finder_app):
fields = finder_app.textFieldsR("*search*")
field = fields[0]
center_position = calculate_center(field.AXSize, field.AXPosition)
field.clickMouseButtonLeft(center_position)
field.sendKeys("hello")
time.sleep(1)
assert field.AXValue == "hello"
field.sendKey("!")
assert field.AXValue == "hello!"
field.sendKeyWithModifiers("a", [SHIFT])
assert field.AXValue == "hello!A"
field.sendGlobalKey("@")
assert field.AXValue == "hello!A@"
field.sendGlobalKeyWithModifiers("b", [SHIFT])
assert field.AXValue == "hello!A@B"


def test_clicks(finder_app):
fields = finder_app.textFieldsR("*search*")
field = fields[0]
center_position = calculate_center(field.AXSize, field.AXPosition)
field.clickMouseButtonLeft(center_position)
field.sendKeys("hey there world")
field.doubleClickMouse(center_position)
selected_text = field.AXSelectedText
assert len(selected_text.split()) == 1
field.tripleClickMouse(center_position)
selected_text = field.AXSelectedText
assert len(selected_text.split()) > 1


def test_drag_folders(finder_app):
Expand Down

0 comments on commit 8ed61d9

Please sign in to comment.