1

Example of Handling PopUp in selenium webdriver using C#.Net

[TextFixture]
public class PopUp   
{
    IWebDriver driver = null;
    IWebElement element = null;
    [SetUp]
    public void setUP()
    {
        driver = new ChromeDriver("G:\\Selenium_Csharp\\Jar\\chromedriver_win32");
        IOptions options =  driver.Manage();
        options.Window.Maximize();
        driver.Navigate().GoToUrl("file:///G:/Selenium_Csharp/POPUP/alert.html");

    }

    [Test]
    public void HandlingAlert()
    {
        driver.FindElement(By.XPath("//button[@onclick='alertFunction()']")).Click();
        IAlert alert = driver.SwitchTo().Alert();
        Console.WriteLine(alert.Text);
        Thread.Sleep(3000);
        //alert.Accept();
        alert.Dismiss();


    }

    [Test]
    public void HandlingPrompt()
    {
        driver.Navigate().GoToUrl("file:///G:/Selenium_Csharp/POPUP/prompt.html");
        driver.FindElement(By.XPath("//button[@onclick='promptFunction()']")).Click();
        IAlert alert = driver.SwitchTo().Alert();
        alert.SendKeys("Avinash");
        Thread.Sleep(3000);
        alert.Accept();
        //element = driver.FindElement(By.Id("promptdemo"));
        //element.Text == "Hello Avinash! Welcome to Selenium Easy..";
        WebDriverWait wait = new WebDriverWait(driver,TimeSpan.FromSeconds(10));
        wait.Until(ExpectedConditions.ElementExists(By.Id("promptdemo")));

        element = driver.FindElement(By.Id("promptdemo"));

            Assert.AreEqual("Hello Avinash! Welcome to Selenium Easy..",element.Text);

    }

    [Test]
    public void HandlingConfirmation()
    {
        driver.Navigate().GoToUrl("file:///G:/Selenium_Csharp/POPUP/Confirmation.html");
        driver.FindElement(By.XPath(("//button[@onclick='confirmFunction()']"))).Click();
        IAlert alert = driver.SwitchTo().Alert();
        Console.WriteLine(alert.Text);
        Thread.Sleep(2000);
        alert.Accept();
        driver.SwitchTo().DefaultContent();

    }

    [TearDown]
    public void tearDown()
    {
        driver = null;
        element = null;
    }
}
2
  • 1
    Is this a question? what are you trying to accomplish here?
    – DEnumber50
    Jun 1, 2015 at 18:14
  • 1
    what is the question here btw? Jul 2, 2015 at 3:02

1 Answer 1

0

ToolsQA also has a good write up, here. It is Java but the same concepts apply.

Not the answer you're looking for? Browse other questions tagged or ask your own question.