【问题标题】:C# Exception: NullReferenceExceptionC# 异常:NullReferenceException
【发布时间】:2011-06-10 16:34:32
【问题描述】:

我在运行测试时收到以下消息。

消息:

测试方法Automation.Test1.General 抛出异常: System.NullReferenceException:对象引用未设置为对象的实例。

C:\Documents and Settings\Administrator\My Documents\Visual Studio 2010\Projects\Automation\Automation\Library.cs 中的 Automation.Library.CheckLogIn():第 152 行

C:\Documents and Settings\Administrator\My Documents\Visual Studio 2010\Projects\Automation\Automation\Test1.cs 中的 Automation.Test1.General():第 72 行

Library.cs(超类)

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(子类)

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=&quot;LogIn&quot;]"/>
<add key="UserNameField" value="//*[@id=&quot;username&quot;]"/>
<add key="PWDField" value="//*[@id=&quot;pwd&quot;]"/>
<add key="Go" value="//*[@id=&quot;gobutton&quot;]"/>
<add key="UserName" value="admin"/>
<add key="PWD" value="password"/>
<!-- End of  CheckLogIn-->

<!-- Object Definitions-->

<add key="TAB" value="//*[@id=&quot;Tab&quot;]"/>

<!-- End of Object Definitios-->

【问题讨论】:

    标签: c# selenium


    【解决方案1】:

    好吧,我真的没有在你的帖子中看到任何问题,所以我想我会指出显而易见的......

    您的错误是告诉您在此方法的某处抛出了 NullReferenceException

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

    Selnull。您的代码中没有任何地方实际初始化它。你需要在某个地方初始化它:

    Sel = new SomeTypeThatImplementsISelenium();
    

    此外,GetAppConfig 根据文档已被弃用。

    【讨论】:

    • 感谢您的回复,但我不确定我是否理解您初始化 Sel 的意思。如何初始化它?
    • 作为引用类型,Sel 的默认值将为空。您需要为其分配一个实现 ISelenium 的实例,方法是将其作为构造函数的参数或创建一个新对象。
    • 谢谢!!谢谢!!非常感谢!
    【解决方案2】:

    是的,您可能需要先初始化一个对象实例,然后才能使用它的任何方法。解决这个问题的一个简单方法是在抛出错误之前在某处添加一个断点,并使用监视窗口初始化 selenium 并查看 Sel.IsElementPresent(GetAppConfig("SignOn")) 返回真或假。

    【讨论】:

      【解决方案3】:

      公共 ISelenium Sel;

      我可以看到 Sel 已定义,但从未为其赋值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-04-01
        • 1970-01-01
        • 2017-07-27
        • 2014-07-01
        • 2012-06-17
        • 2010-12-22
        • 2014-08-17
        相关资源
        最近更新 更多