0

When I am trying to run the code then I get this error "System.ArgumentNullException: text cannot be null Parameter name: text" it doesn't pull the data from excel file; can anyone please give me the suggestion that how to fix this issue?

here is the script

ExcelLib.cs

using ExcelDataReader;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;

namespace Driven
{
    public class ExcelLib
    {
        public static DataTable ExcelToDataTable(string fileName)
        {
            //open file and returns as Stream
            FileStream stream = File.Open(fileName, FileMode.Open, FileAccess.Read);
            {
                using (var reader = ExcelReaderFactory.CreateReader(stream))
                {
                    var result = reader.AsDataSet(new ExcelDataSetConfiguration()
                    {
                        ConfigureDataTable = (data) => new ExcelDataTableConfiguration()
                        {
                            UseHeaderRow = true
                        }
                    });

                    //Get all the Tables
                    DataTableCollection table = result.Tables;
                    //Store it in DataTable
                    DataTable resultTable = table["Sheet1"];

                    //return
                    return resultTable;
                }
            }
        }

            static List<Datacollection> dataCol = new List<Datacollection>();

        public static void PopulateInCollection(string fileName)
        {
            DataTable table = ExcelToDataTable(fileName);

            //Iterate through the rows and columns of the Table
            for (int row = 1; row <= table.Rows.Count - 1; row++)
            {
                for (int col = 0; col <= table.Columns.Count; col++)
                {
                    Datacollection dtTable = new Datacollection()
                    {
                        rowNumber = row,
                        colName = table.Columns[col].ColumnName,
                        colValue = table.Rows[row - 1][col].ToString()
                    };
                    //Add all the details for each row
                    dataCol.Add(dtTable);
                }
            }
        }

        public static string ReadData(int rowNumber, string columnName)
        {
            try
            {
                //Retriving Data using LINQ to reduce much of iterations
                string data = (from colData in dataCol
                               where colData.colName == columnName && colData.rowNumber == rowNumber
                               select colData.colValue).SingleOrDefault();

                //var datas = dataCol.Where(x => x.colName == columnName && x.rowNumber == rowNumber).SingleOrDefault().colValue;
                return data.ToString();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return null;
            }
        }
        public class Datacollection
        {
            public int rowNumber { get; set; }
            public string colName { get; set; }
            public string colValue { get; set; }
        }
    }
}

UnitTest1.cs

    using System;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using OpenQA.Selenium;
    using OpenQA.Selenium.Chrome;
    using NUnit.Framework;

    namespace Driven
    {

    [TestClass]
    public class UnitTest1
        {
            [TestMethod]
            public void TestMethod1()
            {
                ExcelLib.PopulateInCollection(@"D:\Data.xlsx");

                IWebDriver driver = new ChromeDriver();
                driver.Manage().Window.Maximize();
                driver.Navigate().GoToUrl("");
                driver.FindElement(By.Id("UserName")).SendKeys(ExcelLib.ReadData(1, "getUserNamefromexcel"));
                driver.FindElement(By.Id("Password")).SendKeys(ExcelLib.ReadData(1, "getPasswordfromexcel"));
                driver.FindElement(By.Name("Action")).Click();
            }
        }
    }

Error after Running method UnitTest1

Message: Test method driven.UnitTest1.TestMethod1 threw exception: System.ArgumentNullException:text cannot be null parameter name: text

Error after Debugging method UnitTest1

An exception of type 'System.ArgumentNullException' occurred in WebDriver.dll but was not handled in user code

John Bull
  • 9
  • 4
  • 1
    On which line do you get the exception? can you post the stack trace? – S Ahmed Apr 21 '19 at 18:50
  • Have you found the solution? – S Ahmed Apr 22 '19 at 12:15
  • @Ahmed thanks for your consideration No i didn't findout any solution up till now I get this error in method UnitTest1 and in line # 20 **line # 20 is** : `driver.FindElement(By.XPath("//*[@id = 'UserName']")).SendKeys(ExcelLib.ReadData(1, "UserName"));` please find the attached links while running I get this error https://i.stack.imgur.com/qVqLh.png while debugging I get this error https://i.stack.imgur.com/LqACK.png – John Bull Apr 22 '19 at 12:26
  • Please reduce your code down to a [mcve] and carefully read that page, especially the last link about "How to debug small programs". It will help you figure out issues like this on your own. – JeffC Apr 22 '19 at 19:46

1 Answers1

0

According to the error message, the ExcelLib.ReadData() is returning null. Null is not acceptable for SendKeys.

You are returning null on exception. That may be one of the reasons.

Check if the data is populated correctly. Then check if data is readable or not. Also, print the stack trace of the exception in the read method.

S Ahmed
  • 1,454
  • 1
  • 8
  • 14