diff --git a/.idea/misc.xml b/.idea/misc.xml index 347fc3f..6973641 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,7 @@ + + \ No newline at end of file diff --git a/pages/__init__.py b/pages/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pages/cart_page.py b/pages/cart_page.py new file mode 100644 index 0000000..07e3caa --- /dev/null +++ b/pages/cart_page.py @@ -0,0 +1,10 @@ +from selenium.webdriver.common.by import By + +class CartPage: + CHECKOUT_BUTTON = "/html/body/div/div/div/div[2]/div[2]/div/div[2]/div/div/div/div[3]/div/div[2]/button" + + def __init__(self, driver): + self.driver = driver + + def checkout_button_exists(self): + return len(self.driver.find_elements(By.XPATH, self.CHECKOUT_BUTTON)) > 0 diff --git a/pages/home_page.py b/pages/home_page.py new file mode 100644 index 0000000..daaaea5 --- /dev/null +++ b/pages/home_page.py @@ -0,0 +1,25 @@ +from selenium.webdriver.common.by import By +from selenium.webdriver.common.action_chains import ActionChains +from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC + +class HomePage: + URL = "https://epicentrk.ua/" + FIRST_PRODUCT = "/html/body/div/div/div/main/div/div/div[3]/div[2]/div[5]/div/div/ul/li[1]/div/div/a" + SEARCH_BUTTON = "//button[@aria-label='Пошук']//*[name()='svg']" + + def __init__(self, driver, timeout=10): + self.driver = driver + self.wait = WebDriverWait(driver, timeout) + + def open(self): + self.driver.get(self.URL) + + def search_button_exists(self): + return len(self.driver.find_elements(By.XPATH, self.SEARCH_BUTTON)) > 0 + + def click_first_product(self): + product = self.wait.until( + EC.element_to_be_clickable((By.XPATH, self.FIRST_PRODUCT)) + ) + ActionChains(self.driver).move_to_element(product).pause(0.1).click().perform() diff --git a/pages/login_page.py b/pages/login_page.py new file mode 100644 index 0000000..d20621e --- /dev/null +++ b/pages/login_page.py @@ -0,0 +1,47 @@ +from selenium.webdriver.common.by import By +from selenium.webdriver.common.action_chains import ActionChains +from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC +from selenium.common.exceptions import TimeoutException +import time + + +class LoginPage: + LOGIN_BUTTON = "//button[@data-testid='login']" + INPUT_PHONE = "//input[@name='login']" + INPUT_PASSWORD = "//input[@type='password']" + SUBMIT_BUTTON = "//button[@data-auth-type='login']" + ERROR_MESSAGE = "//*[contains(text(),'Невірний') or contains(text(),'помилка')]" + + def __init__(self, driver, timeout=12): + self.driver = driver + self.wait = WebDriverWait(driver, timeout) + + def open_login_form(self): + el = self.wait.until(EC.element_to_be_clickable((By.XPATH, self.LOGIN_BUTTON))) + ActionChains(self.driver).move_to_element(el).click().perform() + print("✅ Відкрили форму входу") + time.sleep(2) + + def enter_phone(self, phone): + field = self.wait.until(EC.element_to_be_clickable((By.XPATH, self.INPUT_PHONE))) + field.click() + field.clear() + field.send_keys(phone) + + def enter_password(self, password): + field = self.wait.until(EC.element_to_be_clickable((By.XPATH, self.INPUT_PASSWORD))) + field.click() + field.clear() + field.send_keys(password) + + def submit_login(self): + btn = self.wait.until(EC.element_to_be_clickable((By.XPATH, self.SUBMIT_BUTTON))) + ActionChains(self.driver).move_to_element(btn).click().perform() + + def error_displayed(self): + try: + self.wait.until(EC.visibility_of_element_located((By.XPATH, self.ERROR_MESSAGE))) + return True + except TimeoutException: + return False diff --git a/pages/product_page.py b/pages/product_page.py new file mode 100644 index 0000000..29042b7 --- /dev/null +++ b/pages/product_page.py @@ -0,0 +1,18 @@ +from selenium.webdriver.common.by import By +from selenium.webdriver.common.action_chains import ActionChains +from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC + +class ProductPage: + BUY_BTN = "//button[@data-product-buy-button]" + + def __init__(self, driver, timeout=10): + self.driver = driver + self.wait = WebDriverWait(driver, timeout) + + def click_buy(self): + btn = self.wait.until(EC.element_to_be_clickable((By.XPATH, self.BUY_BTN))) + ActionChains(self.driver).move_to_element(btn).click().perform() + + def buy_button_exists(self): + return len(self.driver.find_elements(By.XPATH, self.BUY_BTN)) > 0 \ No newline at end of file diff --git a/pages/search_page.py b/pages/search_page.py new file mode 100644 index 0000000..5ae7b27 --- /dev/null +++ b/pages/search_page.py @@ -0,0 +1,23 @@ + +from selenium.webdriver.common.by import By +from selenium.webdriver.common.action_chains import ActionChains +from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC + +class HomePage: + URL = "https://epicentrk.ua/" + + FIRST_PRODUCT = "(//main//li//a[contains(@href,'/p/')])[1]" + + def __init__(self, driver, timeout=10): + self.driver = driver + self.wait = WebDriverWait(driver, timeout) + + def open(self): + self.driver.get(self.URL) + + def click_first_product(self): + product = self.wait.until( + EC.element_to_be_clickable((By.XPATH, self.FIRST_PRODUCT)) + ) + ActionChains(self.driver).move_to_element(product).click().perform() diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_buy_flow.py b/tests/test_buy_flow.py new file mode 100644 index 0000000..9d2c343 --- /dev/null +++ b/tests/test_buy_flow.py @@ -0,0 +1,35 @@ +import time +from selenium import webdriver +from selenium.webdriver.chrome.service import Service +from webdriver_manager.chrome import ChromeDriverManager + +from pages.home_page import HomePage +from pages.product_page import ProductPage +from pages.cart_page import CartPage + +driver = None +home = None +product = None +cart = None +def setup_module(): + global driver, home, product, cart + driver = webdriver.Chrome(service=Service(ChromeDriverManager().install())) + driver.maximize_window() + home = HomePage(driver) + product = ProductPage(driver) + cart = CartPage(driver) +def teardown_module(): + driver.quit() +def test_step1_open_product(): + home.open() + time.sleep(2) + home.click_first_product() + time.sleep(3) + assert "/ua/shop/" in driver.current_url and driver.current_url.endswith(".html"), \ + " Не потрапили на сторінку товару!" +def test_step2_buy_btn_exists(): + assert product.buy_button_exists(), " Кнопка 'Купити' не знайдена!" +def test_step3_buy_and_checkout_btn(): + product.click_buy() + time.sleep(5) + assert cart.checkout_button_exists(), " Кнопка 'Оформити покупку' не знайдена!" diff --git a/tests/test_buy_product.py b/tests/test_buy_product.py new file mode 100644 index 0000000..a806801 --- /dev/null +++ b/tests/test_buy_product.py @@ -0,0 +1,34 @@ +import time +from selenium import webdriver +from selenium.webdriver.chrome.service import Service +from webdriver_manager.chrome import ChromeDriverManager + +from pages.home_page import HomePage +from pages.product_page import ProductPage +from pages.cart_page import CartPage + + +def test_buy_first_product(): + driver = webdriver.Chrome(service=Service(ChromeDriverManager().install())) + driver.maximize_window() + + home = HomePage(driver) + product = ProductPage(driver) + cart = CartPage(driver) + home.open() + time.sleep(2) + + assert home.search_button_exists(), " Кнопка пошуку не знайдена!" + print(" Кнопка пошуку знайдена") + home.click_first_product() + time.sleep(3) + + assert product.buy_button_exists(), " Кнопка 'Купити' не знайдена!" + print("Кнопка 'Купити' знайдена") + + product.click_buy() + time.sleep(3) + assert cart.checkout_button_exists(), " Кнопка Оформити покупку не знайдена!" + print(" Кнопка 'Оформити покупку' знайдена ") + + driver.quit() diff --git a/tests/test_login.py b/tests/test_login.py new file mode 100644 index 0000000..a4c0b4c --- /dev/null +++ b/tests/test_login.py @@ -0,0 +1,40 @@ +import time +from selenium import webdriver +from selenium.webdriver.chrome.service import Service +from webdriver_manager.chrome import ChromeDriverManager +from selenium.webdriver.common.by import By +from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC + +from pages.home_page import HomePage +from pages.login_page import LoginPage + + +def test_login_invalid_credentials(): + driver = webdriver.Chrome(service=Service(ChromeDriverManager().install())) + driver.maximize_window() + wait = WebDriverWait(driver, 10) + + home = HomePage(driver) + login = LoginPage(driver) + home.open() + time.sleep(2) + login.open_login_form() + time.sleep(2) + login.enter_phone("+38 (097) 904-46-37") + login.enter_password(".WMWAzPp%w,/_6b") + time.sleep(1) + login.submit_login() + time.sleep(3) + driver.refresh() + time.sleep(3) + user_name_xpath = "/html/body/div/div/div/div[1]/header/div/div[1]/div[6]/div/button/span[2]" + try: + user_name = wait.until( + EC.visibility_of_element_located((By.XPATH, user_name_xpath)) + ) + print(" Ім’я:", user_name.text) + except: + raise AssertionError("❌ Не знайдено ім’я користувача після входу!") + + driver.quit() diff --git a/tests/test_open_product_page.py b/tests/test_open_product_page.py new file mode 100644 index 0000000..1d69ef9 --- /dev/null +++ b/tests/test_open_product_page.py @@ -0,0 +1,35 @@ +from selenium import webdriver +from selenium.webdriver.common.by import By +from selenium.webdriver.chrome.service import Service +from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC +from webdriver_manager.chrome import ChromeDriverManager +from selenium.webdriver.common.action_chains import ActionChains +import time + +def test_search_kley(): + driver = webdriver.Chrome(service=Service(ChromeDriverManager().install())) + driver.maximize_window() + + driver.get("https://epicentrk.ua/") + time.sleep(3) + search_input = driver.find_element(By.CSS_SELECTOR, "input[type='search']") + search_input.send_keys("клей") + time.sleep(1) + search_button = driver.find_element(By.CSS_SELECTOR, "button[aria-label='Пошук']") + driver.execute_script("arguments[0].scrollIntoView({block: 'center'});", search_button) + time.sleep(0.5) + button_svg = driver.find_element(By.XPATH, "//button[@aria-label='Пошук']//*[name()='svg']") + ActionChains(driver).move_to_element(button_svg).pause(0.1).click().perform() + print("✅ Клік по кнопці пошуку виконано!") + WebDriverWait(driver, 10).until( + EC.url_contains("/ua/shop/kley/") + ) + current_url = driver.current_url + print("🔎 Поточний URL:", current_url) + + assert "/ua/shop/kley/" in current_url, \ + f"❌ Помилка переходу: {current_url}" + + time.sleep(3) + driver.quit()