0

我刚刚开始使用 Expensify Windows Phone 测试框架,它似乎非常符合我对 Windows Phone 7.1 自动化测试的要求。我可以让它触发屏幕上的普通按钮,但不能让它触发我的任何一个 ApplicationBar 按钮。

此测试页的 XAML 是...

<phone:PhoneApplicationPage x:Class="SampleApp1.MainPage"
                        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
                        xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
                        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
                        xmlns:AppBarUtils="clr-namespace:AppBarUtils;assembly=AppBarUtils"
                        FontFamily="{StaticResource PhoneFontFamilyNormal}"
                        FontSize="{StaticResource PhoneFontSizeNormal}"
                        Foreground="{StaticResource PhoneForegroundBrush}"
                        SupportedOrientations="Portrait"
                        Orientation="Portrait"
                        mc:Ignorable="d"
                        d:DesignWidth="480"
                        d:DesignHeight="768"
                        shell:SystemTray.IsVisible="True"
                        DataContext="{Binding Main, Source={StaticResource Locator}}">

<!--LayoutRoot contains the root grid where all other page content is placed-->
<Grid x:Name="LayoutRoot"
      Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel x:Name="TitlePanel"
                Grid.Row="0"
                Margin="24,24,0,12">
        <TextBlock x:Name="ApplicationTitle"
                   Text="{Binding ApplicationTitle}"
                   Style="{StaticResource PhoneTextNormalStyle}" />
        <TextBlock x:Name="PageTitle"
                   Text="{Binding PageName}"
                   Margin="-3,-8,0,0"
                   Style="{StaticResource PhoneTextTitle2Style}" />
    </StackPanel>

    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentGrid"
          Grid.Row="1">
        <StackPanel Margin="24,20,0,123" Orientation="Vertical">
            <TextBlock Text="{Binding Welcome, Mode=OneWay}" 
                       Style="{StaticResource PhoneTextNormalStyle}"
                       HorizontalAlignment="Center" VerticalAlignment="Center"
                       Margin="6,0,19,0" TextWrapping="Wrap" Width="431" />
            <TextBlock Text="Enter first number" 
                       HorizontalAlignment="Left" VerticalAlignment="Top"
                       Margin="6,20,0,0" TextWrapping="Wrap" />
            <TextBox TextChanged="OnTextBoxTextChanged"
                     Text="{Binding EnteredAmount1, Mode=TwoWay, UpdateSourceTrigger=Explicit}"
                     x:Name="txtValue1" 
                     HorizontalAlignment="Left" Height="72" VerticalAlignment="Top" Width="456" 
                     TextWrapping="Wrap" InputScope="Number"/>
            <TextBlock Text="Enter second number" 
                       HorizontalAlignment="Left" VerticalAlignment="Top" 
                       Margin="6,0,0,0" TextWrapping="Wrap" />
            <TextBox TextChanged="OnTextBoxTextChanged"
                     Text="{Binding EnteredAmount2, Mode=TwoWay, UpdateSourceTrigger=Explicit}"
                     x:Name="txtValue2" 
                     HorizontalAlignment="Left" Height="72" VerticalAlignment="Top" Width="456" 
                     TextWrapping="Wrap" InputScope="Number"/>
            <TextBlock Text="Result" 
                       HorizontalAlignment="Left" VerticalAlignment="Top"
                       Margin="6,20,0,0" TextWrapping="Wrap"/>
            <TextBlock Text="{Binding Total, Mode=OneWay}" 
                       HorizontalAlignment="Left" VerticalAlignment="Top" 
                       Margin="15,0,0,0" TextWrapping="Wrap" 
                       Width="422" Height="45"/>
            <Button x:Name="btnAdd" Command="{Binding AddCommand}" Content="Add" Margin="240,0,0,0" />
        </StackPanel>
    </Grid>
</Grid>

<phone:PhoneApplicationPage.ApplicationBar>
    <shell:ApplicationBar IsVisible="True" IsMenuEnabled="False">
        <shell:ApplicationBarIconButton x:Name="AddAppBarBtn" IconUri="/Images/ApplicationBar.Check.png" Text="add"></shell:ApplicationBarIconButton>
        <shell:ApplicationBarIconButton x:Name="SubtractAppBarBtn" IconUri="/Images/ApplicationBar.Cancel.png" Text="subtract"></shell:ApplicationBarIconButton>

    </shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>

<i:Interaction.Behaviors>
    <AppBarUtils:AppBarItemCommand Id="add" Command="{Binding AddCommand}"/>
    <AppBarUtils:AppBarItemCommand Id="subtract" Command="{Binding SubtractCommand}"/>
</i:Interaction.Behaviors>


<i:Interaction.Triggers>
    <AppBarUtils:AppBarItemTrigger Type="Button" Id="add" Text="add" />
    <AppBarUtils:AppBarItemTrigger Type="Button" Id="subtract" Text="subtract" />
</i:Interaction.Triggers>

</phone:PhoneApplicationPage>

SpecFlow 功能定义如下...

Scenario: AppBar Add Two Numbers
Given my app is uninstalled
And my app is installed
And my app is running within 20 seconds
Then I enter "34" into the control "txtValue1"
Then I enter "23" into the control "txtValue2"
Then I press the control "AddAppBarBtn"

Scenario: Button Add Two Numbers
Given my app is uninstalled
And my app is installed
And my app is running within 20 seconds
Then I enter "34" into the control "txtValue1"
Then I enter "23" into the control "txtValue2"
Then I press the control "btnAdd"

ButtonAddTwoNumbers 方案完全有效。AppBarAddTwoNumbers 填写文本框值但拒绝按下按钮,我得到的错误是“无法设置焦点以控制'AddAppBarBtn'”。

对此的任何帮助将不胜感激。

4

1 回答 1

0

我认为你的工作水平太高了。就个人而言,我通常不会测试 UI 本身,因为从长远来看,它只会导致需要不断更改的脆弱测试。相反,我通常会归结为 ViewModel。

因此,鉴于您的 txtValue1 绑定到 EnteredAmount1,txtValue2 是 EnteredAmount2 并且 AddAppBarBtn 是 AddCommand,您可以将业务逻辑测试为

Scenario: AppBar Add Two Numbers
Given I enter 34 into Amount1
And I enter 23 into Amount2 
When I add them  #Calls AddCommand
Then Total should be 57

这意味着您可以非常轻松地重新设计您的 UI,例如,如果我们想使用滑块而不是文本框,则上述逻辑测试不会发生任何变化。

此外,您仍然可以将 UI 功能作为单独的功能进行测试

Scenario: Check startup
Given my app is uninstalled
When my app is installed
Then my app should be running within 20 seconds

如果你真的想要,你仍然可以检查你的绑定

 Scenario: Check bindings
 Given I enter 34 into Amount1
 Then control txtValue1 should be 34

通过这种方式,您可以将您的测试分解成更小的应用程序块,并为自己提供更多有针对性的故障,这在跟踪任何问题时非常有用。

于 2013-01-21T15:38:34.147 回答