Robot Framework SeleniumLibrary
tableelement.py
Go to the documentation of this file.
1 # Copyright 2008-2011 Nokia Networks
2 # Copyright 2011-2016 Ryan Tomac, Ed Manlove and contributors
3 # Copyright 2016- Robot Framework Foundation
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 
17 from SeleniumLibrary.base import LibraryComponent, keyword
18 
19 
21 
22  @keyword
23 
40  def get_table_cell(self, locator, row, column, loglevel='TRACE'):
41  row = int(row)
42  column = int(column)
43  if row == 0 or column == 0:
44  raise ValueError('Both row and column must be non-zero, '
45  'got row %d and column %d.' % (row, column))
46  try:
47  cell = self._get_cell_get_cell(locator, row, column)
48  except AssertionError:
49  self.log_sourcelog_source(loglevel)
50  raise
51  return cell.text
52 
53  def _get_cell(self, locator, row, column):
54  rows = self._get_rows_get_rows(locator, row)
55  if len(rows) < abs(row):
56  raise AssertionError("Table '%s' should have had at least %d "
57  "rows but had only %d."
58  % (locator, abs(row), len(rows)))
59  index = row - 1 if row > 0 else row
60  cells = rows[index].find_elements_by_xpath('./th|./td')
61  if len(cells) < abs(column):
62  raise AssertionError("Table '%s' row %d should have had at "
63  "least %d columns but had only %d."
64  % (locator, row, abs(column), len(cells)))
65  index = column - 1 if column > 0 else column
66  return cells[index]
67 
68  def _get_rows(self, locator, count):
69  # Get rows in same order as browsers render them.
70  table = self.find_elementfind_element(locator, tag='table')
71  rows = table.find_elements_by_xpath("./thead/tr")
72  if count < 0 or len(rows) < count:
73  rows.extend(table.find_elements_by_xpath("./tbody/tr"))
74  if count < 0 or len(rows) < count:
75  rows.extend(table.find_elements_by_xpath("./tfoot/tr"))
76  return rows
77 
78  @keyword
79 
84  def table_cell_should_contain(self, locator, row, column, expected, loglevel='TRACE'):
85  content = self.get_table_cellget_table_cell(locator, row, column, loglevel)
86  if expected not in content:
87  self.ctxctx.log_source(loglevel)
88  raise AssertionError("Table '%s' cell on row %s and column %s "
89  "should have contained text '%s' but it had "
90  "'%s'."
91  % (locator, row, column, expected, content))
92  self.infoinfo("Table cell contains '%s'." % content)
93 
94  @keyword
95 
111  def table_column_should_contain(self, locator, column, expected, loglevel='TRACE'):
112  element = self._find_by_column_find_by_column(locator, column, expected)
113  if element is None:
114  self.ctxctx.log_source(loglevel)
115  raise AssertionError("Table '%s' column %s did not contain text "
116  "'%s'." % (locator, column, expected))
117 
118  @keyword
119 
130  def table_footer_should_contain(self, locator, expected, loglevel='TRACE'):
131  element = self._find_by_footer_find_by_footer(locator, expected)
132  if element is None:
133  self.ctxctx.log_source(loglevel)
134  raise AssertionError("Table '%s' footer did not contain text "
135  "'%s'." % (locator, expected))
136 
137  @keyword
138 
149  def table_header_should_contain(self, locator, expected, loglevel='TRACE'):
150  element = self._find_by_header_find_by_header(locator, expected)
151  if element is None:
152  self.ctxctx.log_source(loglevel)
153  raise AssertionError("Table '%s' header did not contain text "
154  "'%s'." % (locator, expected))
155 
156  @keyword
157 
173  def table_row_should_contain(self, locator, row, expected, loglevel='TRACE'):
174  element = self._find_by_row_find_by_row(locator, row, expected)
175  if element is None:
176  self.ctxctx.log_source(loglevel)
177  raise AssertionError("Table '%s' row %s did not contain text "
178  "'%s'." % (locator, row, expected))
179 
180  @keyword
181 
189  def table_should_contain(self, locator, expected, loglevel='TRACE'):
190  element = self._find_by_content_find_by_content(locator, expected)
191  if element is None:
192  self.ctxctx.log_source(loglevel)
193  raise AssertionError("Table '%s' did not contain text '%s'."
194  % (locator, expected))
195 
196  def _find_by_content(self, table_locator, content):
197  return self._find_find(table_locator, '//*', content)
198 
199  def _find_by_header(self, table_locator, content):
200  return self._find_find(table_locator, '//th', content)
201 
202  def _find_by_footer(self, table_locator, content):
203  return self._find_find(table_locator, '//tfoot//td', content)
204 
205  def _find_by_row(self, table_locator, row, content):
206  position = self._index_to_position_index_to_position(row)
207  locator = '//tr[{}]'.format(position)
208  return self._find_find(table_locator, locator, content)
209 
210  def _find_by_column(self, table_locator, col, content):
211  position = self._index_to_position_index_to_position(col)
212  locator = '//tr//*[self::td or self::th][{}]'.format(position)
213  return self._find_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_elementfind_element(table_locator)
227  elements = self.find_elementsfind_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
def find_element(self, locator, tag=None, required=True, parent=None)
Find element matching locator.
Definition: context.py:72
def find_elements(self, locator, tag=None, parent=None)
Find all elements matching locator.
Definition: context.py:88
def _find_by_row(self, table_locator, row, content)
def table_footer_should_contain(self, locator, expected, loglevel='TRACE')
Verifies table footer contains text expected.
def _find_by_column(self, table_locator, col, content)
def table_column_should_contain(self, locator, column, expected, loglevel='TRACE')
Verifies table column contains text expected.
def table_header_should_contain(self, locator, expected, loglevel='TRACE')
Verifies table header contains text expected.
def table_row_should_contain(self, locator, row, expected, loglevel='TRACE')
Verifies that table row contains text expected.
def get_table_cell(self, locator, row, column, loglevel='TRACE')
Returns contents of table cell.
Definition: tableelement.py:40
def _find(self, table_locator, locator, content)
def table_should_contain(self, locator, expected, loglevel='TRACE')
Verifies table contains text expected.
def table_cell_should_contain(self, locator, row, column, expected, loglevel='TRACE')
Verifies table cell contains text expected.
Definition: tableelement.py:84