Skip to content

Commit 92295b9

Browse files
committed
tests: test case 005 implemented - successfully buy a product
1 parent ca71a0e commit 92295b9

5 files changed

Lines changed: 181 additions & 0 deletions

File tree

tests/pages/CartPage.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,19 @@ class CartPage(BasePageObject):
1414
loc_cart_item_quantity_class = "cart_quantity"
1515
loc_cart_item_name_class = "inventory_item_name"
1616
loc_cart_item_price_class = "inventory_item_price"
17+
loc_cart_checkout_button_selector = '[data-test="checkout"]'
18+
1719

1820
def is_cart_page_url(self) -> bool:
1921
return self.driver.current_url == self.URL
2022

23+
2124
def get_cart_items(self) -> list:
25+
"""
26+
Retrieve a list of Products and its information from the Cart page
27+
28+
@return list[ (element: WebElement, element_name: str, element_price: str, element_quantity: str) ]
29+
"""
2230
cart_items = self.driver.find_elements(By.CSS_SELECTOR, self.loc_cart_list_selector)
2331
cart_item_list = []
2432

@@ -30,6 +38,7 @@ def get_cart_items(self) -> list:
3038

3139
return cart_item_list
3240

41+
3342
def verify_product_is_in_cart(self, product_name: str, product_price: str) -> bool:
3443
cart_list = self.get_cart_items()
3544

@@ -39,4 +48,8 @@ def verify_product_is_in_cart(self, product_name: str, product_price: str) -> bo
3948
if added_product_name == product_name.lower() and added_product_price == product_price.lower(): return True
4049

4150
return False
51+
52+
53+
def click_on_checkout(self):
54+
self.driver.find_element(By.CSS_SELECTOR, self.loc_cart_checkout_button_selector).click()
4255

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
from selenium.webdriver.common.by import By
3+
4+
from tests.pages.BasePageObject import BasePageObject
5+
6+
7+
class CheckoutCompletePage(BasePageObject):
8+
9+
URL = "https://www.saucedemo.com/checkout-complete.html"
10+
11+
loc_page_title_selector = ".header_secondary_container span.title"
12+
loc_appreciation_message_class = "complete-header"
13+
loc_back_home_button_selector = '[data-test="back-to-products"]'
14+
15+
16+
def verify_page_title_is(self, expected_value: str = ""):
17+
page_title = self.driver.find_element(By.CSS_SELECTOR, self.loc_page_title_selector).text
18+
return page_title.lower() == expected_value.lower()
19+
20+
21+
def verify_appreciation_message_is(self, expected_message: str):
22+
displayed_message = self.driver.find_element(By.CLASS_NAME, self.loc_appreciation_message_class).text
23+
return displayed_message.lower() == expected_message.lower()
24+
25+
def click_back_home(self):
26+
self.driver.find_element(By.CSS_SELECTOR, self.loc_back_home_button_selector).click()
27+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
from selenium.webdriver.common.by import By
3+
4+
from tests.pages.BasePageObject import BasePageObject
5+
6+
7+
class CheckoutInformationPage(BasePageObject):
8+
9+
URL = "https://www.saucedemo.com/checkout-step-one.html"
10+
11+
loc_page_title_selector = ".header_secondary_container span.title"
12+
loc_first_name_input_selector = '[data-test="firstName"]'
13+
loc_last_name_input_selector = '[data-test="lastName"]'
14+
loc_zipcode_input_selector = '[data-test="postalCode"]'
15+
loc_continue_button_selector = '[data-test="continue"]'
16+
17+
18+
def is_checkout_information_page_url(self) -> bool:
19+
return self.driver.current_url == self.URL
20+
21+
22+
def verify_page_title_is(self, expected_value: str = ""):
23+
page_title = self.driver.find_element(By.CSS_SELECTOR, self.loc_page_title_selector).text
24+
return page_title.lower() == expected_value.lower()
25+
26+
27+
def type_first_name(self, value="Not given"):
28+
self.driver.find_element(By.CSS_SELECTOR, self.loc_first_name_input_selector).send_keys(value)
29+
30+
31+
def type_last_name(self, value="Not given"):
32+
self.driver.find_element(By.CSS_SELECTOR, self.loc_last_name_input_selector).send_keys(value)
33+
34+
35+
def type_zipcode(self, value="Not given"):
36+
self.driver.find_element(By.CSS_SELECTOR, self.loc_zipcode_input_selector).send_keys(value)
37+
38+
39+
def type_valid_client_information(self):
40+
self.type_first_name("Etta")
41+
self.type_last_name("James")
42+
self.type_zipcode("123-321")
43+
44+
45+
def click_to_continue_with_checkout(self):
46+
self.driver.find_element(By.CSS_SELECTOR, self.loc_continue_button_selector).click()
47+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
2+
from selenium.webdriver.common.by import By
3+
4+
from tests.pages.BasePageObject import BasePageObject
5+
6+
7+
class CheckoutOverviewPage(BasePageObject):
8+
9+
URL = "https://www.saucedemo.com/checkout-step-two.html"
10+
11+
loc_page_title_selector = ".header_secondary_container span.title"
12+
loc_checkout_list_selector = "#checkout_summary_container .cart_list"
13+
loc_checkout_item_class = "cart_item"
14+
loc_checkout_item_quantity_class = "cart_quantity"
15+
loc_checkout_item_name_class = "inventory_item_name"
16+
loc_checkout_item_price_class = "inventory_item_price"
17+
loc_finish_checkout_button_selector = '[data-test="finish"]'
18+
19+
20+
def verify_page_title_is(self, expected_value: str = ""):
21+
page_title = self.driver.find_element(By.CSS_SELECTOR, self.loc_page_title_selector).text
22+
return page_title.lower() == expected_value.lower()
23+
24+
25+
def get_checkout_items(self) -> list:
26+
"""
27+
Retrieve a list of Products and its information from the Checkout Overview page
28+
29+
@return list[ (element: WebElement, element_name: str, element_price: str, element_quantity: str) ]
30+
"""
31+
checkout_items = self.driver.find_elements(By.CSS_SELECTOR, self.loc_checkout_list_selector)
32+
checkout_item_list = []
33+
34+
for item in checkout_items:
35+
item_name = item.find_element(By.CLASS_NAME, self.loc_checkout_item_name_class).text
36+
item_price = item.find_element(By.CLASS_NAME, self.loc_checkout_item_price_class).text
37+
item_quantity = item.find_element(By.CLASS_NAME, self.loc_checkout_item_quantity_class).text
38+
checkout_item_list.append((item, item_name, item_price, item_quantity))
39+
40+
return checkout_item_list
41+
42+
43+
def verify_if_item_is_part_of_checkout_list(self, item) -> bool:
44+
"""Compare a given item with the ones present on the Checkout Summary items list and return if its part or not the checkout list"""
45+
46+
is_item_part_of_checkout_list = False
47+
_, expected_name, expected_price, expected_quantity = item
48+
49+
checkout_list = self.get_checkout_items()
50+
for checkout_item in checkout_list:
51+
_, checkout_item_name, checkout_item_price, checkout_item_quantity = checkout_item
52+
if expected_name == checkout_item_name and expected_price == checkout_item_price and expected_quantity == checkout_item_quantity:
53+
is_item_part_of_checkout_list = True
54+
print("[ debug ] Checkout Item comparison - Name: {} - Price: {} - Quantity: {}"
55+
.format(expected_name, expected_price, expected_quantity))
56+
57+
return is_item_part_of_checkout_list
58+
59+
60+
61+
def click_finish_checkout(self):
62+
self.driver.find_element(By.CSS_SELECTOR, self.loc_finish_checkout_button_selector).click()
63+

