흰색으로 MessageBox에 액세스하려면 어떻게합니까?
-
02-07-2019 - |
문제
WPF 응용 프로그램에 다음과 같이 시작된 간단한 메시지 상자가 있습니다.
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Howdy", "Howdy");
}
나는 얻을 수있다 하얀 내 버튼을 클릭하고 메시지 상자를 시작하십시오.
Uispy는 그것을 내 창문의 아이로 보여줍니다. 나는 그것을 액세스하는 방법을 해결할 수 없었습니다.
내용을 확인하기 위해 내 MessageBox에 액세스하려면 어떻게해야합니까?
해결책
그것을 발견! 창 클래스에는 트릭을 수행하는 MessageBox 메소드가 있습니다.
var app = Application.Launch(@"c:\ApplicationPath.exe");
var window = app.GetWindow("Window1");
var helloButton = window.Get<Button>("Hello");
Assert.IsNotNull(helloButton);
helloButton.Click();
var messageBox = window.MessageBox("Howdy");
Assert.IsNotNull(messageBox);
다른 팁
이것을 시도하십시오
Window messageBox = window.MessageBox("");
var label = messageBox.Get<Label>(SearchCriteria.Indexed(0));
Assert.AreEqual("Hello",label.Text);
흰색 소스 코드에 포함 된 일부 UI 테스트 프로젝트 (흰색 자체를 테스트하기 위해)가 포함되어 있습니다.
테스트 중 하나에는 표시된 메시지를 얻는 방법이 포함 된 MessageBox 테스트가 포함됩니다.
[TestFixture, WinFormCategory, WPFCategory]
public class MessageBoxTest : ControlsActionTest
{
[Test]
public void CloseMessageBoxTest()
{
window.Get<Button>("buttonLaunchesMessageBox").Click();
Window messageBox = window.MessageBox("Close Me");
var label = window.Get<Label>("65535");
Assert.AreEqual("Close Me", label.Text);
messageBox.Close();
}
[Test]
public void ClickButtonOnMessageBox()
{
window.Get<Button>("buttonLaunchesMessageBox").Click();
Window messageBox = window.MessageBox("Close Me");
messageBox.Get<Button>(SearchCriteria.ByText("OK")).Click();
}
}
분명히, 문자 메시지를 표시하는 데 사용되는 레이블은 메시지 상자를 표시하는 창에서 소유하며 기본 식별은 최대 단어 값 (65535)입니다.
Window.MessageBox ()는 좋은 솔루션입니다 !!
그러나이 방법은 할 것입니다 오랫동안 붙어 있습니다 메시지 상자 인 경우 나타나지 않습니다. 때때로 나는 확인하고 싶다 "외관이 아닙니다"메시지 상자 (경고, 오류 등). 그래서 나는 스레딩을 통해 타임 아웃을 설정하는 메소드를 작성합니다.
[TestMethod]
public void TestMethod()
{
// arrange
var app = Application.Launch(@"c:\ApplicationPath.exe");
var targetWindow = app.GetWindow("Window1");
Button button = targetWindow.Get<Button>("Button");
// act
button.Click();
var actual = GetMessageBox(targetWindow, "Application Error", 1000L);
// assert
Assert.IsNotNull(actual); // I want to see the messagebox appears.
// Assert.IsNull(actual); // I don't want to see the messagebox apears.
}
private void GetMessageBox(Window targetWindow, string title, long timeOutInMillisecond)
{
Window window = null ;
Thread t = new Thread(delegate()
{
window = targetWindow.MessageBox(title);
});
t.Start();
long l = CurrentTimeMillis();
while (CurrentTimeMillis() - l <= timeOutInMillsecond) { }
if (window == null)
t.Abort();
return window;
}
public static class DateTimeUtil
{
private static DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public static long currentTimeMillis()
{
return (long)((DateTime.UtcNow - Jan1st1970).TotalMilliseconds);
}
}
제휴하지 않습니다 StackOverflow