-2

It would be great if somebody helps me in solving below issue that I am facing during automation execution:

I am trying to bring in test case looping based on number of lines in the excel. I am using Data-driven capability in visual studio to achieve this. This works perfectly in Visual studio environment when i execute in "Test Explorer". But, when I build this project’s DLL and copy to the working folder of the automation tool (Where it picks for execution), below error is triggered:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object.

Please see the below code snippet:

[DataSource("System.Data.Odbc", "Dsn=Excel Files;Driver={Microsoft Excel Driver (*.xlsx)};dbq=J:\\Automation_Working_Directory\\Automation_Files_Demo\\Test_Report_01Base.xlsx;defaultdir=.;driverid=790;maxbuffersize=2048;pagetimeout=5;readonly=true", "SrsReportRunController_Dialog$", DataAccessMethod.Sequential)]
    [TestMethod]
    public void Test_Report_01()
    {
        #region Fetch data from Excel file

        string fromDate = TestContext.DataRow["From date"].ToString();
        string startDate = TestContext.DataRow["Report period start date"].ToString();
        string toDate = TestContext.DataRow["To date"].ToString();

        #endregion Fetch data from Excel file

        Application.Control.TextBox().SetValue(fromDate);}

When i am trying to use the "fromDate" value to enter in the application, I am getting NullReferenceExecption.

Thanks in Advance.

Shashank R
  • 19
  • 1
  • 7
  • Possible duplicate : https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it – OhmnioX Aug 22 '18 at 11:11

1 Answers1

0

First, you should add try-catch in your code, which will help you identifying exact error line number

I think maybe ToString() is causing this issue in case of DataRow value is null.

Try using Convert Class this will help to avoid exception to some extent

 public void Test_Report_01()
 {
     try{
         #region Fetch data from Excel file

        string fromDate = Convert.ToString(TestContext.DataRow["From date"]);
        string startDate = Convert.ToString(TestContext.DataRow["Report period start date"]);
        string toDate = Convert.ToString(TestContext.DataRow["To date"]);

        #endregion Fetch data from Excel file

        Application.Control.TextBox().SetValue(fromDate);
     }
     catch(Exception e){
         Console.WriteLine(e.toString());
     }
}
Suresh Prajapati
  • 3,991
  • 5
  • 26
  • 38
  • The code is perfect, since this executes without issues in Visual studio environment. I am getting issue while i am using this DLL for execution from an automation tool. – Shashank R Aug 22 '18 at 11:23
  • Your code is not handled for runtime exceptions. Adding try-catch will prevent from crashing your application due to the unhandled exception. – Suresh Prajapati Aug 22 '18 at 11:24
  • Look for details in exception log, you will come to know why it's throwing that – Suresh Prajapati Aug 22 '18 at 11:26
  • Thanks for the reply. I have used try-catch, the code snippet shown is for illustration purpose of how i am using Data-driven concept. "Application.Control.Textbox().SetValue(fromDate);" line is for letting know the place where the data read from excel is used. – Shashank R Aug 22 '18 at 11:28