diff --git a/features/checkboxes_page.feature b/features/checkboxes_page.feature new file mode 100644 index 0000000..f32b65e --- /dev/null +++ b/features/checkboxes_page.feature @@ -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 checkbox + When I click on the checkbox + Then the checkbox is + Examples: + | ordinal | state | + | 1st | checked | + | 2nd | unchecked | + +Scenario Outline: Click on checkbox twice + When I click on the checkbox + Then the checkbox is + When I click on the checkbox + Then the checkbox is + Examples: + | ordinal | first_state | second_state | + | 1st | checked | unchecked | + | 2nd | unchecked | checked | \ No newline at end of file diff --git a/pages/checkboxes.py b/pages/checkboxes.py new file mode 100644 index 0000000..64618a9 --- /dev/null +++ b/pages/checkboxes.py @@ -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() + + diff --git a/step_defs/test_checkboxes_page_steps.py b/step_defs/test_checkboxes_page_steps.py new file mode 100644 index 0000000..c164713 --- /dev/null +++ b/step_defs/test_checkboxes_page_steps.py @@ -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 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 checkbox is ') +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 checkbox is ') +def verify_checkbox_first_state(browser, ordinal, first_state): + verify_checkbox_state(browser, ordinal, first_state) + +@then('the checkbox is ') +def verify_checkbox_second_state(browser, ordinal, second_state): + verify_checkbox_state(browser, ordinal, second_state) \ No newline at end of file