from selenium.webdriver.common.by import By
from behave import given, when, then
from selenium.webdriver.common.keys import Keys


@when('Search for {search_word}')
def search_for_product(context, search_word):
    context.driver.find_element(By.ID, 'twotabsearchtextbox').send_keys(search_word, Keys.ENTER)


@when('Click on the first product')
def click_first_product(context):
    context.driver.find_element \
        (By.XPATH, "//div[@data-component-type='s-search-result']//a[.//span[@class='a-price']]").click()


@when('Click on add to cart button')
def click_add_to_cart(context):
    context.driver.find_element(By.ID, 'add-to-cart-button').click()


@then('Verify that cart has {expected_count} item')
def verify_cart_count(context, expected_count):
    items_in_cart = context.driver.find_element(By.ID, 'nav-cart-count').text
    assert items_in_cart == expected_count, f'Expected {expected_count}, but got {items_in_cart}'
