编码的UI控件存在。 System.NullReferenceException

编程入门 行业动态 更新时间:2024-10-25 09:38:58
本文介绍了编码的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

本文发布于:2023-11-17 03:27:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608652.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控件   UI   NullReferenceException   System

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!