diff --git a/features/dropdown_page.feature b/features/dropdown_page.feature new file mode 100644 index 0000000..cb4f914 --- /dev/null +++ b/features/dropdown_page.feature @@ -0,0 +1,32 @@ +Feature: Dropdown Page + + Background: Open Dropdown page + Given I have navigated to the 'the-internet' "Dropdown" page + + Scenario: Verify Dropdown page contents are correct + Then the page title is "Dropdown List" + And the page contains 1 dropdown list control + And the dropdown defaults to "Please select an option" + And the dropdown contains the following options + | options | + | Please select an option | + | Option 1 | + | Option 2 | + And the dropdown only support selection of a single option at a time + 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: Select dropdown option by text + When I select "" from the dropdown list + Then the dropdown value is "" + Examples: + | option_text | + | Option 1 | + | Option 2 | + +Scenario: Select dropdown options by index + When I select the 2nd option from the dropdown list + Then the dropdown value is "Option 2" + When I select the 1st option from the dropdown list + Then the dropdown value is "Option 1" \ No newline at end of file diff --git a/pages/dropdown.py b/pages/dropdown.py new file mode 100644 index 0000000..3f969e5 --- /dev/null +++ b/pages/dropdown.py @@ -0,0 +1,43 @@ +from selenium.webdriver.common.by import By +from pages.base import BasePage +from selenium.webdriver.support.select import Select + +class DropdownPage(BasePage): + + @property + def PAGE_TITLE(self): + return (By.TAG_NAME, 'h3') + DROPDOWN_LIST = (By.TAG_NAME, 'select') + # 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_dropdowns(self): + return len(self.browser.find_elements(*self.DROPDOWN_LIST)) + + def get_current_dropdown_value(self): + dropdown = Select(self.browser.find_element(*self.DROPDOWN_LIST)) + return dropdown.first_selected_option.text + + def get_dropdown_options(self): + dropdown = Select(self.browser.find_element(*self.DROPDOWN_LIST)) + options = [option.text for option in dropdown.options] + return options + + def get_dropdown_supports_multi_select(self): + dropdown = Select(self.browser.find_element(*self.DROPDOWN_LIST)) + return dropdown.is_multiple != None + + def select_option_by_text(self, text): + dropdown = Select(self.browser.find_element(*self.DROPDOWN_LIST)) + dropdown.select_by_visible_text(text) + + def select_option_by_index(self, index): + dropdown = Select(self.browser.find_element(*self.DROPDOWN_LIST)) + dropdown.select_by_index(index) + \ No newline at end of file diff --git a/step_defs/test_dropdown_page_steps.py b/step_defs/test_dropdown_page_steps.py new file mode 100644 index 0000000..3f70c66 --- /dev/null +++ b/step_defs/test_dropdown_page_steps.py @@ -0,0 +1,51 @@ +from pytest_bdd import scenarios, when, then, parsers +from pages.dropdown import DropdownPage +from sttable import parse_str_table + +scenarios('../features/dropdown_page.feature') + + +@when('I select "" from the dropdown list') +def select_dropdown_option_by_text(browser, option_text): + DropdownPage(browser).select_option_by_text(option_text) + + +@when(parsers.parse('I select the {ordinal} option from the dropdown list')) +def select_dropdown_option_by_index(browser, ordinal): + index = int(ordinal[0:len(ordinal)-2]) + DropdownPage(browser).select_option_by_index(index) + + +@then(parsers.parse('the page title is "{title}"')) +def verify_page_title(browser, title): + assert title == DropdownPage(browser).get_page_title_text() + + +@then(parsers.parse('the page contains {num:d} dropdown list control')) +def verify_num_dropdowns(browser, num): + assert isinstance(num, int) + assert num == DropdownPage(browser).get_num_dropdowns() + + +@then(parsers.parse('the dropdown defaults to "{text}"')) +def verify_default_dropdown_option(browser, text): + assert text == DropdownPage(browser).get_current_dropdown_value() + + +@then(parsers.parse('the dropdown contains the following options\n{options}')) +def verify_dropdown_options(browser, datatable, options): + expected = parse_str_table(options) + for field in expected.fields: + assert expected.columns[field] == DropdownPage( + browser).get_dropdown_options() + + +@then('the dropdown only support selection of a single option at a time') +def verify_only_single_selection(browser): + assert False == DropdownPage(browser).get_dropdown_supports_multi_select() + + +@then('the dropdown value is ""') +@then(parsers.parse('the dropdown value is "{option_text}"')) +def verify_selected_option(browser, option_text): + assert option_text == DropdownPage(browser).get_current_dropdown_value()