TestStack.White ListBox.SelectedItemText不起作用

时间:2019-01-16 17:24:13

标签: c# winforms nunit-console teststack

我有一个带有2个WinForms控件的ListBox对话框。在被测试的应用程序中,双击列表框控件之一(我将其称为CON​​TROL LISTBOX)中的任何项都会导致选择另一个列表框(SLAVE LISTBOX)中的匹配项。

我的测试导致在“控制列表”框中进行多个输入。然后,该测试在每个CONTROL lISTBOX项上执行ListBox.SelectedItem.DoubleClick(),比较两个列表框控件中的ListBox.SelectedItemText

在应用程序用户界面中,此操作始终有效,但是对SLAVE LISTBOX的ListBox.SelectedItemText调用的测试仅在doubleclick \ compare的初始迭代中返回与用户界面中正确显示的内容匹配的文本。 / p>

有人可以帮助我弄清楚我做错了什么吗?谢谢!

这是我的代码:

public bool SelectMainEventViaErrorEvent(int eventIdx)
{
    bool bSuccess = false;

    errorEvents.Items.Select(eventIdx);
    System.Threading.Thread.Sleep(1000);

    errorEvents.Items.SelectedItem.DoubleClick();
    System.Threading.Thread.Sleep(1000);

    if (eventIdx > 0)
    {
        IVScrollBar vertScroll = mainEvents.ScrollBars.Vertical;
        vertScroll.ScrollDownLarge();
    }

    if (errorEvents.SelectedItemText == mainEvents.SelectedItemText)
    {
        bSuccess = true;
    }

    log.Info($"SelectMainEventViaErrorEvent({eventIdx}) selected error event = {errorEvents.SelectedItemText}");
    log.Info($"SelectMainEventViaErrorEvent({eventIdx}) selected main event = {mainEvents.SelectedItemText}");

    return bSuccess;
}

如您所见,通过下图,两个列表框中的文本是相同的。但是,对顶部列表框(SLAVE LISTBOX)的ListBox.SelectedItemText的调用返回第一次迭代中的值,该值与doubleclick / compare的第一次迭代期间底部列表框(CONTROL LISTBOX)中的第一项匹配。 / p>

Proof that the text of the selected listbox items match

2 个答案:

答案 0 :(得分:0)

与纯文本进行比较不是一个好主意,因为“ Text”!=“ Text”。在这种情况下,您可以使用DisplayMemberValueMember属性。

我将通过手动填充列表框为您演示它,但是您可以从数据库中完成它,也可以这样做。

首先创建class,它将存储您的值及其ID。我通常是这样创建的(这样我以后就可以使用该类做其他事情了)

public class Int_String
{
    public int _int { get; set; } // Important to be declared like properties and not like variables
    public string _string { get; set; }
}

现在让我们这样填充列表框:

public YourForm()
{
    List<Int_String> list = new List<Int_String>();
    list.Add(new Int_String { _int = 1, _string = "Some text" }); // I am populating it manually but you will populate it from DB or somewhere else
    list.Add(new Int_String { _int = 2, _string = "Some other text" });
    list.Add(new Int_String { _int = 3, _string = "One more text" });

    // Now when we have list we need to bind it to our listbox.

    // IMPORTANT!!!!!
    // Display member and Value member properties SHOULD be able to be added before and after assigning datasource to control (like combobox) BUT for some reason on listbox it only works when you assign it AFTER you bind your datasource to listbox.
    // If you ever work with other controls and use these values, ALWAYS declare display member and value member BEFORE you bind datasource. Why? For now let's just say it is much faster but explanation is for other question

    myListBox1.DataSource = list;
    myListBox1.DisplayMember = "_string"; // When you start typing .DisplayMember, you will not get that property in recommendation since it is hidden so do not think there is not that property there.
    myListBox1.ValueMember = "_int"; // Same as above
}

现在,当您以相同的方式用相同的ID填充这样的列表框和第二个列表框时,即使它们的文本不相等但ID相同,您也可以简单地if(listbox1.SelectedValue == listbox2.SelectedValue)进行比较。

奖金: 另外,您可以做的是像这样展开类:

public class Int_String
{    
    public int _int { get; set; }
    public string _string { get; set; }

    public string SomethingOther = "AsD";


    public bool IsTrue()
    {
        return true;
    }
}

然后以相同方式绑定它并执行以下操作:

Int_String item = listbox1.SelectedItem as Int_String;
bool check = item.IsTrue();
MessageBox.Show(item.SomethingOther);

因此,基本上,您为列表框中的每个项目绑定了整个类,向用户显示其中一个变量(在我们的情况下为_string),将ValueMember设置为其他唯一变量,因此可以轻松搜索整个列表框,并在需要时从该项目中获取整个课程。

答案 1 :(得分:0)

我无法通过自动测试代码中的df[~df.C.str.contains("?")] 列表框进行迭代来使此工作正常进行,但是当向后遍历df[~df.C.str.contains("abc")] 列表框时它可以工作。

呼叫代码:

errorEvent

验证码:

errorEvent
相关问题