{"id":63014,"date":"2024-10-05T10:29:31","date_gmt":"2024-10-05T04:59:31","guid":{"rendered":"https:\/\/www.guvi.in\/blog\/?p=63014"},"modified":"2025-10-07T12:06:12","modified_gmt":"2025-10-07T06:36:12","slug":"toast-messages","status":"publish","type":"post","link":"https:\/\/www.guvi.in\/blog\/toast-messages\/","title":{"rendered":"Working with Toast Messages using Python Selenium and Pytest"},"content":{"rendered":"\n<p>In the intricate world of user and interface, every interaction counts. A well-designed web application is not just about aesthetics but also it is about fluid communication as well. Enter the Toast Message,&nbsp; a seemingly small element that packs a powerful punch in conveying essential information to users.<\/p>\n\n\n\n<p>A toast message is a brief non-intrusive notification that appears at the bottom or at the top of a web page for a very short duration.<\/p>\n\n\n\n<p>It is quite interesting to learn so let us now see more about toast messages in this article! <\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Introduction to Toast Messages<\/strong><\/h2>\n\n\n\n<p>Unlike traditional alert boxes, which disrupt the user&#8217;s flow, toast messages maintain focus on the current task while providing timely feedback. This simple, small yet subtle approach enhances user experience by avoiding interruptions and allowing users to continue their interactions seamlessly and hassle-free.<\/p>\n\n\n\n<p>Toast messages are versatile tools with a wide range of applications. They can confirm successful actions such as saving a document or submitting a form. They can provide subtle guidance, like suggesting alternative options or prompting for further actions. <\/p>\n\n\n\n<p>Additionally, toasts can inform users about errors or unexpected events without causing alarm. By delivering concise and relevant information, toasts empower users to make informed decisions and progress efficiently.&nbsp;<\/p>\n\n\n\n<p>The effectiveness of toast messages lies in their brevity and clarity. A well-crafted toast conveys its message quickly and disappears before becoming intrusive. The design should complement the overall aesthetic of the application, ensuring consistency and visual appeal. <\/p>\n\n\n\n<p>Furthermore, the placement of the toast message is crucial. Displaying it at the bottom of the screen allows users to focus on the main content while still being aware of the notification.&nbsp;&nbsp;<\/p>\n\n\n\n<p>In conclusion, toast messages are indispensable components of modern web applications. Their ability to provide timely feedback without disrupting the user experience makes them invaluable for enhancing usability and user satisfaction. <\/p>\n\n\n\n<p>By understanding the nuances of toast message design and implementation, developers can create applications that are both efficient and enjoyable for users.&nbsp;&nbsp;<\/p>\n\n\n\n<p>Would you like to know how to implement toast messages in a specific web development framework?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Prerequisites<\/strong><\/h2>\n\n\n\n<p>For carrying out the automation testing using <a href=\"https:\/\/www.guvi.in\/hub\/python\/\" target=\"_blank\" rel=\"noreferrer noopener\">Python<\/a> for Toast Messages, we need a web application. For this example, I am using the demo URL <a href=\"https:\/\/sumantoastmessage.netlify.app\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">https:\/\/sumantoastmessage.netlify.app\/<\/a>&nbsp;<\/p>\n\n\n\n<p>We need&nbsp; PyCharm, VSCode, or any other code editor for the same.<\/p>\n\n\n\n<p>Also, for the project we need to have Python Selenium, Python WebDriver Manager, and Pytest framework installed on our machine.<\/p>\n\n\n\n<p>The project structure will comprise of&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXfPYUibgXbHYI4_8FqYShdzdj73F1p9-UD765P-NskMnQK9q9NOyFt-gQdBIrCTSNxeRyfqPkvn7jMfruEreqWWaTFDv6eP6yjSbuPGs9uzeiotleKI8WGJzBfuq-aNQl4x7wiRZyUiHyeJgo2dnTVHMzMs?key=ciaKDnIKmdUqmT2y2CWvpw\" alt=\"Prerequisites for toast message\" title=\"\"><\/figure>\n\n\n\n<p><strong>PageObjects : <\/strong>This folder will contain all the automation code inside the Python file \u201ctoast_messages.py\u201d<\/p>\n\n\n\n<p><strong>TestCodes : <\/strong>This folder will contain the Pytest file \u201ctest_toast_messages.py\u201d to generate the Pytest test-case report in <a href=\"https:\/\/www.guvi.in\/blog\/html-tutorial-guide-for-web-development\/\" target=\"_blank\" rel=\"noreferrer noopener\">HTML format<\/a>.<\/p>\n\n\n\n<p><strong>Reports :<\/strong> This will contain the Pytest-generated HTML test-case report<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>The Automation Testing Code<\/strong><\/h2>\n\n\n\n<p>Since we have created our project structure we are going to start coding for your project. To do so, we will write our <a href=\"https:\/\/www.guvi.in\/blog\/selenium-essentials\/\" target=\"_blank\" rel=\"noreferrer noopener\">Python Selenium<\/a> automation scripts in the \u201ctoast_messages.py\u201d. Let us now see the code and understand it in detail:-&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"\"\"\"\nFile Name : toast_messages.py\n\n\nProgram : Capturing &amp; Generating Toast Messages using Python Selenium &amp; Pytest\n\"\"\"\n\n\n\n\nfrom selenium import webdriver\nfrom webdriver_manager.chrome import ChromeDriverManager\nfrom selenium.webdriver.common.by import By\nfrom selenium.webdriver.chrome.service import Service\n\n\n# Python Class to hold the Locators\nclass Toast_Locators:\n    success_toast_message = \"\/\/div&#91;@class='column']\/span&#91;text()='Success: This is a success toast.']\"\n    error_toast_message = \"\/\/div&#91;@class='column']\/\/span&#91;text()='Error: This is an error toast.']\"  \n\n\n# Python Class for Automation Testing\nclass Suman_Toast_Messages(Toast_Locators):  \n   \n\n\n    def __init__(self, url):\n        self.url = url\n        self.driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))\n\n\n    # Click the Success Button\n    def click_button(self):\n        self.driver.maximize_window()\n        self.driver.get(self.url)\n        self.driver.implicitly_wait(10)\n        self.driver.find_element(by=By.ID, value=\"success\").click()\n        return True\n\n\n    # Verify whether the success toast message is visible or not\n    def toast_visible(self):\n        if self.driver.find_element(by=By.XPATH, value=self.success_toast_message).is_displayed():\n            return True\n        else:\n            return False\n\n\n    # Extract the Success Toast Message Text using Python Selenium\n    def extract_toast_text(self):\n        if self.toast_visible():\n            text_message = self.driver.find_element(by=By.XPATH, value=self.success_toast_message).text\n            print(text_message)\n            return text_message\n        else:\n            print(\"ERROR : Toast Message not found !\")\n<\/code><\/pre>\n\n\n\n<p>The entire code has been written using Python\u2019s Object Oriented Programming Methodology (OOPS). The need to use the OOPS is so that we cannot only make use of the Page Object Model (POM) but also make our automation testing reusable.<\/p>\n\n\n\n<p>Import the <a href=\"https:\/\/www.guvi.in\/blog\/guide-for-essential-modules-in-python\/\">Python modu<\/a><a href=\"https:\/\/www.guvi.in\/blog\/guide-for-essential-modules-in-python\/\" target=\"_blank\" rel=\"noreferrer noopener\">l<\/a><a href=\"https:\/\/www.guvi.in\/blog\/guide-for-essential-modules-in-python\/\">es<\/a> like Selenium, WebDriver Manager, By and Service into the code. Here, we are using the Python Selenium WebDriver Manager to manage our WebDriver so that the driver can be downloaded and installed by the Python itself.<\/p>\n\n\n\n<p>Create a <a href=\"https:\/\/www.guvi.in\/courses\/programming\/python\/?utm_source=blog&amp;utm_medium=hyperlink&amp;utm_campaign=toast-message\" target=\"_blank\" rel=\"noreferrer noopener\">Python<\/a> Class called \u201cToast_Locators\u201d which will handle all the XPATHs necessary for your automation.<\/p>\n\n\n\n<p>Now, create the main Python Class \u201cSuman_Toast_Messages\u201d which will inherit the \u201cToast_Locator\u201d class. In this class, we will create a constructor method which will have the URL of the web application and the driver object as well.<\/p>\n\n\n\n<p>Now, the \u201cclick_button\u201d method will maximize the Chrome browser window, run the web application using the implicit wait and will find the \u201cSuccess\u201d button and click it. The method code will return True.<\/p>\n\n\n\n<p>The \u201ctoast_visible\u201d method will return True when the \u201cSuccess Message\u201d is displayed on the screen, else it will return False.<\/p>\n\n\n\n<p>The \u201cextract_toast_message\u201d will return a text message and will extract the text else it will throw an error message on the console.<\/p>\n\n\n\n<p>Now, when the automation code is ready it is time to write our Pytest Test-Cases. The code is given as below:-&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"\"\"\nFile Name : test_toast_message.py\n\n\nProgram : Pytest program to validate the Toast Message automation\n\"\"\"\n\n\nimport pytest\nfrom PageObjects.toast_messages import Suman_Toast_Messages\n\n\nurl = \"https:\/\/sumantoastmessage.netlify.app\/\"\nsuman = Suman_Toast_Messages(url)\n\n\n\n\ndef test_click_button():\n    assert suman.click_button() == True\n    print(\"SUCCESS : Automation started\")\n\n\n\n\ndef test_toast_visible():\n    assert suman.toast_visible() == True\n    print(\"SUCCESS : success toast message is visible\")\n\n\n\n\ndef test_extract_toast_text():\n    assert suman.extract_toast_text() == \"Success: This is a success toast.\"\n<\/code><\/pre>\n\n\n\n<p>In the following code, we have to import the PageObjects module into our Pytest file. We have three test cases which are as follows:-&nbsp;<\/p>\n\n\n\n<p>Test_click_button : This test case will assert and validate the click done on the \u201cSuccess\u201d button.<\/p>\n\n\n\n<p>Test_Toast_Visisble : This test case will assert and validate the visibility of the \u201cSuccess\u201d message.<\/p>\n\n\n\n<p>Test_Extract_Toast_Text : This test case will assert and validate the extracted text from the web application and validate with the user-given input.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>The Output<\/strong><\/h2>\n\n\n\n<p>When you will run the Pytest file \u201ctest_toast_messages.py\u201d which is present inside the \u201cTestCases\u201d folder using the command <em>pytest -v -s &#8211;capture=sys &#8211;html=..\/Reports\/toast_messages.html test_toast_messages.py<\/em><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXdkI3PvfIyI19y7dVduFcHPFj72S3dWxiE0VBGbciLlBrDaL0mczImVOGNtnvOvVmYHlMv7fSbTFWOVsrP6SwvNMz9eI1M9Empubts3iN0HMviu6VUJcQvQgGekBulazoqMqif3JC6A1X2gw8jEgVpToaT3?key=ciaKDnIKmdUqmT2y2CWvpw\" alt=\"Output of toast message\" title=\"\"><\/figure>\n\n\n\n<p>You will see a Test-Case report in HTML format has been created inside the Reports folder.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXecKuEyxzh4xBurUg5lraHRjrtBApSSPg-epkZElfqMLAVML_NNLN_l_hGE59gfrgFV1Sm4TGUE_UpvJKlumasd_SMX127TuZtUMPjtk4smAmpBXkq3u7raS0oA_FZcqM_lqiySVXiOZd6cC4xuli-T4rw?key=ciaKDnIKmdUqmT2y2CWvpw\" alt=\"Test-Case report in HTML format\" title=\"\"><\/figure>\n\n\n\n<p>The detailed Pytest report is given as below<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXcsMj7U7kPpuUJgASfoS8XMhpzjF1xTh4uxhY7Dg3LCMJmpCaO-1493bH2JtioYtdyQjqVm3U6e8G8VWfDRoyr0fh9icaBPKG_MORwgDl9ahB7FzSVneuB0UvVAG2qqA55ZCdLMoNakNVLJTjXHIdaylSMG?key=ciaKDnIKmdUqmT2y2CWvpw\" alt=\"Pytest report \" title=\"\"><\/figure>\n\n\n\n<p>In case you want to learn more about automation testing with Selenium, consider enrolling for HCL GUVI&#8217;s certified <a href=\"https:\/\/www.guvi.in\/zen-class\/selenium-automation-testing-course\/\" data-type=\"link\" data-id=\"https:\/\/www.guvi.in\/zen-class\/selenium-automation-testing-course\/\" target=\"_blank\" rel=\"noreferrer noopener\">Selenium Automation Testing Course<\/a> online course that teaches you everything from scratch and also provides you with an industry-grade certificate!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>In conclusion, we can see that working with toast messages and automating and testing them is important work for any automation tester and with the help of Python, the entire process is super easy and hassle-free.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the intricate world of user and interface, every interaction counts. A well-designed web application is not just about aesthetics but also it is about fluid communication as well. Enter the Toast Message,&nbsp; a seemingly small element that packs a powerful punch in conveying essential information to users. A toast message is a brief non-intrusive [&hellip;]<\/p>\n","protected":false},"author":38,"featured_media":64243,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40,717],"tags":[],"views":"6211","authorinfo":{"name":"Suman Gangopadhyay","url":"https:\/\/www.guvi.in\/blog\/author\/suman-gangopadhyay\/"},"thumbnailURL":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/10\/Working-with-Toast-Messages-using-Python-Selenium-and-Pytest-300x116.png","jetpack_featured_media_url":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/10\/Working-with-Toast-Messages-using-Python-Selenium-and-Pytest.png","_links":{"self":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/63014"}],"collection":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/users\/38"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/comments?post=63014"}],"version-history":[{"count":22,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/63014\/revisions"}],"predecessor-version":[{"id":88911,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/63014\/revisions\/88911"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media\/64243"}],"wp:attachment":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media?parent=63014"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/categories?post=63014"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/tags?post=63014"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}