Handling alerts in Selenium Webdriver | EngineerDiaries

Jan 20, 2021, 1313 Views, 0

Contents

  • Alerts, Confirmations, and Prompts Handling
  • Alerts Handling in Selenium Webdriver
  • Confirmation Handling in Selenium Webdriver
  • Prompts Handling in Selenium Webdriver
  • Summary
  • 1. Alerts, Confirmations, and Prompts Handling

    While testing the UI of a web application in a browser, one needs to be able to handle all the elements and events, and alerts, prompts, and confirmations play a crucial part in a site. Now alerts are display messages with OK button as a way to close the dialog box. Confirmations on the other provide a feature to accept or deny the display message. And ultimately Prompts are more interactive as they are forms where you can enter data also. Now, when we talk about handling them using a selenium webdriver we need to know the text and be able to interact in case of prompts and confirmation. That is what we are going to learn below handling alerts, prompts, and confirmations using Selenium Webdriver in C#. Also, we would be making use of Explicit wait and ExpectedConditions about which we talked in detail here. Below picture represents a test case with three buttons for alerts, confirmations and prompts respectively and their output too is displayed.
    Handling alerts in Selenium Webdriver | EngineerDiaries

    Handling alerts in Selenium Webdriver | EngineerDiaries

    Handling alerts in Selenium Webdriver | EngineerDiaries

    Handling alerts in Selenium Webdriver | EngineerDiaries

    2. Alerts Handling in Selenium Webdriver

    There is no much space to play around alerts specifically than getting the text. In alert accept() and dismiss(), both just close the dialog box. As above we created a test site with url "http://localhost:4200/test". Also SeleniumExtras WaitHelpers has to be added separately through nuget package manager. Here we are using explicit wait where the wait condition is applied on the element till the max time is reach, which in our case we have set for 30secs. So, lets see the self-explanatory code here.
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using WebApplication1.Models;
    using OpenQA.Selenium;
    using OpenQA.Selenium.Chrome;
    using OpenQA.Selenium.Remote;
    using OpenQA.Selenium.Support.UI;
    using SeleniumExtras.WaitHelpers;

    namespace WebApplication1.Controllers
    {
        public class HomeController : Controller
        {
            public IActionResult Index()
            {

                ChromeOptions options = new ChromeOptions();
                options.BinaryLocation = "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe";

                DesiredCapabilities capabilities = new DesiredCapabilities();
                capabilities.SetCapability(ChromeOptions.Capability, options);
                ChromeDriver driver = new ChromeDriver("C:\\WebDriver\\bin", options);
                driver.Navigate().GoToUrl(@"http://localhost:4200/test");
                driver.FindElement(By.Id("alert")).Click();
                WebDriverWait wait = new WebDriverWait(driver, timeout: TimeSpan.FromSeconds(30));
                IAlert alert = wait.Until(ExpectedConditions.AlertIsPresent());
                string text = alert.Text;
                alert.Accept();
                return View();
            }
        }
    }

    3. Confirmation Handling in Selenium Webdriver

    With Confirmation we still have a decent role to play in where you have two buttons and no input form. Hence, accept() and reject() functionality. Also, to get the element and prompt in all the three examples we are using By.LinkText("Text") here where text is the element button <button>or <a> which you click on to get the popup. IAlert alert would store the element as a variable which could be reused like below as retrieving text value from there.
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using WebApplication1.Models;
    using OpenQA.Selenium;
    using OpenQA.Selenium.Chrome;
    using OpenQA.Selenium.Remote;
    using OpenQA.Selenium.Support.UI;
    using SeleniumExtras.WaitHelpers;

    namespace WebApplication1.Controllers
    {
        public class HomeController : Controller
        {
            public IActionResult Index()
            {

                ChromeOptions options = new ChromeOptions();
                options.BinaryLocation = "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe";

                DesiredCapabilities capabilities = new DesiredCapabilities();
                capabilities.SetCapability(ChromeOptions.Capability, options);
                ChromeDriver driver = new ChromeDriver("C:\\WebDriver\\bin", options);
                driver.Navigate().GoToUrl(@"http://localhost:4200/test");
                driver.FindElement(By.Id("confirm")).Click();
                WebDriverWait wait = new WebDriverWait(driver, timeout: TimeSpan.FromSeconds(30));
                IAlert alert = wait.Until(ExpectedConditions.AlertIsPresent());
                string text = alert.Text;
                alert.Dismiss();
                return View();
            }
        }
    }

    4. Prompts Handling in Selenium Webdriver

    With Prompts we can send in value using sendKeys() which in webdrivers help to interact with keyboard as the sole purpose of automating ui/ux for cross-browser using selenium is to intervene every manual interaction and automate elements and popups. As seen we send in a name and it alerts the name as part of front-end logic but the name is supplied through selenium webdriver interaction.
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using WebApplication1.Models;
    using OpenQA.Selenium;
    using OpenQA.Selenium.Chrome;
    using OpenQA.Selenium.Remote;
    using OpenQA.Selenium.Support.UI;
    using SeleniumExtras.WaitHelpers;

    namespace WebApplication1.Controllers
    {
        public class HomeController : Controller
        {
            public IActionResult Index()
            {

                ChromeOptions options = new ChromeOptions();
                options.BinaryLocation = "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe";

                DesiredCapabilities capabilities = new DesiredCapabilities();
                capabilities.SetCapability(ChromeOptions.Capability, options);
                ChromeDriver driver = new ChromeDriver("C:\\WebDriver\\bin", options);
                driver.Navigate().GoToUrl(@"http://localhost:4200/test");
                 driver.FindElement(By.Id("prompt")).Click();
                WebDriverWait wait = new WebDriverWait(driver, timeout: TimeSpan.FromSeconds(30));
                IAlert alert = wait.Until(ExpectedConditions.AlertIsPresent());
                alert.SendKeys("Priyanka");
                alert.Accept();
                return View();
            }
        }
    }


    5. Summary

    1. We interacted with alerts where the value of alert was stored and dismiss and accept had same purpose.
    2. Confirmations allow to store the state of alert when Accept() and Dismiss() would do separate task. Also there are no form inputs to it.
    3. Prompts have more room as they are popups with form and could enter value using sendKeys() in sequential order and driver would interact
    perfectly like user would. This covers alerts, confirmations and prompts handling in Selenium Webdriver.

    Comments Section: