Skip to content

Commit

Permalink
Add getparent method to LxmlElem class
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanprjcts committed Aug 8, 2017
1 parent 42fe0d6 commit ea1bc9c
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 2 deletions.
5 changes: 5 additions & 0 deletions docs/about/release-notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ To upgrade Sdklib to the latest version, use pip:
Sdklib 1.9.x series
===================

Sdklib 1.9.1
------------

- Add getparent method to LxmlElem class.

Sdklib 1.9.0
------------

Expand Down
2 changes: 1 addition & 1 deletion sdklib/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.9.0'
__version__ = '1.9.1'
9 changes: 9 additions & 0 deletions sdklib/html/elem.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ class ElemLxml(HTMLLxmlMixin, AbstractBaseHTMLElem):
def __init__(self, lxml_elem):
self.html_obj = lxml_elem

def getparent(self, height=1):
parent = self.html_obj.getparent()
for _ in range(0, height):
if parent is None:
return
parent = parent.getparent()
if parent is not None:
return ElemLxml(parent)


class Elem5lib(HTML5libMixin, AbstractBaseHTMLElem):
"""
Expand Down
2 changes: 1 addition & 1 deletion tests/resources/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -995,7 +995,7 @@ <h1 class="logo" id="logo"><a href="index.html" title="ElevenPaths, radical and
</li>
<li class="events-menu mega-menu-item mega-menu-fullwidth dropdown-submenu"><a href="index.html#">Events</a>
<ul class="dropdown-menu">
<li><a href="news-events/events/index.html">Next events</a></li>
<li><a id="child-elem" href="news-events/events/index.html">Next events</a></li>
<li><a href="news-events/past-events/index.html">Past events</a></li>
<li><a href="news-events/security-day/index.html">Security Day</a></li>
<li><a href="news-events/security-innovation-day/index.html">Security Innovation Day</a></li>
Expand Down
13 changes: 13 additions & 0 deletions tests/test_html_elem.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,16 @@ def test_get_attribute_html5lib(self):

elem = html.find_element_by_id('primary-nav')
self.assertEqual("class", elem.get_attribute("nav-main mega-menu menu_float_left"))

def test_get_parent(self):
elem = self.html.find_element_by_id('child-elem')
self.assertEqual("dropdown-menu", elem.getparent().get("class"))

def test_get_parent_with_height(self):
elem = self.html.find_element_by_id('child-elem')
self.assertEqual("events-menu mega-menu-item mega-menu-fullwidth dropdown-submenu",
elem.getparent(height=2).get("class"))

def test_get_non_existing_parent(self):
elem = self.html.find_element_by_id('child-elem')
self.assertIsNone(elem.getparent(height=20))

0 comments on commit ea1bc9c

Please sign in to comment.