11from random import choice
22from selenium .webdriver .common .by import By
33from selenium .webdriver .remote .webelement import WebElement
4+ from selenium .webdriver .support .select import Select
5+ from selenium .webdriver .support .wait import WebDriverWait
46
57from 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+
0 commit comments