Skip to content

Commit

Permalink
NEW: command.select_all
Browse files Browse the repository at this point in the history
– to simulate ctrl+a or cmd+a on mac
  • Loading branch information
yashaka committed Apr 13, 2023
1 parent 421b868 commit 933a893
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions selene/core/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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]:
Expand Down
3 changes: 2 additions & 1 deletion selene/core/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions tests/integration/command__select_all_test.py
Original file line number Diff line number Diff line change
@@ -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(
'''
<input id="text-field" value="old text"></input>
'''
)

browser.element('#text-field').perform(command.select_all).type('new text')

browser.element('#text-field').should(have.value('new text'))

0 comments on commit 933a893

Please sign in to comment.