简体   繁体   English

C#Teststack.White:有时无法找到按钮

[英]C# Teststack.White: sometimes unable to find button

I have a WPF application with one window. 我有一个窗口的WPF应用程序。 In that window I have several StackPanels and several Buttons. 在那个窗口中,我有几个StackPanels和几个Button。 White can find each Button perfectly, but with exact the same code just one single Button is seldom found and often not found. White可以完美地找到每个Button,但是使用完全相同的代码,很少会找到一个Button,而且经常找不到。 I was unable to find out what the cause is for this behaviour. 我无法找出导致此行为的原因。 Thats why I came to you guys. 那就是为什么我来找你们。

Note: Before the buttons are shown, there is another view (in the same window) that waits for the server to respond. 注意:在显示按钮之前,还有另一个视图(在同一窗口中)等待服务器响应。 As soon as the response is in, the buttons get displayed. 收到响应后,将立即显示按钮。

Here my testcode: 这是我的测试代码:

[TestMethod]
public void ClickBarPayment_ViewExchanges()
{
    DelayedStart(5);
    Application app = null;
    try
    {
        app = Application.Launch(SutPath);
        try
        {
            var window = app.GetWindow("CfeMain");
            var button = (Button)window.Get(SearchCriteria.ByAutomationId("CashButton"), TimeSpan.FromSeconds(60));
            button.Click();
            Assert.AreEqual(false, button.Visible);
        }
        catch (Exception e)
        {
            Assert.Fail(e.Message);
        }
    }
    finally
    {
        app?.Close();
    }
}

You can assume, that the Buttons are all equal and all have a Tag " Name="" ". 您可以假设这些按钮都是相等的,并且都具有标签“ Name =“”“。 Here is the XAML code, I remove unnecessary names with "Blabla" aso 这是XAML代码,我用“ Blabla” aso删除了不必要的名称

<StackPanel Name="PaymentContent"  Visibility="Visible" VerticalAlignment="Center"  Margin="0,0,0,0">
        <StackPanel Height="60"  VerticalAlignment="top" Margin="20,0,10,0">
            <TextBlock FontSize="24" Foreground="#0b0b0b" Name="info" HorizontalAlignment="Stretch">Bla</TextBlock>
        </StackPanel>

        <StackPanel Name="options" Orientation="Vertical" HorizontalAlignment="Center" Grid.RowSpan="2" Width="1004">

            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="5,15,5,0">
                ...
                ...
            </StackPanel>

            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="5,0,5,5">

                <Button Name="Blablabla" Click="Blablabl_OnClick" Style="{StaticResource MainButtonBottomLeft}" Background="#efefef" Margin="0,10,10,0" HorizontalAlignment="Center" Width="490" Height="200">
                    <StackPanel Orientation="Vertical" HorizontalAlignment="Left" Width="400" Height="200">
                        <TextBlock FontSize="28" Height="40" Margin="0,10,0,0" Name="BlablaText">Bla</TextBlock>
                        <TextBlock FontSize="18" Height="30" Name="BlablaInfo"/>
                        <Separator Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}" Background="#859fcd" Margin="0,10,0,0" Width="460" />
                        <StackPanel Name="BlablablIssuer" Height="100" Orientation="Horizontal">
                            <Image Source="/Images/Blasdsd.png" Width="95" Height="35" />
                        </StackPanel>
                    </StackPanel>
                </Button>

                <Button Name="CashButton" Click="Cash_OnClick" Style="{StaticResource MainButtonBottomRight}" Background="#efefef"  Margin="0,10,0,0" HorizontalAlignment="Center" Width="490" Height="200">
                    <StackPanel Orientation="Vertical" HorizontalAlignment="Left" Width="400" Height="200">
                        <TextBlock FontSize="28" Height="40" Margin="0,10,0,0" Name="NoteText">Bla</TextBlock>
                        <TextBlock FontSize="18" Height="30" Name="NoteInfo"><Run Text="Schweizer Franken Banknoten"/></TextBlock>
                        <Separator Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}" Background="#859fcd" Margin="0,10,0,0" Width="460" />
                        <StackPanel Name="noteIssuer" Height="100" Orientation="Horizontal">
                            <Image Source="/Images/100frank.jpg" Width="85" Height="55" Margin="0,0,5,0"/>
                            <Image Source="/Images/50frank.jpg" Width="80" Height="55" Margin="0,0,5,0" />
                            <Image Source="/Images/20frank.jpg" Width="80" Height="55" Margin="0,0,5,0" />
                            <Image Source="/Images/10frank.jpg" Width="75" Height="55" Margin="0,0,0,0" />
                            <Image Source="/Images/qrCode.png" Width="51" Height="45" Margin="0,28" />
                        </StackPanel>
                    </StackPanel>
                </Button>

            </StackPanel>

        </StackPanel>
    </StackPanel>

Without actually facing the issue I can make only suggestions. 如果没有实际面对这个问题,我只能提出建议。

Potential reason could be that window object getting initialized prior to having all buttons enabled in actual Application window. 可能的原因可能是在实际的“应用程序”窗口中启用所有按钮之前初始化了窗口对象。

You could add a wait time prior to initialize window 您可以在初始化窗口之前添加等待时间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM