如何访问ClassName RichTextBox的文本

时间:2016-09-13 22:15:54

标签: c# richtextbox ui-automation white teststack

我试图在我的窗口上找到一个文档,其中包含一个带有文本的富文本框,其中包含TestStack White,并提取该文本。

我试图使用UIItem标签& TextBox,但White似乎无法在我的测试中找到对象。可以使用通用UIItem找到该对象,但我希望能够访问它所拥有的文本。

我正在实施它:

public [Unsure] MyRichTextBox { get { return Window.Get<[Unsure]>(SearchCriteria.ByClassName("RichTextBox")); } }

我希望能够说:

Assert.That(MyRichTextBox.Text.Equals(x));

但如果我将其标记为Label或TextBox,它无法找到我要查找的内容,如果我将其声明为UIItem,则无法访问.Text。< / p>

1 个答案:

答案 0 :(得分:2)

您想要使用TextBox的类型。然后,您可以使用BulkText访问RichEditBox中的文本。

首先是窗口:

TestStack.White.UIItems.WindowItems.Window _mainWindow;    
app = TestStack.White.Application.Launch(startInfo);
_mainWindow = app.GetWindow("MyDialog");

然后找到richEditBox:

public string _richeditdocument_ID = "rtbDocument";
private TextBox _richeditdocument_ = null;
public TextBox RichEditDocument 
{ 
    get 
    { 
         if (null == _richeditdocument_) 
                 _richeditdocument_ = _mainWindow.Get<TextBox>(SearchCriteria.ByAutomationId(_richeditdocument_ID)); 
                 return _richeditdocument_;
     } 
}

然后使用以下内容访问文本:

string data = RichEditDocument.BulkText;

以下是使用白色文字方法的代码注释:

    // Summary:
    //     Enters the text in the textbox. The text would be cleared first. This is
    //     not as good performing as the BulkText method. This does raise all keyboard
    //     events - that means that your string will consist of letters that match the
    //     letters of your string but in current input language.
    public virtual string Text { get; set; }

以下是使用BulkText的注释:

        // Summary:
        //     Sets the text in the textbox. The text would be cleared first. This is a
        //     better performing than the Text method. This doesn't raise all keyboard events.
        //      The string will be set exactly as it is in your code.