Skip to content

Commit

Permalink
Rename tag to role.
Browse files Browse the repository at this point in the history
  • Loading branch information
realtimeprojects committed Sep 6, 2023
1 parent 98a9fa8 commit d9298bb
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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="[email protected]"}
Expand Down
1 change: 0 additions & 1 deletion src/yaxp/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from ._xpath import XPG

xpath = XPG()
xpath._xpath = ""
32 changes: 20 additions & 12 deletions src/yaxp/_xpath.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -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":
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down
5 changes: 3 additions & 2 deletions tests/test_xpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]'),
Expand Down Expand Up @@ -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"]'),
Expand All @@ -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 "_"
Expand Down

0 comments on commit d9298bb

Please sign in to comment.