sai sindhu
sai sindhu

Reputation: 1155

Unable to catch the exception from selenium in C#

I have written some code which deals with C# reflections and selenium to automate the build process of a URL.

But I am unable to catch the exception. What I did is , I exported into .html format from selenium IDE. and parsed and it automatically calls the function related to it from c# code.

but I am unable to catch it. I need help in this regard? Any guesses why it is unable to catch the exception..

I am using Visual Studio Microsoft Visual C# 2010 Express.

And the code is as follows.

    using System;
    using System.Text;
    using System.Text.RegularExpressions;
    using NUnit.Framework;
    using Selenium;
    using System.Reflection;
    using System.IO;

namespace SeleniumTests
{
    public class Program
    {
        public ISelenium selenium;

        public void SetupTest()
        {
            selenium = new DefaultSelenium("localhost", 4444, "*chrome", "URL");
            selenium.Start();
        }

        //[TearDown]
        public void TeardownTest()
        {
            try
            {
                selenium.Stop();
            }
            catch (Exception)
            {

            }
        }

        public void myFun(string file) 
        {
             bool flag = false;
             string targetString = "", valueString = "", commandString = "";
             string subString1, subString2;
             HtmlAgilityPack.HtmlNode commandNode=null;
             HtmlAgilityPack.HtmlNode targetNode=null;
             HtmlAgilityPack.HtmlNode valueNode=null;

                HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();

                doc.Load(file);
                doc.OptionCheckSyntax = true;
                doc.OptionFixNestedTags = true;
                doc.OptionAutoCloseOnEnd = true;
                doc.OptionOutputAsXml = true;
                doc.OptionDefaultStreamEncoding = Encoding.Default;

                HtmlAgilityPack.HtmlNode table = doc.DocumentNode.SelectSingleNode("//table");
                foreach (var row in table.SelectNodes("//tr"))
                {
                    commandNode = row.SelectSingleNode("td[1]");
                    commandString = commandNode.InnerHtml.ToString();

                    subString1 = commandString.Substring(0, 1);

                    subString1 = subString1.ToUpper();
                    subString2 = commandString.Substring(1, commandString.Length - 1);

                    commandString = subString1 + subString2;

                     targetNode = row.SelectSingleNode("td[2]");

                    if (targetNode != null)
                    {
                        targetString = targetNode.InnerHtml.ToString();

                        if (targetString.Length == 0)
                        {
                            targetNode = null;
                        }
                    }

                   valueNode = row.SelectSingleNode("td[3]");
                    if (valueNode != null)
                    {
                        valueString = valueNode.InnerHtml.ToString();

                        if (valueString.Length == 0)
                        {
                            valueNode = null;
                        }

                    }

                    MethodInfo SeleniumMethod = typeof(ISelenium).GetMethod(commandString);

                    if (SeleniumMethod == null)
                    {
                        // Console.WriteLine(" \n NULL " + commandString);
                        continue;
                    }

                    if (targetNode == null && valueNode == null)
                        continue;
                    if (targetNode != null && valueNode != null)
                        {
                            String[] SeleniumArgs = new String[2];
                            SeleniumArgs[0] = targetNode.InnerHtml.ToString();
                            SeleniumArgs[1] = valueNode.InnerHtml.ToString();
                            try
                            {
                                SeleniumMethod.Invoke(selenium, SeleniumArgs);
                            }

                            catch (System.Reflection.TargetInvocationException)
                            {
                            }

                            catch (Selenium.SeleniumException se)
                            {
                                flag = true;
                                string lines = "\n Selenium Exception: Caught an exception while executing the script : " + file + "  with the command : " + commandNode.InnerHtml.ToString() + "  and the  XPath is: " + targetNode.InnerHtml.ToString() + " and the value is : " + valueNode.InnerHtml.ToString() + " and the exception is as follows :  ";
                                using (StreamWriter writer = new StreamWriter("Log.txt", true))
                                {
                                    writer.WriteLine(lines);
                                    writer.Flush();
                                    writer.Close();

                                }
                            }
                            catch (Exception e)
                            {

                                flag = true;
                                string lines = "\n  Exception: Caught an exception while executing the script : " + file + "  with the command : " + commandNode.InnerHtml.ToString() + "  and the  XPath is: " + targetNode.InnerHtml.ToString() + " and the value is : " + valueNode.InnerHtml.ToString() + " and the exception is as follows :  ";
                                using (StreamWriter writer = new StreamWriter("Log.txt", true))
                                {
                                    writer.WriteLine(lines);
                                    writer.Flush();
                                    writer.Close();

                                }

                           }

                        }

                        else if (targetNode != null && valueNode == null)
                        {
                            String[] SeleniumArgs = new String[1];
                            SeleniumArgs[0] = targetNode.InnerHtml.ToString();
                            SeleniumMethod.Invoke(selenium, SeleniumArgs);

                        }
                    else if (valueNode != null)
                    {
                        String[] SeleniumArgs = new String[1];
                        SeleniumArgs[0] = valueNode.InnerHtml.ToString();
                        SeleniumMethod.Invoke(selenium, SeleniumArgs);
                    }

                }// end of for

            string line = "\n Script executed successfully ";
            if (flag == false)
            {
                using (StreamWriter writer = new StreamWriter("Log.txt", true))
                {
                    writer.WriteLine(line);
                    writer.Flush();
                    writer.Close();
                }
            }

        }
    }

    public class TestProgram
    {
        static void Main(string[] args)
        {
            try
            {
                Program p = new Program();
                p.SetupTest();

                string file = @"1.html";
                p.myFun(file);
                p.TeardownTest();
           }
            catch  { }
        }

    }
}

Upvotes: 1

Views: 3007

Answers (1)

tobias86
tobias86

Reputation: 5029

If you are trying to catch the exception in your Main() method, you need to bubble your exceptions up in your myFun method. At the moment you are drowning any exceptions in your myFun method.

e.g.

try
{
    SeleniumMethod.Invoke(selenium, SeleniumArgs);
}
catch (System.Reflection.TargetInvocationException)
{
    throw; //make this bubble up to the calling method.
}
catch (Selenium.SeleniumException se)
{
    flag = true;
    string lines = "\n Selenium Exception: Caught an exception while executing the script : " + file + "  with the command : " + commandNode.InnerHtml.ToString() + "  and the  XPath is: " + targetNode.InnerHtml.ToString() + " and the value is : " + valueNode.InnerHtml.ToString() + " and the exception is as follows :  ";
    using (StreamWriter writer = new StreamWriter("Log.txt", true))
    {
        writer.WriteLine(lines);
        writer.Flush();
        writer.Close();
    }
    throw se; //bubble up to calling method
}
//etc...

Upvotes: 2

Related Questions