-
Notifications
You must be signed in to change notification settings - Fork 509
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add keyboard class and Element.press()
- Loading branch information
Showing
12 changed files
with
225 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
from typing import Union | ||
|
||
from selenium.webdriver.common.action_chains import ActionChains | ||
from selenium.webdriver.common.keys import Keys | ||
from selenium.webdriver.remote.webelement import WebElement | ||
|
||
|
||
class Keyboard: | ||
"""""" | ||
|
||
def __init__(self, driver, element: Union[WebElement, None] = None) -> None: | ||
self.driver = driver | ||
|
||
self.element = element | ||
|
||
def _resolve_key_down_action(self, action_chain: ActionChains, key: str) -> ActionChains: | ||
# If in selenium keys, use it, if not, assume literal. | ||
key_value = getattr(Keys, key, None) | ||
|
||
if key_value: | ||
chain = action_chain.key_down(key_value, self.element) | ||
elif self.element: | ||
action_chain.send_keys_to_element(self.element, key) | ||
else: | ||
chain = action_chain.send_keys(key) | ||
|
||
return chain | ||
|
||
def _resolve_key_up_action(self, action_chain: ActionChains, key: str) -> ActionChains: | ||
key_value = getattr(Keys, key, None) | ||
|
||
chain = action_chain | ||
if key_value: | ||
chain = action_chain.key_up(key_value, self.element) | ||
|
||
return chain | ||
|
||
def down(self, key: str) -> "Keyboard": | ||
"""""" | ||
chain = ActionChains(self.driver) | ||
chain = self._resolve_key_down_action(chain, key) | ||
chain.perform() | ||
return self | ||
|
||
def up(self, key: str) -> "Keyboard": | ||
"""""" | ||
chain = ActionChains(self.driver) | ||
chain = self._resolve_key_up_action(chain, key) | ||
chain.perform() | ||
return self | ||
|
||
def press(self, key: str, delay: int = 0) -> "Keyboard": | ||
"""""" | ||
key_pattern = key.split("+") | ||
|
||
chain = ActionChains(self.driver) | ||
|
||
for item in key_pattern: | ||
chain = self._resolve_key_down_action(chain, item) | ||
|
||
if delay: | ||
chain = chain.pause(delay) | ||
|
||
for item in key_pattern: | ||
chain = self._resolve_key_up_action(chain, item) | ||
|
||
chain.perform() | ||
|
||
return self |
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,40 @@ | ||
from splinter.driver.webdriver import Keyboard | ||
|
||
|
||
class KeyboardTest: | ||
def test_keyboard_down(self): | ||
keyboard = Keyboard(self.browser.driver) | ||
|
||
keyboard.down("CONTROL") | ||
|
||
elem = self.browser.find_by_css("#keypress_detect") | ||
|
||
assert elem.first | ||
|
||
def test_keyboard_up(self): | ||
keyboard = Keyboard(self.browser.driver) | ||
|
||
keyboard.down("CONTROL") | ||
keyboard.up("CONTROL") | ||
|
||
elem = self.browser.find_by_css("#keyup_detect") | ||
|
||
assert elem.first | ||
|
||
def test_keyboard_press(self): | ||
keyboard = Keyboard(self.browser.driver) | ||
|
||
keyboard.press("CONTROL") | ||
|
||
elem = self.browser.find_by_css("#keyup_detect") | ||
|
||
assert elem.first | ||
|
||
def test_element_press_combo(self): | ||
keyboard = Keyboard(self.browser.driver) | ||
|
||
keyboard.press("CONTROL+a") | ||
|
||
elem = self.browser.find_by_css("#keypress_detect_a") | ||
|
||
assert elem.first |
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,31 @@ | ||
import pytest | ||
|
||
|
||
def skip_if_zope(f): | ||
def wrapper(self, *args, **kwargs): | ||
if self.__class__.__name__ == "TestZopeTestBrowserDriver": | ||
return pytest.skip("skipping this test for zope testbrowser") | ||
else: | ||
f(self, *args, **kwargs) | ||
|
||
return wrapper | ||
|
||
|
||
def skip_if_django(f): | ||
def wrapper(self, *args, **kwargs): | ||
if self.__class__.__name__ == "TestDjangoClientDriver": | ||
return pytest.skip("skipping this test for django") | ||
else: | ||
f(self, *args, **kwargs) | ||
|
||
return wrapper | ||
|
||
|
||
def skip_if_flask(f): | ||
def wrapper(self, *args, **kwargs): | ||
if self.__class__.__name__ == "TestFlaskClientDriver": | ||
return pytest.skip("skipping this test for flask") | ||
else: | ||
f(self, *args, **kwargs) | ||
|
||
return wrapper |
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