编码的UI控件存在。 System.NullReferenceException [英] Coded UI Control.Exists. System.NullReferenceException

查看:64
本文介绍了编码的UI控件存在。 System.NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查一些操作后该窗口是否存在。
我尝试:

I want to check that window exists after some actions. I try:

 protected override Boolean IsPresent()
    {
        if (_mainWindow == null)
        {
            _mainWindow = new WinWindow();
            _mainWindow.SearchProperties[WinWindow.PropertyNames.ControlName] = "MainWindow";
        }
        return _mainWindow.Exists;
    }

但是如果控件不存在 mainWindow.Exists 抛出System.NullReferenceException。我不明白为什么会这样,因为此代码中的mainWindow引用不能为null。
如何验证_mainWindow是否成立?

But if control does not exist mainWindow.Exists throws System.NullReferenceException. I can't understand why it happens because mainWindow reference in this code can't be null. How can I verify is _mainWindow founded or not?

我已经完成了等待窗口加载并超时的操作。我也尝试使用 MainWindow.FindMainWindow()。WaitForControlExist(100000),但它没有等待所需的超时。
此代码也未设置我所需的时间:

I've did it to wait for window loading with timeout. I've also tried to use MainWindow.FindMainWindow().WaitForControlExist(100000) but it doesn’t wait needed timeout. This code also not set my needed timout:

Playback.PlaybackSettings.SearchTimeout = 100000;
Playback.PlaybackSettings.WaitForReadyTimeout = 100000;

我使用VS2013。

UPD:

这是我的NRE检查代码:

This is my code with NRE check:

protected override Boolean IsPresent()
{
    if (_mainWindow == null)
    {
        _mainWindow = new WinWindow();
        _mainWindow.SearchProperties[WinWindow.PropertyNames.ControlName] = "MainWindow";
    }
    try
    {
        return _mainWindow.TryFind(); //TODO WTF?
    }
    catch (NullReferenceException e)
    {
        Console.WriteLine("We've got a NullReferenceException");
        Console.WriteLine("_mainWindow reference is " + ((_mainWindow == null) ? "NULL" : "NOT NULL"));
        throw e;  //Line 41
    }
}

这是结果:

We've got a NullReferenceException
_mainWindow reference is NOT NULL
Attachments:

file:///Project/TestResults/User_WIN-FP7FMM7PUB1%202017-04-09%2015_57_34/In/4acd6ac8-92ce-4746-8787-3aecfd63bdd8/WIN-FP7FMM7PUB1/SuccessLoginTest.png

Test method UITest.AutoTests.LoginTests.SuccessLoginTest threw exception: 

System.NullReferenceException: object reference not set to an instance of an object.
   in UITest.Locators.MainWindow.IsPresent() in MainWindow.cs: line 41
   in UITest.Locators.BaseWindow.Wait() in BaseWindow.cs: line 34
   in UITest.Locators.MainWindow..ctor() in MainWindow.cs: line 18
   in UITest.Locators.LoginWindow.ClickEnterButton() in LoginWindow.cs: line 57
   in UITest.AutoTests.LoginTests.SuccessLoginTest() in LoginTests.cs: line 32


推荐答案

问题出在版本中VS2013。
我已经安装了Update 5,如果没有找到对象,则TryFind()返回False。

The problem was in version of VS2013. I have installed Update 5 and now TryFind() returns False if object not found.

这篇关于编码的UI控件存在。 System.NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