Skip to content

Commit 666ce69

Browse files
pekkaklarckaaltat
authored andcommitted
Remove TableElementFinder. (#993)
* Remove TableElementFinder. This class was pretty useless after latest cleanups and having it as an attribute in library itself didn't make much sense. It was also only used by TableElementKeywords, which somewhat inconsistently handled finding table cells itself. This commit moves TableElementFinder functionality into TableElementKeywords.
1 parent 021ded5 commit 666ce69

File tree

6 files changed

+52
-78
lines changed

6 files changed

+52
-78
lines changed

src/SeleniumLibrary/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
TableElementKeywords,
3535
WaitingKeywords,
3636
WindowKeywords)
37-
from SeleniumLibrary.locators import ElementFinder, TableElementFinder
37+
from SeleniumLibrary.locators import ElementFinder
3838
from SeleniumLibrary.utils import (Deprecated, LibraryListener, timestr_to_secs,
3939
WebDriverCache)
4040

@@ -347,7 +347,6 @@ def __init__(self, timeout=5.0, implicit_wait=0.0,
347347
DynamicCore.__init__(self, libraries)
348348
self.ROBOT_LIBRARY_LISTENER = LibraryListener()
349349
self._element_finder = ElementFinder(self)
350-
self._table_element_finder = TableElementFinder(self)
351350

352351
_speed_in_secs = Deprecated('_speed_in_secs', 'speed')
353352
_timeout_in_secs = Deprecated('_timeout_in_secs', 'timeout')

src/SeleniumLibrary/base/context.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ def drivers(self):
3939
def element_finder(self):
4040
return self.ctx._element_finder
4141

42-
@property
43-
def table_element_finder(self):
44-
return self.ctx._table_element_finder
45-
4642
def find_element(self, locator, tag=None, required=True, parent=None):
4743
"""Find element matching `locator`.
4844

src/SeleniumLibrary/keywords/tableelement.py

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def table_column_should_contain(self, locator, column, expected, loglevel='INFO'
109109
See `Page Should Contain Element` for explanation about the
110110
``loglevel`` argument.
111111
"""
112-
element = self.table_element_finder.find_by_column(locator, column, expected)
112+
element = self._find_by_column(locator, column, expected)
113113
if element is None:
114114
self.ctx.log_source(loglevel)
115115
raise AssertionError("Table '%s' column %s did not contain text "
@@ -128,7 +128,7 @@ def table_footer_should_contain(self, locator, expected, loglevel='INFO'):
128128
See `Page Should Contain Element` for explanation about the
129129
``loglevel`` argument.
130130
"""
131-
element = self.table_element_finder.find_by_footer(locator, expected)
131+
element = self._find_by_footer(locator, expected)
132132
if element is None:
133133
self.ctx.log_source(loglevel)
134134
raise AssertionError("Table '%s' footer did not contain text "
@@ -147,7 +147,7 @@ def table_header_should_contain(self, locator, expected, loglevel='INFO'):
147147
See `Page Should Contain Element` for explanation about the
148148
``loglevel`` argument.
149149
"""
150-
element = self.table_element_finder.find_by_header(locator, expected)
150+
element = self._find_by_header(locator, expected)
151151
if element is None:
152152
self.ctx.log_source(loglevel)
153153
raise AssertionError("Table '%s' header did not contain text "
@@ -171,7 +171,7 @@ def table_row_should_contain(self, locator, row, expected, loglevel='INFO'):
171171
See `Page Should Contain Element` for explanation about the
172172
``loglevel`` argument.
173173
"""
174-
element = self.table_element_finder.find_by_row(locator, row, expected)
174+
element = self._find_by_row(locator, row, expected)
175175
if element is None:
176176
self.ctx.log_source(loglevel)
177177
raise AssertionError("Table '%s' row %s did not contain text "
@@ -187,8 +187,47 @@ def table_should_contain(self, locator, expected, loglevel='INFO'):
187187
See `Page Should Contain Element` for explanation about the
188188
``loglevel`` argument.
189189
"""
190-
element = self.table_element_finder.find_by_content(locator, expected)
190+
element = self._find_by_content(locator, expected)
191191
if element is None:
192192
self.ctx.log_source(loglevel)
193193
raise AssertionError("Table '%s' did not contain text '%s'."
194194
% (locator, expected))
195+
196+
def _find_by_content(self, table_locator, content):
197+
return self._find(table_locator, '//*', content)
198+
199+
def _find_by_header(self, table_locator, content):
200+
return self._find(table_locator, '//th', content)
201+
202+
def _find_by_footer(self, table_locator, content):
203+
return self._find(table_locator, '//tfoot//td', content)
204+
205+
def _find_by_row(self, table_locator, row, content):
206+
position = self._index_to_position(row)
207+
locator = '//tr[{}]'.format(position)
208+
return self._find(table_locator, locator, content)
209+
210+
def _find_by_column(self, table_locator, col, content):
211+
position = self._index_to_position(col)
212+
locator = '//tr//*[self::td or self::th][{}]'.format(position)
213+
return self._find(table_locator, locator, content)
214+
215+
def _index_to_position(self, index):
216+
index = int(index)
217+
if index == 0:
218+
raise ValueError('Row and column indexes must be non-zero.')
219+
if index > 0:
220+
return str(index)
221+
if index == -1:
222+
return 'position()=last()'
223+
return 'position()=last()-{}'.format(abs(index) - 1)
224+
225+
def _find(self, table_locator, locator, content):
226+
table = self.find_element(table_locator)
227+
elements = self.find_elements(locator, parent=table)
228+
for element in elements:
229+
if content is None:
230+
return element
231+
if element.text and content in element.text:
232+
return element
233+
return None

src/SeleniumLibrary/locators/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,4 @@
1616

1717
from .customlocator import CustomLocator
1818
from .elementfinder import ElementFinder
19-
from .tableelementfinder import TableElementFinder
2019
from .windowmanager import WindowManager

src/SeleniumLibrary/locators/tableelementfinder.py

Lines changed: 0 additions & 59 deletions
This file was deleted.

test/unit/locators/test_tableelementfinder.py renamed to test/unit/keywords/test_tablekeywords.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,35 @@
22

33
from mockito import mock, when, unstub
44

5-
from SeleniumLibrary.locators.tableelementfinder import TableElementFinder
5+
from SeleniumLibrary.keywords import TableElementKeywords
66

77

8-
class ElementFinderTest(unittest.TestCase):
8+
class TableKeywordsTest(unittest.TestCase):
99

1010
def setUp(self):
1111
self.ctx = mock()
1212
self.ctx._element_finder = mock()
13-
self.finder = TableElementFinder(self.ctx)
13+
self.finder = TableElementKeywords(self.ctx)
1414

1515
def tearDown(self):
1616
unstub()
1717

1818
def test_find_by_column(self):
1919
xpath = '//tr//*[self::td or self::th][1]'
2020
when(self.finder)._find('id:table', xpath, 'content').thenReturn(mock())
21-
self.finder.find_by_column('id:table', 1, 'content')
21+
self.finder._find_by_column('id:table', 1, 'content')
2222

2323
def test_find_by_column_with_negative_index(self):
2424
xpath = '//tr//*[self::td or self::th][position()=last()]'
2525
when(self.finder)._find('id:table', xpath, 'content').thenReturn(mock())
26-
self.finder.find_by_column('id:table', -1, 'content')
26+
self.finder._find_by_column('id:table', -1, 'content')
2727

2828
def test_find_by_row(self):
2929
xpath = '//tr[2]'
3030
when(self.finder)._find('xpath=//table', xpath, 'content').thenReturn(mock())
31-
self.finder.find_by_row('xpath=//table', 2, 'content')
31+
self.finder._find_by_row('xpath=//table', 2, 'content')
3232

3333
def test_find_by_row_with_negative_index(self):
3434
xpath = '//tr[position()=last()-2]'
3535
when(self.finder)._find('xpath=//table', xpath, 'content').thenReturn(mock())
36-
self.finder.find_by_row('xpath=//table', -3, 'content')
36+
self.finder._find_by_row('xpath=//table', -3, 'content')

0 commit comments

Comments
 (0)