tests/test_ct005.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
import pytest
3+
4+
from tests.pages.CartPage import CartPage
5+
from tests.pages.CheckoutCompletePage import CheckoutCompletePage
6+
from tests.pages.CheckoutInformationPage import CheckoutInformationPage
7+
from tests.pages.CheckoutOverviewPage import CheckoutOverviewPage
8+
9+
@pytest.mark.FORCE_BROWSER("firefox")
10+
def test_successfully_buy_a_product(start_with_one_product_added):
11+
cart_page = CartPage(driver = start_with_one_product_added.driver)
12+
cart_products_list = cart_page.get_cart_items()
13+
cart_page.click_on_checkout()
14+
15+
checkout_info_page = CheckoutInformationPage(driver = cart_page.driver)
16+
assert checkout_info_page.is_checkout_information_page_url()
17+
assert checkout_info_page.verify_page_title_is("Checkout: Your Information"), "Checkout Information should be the page title"
18+
checkout_info_page.type_valid_client_information()
19+
checkout_info_page.click_to_continue_with_checkout()
20+
21+
checkout_overview_page = CheckoutOverviewPage(driver = checkout_info_page.driver)
22+
assert checkout_overview_page.verify_page_title_is("Checkout: Overview"), "Checkout Overview should be the page title"
23+
for item in cart_products_list:
24+
assert checkout_overview_page.verify_if_item_is_part_of_checkout_list(item)
25+
26+
27+
checkout_overview_page.click_finish_checkout()
28+
29+
checkout_complete_page = CheckoutCompletePage(driver = checkout_overview_page.driver)
30+
checkout_complete_page.verify_page_title_is("Checkout: Complete!"), "Checkout Complete! should be the page title"
31+
checkout_complete_page.verify_appreciation_message_is("Thank you for your order!"), "Should have an appreciation message"

0 commit comments

Comments
 (0)