From d9298bbea72fdf8352a79f4cdb0a34a6b1d6fa27 Mon Sep 17 00:00:00 2001 From: Claudio Klingler Date: Wed, 6 Sep 2023 12:21:48 +0200 Subject: [PATCH] Rename tag to role. --- pyproject.toml | 2 +- src/yaxp/__init__.py | 1 - src/yaxp/_xpath.py | 32 ++++++++++++++++++++------------ tests/test_xpath.py | 5 +++-- 4 files changed, 24 insertions(+), 16 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index ddd4583..264bb12 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "yaxp" -version = "0.1.0" +version = "0.2.0" description="yet another xpath generator" authors = [ { name="Claudio Klingler", email="ck@realtime-projects.com"} diff --git a/src/yaxp/__init__.py b/src/yaxp/__init__.py index 44b36a6..214f072 100644 --- a/src/yaxp/__init__.py +++ b/src/yaxp/__init__.py @@ -1,4 +1,3 @@ from ._xpath import XPG xpath = XPG() -xpath._xpath = "" diff --git a/src/yaxp/_xpath.py b/src/yaxp/_xpath.py index 43fe01c..0dc113d 100644 --- a/src/yaxp/_xpath.py +++ b/src/yaxp/_xpath.py @@ -1,13 +1,16 @@ +import copy + + class XPG: """ Base class for generating xpathes. Usually, you don't use the constructor directly, but the xpath instance of the root element instead, like in the example in the class description. However, since this will call this - constructor, all parameters (except tag) can be passed like in the example of the class + constructor, all parameters (except role) can be passed like in the example of the class description. - :param tag: Tag for the xpath, e.g. `span`, `h1`, etc. - :param direct: If set to `True`, the tag must be a direct descendant of the parent element + :param role: role for the xpath, e.g. `span`, `h1`, etc. + :param direct: If set to `True`, the role must be a direct descendant of the parent element otherwise it can be nested in any sub-element as well. :param parent: If set, this is the xpath of the parent element to search from, otherwise the search starts at the root element of the document. **parent** can either be @@ -18,15 +21,18 @@ class XPG: like `class` as attributes using: `Xpath.h5(_class="#title")` """ - def __init__(self, tag=None, parent=None): - self._parent = parent - self._tag = tag + def __init__(self, role=None, parent=None): + self._parent = str(parent) if parent else "" + self._role = role self._filter = [] self._direct = False def by(self, **kwargs): - self._add_filter(**kwargs) - return self + _xp = XPG(self._role, self._parent) + _xp._filter = self._filter + _xp._direct = self._direct + _xp._add_filter(**kwargs) + return _xp def __call__(self, **kwargs): self._add_filter(**kwargs) @@ -39,7 +45,10 @@ def _add_filter(self, **kwargs): self._direct = values continue if arg == "parent": - self._parent = values + self._parent = str(values) + continue + if arg == "role": + self._role = values continue wildcard = False if arg == "_text": @@ -67,7 +76,7 @@ def _add_filter(self, **kwargs): @staticmethod def _by_xpath(xpath): - return XPG(tag=xpath) + return XPG(role=xpath) @property def xpath(self): @@ -77,13 +86,12 @@ def get_xpath(self): """ :return: The generated xpath as string """ _xp = str(self._parent) if self._parent else "" pre = "/" if self._direct else "//" - _xp += pre + self._tag if self._tag else "" + _xp += pre + self._role if self._role else "" for _f in self._filter: _xp += _f return _xp def __repr__(self): - print(self.get_xpath()) return self.get_xpath() def has(self, *args): diff --git a/tests/test_xpath.py b/tests/test_xpath.py index 9402516..2ce168a 100644 --- a/tests/test_xpath.py +++ b/tests/test_xpath.py @@ -5,7 +5,7 @@ testdata = [ # basic xpath (xp.h1, '//h1'), - (xp.div.h1, '//div//h1'), + (xp.div.h1, '//div//h1'), # full class specification (xp.h2.by(_id="huhu"), '//h2[@id="huhu"]'), @@ -48,6 +48,7 @@ # xpath as parent (xp.span(_id="myid", parent=xp.div()), '//div//span[@id="myid"]'), + (xp.by(role="h1", parent=xp.div), '//div//h1'), # multiple chaining (xp.span(_id="myid", parent=xp.div(parent=xp.body())), '//body//div//span[@id="myid"]'), @@ -68,7 +69,7 @@ # short chaining (xp.div.h1(_class="myclass"), '//div//h1[@class="myclass"]'), - # tags containing "." + # roles containing "." (xp.Android_Container(_id="huhu"), '//Android.Container[@id="huhu"]'), # tags containing "_"