C# 对象引用未设置为对象的实例
我得到一个对象引用,该引用未设置为此程序中对象错误的实例。selenium的初学者希望尝试通过xmlreader打开浏览器。我在本地库中有xmlreader代码和openurl(只需打开一个google页面)。我想通过创建实例在[setup]和[test]中调用这些方法。谢谢你的帮助C# 对象引用未设置为对象的实例,c#,nunit,selenium-webdriver,C#,Nunit,Selenium Webdriver,我得到一个对象引用,该引用未设置为此程序中对象错误的实例。selenium的初学者希望尝试通过xmlreader打开浏览器。我在本地库中有xmlreader代码和openurl(只需打开一个google页面)。我想通过创建实例在[setup]和[test]中调用这些方法。谢谢你的帮助 namespace ClassLibrary1 { [TestFixture] public class Class1 { IWebDriver driver = null; LocalLib lo
namespace ClassLibrary1
{
[TestFixture]
public class Class1
{
IWebDriver driver = null;
LocalLib localLib;
[SetUp]
public void openBrowser()
{
localLib = new LocalLib(driver);
localLib.StartDriverUsingXMLReader();
}
[Test]
public void url()
{
localLib.openUrl();
}
}
public class LocalLib
{
private IWebDriver driver = null;
public LocalLib(IWebDriver _driver)
{
this.driver = _driver;
}
public IWebDriver StartDriverUsingXMLReader()
{
try
{
XmlReader reader = XmlReader.Create(@"C:\Users\XXXXX\Desktop\XML.xml");
while (reader.Read())
{
if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "settings"))
{
if (reader.HasAttributes)
{
string parentBrowser = reader.GetAttribute("browser");
if (parentBrowser.ToLower().Equals("Firefox".ToLower()))
{
driver = new FirefoxDriver();
}
else if (parentBrowser.ToLower().Equals("googlechrome".ToLower()))
{
driver = new ChromeDriver(@"C:\chromedriver2_win32_0.8");
}
else if (parentBrowser.ToLower().Equals("Iexplore".ToLower()))
{
driver = new InternetExplorerDriver(@"C:\IEDriverServer_x64_2.32.3");
}
}
}
} return driver;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
throw;
}
}
public void openUrl()
{
driver.Navigate().GoToUrl("www.google.com");
}
}
}
错误:
对象引用未设置为对象的实例。
在C:\Users\XXXX\XML测试项目\Class1.cs中的ClassLibrary1.LocalLib.StartDriverUsingXMLReader()中:第52行
第52行:
字符串parentBrowser=reader.GetAttribute(“浏览器”)
Xml文件信息
<?xml version="1.0" encoding="utf-8" ?>
<settings>
<!--browser info-->
<settings browser="firefox"/>
</settings>
确认XML文件是否具有
浏览器属性。如果没有,或者它是一个空字符串,GetAttribute
将返回null,当您尝试调用parentBrowser
上的ToLower
时,下一行将出现NullReferenceException
。另一个潜在的问题是reader
为空,但我认为如果是这样的话,它会在前面抛出
顺便说一下,NullReferenceExceptions
是C#code中最常见的运行时错误之一。您应该看看其他类似的问题,因为问题总是一样的,您有一些引用类型,当它为空时,您尝试使用
操作符。在使用
操作符之前,找出它为null的原因并阻止它或添加检查总是可以解决这个问题。使用一个工作示例仔细检查XML文件的内容
尝试将其用作XML的内容
<?xml version="1.0" encoding="utf-8" ?>
<!--browser info-->
<settings browser="firefox"/>
如果要添加更多信息,应向设置节点添加更多属性,如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<settings browser="firefox" username="abcd@yahoo.com" password="abcd" implicitWait="3" explicitWait="3"/>
这里有一个设置节点,具有浏览器、用户名、密码、隐式等待和显式等待属性
然后,从C#代码中,您可以按照已经读取浏览器属性的相同方式读取每个属性:
字符串parentBrowser=reader.GetAttribute(“浏览器”);
字符串username=reader.GetAttribute(“用户名”);
字符串密码=reader.GetAttribute(“密码”);
字符串implicitWait=reader.GetAttribute(“implicitWait”);
string explicitWait=reader.GetAttribute(“explicitWait”)
请注意,属性将被读取为字符串,如果需要将其转换为整数,则需要使用如下方式进行转换:
<?xml version="1.0" encoding="utf-8" ?>
<settings browser="firefox" username="abcd@yahoo.com" password="abcd" implicitWait="3" explicitWait="3"/>
int implicitWait=int.Parse(reader.GetAttribute(“implicitWait”) 您试图读取的节点可能没有“browser”属性,XML文件的内容是什么?我已经更新了XML内容。奇怪的是,您为什么不将app.config
与ConfigurationManager
一起使用?@Arran。。C#初学者。。我试过了,但不知道怎么用。所以用XML尝试了一下。谢谢。xml部分现在可以工作了。但是,如果我想添加更多的字段,如用户名、密码,我可以这样写谢谢。问题出在xml中