Skip to content

Commit 478b210

Browse files
committed
tests: test case 006 implemented - applying low to high filter
1 parent 751db27 commit 478b210

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

tests/pages/InventoryPage.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from random import choice
22
from selenium.webdriver.common.by import By
33
from selenium.webdriver.remote.webelement import WebElement
4+
from selenium.webdriver.support.select import Select
5+
from selenium.webdriver.support.wait import WebDriverWait
46

57
from tests.pages.BasePageObject import BasePageObject
68

@@ -15,6 +17,8 @@ class InventoryPage(BasePageObject):
1517
loc_inventory_item_price_class = "inventory_item_price"
1618
loc_inventory_item_add_button_selector = "button.btn_primary"
1719
loc_inventory_item_remove_button_selector = "button.btn_secondary"
20+
loc_inventory_filter_selector = '[data-test="product_sort_container"]'
21+
loc_inventory_lohi_filter_selector = 'option[value="lohi"]'
1822

1923

2024
def has_access_to_inventory(self) -> bool:
@@ -40,11 +44,48 @@ def choose_an_item_randomsly(self):
4044

4145
return (selected_item, selected_item_name, selected_item_price)
4246

47+
4348
def add_item_to_cart(self, product_item: WebElement):
4449
"""Click on item's Add to Cart button"""
4550
add_button = product_item.find_element(By.CSS_SELECTOR, self.loc_inventory_item_add_button_selector)
4651
add_button.click()
4752

53+
4854
def get_item_button_text(self, product_item: WebElement) -> str:
4955
"""Returns the Text value of the item's button"""
50-
return product_item.find_element(By.TAG_NAME, "button").text
56+
return product_item.find_element(By.TAG_NAME, "button").text
57+
58+
59+
def select_a_filter_by_type(self, filter_type: str):
60+
"""Use the <select> to filter the inventory items"""
61+
62+
wait = WebDriverWait(self.driver, timeout = self.DEFAULT_TIMEOUT)
63+
wait.until(
64+
lambda e : self.driver.find_element(
65+
By.CSS_SELECTOR, self.loc_inventory_filter_selector
66+
).is_displayed()
67+
)
68+
69+
select_element = self.driver.find_element(By.CSS_SELECTOR, self.loc_inventory_filter_selector)
70+
filter_select = Select(select_element)
71+
filter_select.select_by_visible_text(filter_type)
72+
73+
74+
def is_the_low_to_high_price_filter_selected(self):
75+
return self.driver.find_element(By.CSS_SELECTOR, self.loc_inventory_lohi_filter_selector).is_displayed()
76+
77+
78+
def _convert_price_text_to_float(self, price_as_text: str) -> float:
79+
return float(price_as_text.split("$")[1])
80+
81+
82+
def verify_items_are_ordered_by_price_low_to_high(self) -> bool:
83+
"""Verify if inventory items are ordered by price Low to High"""
84+
items_list = self._get_list_of_products()
85+
prices = [self._convert_price_text_to_float(item.find_element(By.CLASS_NAME, self.loc_inventory_item_price_class).text) for item in items_list]
86+
87+
expected_ordered_prices = sorted(prices)
88+
print("[ debug ] - actual prices: {}".format(prices))
89+
print("[ debug ] - expected prices: {}".format(expected_ordered_prices))
90+
return prices == expected_ordered_prices
91+

tests/test_ct006.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
import pytest
3+
4+
from tests.pages.InventoryPage import InventoryPage
5+
6+
@pytest.mark.parametrize(
7+
(""), [
8+
pytest.param(id="firefox", marks=pytest.mark.FORCE_BROWSER("firefox")),
9+
pytest.param(id="chrome", marks=pytest.mark.FORCE_BROWSER("chrome")),
10+
pytest.param(id="safari", marks=pytest.mark.FORCE_BROWSER("safari"))
11+
]
12+
)
13+
def test_apply_low_to_high_filter_on_inventory(start_already_logged):
14+
15+
inventory_page: InventoryPage = start_already_logged
16+
inventory_page.select_a_filter_by_type("Price (low to high)")
17+
18+
assert inventory_page.is_the_low_to_high_price_filter_selected(), "Filter Price (High to low) should be selected"
19+
assert inventory_page.verify_items_are_ordered_by_price_low_to_high(), "Prices are NOT ordered from Higher to Lower"
20+

0 commit comments

Comments
 (0)