Stop button event WPF, Selenium

201 views Asked by At

How to stop button event when exception fired..?I used try catch blocks, if exception fired it will try to execute next code, i dont want that...pls check comments in code..M i using try catch blocks wrongly..?

here is my code:

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    try
    {
         driver.FindElement(By.CssSelector(".username")).SendKeys("abc");
         driver.FindElement(By.CssSelector(".password")).SendKeys("abcpassword");
         driver.FindElement(By.XPath("//a[2]/img")).Click();
    }
    catch(Exception ex)
    {
        driver.Quit();
        MessegeBox.Show("Log in failed");
        //stop button event here only if exception fired
    }

    try
    {
        new SelectElement(driver.FindElement(By.Id("CId"))).SelectByText("CVB");
        driver.FindElement(By.Name("ddd")).Click();
    }
    catch(Exception ex)
    {   
        driver.Quit();
        MessegeBox.Show("Log in failed");
        //stop button event here only if exception fired
    }

    Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
    Excel.Workbook xlWorkBook;
    Excel.Worksheet xlWorkSheet;
    xlApp.DisplayAlerts = false;
    object misValue = System.Reflection.Missing.Value;
}
2

There are 2 answers

0
Sinatr On

The catch only breaks execution of code inside try block. It doesn't return from the method. Typical scenario is indeed just one try/catch block in the method (so that catch will be also the last in your method -> method will ends):

public void SomeMethod()
{
    try
    {
        ...
    }
    catch(...)
    {
        ...
    }
    ... // code will continue to run here, if no code left method will ends
}

In your case you have to exit method to avoid other try/catch to run, means use return keyword like this:

public void SomeMethod()
{
    try
    {
        ...
    }
    catch(...)
    {
        ...
        return;
    }
    ... // this code will not run in case of exception in try block
}
0
Suresh Kumar Veluswamy On

I would suggest that you restructure you code like below. Even better is to handle specific exceptions rather than handling the generic Exception.

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        try
        {
            driver.FindElement(By.CssSelector(".username")).SendKeys("abc");
            driver.FindElement(By.CssSelector(".password")).SendKeys("abcpassword");
            driver.FindElement(By.XPath("//a[2]/img")).Click();

            new SelectElement(driver.FindElement(By.Id("CId"))).SelectByText("CVB");
            driver.FindElement(By.Name("ddd")).Click();
        }
        catch (Exception ex)
        {
            driver.Quit();
            MessegeBox.Show("Log in failed");
            return;
        }

        Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
        Excel.Workbook xlWorkBook;
        Excel.Worksheet xlWorkSheet;
        xlApp.DisplayAlerts = false;
        object misValue = System.Reflection.Missing.Value;
    }