C# Exception: NullReferenceException
I am getting the following message whilt running my test.
Message:
Test method Automation.Test1.General threw exception: System.NullReferenceException: Object reference not set to an instance of an object.
Automation.Library.CheckLogIn() in C:\Documents and Settings\Administrator\My Documents\Visual Studio 2010\Projects\Automation\Automation\Library.cs: line 152
Automation.Test1.General() in C:\Documents and Settings\Administrator\My Documents\Visual Studio 2010\Projects\Automation\Automation\Test1.cs: line 72
Library.cs (superclass)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Selenium;
using System.IO;
using System.Reflection;
using System.Net;
using System.Configuration;
namespace Automation
{
[TestClass]
开发者_如何转开发public class Library
{
public ISelenium Sel;
// Open browser
public void OpenBrowser(out ISelenium selenium, out StringBuilder verificationErrors)
{
selenium = new DefaultSelenium(GetAppConfig("TestMachine"), 4444, GetAppConfig("Browser"), GetAppConfig("URL"));
selenium.Start();
selenium.Open(GetAppConfig("URL"));
verificationErrors = new StringBuilder();
}
// Returns the value of the passed key from App.config
public string GetAppConfig(string key)
{
return ConfigurationManager.AppSettings[key].ToString();
}
// Check for Login
public void CheckLogIn()
{
if (Sel.IsElementPresent(GetAppConfig("SignOn")))
{
Sel.Type(GetAppConfig("UserNameField"), GetAppConfig("UserName"));
Sel.Type(GetAppConfig("PWDField"), GetAppConfig("PWD"));
Sel.Click(GetAppConfig("Go"));
}
else
{
// do nothing
}
}
}
}
Test1.cs (sub class)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Selenium;
using System.IO;
using System.Reflection;
using System.Net;
using System.Configuration;
namespace Automation
{
[TestClass]
public class Test1
{
public ISelenium Sel;
public StringBuilder Err;
Boolean bNextFlag = false;
Library Lib = new Library();
// Constructor
public Test1()
{
// Launch browser with application URL
Lib.OpenBrowser(out Sel, out Err);
Sel.WindowMaximize();
Lib.CheckLogIn();
}
[TestMethod]
public void General()
{
// Verify Tab
if (Sel.IsElementPresent(Lib.GetAppConfig("TAB")))
{
Sel.Click(Lib.GetAppConfig("TAB"));
bNextFlag = true;
}
else
{
// do something
}
}
}
}
app.config.xml
<?xml version="1.0" encoding="utf-8" ?>
<add key="TestMachine" value="localhost"/>
<add key="Browser" value="*iexplore"/>
<add key="URL" value="http://localhost//Default.aspx"/>
<!-- CheckLogIn-->
<add key="SignOn" value="//*[@id="LogIn"]"/>
<add key="UserNameField" value="//*[@id="username"]"/>
<add key="PWDField" value="//*[@id="pwd"]"/>
<add key="Go" value="//*[@id="gobutton"]"/>
<add key="UserName" value="admin"/>
<add key="PWD" value="password"/>
<!-- End of CheckLogIn-->
<!-- Object Definitions-->
<add key="TAB" value="//*[@id="Tab"]"/>
<!-- End of Object Definitios-->
Well I don't really see a question in your post, so I suppose I will point out the obvious...
Your error is telling you that you have a NullReferenceException
being thrown somewhere in this method:
public void CheckLogIn()
{
if (Sel.IsElementPresent(GetAppConfig("SignOn")))
{
Sel.Type(GetAppConfig("UserNameField"), GetAppConfig("UserName"));
Sel.Type(GetAppConfig("PWDField"), GetAppConfig("PWD"));
Sel.Click(GetAppConfig("Go"));
}
else
{
// do nothing
}
}
Sel
is null
. Nowhere in your code do you actually initialize it. You'll need to initialize it somewhere like:
Sel = new SomeTypeThatImplementsISelenium();
Also, GetAppConfig is deprecated per the docs.
Yeah you might need to initialize an instance of object before you can use any of its methods. An easy way to sort this out is to add a break point somewhere before the error is thrown, and use the watch window to initialize selenium and see what Sel.IsElementPresent(GetAppConfig("SignOn"))returns- true or false.
public ISelenium Sel;
I can see Sel is defined but never assign value to it.
精彩评论