首页 > 解决方案 > Selenium - System.ArgumentNullException:文本不能为空参数名称:文本

问题描述

当我尝试运行代码时,我收到此错误“System.ArgumentNullException:文本不能为空参数名称:文本”它不会从 excel 文件中提取数据;谁能给我建议如何解决这个问题?

这是脚本

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; }
        }
    }
}

单元测试1.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();
            }
        }
    }

运行方法 UnitTest1 后出错

消息:测试方法驱动。UnitTest1.TestMethod1 抛出异常:System.ArgumentNullException:文本不能为空参数名称:文本

调试方法 UnitTest1 后出错

WebDriver.dll 中出现“System.ArgumentNullException”类型的异常,但未在用户代码中处理

标签: c#unit-testingseleniumargumentnullexception

解决方案


根据错误消息,ExcelLib.ReadData()返回 null。Null 是不可接受的SendKeys

您正在返回null异常。这可能是原因之一。

检查数据是否正确填充。然后检查数据是否可读。此外,在 read 方法中打印异常的堆栈跟踪。


推荐阅读