Структура файлов
tests/
├── conftest.py
└── test_solutions.py
conftest.py — фикстура
# conftest.py
import pytest
from selenium import webdriver
@pytest.fixture
def driver():
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://suninjuly.github.io/cats.html")
yield driver
driver.quit()
Блок 1: find_element() и .text
Решение 1.1 — заголовок страницы
# test_solutions.py
from selenium.webdriver.common.by import By
def test_header_text(driver):
# TAG_NAME "h1" находит первый заголовок h1 на странице
header = driver.find_element(By.TAG_NAME, "h1")
assert header.text == "Cat memes"
# Логика: h1 уникален на странице, TAG_NAME прост и читаем
Решение 1.2 — время первой карточки
# test_solutions.py
def test_time_of_first_cat_card(driver):
# :nth-child(1) выбирает первую .col-sm-4 (первую карточку)
# small — тег с временем внутри карточки
time_first_cat = driver.find_element(
By.CSS_SELECTOR, ".col-sm-4:nth-child(1) small"
)
assert time_first_cat.text == "9 mins"
# Логика: CSS-комбинация карточка + конкретный тег — точно и читаемо
Решение 1.3 — название последней карточки
# test_solutions.py
def test_last_cat_card_name(driver):
# :nth-child(6) — шестая карточка (последняя в сетке)
# p — тег с названием кота
last_card_name = driver.find_element(
By.CSS_SELECTOR, ".col-sm-4:nth-child(6) p"
)
assert last_card_name.text == "I love you so much"
Решение 1.4 — текст "Cats album"
# test_solutions.py
def test_cats_album_text(driver):
# strong — единственный тег strong на странице (у иконки фото)
cats_album = driver.find_element(By.TAG_NAME, "strong")
assert cats_album.text == "Cats album"
Блок 2: is_displayed()
Решение 2.1 — первая карточка отображается
# test_solutions.py
def test_first_cat_card_is_displayed(driver):
# Находим первую карточку, проверяем видимость
first_cat_card = driver.find_element(
By.CSS_SELECTOR, ".col-sm-4:nth-child(1)"
)
assert first_cat_card.is_displayed()
# Важно: is_displayed() вернёт False если display:none,
# visibility:hidden или opacity:0
Решение 2.2 — иконка с фото отображается
# test_solutions.py
def test_photo_icon_is_displayed(driver):
# SVG-иконка фотоаппарата находится по тегу svg
photo_icon = driver.find_element(By.TAG_NAME, "svg")
assert photo_icon.is_displayed()
Блок 3: find_elements()
Решение 3.1 — 6 картинок
# test_solutions.py
def test_check_image_quantity(driver):
# find_elements возвращает список всех img
images = driver.find_elements(By.TAG_NAME, "img")
assert len(images) == 6
Решение 3.2 — 6 карточек
# test_solutions.py
def test_check_cards_quantity(driver):
cards = driver.find_elements(By.CSS_SELECTOR, ".col-sm-4")
assert len(cards) == 6
Решение 3.3 — все карточки видны
# test_solutions.py
def test_check_all_cards_are_displayed(driver):
cards = driver.find_elements(By.CSS_SELECTOR, ".col-sm-4")
for index, card in enumerate(cards, start=1):
assert card.is_displayed(), \
f"Карточка {index} (текст: '{card.text[:20]}') не отображается"
Решение 3.4 — все фото отображаются
# test_solutions.py
def test_check_all_images_are_displayed(driver):
images = driver.find_elements(By.TAG_NAME, "img")
for img in images:
assert img.is_displayed()
Бонус: XPath
Решение 4.1 — кнопка View по XPath
# test_solutions.py
def test_view_button_xpath(driver):
# XPath с text() — поиск по точному тексту кнопки
view_btn = driver.find_element(By.XPATH, '//button[text()="View"]')
assert view_btn.is_displayed()
# Примечание: вернёт первую кнопку "View" на странице
Решение 4.2 — изображение по contains(@src)
# test_solutions.py
def test_serious_cat_image(driver):
# contains() — частичное совпадение атрибута src
serious_cat = driver.find_element(
By.XPATH, '//img[contains(@src, "serious_cat")]'
)
assert serious_cat.is_displayed()
Полный тест-файл
# test_solutions.py
import pytest
from selenium.webdriver.common.by import By
def test_header_text(driver):
header = driver.find_element(By.TAG_NAME, "h1")
assert header.text == "Cat memes"
def test_time_of_first_cat_card(driver):
time_first_cat = driver.find_element(
By.CSS_SELECTOR, ".col-sm-4:nth-child(1) small"
)
assert time_first_cat.text == "9 mins"
def test_last_cat_card_name(driver):
last_card_name = driver.find_element(
By.CSS_SELECTOR, ".col-sm-4:nth-child(6) p"
)
assert last_card_name.text == "I love you so much"
def test_cats_album_text(driver):
cats_album = driver.find_element(By.TAG_NAME, "strong")
assert cats_album.text == "Cats album"
def test_first_cat_card_is_displayed(driver):
first_cat_card = driver.find_element(
By.CSS_SELECTOR, ".col-sm-4:nth-child(1)"
)
assert first_cat_card.is_displayed()
def test_photo_icon_is_displayed(driver):
photo_icon = driver.find_element(By.TAG_NAME, "svg")
assert photo_icon.is_displayed()
def test_check_image_quantity(driver):
images = driver.find_elements(By.TAG_NAME, "img")
assert len(images) == 6
def test_check_cards_quantity(driver):
cards = driver.find_elements(By.CSS_SELECTOR, ".col-sm-4")
assert len(cards) == 6
def test_check_all_cards_are_displayed(driver):
cards = driver.find_elements(By.CSS_SELECTOR, ".col-sm-4")
for index, card in enumerate(cards, start=1):
assert card.is_displayed(), \
f"Карточка {index} не отображается"
def test_check_all_images_are_displayed(driver):
images = driver.find_elements(By.TAG_NAME, "img")
for img in images:
assert img.is_displayed()