Add checkbox page tests

This commit is contained in:
Mat Hare 2021-07-11 12:17:00 +01:00
parent 918181fbc4
commit d9424bcc0f
3 changed files with 136 additions and 0 deletions

View File

@ -0,0 +1,35 @@
Feature: Checkboxes Page
Background: Open Checkboxes page
Given I have navigated to the 'the-internet' "Checkboxes" page
Scenario: Verify Checkboxes page contents are correct
Then the page title is "Checkboxes"
And the page contains 2 checkboxes
And the checkbox labels are
| label |
| checkbox 1 |
| checkbox 2 |
And the 1st checkbox is unchecked
And the 2nd checkbox is checked
And a "Fork me on GitHub" banner is displayed in the top-right corner of the page
And the page has a footer containing "Powered by Elemental Selenium"
And the link in the page footer goes to "http://elementalselenium.com/"
Scenario Outline: Click on <ordinal> checkbox
When I click on the <ordinal> checkbox
Then the <ordinal> checkbox is <state>
Examples:
| ordinal | state |
| 1st | checked |
| 2nd | unchecked |
Scenario Outline: Click on <ordinal> checkbox twice
When I click on the <ordinal> checkbox
Then the <ordinal> checkbox is <first_state>
When I click on the <ordinal> checkbox
Then the <ordinal> checkbox is <second_state>
Examples:
| ordinal | first_state | second_state |
| 1st | checked | unchecked |
| 2nd | unchecked | checked |

32
pages/checkboxes.py Normal file
View File

@ -0,0 +1,32 @@
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from pages.base import BasePage
class CheckboxesPage(BasePage):
@property
def PAGE_TITLE(self):
return (By.TAG_NAME, 'h3')
CHECKBOXES_FORM = (By.ID, 'checkboxes')
ALL_CHECKBOXES = (By.XPATH, '//*[@id="checkboxes"]/input')
def __init__(self, browser):
self.browser = browser
def get_page_title_text(self):
return self.browser.find_element(*self.PAGE_TITLE).text
def get_num_checkboxes(self):
return len(self.browser.find_elements(*self.ALL_CHECKBOXES))
def get_checkbox_labels(self):
return self.browser.find_element(*self.CHECKBOXES_FORM).text.split("\n")
def get_checkbox_checked_state(self, index):
return self.browser.find_elements(*self.ALL_CHECKBOXES)[index].get_attribute("checked") == 'true'
def click_checkbox(self, index):
return self.browser.find_elements(*self.ALL_CHECKBOXES)[index].click()

View File

@ -0,0 +1,69 @@
from pytest_bdd import scenarios, when, then, parsers
from pages.checkboxes import CheckboxesPage as page
from sttable import parse_str_table
scenarios('../features/checkboxes_page.feature')
ORDINALS = ['st', 'nd', 'rd', 'th']
CHECK_STATES = ['checked', 'unchecked']
@when('I click on the <ordinal> checkbox')
def click_checkbox(browser, ordinal):
index = int(ordinal[0:len(ordinal)-2]) - 1
assert isinstance(index, int)
assert index >= 0
page(browser).click_checkbox(index)
@then(parsers.parse('the page title is "{title}"'))
def verify_page_title(browser, title):
assert title == page(browser).get_page_title_text()
@then(parsers.parse('the page contains {num:d} checkboxes'))
def verify_num_checkboxes(browser, num):
assert isinstance(num, int)
assert num == page(browser).get_num_checkboxes()
@then(parsers.parse('the checkbox labels are\n{labels}'))
def verify_checkbox_labels(browser, datatable, labels):
expected = parse_str_table(labels)
for field in expected.fields:
assert expected.columns[field] == page(browser).get_checkbox_labels()
@then(parsers.parse('the {index:d}{ordinal} checkbox is {state}'))
def verify_checkbox_state(browser, index, ordinal, state):
assert isinstance(index, int)
assert index > 0
assert ordinal in ORDINALS
assert state in CHECK_STATES
checked_state = page(browser).get_checkbox_checked_state(index - 1)
if state == 'checked':
assert checked_state == True
else:
assert checked_state == False
@then('the <ordinal> checkbox is <state>')
def verify_checkbox_state(browser, ordinal, state):
# assert isinstance(index, int)
# assert index > 0
# assert ordinal in ORDINALS
index = int(ordinal[0:len(ordinal)-2]) -1
assert state in CHECK_STATES
checked_state = page(browser).get_checkbox_checked_state(index)
if state == 'checked':
assert checked_state == True
else:
assert checked_state == False
@then('the <ordinal> checkbox is <first_state>')
def verify_checkbox_first_state(browser, ordinal, first_state):
verify_checkbox_state(browser, ordinal, first_state)
@then('the <ordinal> checkbox is <second_state>')
def verify_checkbox_second_state(browser, ordinal, second_state):
verify_checkbox_state(browser, ordinal, second_state)