Skip to content

Commit

Permalink
xpath selection for tables
Browse files Browse the repository at this point in the history
  • Loading branch information
ggozad committed Apr 21, 2022
1 parent 3f040d5 commit 0f82608
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 26 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -534,17 +534,35 @@ as well as the directory to output data as parameters. For example,
...
| celln0 | celln1 | ... | cellnm |

- Then the table with xpath "`xpath`" should be
| header1 | header2 | ... | header(m) |
| cell00 | cell01 | ... | cell0m |
| cell10 | cell11 | ... | cell1m |
...
| celln0 | celln1 | ... | cellnm |

- Then the table with id "`id`" should contain the rows
| cell00 | cell01 | ... | cell0m |
| cell10 | cell11 | ... | cell1m |

- Then the table with xpath "`xpath`" should contain the rows
| cell00 | cell01 | ... | cell0m |
| cell10 | cell11 | ... | cell1m |

- Then the table with id "`id`" should not contain the rows
| cell00 | cell01 | ... | cell0m |
| cell10 | cell11 | ... | cell1m |

- Then the table with xpath "`xpath`" should not contain the rows
| cell00 | cell01 | ... | cell0m |
| cell10 | cell11 | ... | cell1m |

- Then row `row_no` in the table with id "`id`" should be
| cell00 | cell01 | ... | cell0m |

- Then row `row_no` in the table with xpath "`xpath`" should be
| cell00 | cell01 | ... | cell0m |

- Alerts & prompts

- When I enter "`text`" to the alert - When I accept the alert - When I dismiss the alert - Then I should see an alert - Then I should see an alert within `timeout` seconds - Then I should see an alert containing "`text`" - Then I should see an alert containing "`text`" within `timeout` seconds
Expand Down
53 changes: 28 additions & 25 deletions src/behaving/web/steps/tables.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from behave import then

from behaving.personas.persona import persona_vars
from splinter.exceptions import ElementDoesNotExist


def _process_table(table):
Expand All @@ -13,14 +14,24 @@ def _process_table(table):
return headers, cells


@then('the table with id "{id}" should be')
@persona_vars
def table_equals(context, id):
def _find_table_by_id_or_xpath(context, selector):
try:
table = context.browser.find_by_id(id).first
except IndexError:
table = context.browser.find_by_id(selector) or context.browser.find_by_xpath(
selector
)
return table.first
except (
IndexError,
ElementDoesNotExist,
):
assert False, "Table not found"


@then('the table with id "{selector}" should be')
@then('the table with xpath "{selector}" should be')
@persona_vars
def table_equals(context, selector):
table = _find_table_by_id_or_xpath(context, selector)
headers, cells = _process_table(table)
if headers:
assert headers == context.table.headings, "Table headers do not match"
Expand All @@ -30,41 +41,33 @@ def table_equals(context, id):
] == cells, "Table cells do not match"


@then('the table with id "{id}" should contain the rows')
@then('the table with id "{selector}" should contain the rows')
@then('the table with xpath "{selector}" should contain the rows')
@persona_vars
def table_contains(context, id):
try:
table = context.browser.find_by_id(id).first
except IndexError:
assert False, "Table not found"

def table_contains(context, selector):
table = _find_table_by_id_or_xpath(context, selector)
_, cells = _process_table(table)
for row in [*context.table.rows, context.table.headings]:

assert [cell for cell in row] in cells, f"{row} not found"


@then('the table with id "{id}" should not contain the rows')
@then('the table with id "{selector}" should not contain the rows')
@then('the table with xpath "{selector}" should not contain the rows')
@persona_vars
def table_does_not_contain(context, id):
try:
table = context.browser.find_by_id(id).first
except IndexError:
assert False, "Table not found"

def table_does_not_contain(context, selector):
table = _find_table_by_id_or_xpath(context, selector)
_, cells = _process_table(table)
for row in [*context.table.rows, context.table.headings]:

assert [cell for cell in row] not in cells, f"{row} found"


@then('row {row_no:d} in the table with id "{id}" should be')
@then('row {row_no:d} in the table with id "{selector}" should be')
@then('row {row_no:d} in the table with xpath "{selector}" should be')
@persona_vars
def row_equals(context, row_no, id):
try:
table = context.browser.find_by_id(id).first
except IndexError:
assert False, "Table not found"
def row_equals(context, row_no, selector):
table = _find_table_by_id_or_xpath(context, selector)

_, cells = _process_table(table)
assert [cell for cell in context.table.headings] == cells[row_no]
17 changes: 16 additions & 1 deletion tests/features/tables.feature
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,37 @@ Feature: HTML tables
| Centro comercial Moctezuma | Francisco Chang | Mexico |
| Ernst Handel | Roland Mendel | Austria |

Then the table with xpath "//table[@id='customers-thead']" should be
| Company | Contact | Country |
| Alfreds Futterkiste | Maria Anders | Germany |
| Centro comercial Moctezuma | Francisco Chang | Mexico |
| Ernst Handel | Roland Mendel | Austria |

@web
Scenario: Asserting table does/(does not) contain rows
When I visit "http://web/tables.html"
Then the table with id "customers-thead" should contain the rows
| Alfreds Futterkiste | Maria Anders | Germany |
| Ernst Handel | Roland Mendel | Austria |

And the table with xpath "//table[@id='customers-thead']" should contain the rows
| Alfreds Futterkiste | Maria Anders | Germany |
| Ernst Handel | Roland Mendel | Austria |

And the table with id "customers-no-headers" should not contain the rows
| foo | Maria Anders | Germany |
| Ernst Handel | bar | Austria |

And the table with xpath "//table[@id='customers-thead']" should not contain the rows
| foo | Maria Anders | Germany |
| Ernst Handel | bar | Austria |

@web
Scenario: Asserting equality on specific rows
When I visit "http://web/tables.html"
Then row 0 in the table with id "customers-thead" should be
| Alfreds Futterkiste | Maria Anders | Germany |
And row 1 in the table with id "customers-thead" should be
| Centro comercial Moctezuma | Francisco Chang | Mexico |

And row 2 in the table with xpath "//table[@id='customers-thead']" should be
| Ernst Handel | Roland Mendel | Austria |

0 comments on commit 0f82608

Please sign in to comment.