C# 测试堆栈.White : sometimes unable to find button

标签 c# wpf ui-testing white-framework teststack

我有一个只有一个窗口的 WPF 应用程序。在那个窗口中,我有几个 StackPanel 和几个按钮。 White 可以完美地找到每个 Button,但是对于完全相同的代码,很少会找到而且经常找不到一个 Button。我无法找出造成这种行为的原因。这就是我来找你们的原因。

注意:在显示按钮之前,还有另一个 View (在同一窗口中)等待服务器响应。一旦有响应,按钮就会显示出来。

这是我的测试代码:

[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();
    }
}

您可以假设所有按钮都是相同的,并且都有一个标签“Name="""。这是 XAML 代码,我用“Blabla”a.s.o 删除了不必要的名称。

<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>

最佳答案

如果没有实际面对问题,我只能提出建议。

可能的原因可能是窗口对象在实际应用程序窗口中启用所有按钮之前进行了初始化。

您可以在初始化窗口之前添加等待时间

关于C# 测试堆栈.White : sometimes unable to find button,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48299452/

相关文章:

c# - LargeImages 到 WPF ListView 端口

c# - 如何检测当前 session 是否通过 RDP 启动?

c# - 在 WPF 中使用 CollectionView 时无法从数据集中的连接表中获取数据

xcode7 - XCode 7 UI 测试 : Dismissal of system-generated UIAlertController does not work

c# - 无法在远程计算机上执行命令

c# 如何检测特定方法是否有重载版本?

.net - WPF 数据绑定(bind)线程安全吗?

wpf - 同步用户控件中多个属性的绑定(bind)

ios - 提高 iOS UI 测试速度

xcode - 使用 Xcode 7 UITest 测试元素是否可见