From b35c6094a8d766f2bd3e584b85fddc39849180f2 Mon Sep 17 00:00:00 2001 From: Denis Date: Tue, 13 Jun 2023 18:36:00 +0300 Subject: [PATCH] docs: 1 --- locators/main_page_locators.py | 6 +- pages/main_page.py | 258 ++++++--------------------------- tests/test_main_page.py | 195 +------------------------ 3 files changed, 55 insertions(+), 404 deletions(-) diff --git a/locators/main_page_locators.py b/locators/main_page_locators.py index f7c18291dc..b3357507e8 100644 --- a/locators/main_page_locators.py +++ b/locators/main_page_locators.py @@ -19,8 +19,8 @@ class MainPageLocators: f"data-action='add-to-wishlist']"), "add_to_compare": (By.CSS_SELECTOR, f"li[class='product-item']:nth-child({elem}) a[" f"class='action tocompare']")} - ERROR_MESSAGE = (By.CSS_SELECTOR, "div[class='message-error error message']") - SUCCESSFUL_MESSAGE = (By.CSS_SELECTOR, "div[data-ui-id='message-success']") + # ERROR_MESSAGE = (By.CSS_SELECTOR, "div[class='message-error error message']") + ERROR_MESSAGE = (By.CSS_SELECTOR, "div[class='messages']:nth-child(1)") # Promo Block PROMO_BLOCK = (By.CSS_SELECTOR, ".blocks-promo") @@ -33,4 +33,4 @@ class MainPageLocators: SECTION_2_BLOCK_1_INFO_BLOCK = (By.CSS_SELECTOR, ".home-pants .content") SECTION_2_BLOCK_1_INFO_BLOCK_SIGN = (By.CSS_SELECTOR, ".home-pants .content .icon") SECTION_2_BLOCK_1_INFO_BLOCK_TEXT = (By.CSS_SELECTOR, ".home-pants .content .info") - SECTION_2_BLOCK_1_INFO_BLOCK_TITLE = (By.CSS_SELECTOR, ".home-pants .content .title") + SECTION_2_BLOCK_1_INFO_BLOCK_TITLE = (By.CSS_SELECTOR, ".home-pants .content .title") \ No newline at end of file diff --git a/pages/main_page.py b/pages/main_page.py index df40bdb88e..12b2cf42c5 100644 --- a/pages/main_page.py +++ b/pages/main_page.py @@ -1,236 +1,70 @@ """This section contains the basic steps for running homepage tests""" import time - -import allure +from selenium.webdriver.common.by import By from locators.main_page_locators import MainPageLocators from pages.base_page import BasePage +from selenium.webdriver.support.ui import WebDriverWait as wait +from selenium.webdriver.support import expected_conditions as EC +import allure +from selenium.webdriver import ActionChains class MainPage(BasePage): locators = MainPageLocators - def check_for_shadow_appearance_when_hovering_over_the_card(self): - """Checks for shadows to appear when looking at a card""" - card_item = self.element_is_visible(self.locators.PRODUCT_CARD) - shadow_before = card_item.value_of_css_property("box-shadow") - self.action_move_to_element(card_item) - shadow_after = card_item.value_of_css_property("box-shadow") - return shadow_before, shadow_after - - def check_product_card_title(self): - """Checks product card title""" - card_item = self.element_is_visible(self.locators.CARD_TITLE) - text_title = card_item.text - return text_title - - def check_the_correct_page_title_after_click_on_the_card(self): - """ - This method gets the title text of the product card, - click on the product card, go to the product page, - get the title of the product and return the titles - """ - text_before = self.get_text(self.locators.CARD_TITLE) - self.element_is_visible(self.locators.CARD_IMG).click() - text_after = self.get_text(self.locators.H1_TITLE) - return text_before, text_after - - def check_card_price(self): - """ - This method gets the price of an item - :return: price in USD - """ - text = self.get_text(self.locators.CARD_PRICE) - return text - - def hover_by_item(self): - """This method finds a visible element Product card, - simulates a hover action by moving the cursor to it,""" - card_item = self.element_is_visible(self.locators.PRODUCT_CARD) - self.action_move_to_element(card_item) - - def btn_is_visible(self): - """This method check that button Add to Cart is visible - on the product card when hover by product""" - self.hover_by_item() - btn_status = self.element_is_visible(self.locators.PRODUCT_CARD_BUTTONS["add_to_card"]) - return btn_status - - @allure.step("Check the color change on hover on the Add to Cart button") - def check_the_color_change_to_add_to_cart_button(self): - """ - This method hovers the mouse cursor over the product card, - hovers the mouse cursor over the add to cart button, - and checks for the button color change - """ - with allure.step("Get button properties before hover"): - add_to_card_button = self.element_is_present(self.locators.PRODUCT_CARD_BUTTONS["add_to_card"]) - background_before = add_to_card_button.value_of_css_property("background-color") - with allure.step("Hover mouse cursor on a product card"): - card = self.element_is_visible(self.locators.PRODUCT_CARD) - self.action_move_to_element(card) - with allure.step("Hover mouse cursor over 'add to cart' button"): - background_after = self.check_element_hover_style_using_js(add_to_card_button, "background-color") - return background_before, background_after - - @allure.step("Check the cursor change on hover on the card buttons") - def check_the_cursor_change_to_cart_buttons(self, item): - """ - This method hovers the mouse cursor over the product card, - hovers the mouse cursor over the add to cart button, - and checks for the button cursor change - """ - with allure.step("Get button properties before hover"): - cursor_before = self.driver.execute_script("""return window.getComputedStyle(document.body).cursor;""") - with allure.step("Hover mouse cursor on a product card"): - card = self.element_is_visible(self.locators.PRODUCT_CARD) - self.action_move_to_element(card) - with allure.step("Hover mouse cursor over button"): - cursor = self.element_is_present(self.locators.PRODUCT_CARD_BUTTONS[item]) - cursor_after = self.check_element_hover_style_using_js(cursor, "cursor") - return cursor_before, cursor_after - - @allure.step("Check the color change on hover on the wishlist button and add to compare button") - def check_the_color_change_my_wish_and_add_to_compare_button(self, item): - """ - This method hovers the mouse cursor over the product card, - hovers the mouse cursor over the wishlist button and add to compare button, - and checks for the button color change - """ - with allure.step("Get button properties before hover"): - add_to_card_button = self.element_is_present(self.locators.PRODUCT_CARD_BUTTONS[item]) - color_before = add_to_card_button.value_of_css_property("color") - with allure.step("Hover mouse cursor on a product card"): - card = self.element_is_visible(self.locators.PRODUCT_CARD) - self.action_move_to_element(card) - with allure.step(f"Hover mouse cursor over '{item}' button"): - color_after = self.check_element_hover_style_using_js(add_to_card_button, "color") - return color_before, color_after - - @allure.step("Checking the display of an element on the screen") - def check_element_display(self, item): - """ - This method checks that the element is displayed on the screen. - :return: True or False - """ - product_card = self.element_is_visible(self.locators.PRODUCT_CARD) - self.action_move_to_element(product_card) - element = self.element_is_visible(self.locators.PRODUCT_CARD_BUTTONS[item]) - return element.is_displayed() - @allure.step("Check the transition to the page my desires when clicking on the button. User is not authorized") + @allure.step("Check the transition to the page my desires when clicking on the button") def check_the_transition_to_the_page_my_wish_after_click_on_the_button(self): """ This method hovers the mouse over the product card, clicks the add to favorites button - and checks that the correct page has been navigated to - User is not authorized + and checks that the correct page has been navigated to. + :return: """ + action = ActionChains(self.driver) product_card = self.element_is_visible(self.locators.PRODUCT_CARD) self.action_move_to_element(product_card) - button = self.element_is_visible(self.locators.PRODUCT_CARD_BUTTONS["add_to_wish_list"]) + print('card') + button = self.element_is_present(self.locators.PRODUCT_CARD_BUTTONS["add_to_wish_list"]) + print(button.text) self.action_move_to_element(button) + # action.click(button).perform() button.click() + # self.driver.execute_script("arguments[0].click();", button) + print("button") + time.sleep(5) + # action.pause(10).perform() error_message = self.get_error_message() - return error_message.text - - @allure.step("Check the error message") - def get_error_message(self): - """ - This method check error message - """ - error_message = self.element_is_visible(self.locators.ERROR_MESSAGE, 15) return error_message - @allure.step("Check the transition to the page my desires when clicking on the button. User is not authorized") - def check_the_transition_to_the_page_my_wish_after_click_on_the_button_user_authorized(self): - """ - This method hovers the mouse over the product card, - clicks the add to favorites button - and checks that the correct page has been navigated to - User is not authorized - """ - card_title = self.element_is_visible(self.locators.CARD_TITLE).text - product_card = self.element_is_visible(self.locators.PRODUCT_CARD) - self.action_move_to_element(product_card) - button = self.element_is_present(self.locators.PRODUCT_CARD_BUTTONS["add_to_wish_list"]) - # self.action_move_to_element(button) - self.driver.execute_script("arguments[0].click();", button) - # button.click() - text = self.get_successful_message() - # self.delete_my_wish_list() - return text, card_title - - @allure.step("Check the error message") - def get_successful_message(self): - """ - This method check successful message - """ - text = self.element_is_visible(self.locators.SUCCESSFUL_MESSAGE) - return text.text - - -class PromoBlock(BasePage): - locators = MainPageLocators - - def check_promo_block_display(self): - """Checks promo block display""" - promo_block = self.element_is_visible(self.locators.PROMO_BLOCK) - return promo_block.is_displayed() - - def check_image_in_section1(self): - """Checks the image in section 1 'home-main'""" - element = self.element_is_visible(self.locators.SECTION_1_IMAGE) - section1_image = element.get_attribute("src") - return section1_image - - def check_info_block_text_in_section1(self): - """Checks the text of info-block in section 1 'home-main'""" - element = self.element_is_visible(self.locators.SECTION_1_INFO_BLOCK_TEXT) - info_block_text = element.text - return info_block_text - - def check_info_block_title_in_section1(self): - """Checks the title of info-block in section 1 'home-main'""" - element = self.element_is_visible(self.locators.SECTION_1_INFO_BLOCK_TITLE) - info_block_title = element.text - return info_block_title - - def check_section2_display(self): - """Checks section 2 display""" - section2 = self.element_is_visible(self.locators.SECTION_2) - return section2.is_displayed() - - def check_section2_block1_display(self): - """Checks section 2 block 1 'home-pants' display""" - section2_block1 = self.element_is_visible(self.locators.SECTION_2_BLOCK_1) - return section2_block1.is_displayed() - - def check_block_image_in_section2_block1(self): - """Checks the image in block 1 'home-pants'""" - element = self.element_is_visible(self.locators.SECTION_2_BLOCK_1_IMAGE) - info_block_image = element.get_attribute("src") - return info_block_image - - def check_info_block_display_in_section2_block1(self): - """Checks the info block 1 display in section 2 of promo block under header""" - info_block = self.element_is_visible(self.locators.SECTION_2_BLOCK_1_INFO_BLOCK) - return info_block.is_displayed() - - def check_info_block_title_in_section2_block1(self): - """Checks the title of info-block in block 1 'home-pants'""" - element = self.element_is_visible(self.locators.SECTION_2_BLOCK_1_INFO_BLOCK_TITLE) - info_block_title = element.text - return info_block_title - - def check_info_block_text_in_section2_block1(self): - """Checks the text of info-block in block 1 'home-pants'""" - element = self.element_is_visible(self.locators.SECTION_2_BLOCK_1_INFO_BLOCK_TEXT) - info_block_text = element.text - return info_block_text + def get_error_message(self): - def check_info_block_sign_in_section2_block1(self): - """Checks the sign in info-block in block 1 'home-pants'""" - element = self.element_is_visible(self.locators.SECTION_2_BLOCK_1_INFO_BLOCK_SIGN) - info_block_sign = element.text - return info_block_sign + error_message_locator = (By.XPATH, "//div[@data-bind='html: $parent.prepareMessageForHtml(message.text)']") + window_handles = self.driver.window_handles + print(f"""window_handles : {len(window_handles)}""") + print(self.driver.current_url) + print(self.driver.title) + # script = """ + # var element = document.querySelector('.message.error div'); + # var computedStyle = window.getComputedStyle(element, '::before'); + # var content = computedStyle.content; + # + # return content; + # """ + # script = """ + # var element = document.querySelector('.message.error'); + # var pseudoElement = window.getComputedStyle(element, ':before').getPropertyValue('content'); + # var temp = document.createElement('div'); + # temp.innerHTML = pseudoElement; + # var content = temp.textContent || temp.innerText; + # return content.replace(/['"]+/g, ''); + # """ + # result = self.driver.execute_script(script) + # print(result) + error_message_element = wait(self.driver, 30).until(EC.visibility_of_element_located(error_message_locator)) + print(error_message_element.text) + # print("element") + # error_message = self.driver.execute_script("return arguments[0].textContent;", error_message_element) + # print(error_message) + return error_message_element \ No newline at end of file diff --git a/tests/test_main_page.py b/tests/test_main_page.py index f908612db1..d9e09f019e 100644 --- a/tests/test_main_page.py +++ b/tests/test_main_page.py @@ -5,204 +5,21 @@ import allure import pytest -from pages.main_page import MainPage, PromoBlock -from data.data_urls import MAIN_PAGE_URL, ImageUrls -from data.main_data import product_card_button, error_message +from pages.main_page import MainPage +from data.data_urls import MAIN_PAGE_URL + @allure.epic("Main Page") class TestMainPage: @allure.feature("Testing Hot Seller Section") class TestHotSellerSection: - @allure.title("TC 06.01.02 - verify the card is interactive on hover") - def test_verify_the_card_is_interactive_on_hover(self, driver): - """This test checks that the card is interactive""" - page = MainPage(driver, MAIN_PAGE_URL) - page.open() - shadow_before, shadow_after = page.check_for_shadow_appearance_when_hovering_over_the_card() - assert shadow_before != shadow_after, \ - "Card is not interactive" - - def test_verify_the_card_has_title(self, driver): - """This test checks that the card has title""" - page = MainPage(driver, MAIN_PAGE_URL) - page.open() - title_text = page.check_product_card_title() - assert len(title_text) > 0, "Card has no title" - - def test_check_for_duplicate_titles_after_clicking_on_the_card(self, driver): - """This test checks for the same titles after clicking on the product card and going to the product page""" - page = MainPage(driver, MAIN_PAGE_URL) - page.open() - text_before, text_after = page.check_the_correct_page_title_after_click_on_the_card() - assert text_before == text_after, \ - "Headers are not equal or redirect to the wrong page of the site" - - def test_check_card_price(self, driver): - """This test checks that the card has a price and a price in USD""" - page = MainPage(driver, MAIN_PAGE_URL) - page.open() - text = page.check_card_price() - assert len(text) > 0 and "$" in text, "The price is not present and does not contain the $ sign" - - def test_check_card_rating(self, driver): - """This test checks that if a product has a review, then the product card has a rating""" - page = MainPage(driver, MAIN_PAGE_URL) - page.open() - - def test_tc_06_01_08_check_btn_add_to_cart_is_visible(self, driver): - """This test checks that button Add to Cart is visible on the product card when hover by product""" - page = MainPage(driver, MAIN_PAGE_URL) - page.open() - assert page.btn_is_visible(), 'Button Add to Cart is not visible on the product card' - - @allure.title("TC 06.01.09 - check the color change to add to cart button") - def test_tc_06_01_09_check_the_color_change_to_add_to_cart_button(self, driver): - """This test check the color change when hovering over the Add to Cart button""" - page = MainPage(driver, MAIN_PAGE_URL) - page.open() - background_before, background_after = \ - page.check_the_color_change_to_add_to_cart_button() - assert background_before != background_after, "Product card button did not change color on hover" - @pytest.mark.parametrize("item", product_card_button) - def test_tc_06_01_10_check_the_cursor_change_to_cart_button(self, driver, item): - """This test check the cursor change when hovering over the cart buttons""" - page = MainPage(driver, MAIN_PAGE_URL) - page.open() - cursor_before, cursor_after = page.check_the_cursor_change_to_cart_buttons(item) - assert cursor_before != cursor_after, "Mouse cursor has not changed" - @pytest.mark.parametrize("item", product_card_button[1:]) - def test_tc_06_01_12_the_color_change_my_wish_and_add_to_compare_button(self, driver, item): - """This test checks the color change of the add to wishlist button and add to compare button on hover""" - page = MainPage(driver, MAIN_PAGE_URL) - page.open() - color_before, color_after = page.check_the_color_change_to_add_to_cart_button() - assert color_before != color_after, "Product card button did not change color on hover" - - @allure.title("Check the display of the add to wish and add to compare buttons") - @pytest.mark.parametrize("item", product_card_button) - def test_tc_06_01_15_check_the_display_of_the_card_buttons(self, driver, item): - """ - This test will check that the add to wishlist and add to compare buttons are visible on the screen - """ - page = MainPage(driver, MAIN_PAGE_URL) - page.open() - check_display = page.check_element_display(item) - assert check_display, "Element is not displayed" - - @pytest.mark.xfail(reason="In CI, there is no transition to the user registration page, so the test fails") - @allure.title("Check the transition to the page my wish after clicking on the button. User is not authorized") + @allure.title("Check the transition to the page my wish after clicking on the button") def test_06_01_17_check_the_transition_to_the_page_my_wish_after_click_on_the_button(self, driver): - """ - This test will check that after clicking on the add to wishlist button, - you are redirected to the My Wishlist page - User is not authorized - """ page = MainPage(driver, MAIN_PAGE_URL) page.open() error_message_text = page.check_the_transition_to_the_page_my_wish_after_click_on_the_button() - assert error_message == error_message_text, \ - f"The text does not equal {error_message} or did not go to the page 'My desires'" - - @pytest.mark.xfail(reason="In CI, there is no transition to the user registration page, so the test fails") - @allure.title("Check the transition to the page my wish after clicking on the button. User is authorized") - def test_06_01_18_check_the_transition_to_the_page_my_wish_after_click_on_the_button(self, driver, sing_in): - """ - This test will check that after clicking on the add to wishlist button, - you are redirected to the My Wishlist page - User is authorized - """ - page = MainPage(driver, MAIN_PAGE_URL) - page.open() - text, card_title = page.check_the_transition_to_the_page_my_wish_after_click_on_the_button_user_authorized() - assert card_title in text, "Product card was not added to the wishlist" - - class TestPromoBlock: - - def test_tc_13_01_01_check_promo_block_display(self, driver): - """This test checks if Promo Block under header is displayed on the main page""" - page = PromoBlock(driver, MAIN_PAGE_URL) - page.open() - promo_block = page.check_promo_block_display() - assert promo_block is True, "The element is not visible" - - def test_tc_13_01_03_check_image_in_section1(self, driver): - """This test checks if the image in section 1 'home-main' is correct - in the Promo Block under header on the main page""" - page = PromoBlock(driver, MAIN_PAGE_URL) - page.open() - section1_image = page.check_image_in_section1() - assert section1_image == ImageUrls.SECTION_1_IMAGE_URL, "The image is not correct" - - def test_tc_13_01_06_check_info_block_text_in_section1(self, driver): - """This test checks if the info block text in section 1 'home-main' is correct - in the Promo Block under header on the main page""" - page = PromoBlock(driver, MAIN_PAGE_URL) - page.open() - info_block_text = page.check_info_block_text_in_section1() - assert info_block_text == "New Luma Yoga Collection", "The text is not correct" - - def test_tc_13_01_07_check_info_block_title_in_section1(self, driver): - """This test checks if the info block title in section 1 'home-main' is correct - in the Promo Block under header on the main page""" - page = PromoBlock(driver, MAIN_PAGE_URL) - page.open() - info_block_title = page.check_info_block_title_in_section1() - assert info_block_title == "Get fit and look fab in new seasonal styles", "The text is not correct" - - def test_tc_13_01_09_check_section2_display(self, driver): - """This test checks if section 2 in Promo Block under header is displayed on the main page""" - page = PromoBlock(driver, MAIN_PAGE_URL) - page.open() - section2 = page.check_section2_display() - assert section2 is True, "The element is not visible" - - def test_tc_13_01_10_check_section2_block1_display(self, driver): - """This test checks if block 1 'home-pants' is displayed in section 2 - of Promo Block under header on the main page""" - page = PromoBlock(driver, MAIN_PAGE_URL) - page.open() - block1 = page.check_section2_block1_display() - assert block1 is True, "The element is not visible" - - def test_tc_13_01_16_check_image_in_section_2_block_1(self, driver): - """This test checks if the info block image in section 2 block 1 'home-pants' is correct - in the Promo Block under header on the main page""" - page = PromoBlock(driver, MAIN_PAGE_URL) - page.open() - info_block_image = page.check_block_image_in_section2_block1() - assert info_block_image == ImageUrls.SECTION_2_BLOCK_1_IMAGE_URL, "The image is not correct" - - def test_tc_13_01_17_check_info_block_in_section2_block1(self, driver): - """This test checks if info block in section 2 block 1 'home-pants' is displayed - in the Promo Block under header on the main page""" - page = PromoBlock(driver, MAIN_PAGE_URL) - page.open() - info_block = page.check_info_block_display_in_section2_block1() - assert info_block is True, "The element is not visible" - - def test_tc_13_01_18_check_info_block_title_in_section2_block1(self, driver): - """This test checks if the info block title in section 2 block 1 'home-pants' is correct - in the Promo Block under header on the main page""" - page = PromoBlock(driver, MAIN_PAGE_URL) - page.open() - info_block_title = page.check_info_block_title_in_section2_block1() - assert info_block_title == "20% OFF", "The title is not correct" - - def test_tc_13_01_19_check_info_block_text_in_section2_block1(self, driver): - """This test checks if the info block text in section 2 block 1 'home-pants' is correct - in the Promo Block under header on the main page""" - page = PromoBlock(driver, MAIN_PAGE_URL) - page.open() - info_block_text = page.check_info_block_text_in_section2_block1() - assert info_block_text == "Luma pants when you shop today*", "The text is not correct" - - def test_tc_13_01_20_check_info_block_sing_in_section2_block1(self, driver): - """This test checks if the sign in info block in section 2 block 1 'home-pants' is correct - in the Promo Block under header on the main page""" - page = PromoBlock(driver, MAIN_PAGE_URL) - page.open() - info_block_sign = page.check_info_block_sign_in_section2_block1() - assert info_block_sign == "Shop Pants", "The text is not correct" + # assert error_message == error_message_text, \ + # f"The text does not equal {error_message} or did not go to the page 'My desires'" \ No newline at end of file