ElSnoMan / pyleniumio

Bring the best of Selenium and Cypress into a single Python package
https://docs.pylenium.io
MIT License
270 stars 50 forks source link

Add Element.focus() command #257

Closed ElSnoMan closed 2 years ago

ElSnoMan commented 2 years ago

Problem

Currently, if you want to focus an element in Pylenium, you either have to perform some random action on the element or use the Actions class from Selenium. Both work, but it's probably better to have a convenient method to achieve this.

# Current solutions which don't feel so great

## Random action
py.get("#button").hover()

---or---

## Actions class

element = py.get("#button")
actions = Actions(py.webdriver)
actions.move_to_element(element).perform()

Solutions

I recommend adding a new .focus() method to our Element class instead!

Solution 1: Use Selenium's Action class

# Something like this under the Element class

def focus(self) -> "Element":
    actions = Actions(self.py.webdriver)
    actions.move_to_element(self.webelement).perform()
    return self

Solution 2: Use javascript to do the focus

# Something like this under the Element class

def focus(self) -> "Element":
    javascript = "js that does the focus"
    self.py.execute_script(javascript, self.webelement)
    return self

With a solution in place, focusing on an element feels much more explicit and makes sense 🎉

def test_focus(py: Pylenium):
    py.visit("https://google.com")
    py.get("[name='q']").focus()
    ...
ElSnoMan commented 2 years ago

Released in v1.16.1