使用Expensify Windows Phone Test Framework触发ApplicationBar按钮

时间:2013-01-21 14:35:39

标签: windows-phone-7 specflow wp7test

我刚刚开始使用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'”。

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

1 个答案:

答案 0 :(得分:0)

我认为你的工作水平太高了。就个人而言,我通常不会测试UI本身,因为从长远来看,它只会导致不断变化的脆弱测试。相反,我通常会转向ViewModels。

因此,假设您的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

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

相关问题