-
Notifications
You must be signed in to change notification settings - Fork 0
lab6_liubun_selenium #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: test
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -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" | ||||||
|
||||||
| FIRST_PRODUCT = "/html/body/div/div/div/main/div/div/div[3]/div[2]/div[5]/div/div/ul/li[1]/div/div/a" | |
| FIRST_PRODUCT = '(//main//li//a[contains(@href,"/p/")])[1]' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not store CSS/XPath selectors separately.
Create IWebElement as class properties instead.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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(),'помилка')]" | ||
|
Comment on lines
+10
to
+14
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not store CSS/XPath selectors separately. |
||
|
|
||
| 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) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not use Thread.Sleep. |
||
|
|
||
| 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 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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]" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not store CSS/XPath selectors separately. |
||
|
|
||
| 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 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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]" | ||
|
Comment on lines
+8
to
+10
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not store CSS/XPath selectors separately. |
||
|
|
||
| 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() | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -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) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+25
to
+27
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+10
to
+32
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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(): | |
| import pytest | |
| @pytest.fixture(scope="module") | |
| def driver(): | |
| driver = webdriver.Chrome(service=Service(ChromeDriverManager().install())) | |
| driver.maximize_window() | |
| yield driver | |
| driver.quit() | |
| @pytest.fixture(scope="module") | |
| def home(driver): | |
| return HomePage(driver) | |
| @pytest.fixture(scope="module") | |
| def product(driver): | |
| return ProductPage(driver) | |
| @pytest.fixture(scope="module") | |
| def cart(driver): | |
| return CartPage(driver) | |
| def test_step1_open_product(home, driver): | |
| 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(product): | |
| assert product.buy_button_exists(), " Кнопка 'Купити' не знайдена!" | |
| def test_step3_buy_and_checkout_btn(product, cart): |
Copilot
AI
Oct 28, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hardcoded time.sleep() calls make tests slow and unreliable. Replace with explicit waits for specific conditions.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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() | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -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) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+21
to
+30
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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) | |
| # Wait for the login button to be visible on the home page | |
| wait.until(EC.visibility_of_element_located((By.XPATH, "//button[contains(@class, 'login')]"))) | |
| login.open_login_form() | |
| # Wait for the phone input field to be visible in the login form | |
| wait.until(EC.visibility_of_element_located((By.XPATH, "//input[@name='phone']"))) | |
| login.enter_phone("+38 (097) 904-46-37") | |
| login.enter_password(".WMWAzPp%w,/_6b") | |
| # Wait for the password field to be visible before submitting | |
| wait.until(EC.visibility_of_element_located((By.XPATH, "//input[@name='password']"))) | |
| login.submit_login() | |
| # Wait for either an error message or the user name to appear after login attempt | |
| wait.until( | |
| EC.any_of( | |
| EC.visibility_of_element_located((By.XPATH, "/html/body/div/div/div/div[1]/header/div/div[1]/div[6]/div/button/span[2]")), | |
| EC.visibility_of_element_located((By.XPATH, "//div[contains(@class, 'error-message')]")) | |
| ) | |
| ) | |
| driver.refresh() | |
| # Wait for the user name element to be visible after refresh | |
| wait.until(EC.visibility_of_element_located((By.XPATH, "/html/body/div/div/div/div[1]/header/div/div[1]/div[6]/div/button/span[2]"))) |
Copilot
AI
Oct 28, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bare except clause catches all exceptions including system exits. Specify the exception type, such as 'except TimeoutException:' to handle only the expected failure case.
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -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) | ||||||||||||
|
Comment on lines
+15
to
+21
|
||||||||||||
| 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) | ||||||||||||
|
||||||||||||
| time.sleep(3) | |
| # Wait for the search results container to be visible before quitting | |
| WebDriverWait(driver, 10).until( | |
| EC.visibility_of_element_located((By.CSS_SELECTOR, "div.catalog-products")) | |
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Absolute XPath is extremely brittle and will break with any DOM structure change. Use a more robust selector based on element attributes, CSS classes, or data attributes.