简体   繁体   中英

C# Teststack.White: sometimes unable to find button

I have a WPF application with one window. In that window I have several StackPanels and several Buttons. White can find each Button perfectly, but with exact the same code just one single Button is seldom found and often not found. 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="" ". Here is the XAML code, I remove unnecessary names with "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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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