【发布时间】:2019-04-22 23:21:41
【问题描述】:
当我尝试运行代码时,我收到此错误“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; }
}
}
}
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();
}
}
}
运行方法UnitTest1后出错
消息:测试方法驱动。UnitTest1.TestMethod1 抛出异常:System.ArgumentNullException:文本不能为空参数名称:文本
调试方法UnitTest1后出错
WebDriver.dll 中出现“System.ArgumentNullException”类型的异常,但未在用户代码中处理
【问题讨论】:
-
您在哪一行得到异常?你能发布堆栈跟踪吗?
-
找到解决办法了吗?
-
@Ahmed 感谢您的考虑 不,到目前为止,我还没有找到任何解决方案,我在方法 UnitTest1 和第 20 行 第 20 行是 中收到此错误:@ 987654326@ 请在运行时找到附加的链接 我收到此错误i.stack.imgur.com/qVqLh.png 在调试时我收到此错误i.stack.imgur.com/LqACK.png
-
请将您的代码缩减为minimal reproducible example 并仔细阅读该页面,尤其是关于“如何调试小程序”的最后一个链接。它将帮助您自行解决此类问题。
标签: c# unit-testing selenium argumentnullexception