diff --git a/CHANGELOG.md b/CHANGELOG.md index 472af46d..47411f89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -116,6 +116,22 @@ chrome_options.add_argument('--headless') browser.config.driver_options = chrome_options ``` +### command.select_all to simulate ctrl+a or cmd+a on mac + +```python +from selene import browser, by, have, command + +browser.open('https://www.ecosia.org/') + +browser.element(by.name('q')).type('selene').should(have.value('selene')) + +browser.element(by.name('q')).perform(command.select_all).type('github yashaka selene') +browser.element(by.name('q')).should(have.value('github yashaka selene')) + +``` + +Probably might be useful for cases where normal `element.set_value(text)`, while based on `webelement.clear(); webelement.send_keys(text)`, - does not work, in most cases because of some events handled on `clear()`. + ## 2.0.0rc1.post1 (to be released on ??.??.2023) - allow guessing local driver name based on config.driver_options diff --git a/selene/core/command.py b/selene/core/command.py index 697f95c5..8ef9fc5d 100644 --- a/selene/core/command.py +++ b/selene/core/command.py @@ -19,8 +19,12 @@ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. +import sys from typing import Union, Optional +import typing +from selenium.webdriver import Keys + from selene.core.entity import Element, Collection, Browser from selene.core.wait import Command @@ -55,6 +59,16 @@ def save_page_source(path: Optional[str] = None) -> Command[Browser]: return command +select_all: Command[Element] = Command( + 'select all by ctrl+a or cmd+a for mac', + lambda element: typing.cast(Element, element) + .locate() + .send_keys( + (Keys.COMMAND if sys.platform == 'darwin' else Keys.CONTROL) + 'a' + Keys.NULL, + ), +) + + class js: # pylint: disable=invalid-name @staticmethod def set_value(value: Union[str, int]) -> Command[Element]: diff --git a/selene/core/entity.py b/selene/core/entity.py index 2733b249..2e6dc9e4 100644 --- a/selene/core/entity.py +++ b/selene/core/entity.py @@ -467,7 +467,8 @@ def fn(element: Element): def send_keys(self, *value) -> Element: """ To be used for more low level operations like «uploading files», etc. - To simulate normal input of keys by user when typing – use Element.type(self, text). + To simulate normal input of keys by user when typing + - use Element.type(self, text). """ self.wait.command('send keys', lambda element: element().send_keys(*value)) return self diff --git a/tests/integration/command__select_all_test.py b/tests/integration/command__select_all_test.py new file mode 100644 index 00000000..2f1b22a5 --- /dev/null +++ b/tests/integration/command__select_all_test.py @@ -0,0 +1,21 @@ +from selene import have, command +import time + +from selene.core.exceptions import TimeoutException + +from tests.integration.helpers.givenpage import GivenPage + + +def test_select_all_allows_to_overwrite_text_via_type(session_browser): + browser = session_browser.with_(timeout=1) + browser.config.hold_driver_at_exit = True + page = GivenPage(browser.driver) + page.opened_with_body( + ''' + + ''' + ) + + browser.element('#text-field').perform(command.select_all).type('new text') + + browser.element('#text-field').should(have.value('new text'))