From 926880bf6df39d268ce3fd8b2c764a089c8aa77c Mon Sep 17 00:00:00 2001 From: Cruz06 Date: Thu, 19 Oct 2023 13:18:54 +0300 Subject: [PATCH 1/8] another attempt --- lesson1/check_filter.py | 24 ++++++++++++++++++ lesson1/go_to_item_card_by_pic.py | 22 +++++++++++++++++ lesson1/go_to_item_card_by_title.py | 21 ++++++++++++++++ lesson1/test_add_item_from_card.py | 23 ++++++++++++++++++ lesson1/test_add_item_from_catalog.py | 23 ++++++++++++++++++ lesson1/test_add_to_cart.py | 24 ++++++++++++++++++ lesson1/test_auth.py | 13 +--------- lesson1/test_auth_noncorrect.py | 20 +++++++++++++++ lesson1/test_button_about.py | 24 ++++++++++++++++++ lesson1/test_button_logout.py | 34 ++++++++++++++++++++++++++ lesson1/test_button_reset_sidebar.py | 35 +++++++++++++++++++++++++++ lesson1/test_remove_good_from_cart.py | 31 ++++++++++++++++++++++++ 12 files changed, 282 insertions(+), 12 deletions(-) create mode 100644 lesson1/check_filter.py create mode 100644 lesson1/go_to_item_card_by_pic.py create mode 100644 lesson1/go_to_item_card_by_title.py create mode 100644 lesson1/test_add_item_from_card.py create mode 100644 lesson1/test_add_item_from_catalog.py create mode 100644 lesson1/test_add_to_cart.py create mode 100644 lesson1/test_auth_noncorrect.py create mode 100644 lesson1/test_button_about.py create mode 100644 lesson1/test_button_logout.py create mode 100644 lesson1/test_button_reset_sidebar.py create mode 100644 lesson1/test_remove_good_from_cart.py diff --git a/lesson1/check_filter.py b/lesson1/check_filter.py new file mode 100644 index 0000000..55880f7 --- /dev/null +++ b/lesson1/check_filter.py @@ -0,0 +1,24 @@ +from selenium import webdriver +from selenium.webdriver.common.by import By +import time + +driver = webdriver.Chrome() + + +def test_filter_A_Z(): + driver.get("https://www.saucedemo.com/") + # authorization + driver.find_element(By.XPATH, '//input[@data-test="username"]').send_keys("standard_user") + driver.find_element(By.XPATH, '//input[@data-test="password"]').send_keys("secret_sauce") + driver.find_element(By.XPATH, '//input[@data-test="login-button"]').click() + + #container_sort = driver.find_element(By.CLASS_NAME, '[class="product_sort_container"]') + #container_sort.click() + + # click item in menu + sort_element = driver.find_element(By.XPATH, '//*[@id="header_container"]//option[1]') + sort_element.click() + time.sleep(3) + + + assert 1 == 1 \ No newline at end of file diff --git a/lesson1/go_to_item_card_by_pic.py b/lesson1/go_to_item_card_by_pic.py new file mode 100644 index 0000000..08e7758 --- /dev/null +++ b/lesson1/go_to_item_card_by_pic.py @@ -0,0 +1,22 @@ +from selenium import webdriver +from selenium.webdriver.common.by import By + +driver = webdriver.Chrome() + + +def test_go_to_item_card_by_pic(): + driver.get("https://www.saucedemo.com/inventory.html") + # authorization + driver.find_element(By.XPATH, '//input[@data-test="username"]').send_keys("standard_user") + driver.find_element(By.XPATH, '//input[@data-test="password"]').send_keys("secret_sauce") + driver.find_element(By.XPATH, '//input[@data-test="login-button"]').click() + + # click on img + driver.find_element(By.CSS_SELECTOR, "a#item_3_img_link").click() + + # check item card + item_URL = driver.current_url + expected_URL = "https://www.saucedemo.com/inventory-item.html?id=3" + + assert expected_URL == item_URL + diff --git a/lesson1/go_to_item_card_by_title.py b/lesson1/go_to_item_card_by_title.py new file mode 100644 index 0000000..eec210d --- /dev/null +++ b/lesson1/go_to_item_card_by_title.py @@ -0,0 +1,21 @@ +from selenium import webdriver +from selenium.webdriver.common.by import By + +driver = webdriver.Chrome() + + +def test_go_to_item_card_by_title(): + driver.get("https://www.saucedemo.com/inventory.html") + # authorization + driver.find_element(By.XPATH, '//input[@data-test="username"]').send_keys("standard_user") + driver.find_element(By.XPATH, '//input[@data-test="password"]').send_keys("secret_sauce") + driver.find_element(By.XPATH, '//input[@data-test="login-button"]').click() + + # click on title + driver.find_element(By.CSS_SELECTOR, "#item_5_title_link > div").click() + + # check item card + expected_title = "Sauce Labs Fleece Jacket" + actual_title = driver.find_element(By.XPATH, "//*[contains(text(),'Sauce Labs Fleece Jacket')]").text + + assert expected_title == actual_title diff --git a/lesson1/test_add_item_from_card.py b/lesson1/test_add_item_from_card.py new file mode 100644 index 0000000..1e6c587 --- /dev/null +++ b/lesson1/test_add_item_from_card.py @@ -0,0 +1,23 @@ +from selenium import webdriver +from selenium.webdriver.common.by import By + +driver = webdriver.Chrome() + + +def test_add_item_from_card(): + driver.get("https://www.saucedemo.com/inventory.html") + # authorization + driver.find_element(By.XPATH, '//input[@data-test="username"]').send_keys("standard_user") + driver.find_element(By.XPATH, '//input[@data-test="password"]').send_keys("secret_sauce") + driver.find_element(By.XPATH, '//input[@data-test="login-button"]').click() + + # add item + driver.find_element(By.CSS_SELECTOR, "#item_0_title_link > div").click() + driver.find_element(By.XPATH, '//*[@id="add-to-cart-sauce-labs-bike-light"]').click() + # go to cart and check it + driver.find_element(By.CSS_SELECTOR, "a[class='shopping_cart_link']").click() + # check item in cart + expected_name = "Sauce Labs Bike Light" + actual_name = driver.find_element(By.XPATH, "//*[contains(text(),'Sauce Labs Bike Light')]").text + + assert expected_name == actual_name diff --git a/lesson1/test_add_item_from_catalog.py b/lesson1/test_add_item_from_catalog.py new file mode 100644 index 0000000..9398e8b --- /dev/null +++ b/lesson1/test_add_item_from_catalog.py @@ -0,0 +1,23 @@ +from selenium import webdriver +from selenium.webdriver.common.by import By + +driver = webdriver.Chrome() + + +def test_add_item_from_catalog(): + driver.get("https://www.saucedemo.com/inventory.html") + # authorization + driver.find_element(By.XPATH, '//input[@data-test="username"]').send_keys("standard_user") + driver.find_element(By.XPATH, '//input[@data-test="password"]').send_keys("secret_sauce") + driver.find_element(By.XPATH, '//input[@data-test="login-button"]').click() + + # add item + driver.find_element(By.XPATH, '// *[ @ id = "add-to-cart-sauce-labs-bike-light"]').click() + # go to cart and check it + driver.find_element(By.CSS_SELECTOR, "a[class='shopping_cart_link']").click() + # check item in cart + expected_name = "Sauce Labs Bike Light" + actual_name = driver.find_element(By.XPATH, "//*[contains(text(),'Sauce Labs Bike Light')]").text + + assert expected_name == actual_name + \ No newline at end of file diff --git a/lesson1/test_add_to_cart.py b/lesson1/test_add_to_cart.py new file mode 100644 index 0000000..3befdf0 --- /dev/null +++ b/lesson1/test_add_to_cart.py @@ -0,0 +1,24 @@ +from selenium import webdriver +from selenium.webdriver.common.by import By +import time + +driver = webdriver.Chrome() + + +def test_add_good_to_cart(): + driver.get("https://www.saucedemo.com/") + # authorization + driver.find_element(By.XPATH, '//input[@data-test="username"]').send_keys("standard_user") + driver.find_element(By.XPATH, '//input[@data-test="password"]').send_keys("secret_sauce") + driver.find_element(By.XPATH, '//input[@data-test="login-button"]').click() + # select item + text_before = driver.find_element(By.CSS_SELECTOR, "a[id='item_1_title_link']").text + + # add item to cart + driver.find_element(By.XPATH, '//*[@id="add-to-cart-sauce-labs-bolt-t-shirt"]').click() + + # go to cart and check it + driver.find_element(By.CSS_SELECTOR, "a[class='shopping_cart_link']").click() + text_after = driver.find_element(By.CSS_SELECTOR, "a[id='item_1_title_link'] > div[class='inventory_item_name']").text + + assert text_before == text_after \ No newline at end of file diff --git a/lesson1/test_auth.py b/lesson1/test_auth.py index f9f3002..9a671ae 100644 --- a/lesson1/test_auth.py +++ b/lesson1/test_auth.py @@ -5,7 +5,6 @@ driver = webdriver.Chrome() - def test_login_form(): driver.get("https://www.saucedemo.com/") @@ -18,17 +17,7 @@ def test_login_form(): login_button = driver.find_element(By.XPATH, '//input[@data-test="login-button"]') login_button.click() - time.sleep(5) + time.sleep(3) assert driver.current_url == "https://www.saucedemo.com/inventory.html" driver.quit() - - - - - - - - - - diff --git a/lesson1/test_auth_noncorrect.py b/lesson1/test_auth_noncorrect.py new file mode 100644 index 0000000..1109367 --- /dev/null +++ b/lesson1/test_auth_noncorrect.py @@ -0,0 +1,20 @@ +from selenium import webdriver +from selenium.webdriver.common.by import By + +driver = webdriver.Chrome() + + +def test_login_form_noncorrect(): + driver.get("https://www.saucedemo.com/") + + username_field = driver.find_element(By.XPATH, '//input[@data-test="username"]') + username_field.send_keys("other_user") + + password_field = driver.find_element(By.XPATH, '//input[@data-test="password"]') + password_field.send_keys("just_sauce") + + expected_error_message = driver.find_element(By.CSS_SELECTOR, '[class="error-message-container"] ') + + assert expected_error_message.is_displayed() + + driver.quit() \ No newline at end of file diff --git a/lesson1/test_button_about.py b/lesson1/test_button_about.py new file mode 100644 index 0000000..84b2e41 --- /dev/null +++ b/lesson1/test_button_about.py @@ -0,0 +1,24 @@ +from selenium import webdriver +from selenium.webdriver.common.by import By +import time + +driver = webdriver.Chrome() + + +def test_check_button_about(): + driver.get("https://www.saucedemo.com/") + # authorization + driver.find_element(By.XPATH, '//input[@data-test="username"]').send_keys("standard_user") + driver.find_element(By.XPATH, '//input[@data-test="password"]').send_keys("secret_sauce") + driver.find_element(By.XPATH, '//input[@data-test="login-button"]').click() + + driver.find_element(By.ID, 'react-burger-menu-btn').click() + time.sleep(1) + # click item in menu + menu_about = driver.find_element(By.ID, 'about_sidebar_link') + menu_about.click() + time.sleep(1) + url_actual = driver.current_url + url_expected = "https://saucelabs.com/" + assert url_actual == url_expected + \ No newline at end of file diff --git a/lesson1/test_button_logout.py b/lesson1/test_button_logout.py new file mode 100644 index 0000000..6669901 --- /dev/null +++ b/lesson1/test_button_logout.py @@ -0,0 +1,34 @@ +from selenium import webdriver +from selenium.webdriver.common.by import By +import time + +driver = webdriver.Chrome() + + +def test_logout(): + driver.get("https://www.saucedemo.com/") + url_before = driver.current_url + + username_field = driver.find_element(By.XPATH, '//input[@data-test="username"]') + username_field.send_keys("standard_user") + + password_field = driver.find_element(By.XPATH, '//input[@data-test="password"]') + password_field.send_keys("secret_sauce") + + login_button = driver.find_element(By.XPATH, '//input[@data-test="login-button"]') + login_button.click() + # в левой части есть меню магазина, там команда logout + burger_menu = driver.find_element(By.ID, "react-burger-menu-btn") + burger_menu.click() + time.sleep(3) + + logout = driver.find_element(By.CSS_SELECTOR, "#logout_sidebar_link") + logout.click() + time.sleep(1) + + # если вышел , то возврат на начальную страницу, можно проверить через УРЛ + url_after = driver.current_url + #print(url_after) + + assert url_after == url_before + driver.quit() #когда будет закрывающая фикстура, то удалить diff --git a/lesson1/test_button_reset_sidebar.py b/lesson1/test_button_reset_sidebar.py new file mode 100644 index 0000000..8b96a8a --- /dev/null +++ b/lesson1/test_button_reset_sidebar.py @@ -0,0 +1,35 @@ +from selenium import webdriver +from selenium.webdriver.common.by import By +import time + +driver = webdriver.Chrome() +# bag: buttons reset remain ON + + +def test_check_button_reset(): + driver.get("https://www.saucedemo.com/") + # authorization + driver.find_element(By.XPATH, '//input[@data-test="username"]').send_keys("standard_user") + driver.find_element(By.XPATH, '//input[@data-test="password"]').send_keys("secret_sauce") + driver.find_element(By.XPATH, '//input[@data-test="login-button"]').click() + + # add items to cart + driver.find_element(By.ID, "add-to-cart-sauce-labs-bike-light").click() + driver.find_element(By.ID, "add-to-cart-sauce-labs-backpack").click() + + # check nr of items in cart + time.sleep(1) + item_in_the_cart = driver.find_element(By.XPATH, "//span[@class='shopping_cart_badge']") + items_before = item_in_the_cart.text # == '2' + + #go to burger menu + driver.find_element(By.ID, 'react-burger-menu-btn').click() + time.sleep(1) + # click item reset in menu + driver.find_element(By.ID, 'reset_sidebar_link').click() + buttons_remove = driver.find_elements(By.CSS_SELECTOR, '*[name^="remove"]') + + item_in_the_cart = driver.find_element(By.CSS_SELECTOR, "a.shopping_cart_link") + items_after = item_in_the_cart.text # == '0' + assert items_after == "", "cart is not empty" + assert len(buttons_remove) == 0, "cart is empty, but buttons Remove are still on" diff --git a/lesson1/test_remove_good_from_cart.py b/lesson1/test_remove_good_from_cart.py new file mode 100644 index 0000000..1538690 --- /dev/null +++ b/lesson1/test_remove_good_from_cart.py @@ -0,0 +1,31 @@ +from selenium import webdriver +from selenium.webdriver.common.by import By +import time + +driver = webdriver.Chrome() + + +def test_remove_good_from_cart(): + driver.get("https://www.saucedemo.com/") + # authorization + driver.find_element(By.XPATH, '//input[@data-test="username"]').send_keys("standard_user") + driver.find_element(By.XPATH, '//input[@data-test="password"]').send_keys("secret_sauce") + driver.find_element(By.XPATH, '//input[@data-test="login-button"]').click() + + # select item + # add item 1 to cart + driver.find_element(By.XPATH, '//*[@id="add-to-cart-sauce-labs-backpack"]').click() + # add item 2 to cart + driver.find_element(By.XPATH, '//*[@id="add-to-cart-sauce-labs-bolt-t-shirt"]').click() + # go to cart and check it + driver.find_element(By.CSS_SELECTOR, "a[class='shopping_cart_link']").click() + count_before_removing = driver.find_element(By.XPATH, '//*[@id="shopping_cart_container"]/a/span').text + #item_in_cart = driver.find_element(By.XPATH, '//*[@id="item_1_title_link"]/div [contains(text(), "Sauce Labs Bolt T-Shirt")]') + driver.find_element(By.XPATH, '//*[@id="remove-sauce-labs-bolt-t-shirt"]').click() + count_after_removing = driver.find_element(By.XPATH, '//*[@id="shopping_cart_container"]/a/span').text + + assert int(count_after_removing) == int(count_before_removing) - 1 + + +def test_remove_good_from_cart2(): + driver.get("https://www.saucedemo.com/inventory.html") \ No newline at end of file From e3aceea062e72f154cadf3d832af830bc1e6e7d4 Mon Sep 17 00:00:00 2001 From: Cruz06 Date: Thu, 19 Oct 2023 15:07:04 +0300 Subject: [PATCH 2/8] add other projects --- lesson1/check_filter.py | 16 +++- lessonAG/burger_menu.py | 72 +++++++++++++++++ lessonAG/inventory.py | 63 +++++++++++++++ lessonAG/login_test.py | 16 ++++ lessonAG/login_with_checkbox.py | 16 ++++ lessonAG/product_details.py | 14 ++++ lessonAG/setup/conftest.py | 132 ++++++++++++++++++++++++++++++++ lessonAG/setup/data.py | 53 +++++++++++++ lessonAG/setup/locators.py | 56 ++++++++++++++ lessonAG/shopping_cart.py | 54 +++++++++++++ lesson_O/cart_O.py | 78 +++++++++++++++++++ lesson_O/checkout_O.py | 28 +++++++ lesson_O/conftest.py | 28 +++++++ lesson_O/locators.py | 0 lesson_O/open_by_img_O.py | 20 +++++ lesson_O/remove_O.py | 77 +++++++++++++++++++ 16 files changed, 720 insertions(+), 3 deletions(-) create mode 100644 lessonAG/burger_menu.py create mode 100644 lessonAG/inventory.py create mode 100644 lessonAG/login_test.py create mode 100644 lessonAG/login_with_checkbox.py create mode 100644 lessonAG/product_details.py create mode 100644 lessonAG/setup/conftest.py create mode 100644 lessonAG/setup/data.py create mode 100644 lessonAG/setup/locators.py create mode 100644 lessonAG/shopping_cart.py create mode 100644 lesson_O/cart_O.py create mode 100644 lesson_O/checkout_O.py create mode 100644 lesson_O/conftest.py create mode 100644 lesson_O/locators.py create mode 100644 lesson_O/open_by_img_O.py create mode 100644 lesson_O/remove_O.py diff --git a/lesson1/check_filter.py b/lesson1/check_filter.py index 55880f7..8e03ebe 100644 --- a/lesson1/check_filter.py +++ b/lesson1/check_filter.py @@ -14,11 +14,21 @@ def test_filter_A_Z(): #container_sort = driver.find_element(By.CLASS_NAME, '[class="product_sort_container"]') #container_sort.click() + # find all elements + all_elem_ZA = driver.find_elements(By.XPATH, "//*[contains(text(), 'Sauce Labs')]") + # mix the elements + driver.find_element(By.XPATH, '//*[@id="header_container"]//select').click() + sort_element = driver.find_element(By.XPATH, '//*[@id="header_container"]//option[2]') + sort_element.click() + time.sleep(3) + all_elem_ZA = driver.find_elements(By.CSS_SELECTOR, 'inventory_item_name ') # click item in menu sort_element = driver.find_element(By.XPATH, '//*[@id="header_container"]//option[1]') sort_element.click() time.sleep(3) - - - assert 1 == 1 \ No newline at end of file + all_elem_AZ = driver.find_elements(By.CSS_SELECTOR, 'inventory_item_name ') + print(len(all_elem_AZ)) + for a in all_elem_AZ: + print(a.text) + assert all_elem_AZ == all_elem_ZA \ No newline at end of file diff --git a/lessonAG/burger_menu.py b/lessonAG/burger_menu.py new file mode 100644 index 0000000..4fe005c --- /dev/null +++ b/lessonAG/burger_menu.py @@ -0,0 +1,72 @@ +from setup.conftest import * + + +def test_close_menu(driver, burger_menu): + """Бургер меню > Проверка работоспособности кнопки "Close" """ + x = (By.ID, "react-burger-cross-btn") + + +def test_open_inventory(driver, burger_menu): + """Бургер меню > Проверка работоспособности кнопки "AlL Items" """ + # no ? + inventory_button = driver.find_element(*INVENTORY_SIDEBAR_LINK) + assert inventory_button.is_enabled() and inventory_button.is_displayed() + inventory_button.click() + assert driver.current_url == INVENTORY_URL + + +def test_open_about(driver, burger_menu): + """Бургер меню > Проверка работоспособности кнопки "About" """ + + about_button = driver.find_element(*ABOUT_SIDEBAR_LINK) + assert about_button.is_enabled() and about_button.is_displayed() + assert about_button.get_attribute(HREF) == ABOUT_URL + about_button.click() + assert driver.current_url == ABOUT_URL + + +def test_logout(driver, burger_menu): + """Бургер меню > Выход из системы""" + + logout_button = driver.find_element(*LOGOUT_SIDEBAR_LINK) + assert logout_button.is_enabled() and logout_button.is_displayed() + logout_button.click() + assert driver.current_url == BASE_URL + + +def test_reset_app_state(driver, burger_menu): + """Бургер меню > Проверка работоспособности кнопки "Reset App State" """ + + reset_button_is_found = len(driver.find_elements(*RESET_SIDEBAR_LINK)) > 0 + assert reset_button_is_found + + reset_button = driver.find_element(*RESET_SIDEBAR_LINK) + assert reset_button.is_enabled() and reset_button.is_displayed() + + # is_clickable = True + # try: + # reset_button.click() + # except ElementClickInterceptedException: + # is_clickable = False + # assert is_clickable is True + + +def show(elems, loc): + print(f"\r\nFound {len(elems)} elements -> {loc}") + for item in elems: + print(f'text content of {["hidden", "displayed"][item.is_displayed()]} element: \n', item.get_attribute("textContent")) + print(f'outer HTML of {["hidden", "displayed"][item.is_displayed()]} element:', item.get_attribute("outerHTML"), sep="\r\n") + + +def test_about_extended_mode(driver): + + driver.get(ABOUT_URL) + sleep(13) + + v_div = "div.MuiBox-root.css-jv9ibt" + h_div = "div.MuiBox-root.css-mntjpt" + elems = driver.find_elements(By.CSS_SELECTOR, v_div) + show(elems, v_div) + + elems = driver.find_elements(By.CSS_SELECTOR, h_div) + show(elems, h_div) diff --git a/lessonAG/inventory.py b/lessonAG/inventory.py new file mode 100644 index 0000000..4dab28a --- /dev/null +++ b/lessonAG/inventory.py @@ -0,0 +1,63 @@ +from setup.conftest import * + + +def test_open_product_details_from_img(driver, inventory): + """Успешный переход к карточке товара после клика на картинку товара""" + product = choice(driver.find_elements(*INVENTORY_ITEM_IMG)) + product_name = product.get_attribute(ALT) + product.click() + + assert driver.find_element(*INVENTORY_DETAILS_NAME).text == product_name + + +def test_open_product_details_from_name(driver, inventory): + """Успешный переход к карточке товара после клика на название товара""" + product = choice(driver.find_elements(*INVENTORY_ITEM_NAME)) + product_name = product.text + product.click() + + assert driver.find_element(*INVENTORY_DETAILS_NAME).text == product_name + + +def test_add_product_to_shopping_cart(driver, inventory): + """Корзина > Добавление товара в корзину через каталог""" + product_add_button = choice(driver.find_elements(*BTN_INVENTORY)) + product_name = product_add_button.get_attribute(DATA_TEST) + product_add_button.click() + + driver.get(CART_URL) + + assert driver.find_element(*CART_BUTTON).get_attribute(DATA_TEST)[7:] == product_name[12:] + + +@pytest.mark.parametrize('direction', [True, False]) +def test_filter_by_name(driver, inventory, direction): + """Проверка работоспособности фильтра (A to Z) и (Z to A)""" + names = driver.find_elements(*INVENTORY_ITEM_NAME) + initial_order = [x.text for x in names] + + dropdown = Select(driver.find_element(*PRODUCT_SORT_CONTAINER)) + dropdown.select_by_visible_text([FILTER_AZ_TEXT, FILTER_ZA_TEXT][direction]) + + active_option = driver.find_element(*ACTIVE_OPTION) + assert active_option.text == [FILTER_AZ_TEXT, FILTER_ZA_TEXT][direction] + + names = driver.find_elements(*INVENTORY_ITEM_NAME) + final_order = [x.text for x in names] + + assert final_order == sorted(initial_order, reverse=direction) + + +@pytest.mark.parametrize('direction', [True, False]) +def test_filter_by_price(driver, inventory, direction): + """Проверка работоспособности фильтра (low to high) и (high to low)""" + prices = driver.find_elements(*INVENTORY_ITEM_PRICE) + initial_prices = [float(price.text[1:]) for price in prices] + + dropdown = Select(driver.find_element(*PRODUCT_SORT_CONTAINER)) + dropdown.select_by_visible_text([FILTER_LOHI_TEXT, FILTER_HILO_TEXT][direction]) + + prices = driver.find_elements(*INVENTORY_ITEM_PRICE) + final_prices = [float(price.text[1:]) for price in prices] + #print(*final_prices) + assert final_prices == sorted(initial_prices, reverse=direction) diff --git a/lessonAG/login_test.py b/lessonAG/login_test.py new file mode 100644 index 0000000..a60ca52 --- /dev/null +++ b/lessonAG/login_test.py @@ -0,0 +1,16 @@ +from setup.conftest import * + + +@pytest.mark.parametrize('credentials', good_credentials_lst) +def test_good_login(driver, credentials): + """Авторизация > Авторизация используя корректные данные""" + login(driver, *credentials) + assert driver.current_url == INVENTORY_URL + + +@pytest.mark.parametrize('credentials', bad_credentials_lst) +def test_bad_login(driver, credentials): + """Авторизация > Авторизация используя некорректные данные""" + alert, *credentials = credentials + login(driver, *credentials) + assert driver.find_element(*ERROR).text == alert \ No newline at end of file diff --git a/lessonAG/login_with_checkbox.py b/lessonAG/login_with_checkbox.py new file mode 100644 index 0000000..874cb32 --- /dev/null +++ b/lessonAG/login_with_checkbox.py @@ -0,0 +1,16 @@ +from setup.conftest import * + + +def test_login_form_with_checkbox(driver): + """Авторизация c чекбоксом""" + user, password = ("user", "password") + driver.get(AUTH_URL) + driver.find_element(*USERNAME_FIELD).send_keys(user) + driver.find_element(*PASSWORD_FIELD).send_keys(password) + checkbox = driver.find_element(*AGREEMENT_CHECKBOX) + if not checkbox.is_selected(): + checkbox.click() + + driver.find_element(*REGISTER_BUTTON).click() + + assert driver.current_url == AUTH_URL + '?' \ No newline at end of file diff --git a/lessonAG/product_details.py b/lessonAG/product_details.py new file mode 100644 index 0000000..0dc7d93 --- /dev/null +++ b/lessonAG/product_details.py @@ -0,0 +1,14 @@ +from setup.conftest import * + + +def test_add_product_to_cart(driver, product_details): + """Корзина > Добавление товара в корзину из карточки товара""" + add_to_cart_button = driver.find_element(*BTN_INVENTORY) + product_name = add_to_cart_button.get_attribute(DATA_TEST) + add_to_cart_button.click() + + driver.get(CART_URL) + + cart_item_name = driver.find_element(*CART_BUTTON).get_attribute(DATA_TEST) + + assert product_name[12:] == cart_item_name[7:] \ No newline at end of file diff --git a/lessonAG/setup/conftest.py b/lessonAG/setup/conftest.py new file mode 100644 index 0000000..2ef438a --- /dev/null +++ b/lessonAG/setup/conftest.py @@ -0,0 +1,132 @@ +import pytest +from selenium import webdriver +from selenium.webdriver.common.by import By +from lessonAG.setup.data import * +from lessonAG.setup.locators import * +from time import sleep +from random import choice +from selenium.common.exceptions import NoSuchElementException, ElementClickInterceptedException +from selenium.webdriver.support.ui import Select +from selenium.webdriver.firefox.options import Options +from selenium.webdriver.chrome.options import Options +from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC +from selenium.webdriver.chrome.service import Service as ChromeService + + +@pytest.fixture(scope="module") +def mdriver(): + # options = webdriver.ChromeOptions() + # options.add_experimental_option("excludeSwitches", ["enable-automation"]) + # options.add_experimental_option("useAutomationExtension", False) + # service = ChromeService() + # driver = webdriver.Chrome(service=service, options=options) + + options = Options() + # options.add_argument("--headless") + driver = webdriver.Chrome(options=options) + + driver.implicitly_wait(3) + driver.get("https://google.com/") + + yield driver + + driver.quit() + + +@pytest.fixture(scope="function") +def driver(mdriver): + # options = Options() + # options.add_argument("--headless") + # driver = webdriver.Chrome(options=options) + driver = mdriver + original_window = driver.current_window_handle + driver.switch_to.new_window('tab') + + driver.implicitly_wait(5) + driver.get(BASE_URL) + + yield driver + + # driver.quit() + driver.close() + driver.switch_to.window(original_window) + + +@pytest.fixture(scope="function") +def _driver(): + options = Options() + options.add_argument("--headless") + driver = webdriver.Firefox(options=options) + driver.implicitly_wait(3) + driver.get(BASE_URL) + + yield driver + + driver.quit() + +# @pytest.fixture(scope="function") +# def driver(): +# driver = webdriver.Firefox() +# driver.implicitly_wait(3) +# driver.get(BASE_URL) +# +# yield driver +# +# driver.quit() + + +@pytest.fixture(params=[("standard_user", "secret_sauce")]) +def standard_user(request): + return request.param + + +@pytest.fixture(params=[('user', 'user')]) +def user(request): + return request.param + + +# from selenium.webdriver import Firefox, FirefoxOptions +# opts = FirefoxOptions() +# opts.add_argument("--width=2560") +# opts.add_argument("--height=1440") +# driver = Firefox(options=opts) +@pytest.fixture +def chrome_driver(): + driver = webdriver.Chrome() + driver.implicitly_wait(5) + + driver.get(BASE_URL) + yield driver + + driver.quit() + + +def login(driver, username, password): + driver.find_element(*USERNAME).send_keys(username) + driver.find_element(*PASSWORD).send_keys(password) + driver.find_element(*LOGIN_BUTTON).click() + + +@pytest.fixture +def inventory(driver, standard_user): + login(driver, *standard_user) + + +@pytest.fixture +def burger_menu(driver, inventory): + driver.find_element(*BM_BURGER_BUTTON).click() + + +@pytest.fixture +def product_details(driver, inventory): + driver.get(ITEM_URL + str(choice([*range(6)]))) + + +@pytest.fixture +def cart_with_product(driver, inventory): + choice(driver.find_elements(*BTN_INVENTORY)).click() + driver.get(CART_URL) + +# def check_exists(strategy, locator): +# return len(driver.find_elements(strategy, locator)) > 0 diff --git a/lessonAG/setup/data.py b/lessonAG/setup/data.py new file mode 100644 index 0000000..080fc92 --- /dev/null +++ b/lessonAG/setup/data.py @@ -0,0 +1,53 @@ +# AUTH +LOGIN = 'standard_user' +PASSWORD = 'secret_sauce' + +# URLS +ABOUT_URL = "https://saucelabs.com/" +BASE_URL = "https://www.saucedemo.com/" +INVENTORY_URL = BASE_URL + "inventory.html" +CART_URL = BASE_URL + "cart.html" +ITEM_URL = BASE_URL + "inventory-item.html?id=" + +#attr +DATA_TEST = "data-test" +ALT = "alt" +HREF = "href" + +# checkout +CHECKOUT_STEP_ONE_URL = BASE_URL + "checkout-step-one.html" +CHECKOUT_STEP_TWO_URL = BASE_URL + "checkout-step-two.html" +CHECKOUT_COMPLETE_URL = BASE_URL + "checkout-complete.html" + + +# checkout error messages +MSG_FIRST_NAME_REQUIRED = "Error: First Name is required" +MSG_LAST_NAME_REQUIRED = "Error: Last Name is required" +MSG_POSTAL_CODE_REQUIRED = "Error: Postal Code is required" + +# filter options text +# values = ['az', 'za', 'lohi', 'hilo'] +FILTER_AZ_TEXT = "Name (A to Z)" +FILTER_ZA_TEXT = "Name (Z to A)" +FILTER_LOHI_TEXT = "Price (low to high)" +FILTER_HILO_TEXT = "Price (high to low)" + +# auth error messages +MSG_WRONG_CREDENTIALS = "Epic sadface: Username and password do not match any user in this service" +MSG_AUTHORIZED_USERS_ONLY = "Epic sadface: You can only access '/inventory.html' when you are logged in." +MSG_USERNAME_REQUIRED = "Epic sadface: Username is required" +MSG_PASSWORD_REQUIRED = "Epic sadface: Password is required" +MSG_USER_LOCKED_OUT = "Epic sadface: Sorry, this user has been locked out." + +good_credentials_lst = [("standard_user", "secret_sauce"), + ('problem_user', 'secret_sauce'), + ('performance_glitch_user', 'secret_sauce'), + ('error_user', 'secret_sauce'), + ('visual_user', 'secret_sauce')] +bad_credentials_lst = [(MSG_WRONG_CREDENTIALS, 'user', 'user'), + (MSG_USERNAME_REQUIRED, '', 'empty_user'), + (MSG_PASSWORD_REQUIRED, 'empty_password', ''), + (MSG_USER_LOCKED_OUT, 'locked_out_user', 'secret_sauce')] + +# gitHub checkbox login form +AUTH_URL = "https://victoretc.github.io/webelements_information/" \ No newline at end of file diff --git a/lessonAG/setup/locators.py b/lessonAG/setup/locators.py new file mode 100644 index 0000000..898868c --- /dev/null +++ b/lessonAG/setup/locators.py @@ -0,0 +1,56 @@ +from selenium.webdriver.common.by import By + +# auth page +USERNAME = (By.XPATH, '//input[@data-test="username"]') +PASSWORD = (By.XPATH, '//input[@data-test="password"]') +LOGIN_BUTTON = (By.XPATH, '//input[@data-test="login-button"]') + +# auth error +ERROR = (By.XPATH, '//h3[@data-test="error"]') + +# shopping cart locator +SHOPPING_CART_BADGE = (By.CLASS_NAME, "shopping_cart_badge") +CART_BUTTON = (By.CLASS_NAME, "cart_button") + +# burger menu locator +BM_BURGER_BUTTON = (By.CLASS_NAME, "bm-burger-button") + +# burgher menu items +INVENTORY_SIDEBAR_LINK = (By.ID, "inventory_sidebar_link") +ABOUT_SIDEBAR_LINK = (By.ID, "about_sidebar_link") +LOGOUT_SIDEBAR_LINK = (By.ID, "logout_sidebar_link") +RESET_SIDEBAR_LINK = (By.ID, "reset_sidebar_link") + + +# inventory +INVENTORY_ITEM_IMG = (By.CSS_SELECTOR, "img.inventory_item_img") +INVENTORY_ITEM_NAME = (By.CLASS_NAME, "inventory_item_name") +BTN_INVENTORY = (By.CLASS_NAME, "btn_inventory") +INVENTORY_ITEM_PRICE = (By.CLASS_NAME, "inventory_item_price") + + +# inventory item +INVENTORY_DETAILS_NAME = (By.CLASS_NAME, "inventory_details_name") + +# inventory filter +ACTIVE_OPTION = (By.CLASS_NAME, "active_option") +PRODUCT_SORT_CONTAINER = (By.CLASS_NAME, "product_sort_container") + +# checkout +CHECKOUT = (By.ID, "checkout") +FIRST_NAME = (By.ID, "first-name") +LAST_NAME = (By.ID, "last-name") +POSTAL_CODE = (By.ID, "postal-code") +CONTINUE = (By.ID, "continue") +FINISH = (By.ID, "finish") +CONTINUE_SHOPPING = (By.ID, "continue-shopping") +CANCEL = (By.ID, "cancel") +BACK_TO_PRODUCTS = (By.ID, "back-to-products") + + +# github checkbox login form +USERNAME_FIELD = (By.ID, "username") +PASSWORD_FIELD = (By.ID, "password") +AGREEMENT_CHECKBOX = (By.ID, "agreement") +REGISTER_BUTTON = (By.ID, "registerButton") + diff --git a/lessonAG/shopping_cart.py b/lessonAG/shopping_cart.py new file mode 100644 index 0000000..1bcd8e1 --- /dev/null +++ b/lessonAG/shopping_cart.py @@ -0,0 +1,54 @@ +from setup.conftest import * +import time + + +def test_delete_product(driver, cart_with_product): + """Корзина > Удаление товара из корзины через корзину""" + driver.find_element(*CART_BUTTON).click() + + assert len(driver.find_elements(*CART_BUTTON)) == 0 + + # found = True + # try: + # driver.find_element(By.CLASS_NAME, CART_BUTTON) + # except NoSuchElementException: + # found = False + # + # assert found is False + + +def test_delete_product_from_cart(driver, cart_with_product): + """Корзина > Удаление товара из корзины через карточку товара""" + + driver.find_element(*INVENTORY_ITEM_NAME).click() + driver.find_element(*BTN_INVENTORY).click() + + driver.get(CART_URL) + + assert len(driver.find_elements(*CART_BUTTON)) == 0 + + # found = len(driver.find_elements(*CART_BUTTON)) > 0 + # assert found is False + # + # found = True + # try: + # driver.find_element(*CART_BUTTON) + # except NoSuchElementException: + # found = False + # assert found is False + + +def test_checkout(driver, cart_with_product): + """Оформление заказа > Оформление заказа используя корректные данные""" + first_name, last_name, postal_code = ('first', 'last', '1234') + + driver.find_element(*CHECKOUT).click() + driver.find_element(*FIRST_NAME).send_keys(first_name) + driver.find_element(*LAST_NAME).send_keys(last_name) + driver.find_element(*POSTAL_CODE).send_keys(postal_code) + time.sleep(3) + driver.find_element(*CONTINUE).click() + time.sleep(3) + driver.find_element(*FINISH).click() + time.sleep(3) + assert driver.current_url == CHECKOUT_COMPLETE_URL diff --git a/lesson_O/cart_O.py b/lesson_O/cart_O.py new file mode 100644 index 0000000..689c0b0 --- /dev/null +++ b/lesson_O/cart_O.py @@ -0,0 +1,78 @@ +from selenium import webdriver +from selenium.webdriver.common.by import By +import time +from selenium.common.exceptions import NoSuchElementException +from random import choice + + +def test_add_item_from_catalog(driver, login): + + first_item_add_button = driver.find_element(By.XPATH, '//*[@id="add-to-cart-sauce-labs-backpack"]') + first_item_add_button.click() + + item_in_the_cart = driver.find_element(By.XPATH, "//span[@class='shopping_cart_badge']") + #assert item_in_the_cart.is_displayed() + assert item_in_the_cart.text == '1' + + +def test_remove_item_from_the_cart(driver, login): + + first_item_add_button = driver.find_element(By.XPATH, '//*[@id="add-to-cart-sauce-labs-backpack"]') + first_item_add_button.click() + + cart_button = driver.find_element(By.XPATH, "//span[@class='shopping_cart_badge']") + cart_button.click() + + remove_button_cart = driver.find_element(By.XPATH, '//button[@data-test="remove-sauce-labs-backpack"]') + remove_button_cart.click() + + try: + driver.find_element(By.XPATH, '//div[@class="cart_item_label"]') + assert False, 'There are items in the cart' + except NoSuchElementException: + assert True + # assert driver.find_elements(By.XPATH, '//div[@class="removed_cart_item"]') + + +def test_remove_item_from_the_cart_list(driver, login): + random_items = driver.find_elements(By.CSS_SELECTOR, 'button.btn_inventory') + first_item = choice(random_items) + first_item.click() + second_item = choice(random_items) + second_item.click() + + driver.get('https://www.saucedemo.com/cart.html') + # driver.find_element(By.XPATH, "//a[@class='shopping_cart_link']").click() + list_items_before = driver.find_elements(By.CSS_SELECTOR, 'div.cart_item') #2 + # driver.find_element(By.CSS_SELECTOR, 'button.cart_button').click() + driver.find_elements(By.CSS_SELECTOR, 'button.cart_button')[0].click() + list_items_after = driver.find_elements(By.CSS_SELECTOR, 'div.cart_item') #1 + assert len(list_items_before) == len(list_items_after) + 1 + + +def test_add_item_from_items_card(driver, login): + items_image = driver.find_element(By.XPATH, '//img[@alt="Sauce Labs Backpack"]') + items_image.click() + + add_to_cart_button = driver.find_element(By.XPATH, '//*[@id="add-to-cart-sauce-labs-backpack"]') + add_to_cart_button.click() + + item_in_the_cart = driver.find_element(By.XPATH, "//span[@class='shopping_cart_badge']") + # assert item_in_the_cart.is_displayed() + assert item_in_the_cart.text == '1' + + +def test_remove_item_from_cart_from_items_card(driver, login): + + items_image = driver.find_element(By.XPATH, '//img[@alt="Sauce Labs Backpack"]') + items_image.click() + + add_to_cart_button = driver.find_element(By.XPATH, '//*[@id="add-to-cart-sauce-labs-backpack"]') + add_to_cart_button.click() + + remove_button_items_card = driver.find_element(By.XPATH, '//button[@data-test="remove-sauce-labs-backpack"]') + remove_button_items_card.click() + time.sleep(3) + + assert driver.find_elements(By.XPATH, '//*[@id="add-to-cart-sauce-labs-backpack"]') + diff --git a/lesson_O/checkout_O.py b/lesson_O/checkout_O.py new file mode 100644 index 0000000..be0240d --- /dev/null +++ b/lesson_O/checkout_O.py @@ -0,0 +1,28 @@ +import time + +import pytest +from selenium.webdriver.common.by import By +from faker import Faker +from selenium.common.exceptions import NoSuchElementException + + +@pytest.mark.parametrize('name', ['Olya', '@5tg_']) +def test_checkout(driver, login, cart_with_item, name): + driver.find_element(By.XPATH, '//button[@data-test="checkout"]').click() + # заполнение формы + fake = Faker() + driver.find_element(By.XPATH, '//input[@data-test="firstName"]').send_keys(name) + driver.find_element(By.XPATH, '//input[@data-test="lastName"]').send_keys(fake.last_name()) + driver.find_element(By.XPATH, '//input[@data-test="postalCode"]').send_keys(fake.postcode()) + + driver.find_element(By.XPATH, '//input[@data-test="continue"]').click() + driver.find_element(By.XPATH, '//button[@data-test="finish"]').click() + + assert driver.find_element(By.XPATH, '//h2[@class="complete-header"]').text == 'Thank you for your order!' + + +def test_checkout_empty_form_error_message(driver, login, cart_with_item): + driver.find_element(By.XPATH, '//button[@data-test="checkout"]').click() + driver.find_element(By.XPATH, '//input[@data-test="continue"]').click() + error_text = driver.find_element(By.XPATH, '//h3[@data-test="error"]').text + assert error_text == 'Error: First Name is required' \ No newline at end of file diff --git a/lesson_O/conftest.py b/lesson_O/conftest.py new file mode 100644 index 0000000..9f97e19 --- /dev/null +++ b/lesson_O/conftest.py @@ -0,0 +1,28 @@ +import pytest +from selenium import webdriver +from selenium.webdriver.common.by import By + +@pytest.fixture +def driver(): + driver = webdriver.Chrome() + driver.get("https://www.saucedemo.com/") + driver.implicitly_wait(2) + yield driver + driver.quit() + +@pytest.fixture +def login(driver): + username_field = driver.find_element(By.XPATH, '//input[@data-test="username"]') + username_field.send_keys("standard_user") + + password_field = driver.find_element(By.XPATH, '//input[@data-test="password"]') + password_field.send_keys("secret_sauce") + + login_button = driver.find_element(By.XPATH, '//input[@data-test="login-button"]') + login_button.click() + + +@pytest.fixture +def cart_with_item(driver, login): + driver.find_element(By.XPATH, '//*[@id="add-to-cart-sauce-labs-backpack"]').click() + driver.get('https://www.saucedemo.com/cart.html') \ No newline at end of file diff --git a/lesson_O/locators.py b/lesson_O/locators.py new file mode 100644 index 0000000..e69de29 diff --git a/lesson_O/open_by_img_O.py b/lesson_O/open_by_img_O.py new file mode 100644 index 0000000..2394930 --- /dev/null +++ b/lesson_O/open_by_img_O.py @@ -0,0 +1,20 @@ +from selenium import webdriver +from selenium.webdriver.common.by import By +import time + +driver = webdriver.Chrome() + + +def test_open_items_card_by_image(driver, login): + first_item_name = driver.find_element(By.CLASS_NAME, 'inventory_item_name').text + + item_image = driver.find_element(By.CSS_SELECTOR, 'img.inventory_item_img') + # print(item_image.get_attribute('alt')) + item_image.click() + + product_card_item_name = driver.find_element(By.CLASS_NAME, 'inventory_details_name').text + + assert first_item_name == product_card_item_name + +#base64 - сравнить картинки + diff --git a/lesson_O/remove_O.py b/lesson_O/remove_O.py new file mode 100644 index 0000000..3f83f82 --- /dev/null +++ b/lesson_O/remove_O.py @@ -0,0 +1,77 @@ +from selenium import webdriver +from selenium.webdriver.common.by import By +import time +from selenium.common.exceptions import NoSuchElementException +from random import choice + + +def test_add_item_from_catalog(driver, login): + + first_item_add_button = driver.find_element(By.XPATH, '//*[@id="add-to-cart-sauce-labs-backpack"]') + first_item_add_button.click() + + item_in_the_cart = driver.find_element(By.XPATH, "//span[@class='shopping_cart_badge']") + #assert item_in_the_cart.is_displayed() + assert item_in_the_cart.text == '1' + + +def test_remove_item_from_the_cart(driver, login): + + first_item_add_button = driver.find_element(By.XPATH, '//*[@id="add-to-cart-sauce-labs-backpack"]') + first_item_add_button.click() + + cart_button = driver.find_element(By.XPATH, "//span[@class='shopping_cart_badge']") + cart_button.click() + + remove_button_cart = driver.find_element(By.XPATH, '//button[@data-test="remove-sauce-labs-backpack"]') + remove_button_cart.click() + + try: + driver.find_element(By.XPATH, '//div[@class="cart_item_label"]') + assert False, 'There are items in the cart' + except NoSuchElementException: + assert True + # assert driver.find_elements(By.XPATH, '//div[@class="removed_cart_item"]') + + +def test_remove_item_from_the_cart_list(driver, login): + random_items = driver.find_elements(By.CSS_SELECTOR, 'button.btn_inventory') + first_item = choice(random_items) + first_item.click() + second_item = choice(random_items) + second_item.click() + + driver.get('https://www.saucedemo.com/cart.html') + # driver.find_element(By.XPATH, "//a[@class='shopping_cart_link']").click() + list_items_before = driver.find_elements(By.CSS_SELECTOR, 'div.cart_item') #2 + # driver.find_element(By.CSS_SELECTOR, 'button.cart_button').click() + driver.find_elements(By.CSS_SELECTOR, 'button.cart_button')[0].click() + list_items_after = driver.find_elements(By.CSS_SELECTOR, 'div.cart_item') #1 + assert len(list_items_before) == len(list_items_after) + 1 + + +def test_add_item_from_items_card(driver, login): + items_image = driver.find_element(By.XPATH, '//img[@alt="Sauce Labs Backpack"]') + items_image.click() + + add_to_cart_button = driver.find_element(By.XPATH, '//*[@id="add-to-cart-sauce-labs-backpack"]') + add_to_cart_button.click() + + item_in_the_cart = driver.find_element(By.XPATH, "//span[@class='shopping_cart_badge']") + # assert item_in_the_cart.is_displayed() + assert item_in_the_cart.text == '1' + + +def test_remove_item_from_cart_from_items_card(driver, login): + + items_image = driver.find_element(By.XPATH, '//img[@alt="Sauce Labs Backpack"]') + items_image.click() + + add_to_cart_button = driver.find_element(By.XPATH, '//*[@id="add-to-cart-sauce-labs-backpack"]') + add_to_cart_button.click() + + remove_button_items_card = driver.find_element(By.XPATH, '//button[@data-test="remove-sauce-labs-backpack"]') + remove_button_items_card.click() + time.sleep(3) + + assert driver.find_elements(By.XPATH, '//*[@id="add-to-cart-sauce-labs-backpack"]') \ No newline at end of file From c9470b5449e65bcd5aa2cc0db674a2ee9a769914 Mon Sep 17 00:00:00 2001 From: Cruz06 Date: Sat, 21 Oct 2023 19:55:53 +0300 Subject: [PATCH 3/8] test remove 2 --- lesson1/test_remove_good_from_cart.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lesson1/test_remove_good_from_cart.py b/lesson1/test_remove_good_from_cart.py index 1538690..5836e32 100644 --- a/lesson1/test_remove_good_from_cart.py +++ b/lesson1/test_remove_good_from_cart.py @@ -28,4 +28,15 @@ def test_remove_good_from_cart(): def test_remove_good_from_cart2(): - driver.get("https://www.saucedemo.com/inventory.html") \ No newline at end of file + driver.get("https://www.saucedemo.com/inventory.html") + driver.find_element(By.XPATH, '//input[@data-test="username"]').send_keys("standard_user") + driver.find_element(By.XPATH, '//input[@data-test="password"]').send_keys("secret_sauce") + driver.find_element(By.XPATH, '//input[@data-test="login-button"]').click() + driver.find_element(By.XPATH, '//*[@id="add-to-cart-sauce-labs-backpack"]').click() + driver.find_element(By.CSS_SELECTOR, "a[class='shopping_cart_link']").click() + driver.find_element(By.XPATH, '//*[@id="remove-sauce-labs-backpack"]').click() + + assert len(driver.find_elements(By.ID, 'item_4_title_link')) == 0 + + + From fae882c20982ba4b4fad2cc05f279c3ae0877485 Mon Sep 17 00:00:00 2001 From: Cruz06 Date: Mon, 23 Oct 2023 00:01:55 +0300 Subject: [PATCH 4/8] lesson 2 not finished --- lesson1/test_add_item_from_catalog.py | 23 +++++++++++- lesson2_my/catalog.py | 53 +++++++++++++++++++++++++++ lesson2_my/conftest.py | 21 +++++++++++ lesson2_my/item_cart.py | 24 ++++++++++++ lesson2_my/locators.py | 34 +++++++++++++++++ lesson2_my/with_param.py | 16 ++++++++ 6 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 lesson2_my/catalog.py create mode 100644 lesson2_my/conftest.py create mode 100644 lesson2_my/item_cart.py create mode 100644 lesson2_my/locators.py create mode 100644 lesson2_my/with_param.py diff --git a/lesson1/test_add_item_from_catalog.py b/lesson1/test_add_item_from_catalog.py index 9398e8b..c15faa3 100644 --- a/lesson1/test_add_item_from_catalog.py +++ b/lesson1/test_add_item_from_catalog.py @@ -20,4 +20,25 @@ def test_add_item_from_catalog(): actual_name = driver.find_element(By.XPATH, "//*[contains(text(),'Sauce Labs Bike Light')]").text assert expected_name == actual_name - \ No newline at end of file + + +def test_remove_good_from_cart(): + driver.get("https://www.saucedemo.com/") + # authorization + driver.find_element(By.XPATH, '//input[@data-test="username"]').send_keys("standard_user") + driver.find_element(By.XPATH, '//input[@data-test="password"]').send_keys("secret_sauce") + driver.find_element(By.XPATH, '//input[@data-test="login-button"]').click() + + # select item + # add item 1 to cart + driver.find_element(By.XPATH, '//*[@id="add-to-cart-sauce-labs-backpack"]').click() + # add item 2 to cart + driver.find_element(By.XPATH, '//*[@id="add-to-cart-sauce-labs-bolt-t-shirt"]').click() + # go to cart and check it + driver.find_element(By.CSS_SELECTOR, "a[class='shopping_cart_link']").click() + count_before_removing = driver.find_element(By.XPATH, '//*[@id="shopping_cart_container"]/a/span').text + #item_in_cart = driver.find_element(By.XPATH, '//*[@id="item_1_title_link"]/div [contains(text(), "Sauce Labs Bolt T-Shirt")]') + driver.find_element(By.XPATH, '//*[@id="remove-sauce-labs-bolt-t-shirt"]').click() + count_after_removing = driver.find_element(By.XPATH, '//*[@id="shopping_cart_container"]/a/span').text + + assert int(count_after_removing) == int(count_before_removing) - 1 \ No newline at end of file diff --git a/lesson2_my/catalog.py b/lesson2_my/catalog.py new file mode 100644 index 0000000..40c2f48 --- /dev/null +++ b/lesson2_my/catalog.py @@ -0,0 +1,53 @@ +from selenium.webdriver.common.by import By +import locators + + +def test_add_item_from_catalog(driver, login): + # add item + driver.find_element(*locators.ELEM1_ADD_FROM_CATALOG).click() + # go to cart and check it + driver.find_element(*locators.BASKET).click() + # check item in cart + expected_name = "Sauce Labs Bike Light" + actual_name = driver.find_element(By.XPATH, "//*[contains(text(),'Sauce Labs Bike Light')]").text + + assert expected_name == actual_name + + +def test_add_good_to_cart(driver, login): + # select item + text_before = driver.find_element(*locators.ELEM3_IN_CATALOG).text + + # add item to cart + driver.find_element(*locators.ELEM3_ADD_FROM_CATALOG).click() + + # go to cart and check it + driver.find_element(*locators.BASKET).click() + text_after = driver.find_element(By.CSS_SELECTOR, "a[id='item_1_title_link'] > div[class='inventory_item_name']").text + + assert text_before == text_after + + +def test_remove_good_from_cart(driver, login): + # select item + # add item 1 to cart + driver.find_element(*locators.ELEM1_ADD_FROM_CATALOG).click() + # add item 2 to cart + driver.find_element(*locators.ELEM3_ADD_FROM_CATALOG).click() + # go to cart and check it + driver.find_element(*locators.BASKET).click() + count_before_removing = driver.find_element(*locators.COUNT_ITEMS_IN_BASKET).text + driver.find_element(*locators.REMOVE_ELEM3_FROM_BASKET).click() + count_after_removing = driver.find_element(*locators.COUNT_ITEMS_IN_BASKET).text + + assert int(count_after_removing) == int(count_before_removing) - 1 + + +def test_remove_good_from_cart2(driver, login): + driver.find_element(*locators.ELEM3_ADD_FROM_CATALOG).click() + driver.find_element(*locators.BASKET).click() + driver.find_element(*locators.REMOVE_ELEM3_FROM_BASKET).click() + + assert len(driver.find_elements(*locators.ELEM3_TITLE_IN_BASKET)) == 0 + + diff --git a/lesson2_my/conftest.py b/lesson2_my/conftest.py new file mode 100644 index 0000000..2042459 --- /dev/null +++ b/lesson2_my/conftest.py @@ -0,0 +1,21 @@ +import pytest +from selenium import webdriver +from selenium.webdriver.common.by import By + + +@pytest.fixture() +def driver(): + driver = webdriver.Chrome() + driver.get("https://www.saucedemo.com/") + driver.implicitly_wait(5) + yield driver + driver.quit() + + + +@pytest.fixture() +def login(driver): + # authorization + driver.find_element(By.XPATH, '//input[@data-test="username"]').send_keys("standard_user") + driver.find_element(By.XPATH, '//input[@data-test="password"]').send_keys("secret_sauce") + driver.find_element(By.XPATH, '//input[@data-test="login-button"]').click() \ No newline at end of file diff --git a/lesson2_my/item_cart.py b/lesson2_my/item_cart.py new file mode 100644 index 0000000..c97dff9 --- /dev/null +++ b/lesson2_my/item_cart.py @@ -0,0 +1,24 @@ +import locators +from selenium.webdriver.common.by import By + + +def test_go_to_item_card_by_pic(driver, login): + # click on img + driver.find_element(*locators.ELEM4_PIC_IN_CATALOG).click() + + # check item card + item_URL = driver.current_url + expected_URL = locators.CARD_ITEM4_URL + + assert expected_URL == item_URL + + +def test_go_to_item_card_by_title(driver, login): + # click on title + driver.find_element(*locators.ELEM5_TITLE_IN_CATALOG).click() + + # check item card + expected_title = "Sauce Labs Fleece Jacket" + actual_title = driver.find_element(By.XPATH, "//*[contains(text(),'Sauce Labs Fleece Jacket')]").text + + assert expected_title == actual_title diff --git a/lesson2_my/locators.py b/lesson2_my/locators.py new file mode 100644 index 0000000..109e598 --- /dev/null +++ b/lesson2_my/locators.py @@ -0,0 +1,34 @@ +import pytest +from selenium import webdriver +from selenium.webdriver.common.by import By + + +BASE_URL = "https://www.saucedemo.com/" +INVENTORY_URL = "https://www.saucedemo.com/inventory.html" +CARD_ITEM4_URL = "https://www.saucedemo.com/inventory-item.html?id=3" + +BASKET = (By.CSS_SELECTOR, "a[class='shopping_cart_link']") +COUNT_ITEMS_IN_BASKET = (By.XPATH, '//*[@id="shopping_cart_container"]/a/span') + +ELEM1_ADD_FROM_CATALOG = (By.XPATH, '//*[@id="add-to-cart-sauce-labs-bike-light"]') +ELEM1_IN_BASKET = (By.XPATH, "//*[contains(text(),'Sauce Labs Bike Light')]") + +ELEM2_ADD_FROM_CATALOG = (By.XPATH, '//*[@id="add-to-cart-sauce-labs-backpack"]') +ELEM2_IN_BASKET = (By.XPATH, "//*[contains(text(),'Sauce Labs Backpack')]") + +ELEM3_ADD_FROM_CATALOG = (By.XPATH, '//*[@id="add-to-cart-sauce-labs-bolt-t-shirt"]') +ELEM3_IN_BASKET = (By.XPATH, "//*[contains(text(),'Sauce Labs Bolt T Shirt')]") +REMOVE_ELEM3_FROM_BASKET = (By.XPATH, '//*[@id="remove-sauce-labs-bolt-t-shirt"]') +ELEM3_TITLE_IN_BASKET = (By.ID, 'item_4_title_link') +ELEM3_IN_CATALOG = (By.CSS_SELECTOR, "a[id='item_1_title_link']") + +ELEM4_ADD_FROM_CATALOG = (By.XPATH, '//*[@id="add-to-cart-sauce-labs-fleece-jacket"]') +ELEM4_PIC_IN_CATALOG = (By.CSS_SELECTOR, "a#item_3_img_link") + +ELEM5_ADD_FROM_CATALOG = (By.XPATH, '//*[@id="add-to-cart-sauce-labs-onesie"]') +ELEM5_TITLE_IN_CATALOG = (By.CSS_SELECTOR, "#item_5_title_link > div") + +ELEM6_ADD_FROM_CATALOG = (By.XPATH, '//*[@id="add-to-cart-test.allthethings()-t-shirt-(red)"]') + +ALL_ELEM_IN_CATALOG = [ELEM1_ADD_FROM_CATALOG, ELEM2_ADD_FROM_CATALOG, ELEM3_ADD_FROM_CATALOG] + diff --git a/lesson2_my/with_param.py b/lesson2_my/with_param.py new file mode 100644 index 0000000..6834028 --- /dev/null +++ b/lesson2_my/with_param.py @@ -0,0 +1,16 @@ +import locators + + +def test_add_all_items_from_catalog(driver, login): + # add items + for a in locators.ALL_ELEM_IN_CATALOG: + driver.find_element(*a).click() + # go to cart and check it + driver.find_element(*locators.BASKET).click() + # check item in cart + driver.find_element(*locators.BASKET).click() + count_before_removing = driver.find_element(*locators.COUNT_ITEMS_IN_BASKET).text + + assert int(count_before_removing) == 3 + + From 31b9820787823f77a3fbcea013f040fd22e9ee4b Mon Sep 17 00:00:00 2001 From: Cruz06 Date: Sun, 29 Oct 2023 23:54:41 +0300 Subject: [PATCH 5/8] lesson 3 not finished --- lesson2/test_scope_example.py | 3 ++- lesson3_my/test_imp.py | 45 +++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 lesson3_my/test_imp.py diff --git a/lesson2/test_scope_example.py b/lesson2/test_scope_example.py index 4fdb07c..fa9f046 100644 --- a/lesson2/test_scope_example.py +++ b/lesson2/test_scope_example.py @@ -23,4 +23,5 @@ def test_outside_class(): # для запуска необходимо использовать # pytest -s -v --setup-show test_scope_example.py - +# параметр --setup-show показывает, как отрабатывают фикстуры, как они используются +# запустить отдельный тест из файла: pytest -s -v --setup-show test_scope_example.py::test_outside_class() diff --git a/lesson3_my/test_imp.py b/lesson3_my/test_imp.py new file mode 100644 index 0000000..af9b49e --- /dev/null +++ b/lesson3_my/test_imp.py @@ -0,0 +1,45 @@ +from selenium import webdriver +from selenium.webdriver.chrome.options import Options +from selenium.webdriver.common.by import By +from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC +import pytest +import time + + +@pytest.fixture +def chrome_options(): + options = Options() + options.add_argument('--start-maximized') + return options + + +@pytest.fixture +def driver(chrome_options): + driver = webdriver.Chrome(options=chrome_options) + return driver + + +@pytest.fixture +def wait(driver): + wait = WebDriverWait(driver, timeout=10) + return wait + + +def test_visible_after_with_explicit_waits(driver, wait): + driver.get('https://demoqa.com/dynamic-properties') + button_after_5_sec = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="enableAfter"]'))) + assert button_after_5_sec.text == "Will enable 5 seconds" + + +def test_change_color_after_5_sec(driver, wait): + # not works + driver.get('https://demoqa.com/dynamic-properties') + button_color_before = driver.find_element(By.XPATH, '//*[@id="colorChange"]') + b1 = button_color_before.value_of_css_property('font-color') + print(b1) + time.sleep(6) + button_color_after = driver.find_element(By.XPATH, '//*[@id="colorChange"]') + b2 = button_color_after.value_of_css_property('font-color') + assert b1 != b2 + From eb77b2b0a246d49993bec8208588fb13641d06a5 Mon Sep 17 00:00:00 2001 From: Cruz06 Date: Wed, 1 Nov 2023 20:41:19 +0300 Subject: [PATCH 6/8] lesson 3 filters --- lesson2_my/filter_list.py | 23 +++++++++++++++++++++++ lesson2_my/order.py | 24 ++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 lesson2_my/filter_list.py create mode 100644 lesson2_my/order.py diff --git a/lesson2_my/filter_list.py b/lesson2_my/filter_list.py new file mode 100644 index 0000000..c33138a --- /dev/null +++ b/lesson2_my/filter_list.py @@ -0,0 +1,23 @@ +from selenium.webdriver.common.by import By +from selenium.webdriver.support.ui import Select +import locators +import time + + +def test_filter_A_Z(driver, login): + # find all elements + items_before = driver.find_elements(By.CSS_SELECTOR, 'div.inventory_item_name') + list_before = [] + for a in items_before: + list_before.append(a.text) + #filter1.select_by_index(1) + #filter1.select_by_name("az") + filter1 = Select(driver.find_element(By.CSS_SELECTOR, 'select[data-test="product_sort_container"]')) + filter1.select_by_visible_text("Name (Z to A)") + itemsZA = driver.find_elements(By.CSS_SELECTOR, 'div.inventory_item_name') + list_after = [] + for a in itemsZA: + list_after.append(a.text) + assert list_before == sorted(list_after) + #, reverse=True) + diff --git a/lesson2_my/order.py b/lesson2_my/order.py new file mode 100644 index 0000000..76b37c3 --- /dev/null +++ b/lesson2_my/order.py @@ -0,0 +1,24 @@ +from selenium.webdriver.common.by import By +import locators + + +def test_make_order(driver, login): + driver.find_element(*locators.ELEM1_ADD_FROM_CATALOG).click() + driver.find_element(*locators.ELEM3_ADD_FROM_CATALOG).click() + driver.find_element(*locators.BASKET).click() + driver.find_element(By.CSS_SELECTOR, "BUTTON[data-test='checkout']").click() + #time.sleep(3) + driver.find_element(By.CSS_SELECTOR, 'input[data-test="firstName"').send_keys("Ola") + driver.find_element(By.CSS_SELECTOR, 'input[data-test="lastName"').send_keys("Cruz") + driver.find_element(By.CSS_SELECTOR, 'input[data-test="postalCode"').send_keys("99038") + driver.find_element(By.CSS_SELECTOR, 'input[data-test="continue"').click() + driver.find_element(By.CSS_SELECTOR, '[data-test="finish"]').click() + + complete = driver.find_element(By.XPATH, '//*[@id="checkout_complete_container"]/h2') + assert complete.text == "Thank you for your order!" + + # проверить отмену + # проверить что после покупки корзина пуста + + + From 09f5b2e1dff492c9f1becb7d93de5a8c0f553a38 Mon Sep 17 00:00:00 2001 From: Cruz06 Date: Thu, 2 Nov 2023 00:01:48 +0300 Subject: [PATCH 7/8] registration with waits --- lesson4_my/conftest.py | 29 +++++++++++++++++++++++++++++ lesson4_my/locators.py | 11 +++++++++++ lesson4_my/login_form.py | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 lesson4_my/conftest.py create mode 100644 lesson4_my/locators.py create mode 100644 lesson4_my/login_form.py diff --git a/lesson4_my/conftest.py b/lesson4_my/conftest.py new file mode 100644 index 0000000..3c52b18 --- /dev/null +++ b/lesson4_my/conftest.py @@ -0,0 +1,29 @@ +from faker import Faker +from selenium import webdriver +import pytest +from selenium.webdriver.chrome.options import Options +from selenium.webdriver.support.ui import WebDriverWait + +@pytest.fixture +def options(): + options = Options() + options.add_argument('--window-size=2880,1800') + return options + + +@pytest.fixture +def driver(options): + driver = webdriver.Chrome(options=options) + return driver + + +@pytest.fixture +def wait(driver): + wait = WebDriverWait(driver, timeout = 15) + return wait + + +@pytest.fixture +def random_email(): + faker = Faker() + return faker.email() \ No newline at end of file diff --git a/lesson4_my/locators.py b/lesson4_my/locators.py new file mode 100644 index 0000000..f61621d --- /dev/null +++ b/lesson4_my/locators.py @@ -0,0 +1,11 @@ +from selenium.webdriver.common.by import By + + +BASE_URL = 'https://victoretc.github.io/selenium_waits/' +START_BUTTON = (By.XPATH, "//*[@id='startTest']") +INPUT_LOGIN = (By.XPATH, "//*[@id='login']") +INPUT_PSW = (By.XPATH, "//*[@id='password']") +CHECK_BOX = (By.XPATH, "//*[@id='agree']") +CLICK_REGISTER = (By.XPATH, "//*[@id='register']") +LOADER = (By.XPATH, '//*[@id="loader"]') +MSG = (By.XPATH, '//*[@id="successMessage"]') diff --git a/lesson4_my/login_form.py b/lesson4_my/login_form.py new file mode 100644 index 0000000..6b6b3f7 --- /dev/null +++ b/lesson4_my/login_form.py @@ -0,0 +1,34 @@ +from selenium.webdriver.common.by import By +from selenium.webdriver.support import expected_conditions as EC +from locators import BASE_URL, START_BUTTON, INPUT_LOGIN, INPUT_PSW, CHECK_BOX, CLICK_REGISTER, MSG, LOADER + + +def enter_on_victor_login_form(driver, wait): + driver.get(BASE_URL) + wait.until(EC.element_to_be_clickable(START_BUTTON)).click() + expected_text = "Практика с ожиданиями в Selenium" + actual_text = driver.find_element(By.XPATH, '//h1').text + assert actual_text == expected_text + + +def test_input_login_data(driver, wait): + enter_on_victor_login_form(driver, wait) + wait.until(EC.element_to_be_clickable(INPUT_LOGIN)).send_keys("asdd@as.sa") + wait.until(EC.element_to_be_clickable(INPUT_PSW)).send_keys("5677@domn.dr") + checkbox_agree = wait.until(EC.element_to_be_clickable(CHECK_BOX)) + checkbox_agree.click() + assert checkbox_agree.is_selected() == True + + +def test_registration(driver, wait): + enter_on_victor_login_form(driver, wait) + wait.until(EC.element_to_be_clickable(INPUT_LOGIN)).send_keys("asdd@as.sa") + wait.until(EC.element_to_be_clickable(INPUT_PSW)).send_keys("5677@domn.dr") + wait.until(EC.element_to_be_clickable(CHECK_BOX)).click() + wait.until(EC.element_to_be_clickable(CLICK_REGISTER)) + wait.until(EC.visibility_of_element_located(LOADER)) + msg = wait.until(EC.visibility_of_element_located(MSG)) + expected_text = "Вы успешно зарегистрированы!" + actual_text = msg.text + assert expected_text == actual_text + From b9807743a3c5a5c864c9e5f9784e6062175be15c Mon Sep 17 00:00:00 2001 From: Cruz06 Date: Mon, 6 Nov 2023 12:46:46 +0300 Subject: [PATCH 8/8] registration with waits 0611 --- .../pom_practice/pages/registration_page.py | 4 +-- lesson4/waits_practice/conftest.py | 7 +++-- .../waits_practice/registration/conftest.py | 26 ++++++++++++++++--- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/lesson4/pom_practice/pages/registration_page.py b/lesson4/pom_practice/pages/registration_page.py index 72b871c..9cb7704 100644 --- a/lesson4/pom_practice/pages/registration_page.py +++ b/lesson4/pom_practice/pages/registration_page.py @@ -1,5 +1,5 @@ -from base.seleniumbase import BasePage -from locators import HEADER, START_TESTING_BUTTON, LOGIN_FIELD, PASSWORDD_FIELD, AGREE_CHECKBOX, REGISTRATION_BUTTON, LOADER, SUCCESS_MESSAGE +from ..base.seleniumbase import BasePage +from ..tests.registration.locators import HEADER, START_TESTING_BUTTON, LOGIN_FIELD, PASSWORDD_FIELD, AGREE_CHECKBOX, REGISTRATION_BUTTON, LOADER, SUCCESS_MESSAGE class RegistrationPage(BasePage): def header(self): diff --git a/lesson4/waits_practice/conftest.py b/lesson4/waits_practice/conftest.py index 0cc86dd..4e0cc77 100644 --- a/lesson4/waits_practice/conftest.py +++ b/lesson4/waits_practice/conftest.py @@ -7,10 +7,10 @@ def options(): options = Options() options.add_argument('--window-size=2880,1800') - return options + return options -@pytest.fixture +@pytest.fixture def driver(options): driver = webdriver.Chrome(options=options) return driver @@ -19,5 +19,4 @@ def driver(options): @pytest.fixture def wait(driver): wait = WebDriverWait(driver, timeout = 15) - return wait - + return wait diff --git a/lesson4/waits_practice/registration/conftest.py b/lesson4/waits_practice/registration/conftest.py index 15fbd70..8f70f80 100644 --- a/lesson4/waits_practice/registration/conftest.py +++ b/lesson4/waits_practice/registration/conftest.py @@ -1,5 +1,26 @@ -from faker import Faker -import pytest +from faker import Faker +from selenium import webdriver +import pytest +from selenium.webdriver.chrome.options import Options +from selenium.webdriver.support.ui import WebDriverWait + +@pytest.fixture +def options(): + options = Options() + options.add_argument('--window-size=2880,1800') + return options + + +@pytest.fixture +def driver(options): + driver = webdriver.Chrome(options=options) + return driver + + +@pytest.fixture +def wait(driver): + wait = WebDriverWait(driver, timeout = 15) + return wait @pytest.fixture @@ -7,4 +28,3 @@ def random_email(): faker = Faker() return faker.email() -