2008-09-27 6 views
4

아래에 시작된 WPF 응용 프로그램에서 간단한 메시지 상자가 있습니다.흰색으로 MessageBox에 액세스하려면 어떻게합니까?

private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    MessageBox.Show("Howdy", "Howdy"); 
} 

white에서 내 단추를 클릭하고 메시지 상자를 시작할 수 있습니다.

UISpy는 내 창 자식으로 보여줍니다. 액세스 방법을 찾지 못했습니다.

MessageBox의 내용을 확인하기 위해 MessageBox에 액세스하려면 어떻게합니까?

답변

3

발견! 이 창 클래스에는 트릭을 수행하는 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); 
1

메시지 상자에서 메시지를 가져 오는 것은 어떻습니까? 어쨌든 화이트에서 이렇게 할 수는없는 것 같습니다. 나는 메시지가 맞다는 것을 주장하는 것이 아주 중요하다고 생각한다.

1

화이트 소스 코드에는 White 테스트를위한 몇 가지 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)이다.

3

은 좋은 해결책이이

 Window messageBox = window.MessageBox(""); 
     var label = messageBox.Get<Label>(SearchCriteria.Indexed(0)); 
     Assert.AreEqual("Hello",label.Text); 
1

window.MessageBox()를 시도하십시오!

그러나 메시지 박스 를 표시하지 않는 경우이 방법은 오랜 시간을 위해 붙어있다. 때로는 " 외양이"이라는 메시지 상자 ( 경고, 오류 등)를 확인하고 싶습니다. 그래서 threading으로 timeOut을 설정하는 메소드를 작성합니다.

[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); 
    } 
} 
관련 문제