-
Notifications
You must be signed in to change notification settings - Fork 508
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
8 changed files
with
164 additions
and
21 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,47 @@ | ||
from selenium.webdriver.common.action_chains import ActionChains | ||
from selenium.webdriver.common.keys import Keys | ||
|
||
|
||
class Keyboard: | ||
def __init__(self, driver) -> None: | ||
self.driver = driver | ||
|
||
def down(self, key: str) -> None: | ||
key_value = getattr(Keys, key) | ||
|
||
chain = ActionChains(self.driver) | ||
chain.key_down(key_value) | ||
chain.perform() | ||
|
||
def up(self, key: str) -> None: | ||
key_value = getattr(Keys, key) | ||
|
||
chain = ActionChains(self.driver) | ||
chain.key_up(key_value) | ||
chain.perform() | ||
|
||
def press(self, key: str, delay: int = 0) -> None: | ||
key_pattern = key.split("+") | ||
|
||
chain = ActionChains(self.driver) | ||
|
||
for item in key_pattern: | ||
# If in selenium keys, use it, if not, assume literal. | ||
key_value = getattr(Keys, item) | ||
|
||
if key_value: | ||
chain = chain.key_down(key_value) # , self._element) | ||
else: | ||
chain = chain.send_keys(key) # , self._element) | ||
|
||
if delay: | ||
chain = chain.pause(delay) | ||
|
||
for item in key_pattern: | ||
# If in selenium keys, use it, if not, assume literal. | ||
key_value = getattr(Keys, item) | ||
|
||
if key_value: | ||
chain = chain.key_up(key_value) # , self._element) | ||
|
||
chain.perform() |
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,13 @@ | ||
from splinter.driver.webdriver import Keyboard | ||
|
||
|
||
class KeyboardTest: | ||
def test_keyboard_down(self): | ||
keyboard = Keyboard(self.browser) | ||
|
||
keyboard.down("SHIFT") | ||
|
||
elem = self.browser.find_by_css("[name='q']") | ||
elem.fill("hello") | ||
|
||
assert elem.text == "HELLO" |
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 |