Control de IU codificado. Existe. System.NullReferenceException
Quiero comprobar que existe una ventana después de algunas acciones. Lo intento:
protected override Boolean IsPresent()
{
if (_mainWindow == null)
{
_mainWindow = new WinWindow();
_mainWindow.SearchProperties[WinWindow.PropertyNames.ControlName] = "MainWindow";
}
return _mainWindow.Exists;
}
Pero si el control no existemainWindow.Exists
lanza System.NullReferenceException. No puedo entender por qué sucede porque la referencia de mainWindow en este código no puede ser nula. ¿Cómo puedo verificar si _mainWindow está fundada o no?
Lo hice para esperar a que se cargue la ventana con tiempo de espera. También he tratado de usarMainWindow.FindMainWindow().WaitForControlExist(100000)
pero no espera el tiempo de espera necesario. Este código tampoco establece mi tiempo de espera necesario:
Playback.PlaybackSettings.SearchTimeout = 100000;
Playback.PlaybackSettings.WaitForReadyTimeout = 100000;
Yo uso VS2013.
UPD:
Este es mi código con verificación NRE:
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
}
}
Y este es el resultado:
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