Handling Alerts in Selenium

  • Home
  • C#
  • Handling Alerts in Selenium


using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System.Threading;

namespace Selenium_Demo
{
public class HandlingAlers
{
public IWebDriver dr;
[SetUp]
public void Setup()
{
Console.WriteLine("I am from setup method");
dr = new ChromeDriver(@"C:\Users\userid\Desktop");    //path of your chrome driver
}
    [Test]
    public void HandlingAlerts()
    {
        // Alert Message handling
        dr.Navigate().GoToUrl("http://demo.guru99.com/test/delete_customer.php");
        dr.FindElement(By.Name("cusid")).SendKeys("53920");
        dr.FindElement(By.Name("submit")).Click();
        // Switching to Alert        
        IAlert alert = dr.SwitchTo().Alert();
        // Capturing alert message.    
        String alertMessage = dr.SwitchTo().Alert().Text;
        // Displaying alert message        
        Console.WriteLine(alertMessage);

        Thread.Sleep(5000);
        if(alertMessage== "Do you really want to delete this Customer?")
        {
            // Accepting alert        
            alert.Accept();
        }
        Thread.Sleep(2000);
        IAlert alertDelete = dr.SwitchTo().Alert();
        string actualMsg = alertDelete.Text;
        Assert.AreEqual("Customer Successfully Delete!", actualMsg,"Customer is not deleted");
        alertDelete.Accept(); //Click on OK button
    }
}
}

Tags: , ,
Leave a comment

Your email address will not be published. Required fields are marked *

Subscribe now

Receive weekly newsletter with educational materials, new courses, most popular posts, popular books and much more!

https://bridgejunks.com/ https://crownmakesense.com/ https://brithaniabookjudges.com/ https://hughesroyality.com/ https://rhythmholic.com/ https://bandar89.simnasfikpunhas.com/ https://www.100calshop.co.il/products/thailand/ https://myasociados.com/ https://solyser.com/ http://konfidence.cz/ https://muscadinepdx.com/ https://bandar89.parajesandinos.com.ve/ https://goremekoop.com/ https://oncoswisscenter.com/ https://www.turunclifehotel.com/bandar89/ https://www.houseofproducts.biz/ https://taimoormphotography.com/
BIJI18 BIJI18 BIJI18