Building Targets is tedious... so lets automate that.... #87
Replies: 2 comments 1 reply
-
You'll notice I use a custom from __future__ import annotations
import re
from selenium.webdriver.common.by import By as _By
T = tuple[str, str]
class By(_By):
@staticmethod
def cls(s: str) -> T:
return By.css(f".{s}")
@staticmethod
def css(s: str) -> T:
return (By.CSS_SELECTOR, s)
@staticmethod
def css_attr(s: str, attrib: str) -> T:
return By.css(f'[{attrib}="{s}"]')
@staticmethod
def dti(s: str) -> T:
return By.css_attr(s, "data-testid")
@staticmethod
def id(s: str) -> T: # noqa: A003
return (By.ID, s)
@staticmethod
def name(s: str) -> T:
return By.css_attr(s, "name")
@staticmethod
def tag(s: str) -> T:
return (By.TAG_NAME, s)
@staticmethod
def xpath(s: str) -> T:
return (By.XPATH, s)
def escape_css_selector(s: str) -> str:
return re.sub(r"([!\"#$%&'()*+,\-./:;<=>?@\[\\\]^`{|}~])", r"\\\g<1>", s) |
Beta Was this translation helpful? Give feedback.
-
I love that you turned it into a Performable so an Actor could do it, rather than just a straight script. Personally, i tend to view the sum total of Targets in my project as something that should change as the scripts change—only things that are actually being used by a test should be in there. But i recognize that's not everyone's opinion, and definitely having a script like this would make updating your Targets much much easier. It's almost similar to automated API schema generation or something, it's neat! If your dev team is consistent with their IDs, maybe you could have a pre-test chore where your Actor goes to each page and updates the Target files for the tests to use... i wonder if that would work. |
Beta Was this translation helpful? Give feedback.
-
I created a custom action to automatically generate the targets on a page or even within a section of a page.
Usage:
Sample output:
(yeah, google.com has some goofy ids)
Beta Was this translation helpful? Give feedback.
All reactions