2017-02-27 78 views
1

我正在使用NUnit测试运行器的specflow。当我写我的特征文件,并要求specflow生成步骤,它输出以下代码:如何获得被测试应用程序的参考?

using System; 
using TechTalk.SpecFlow; 
using Xamarin.UITest.Android; 

namespace UITest1 
{ 
    [Binding] 
    public class CategoryPagerSteps 
    { 
     [Given(@"The (.*)st category is selected")] 
     public void GivenTheStCategoryIsSelected(int p0) 
     { 
      ScenarioContext.Current.Pending(); 
     } 

     [When(@"I swipe left")] 
     public void WhenISwipeLeft() 
     { 
      ScenarioContext.Current.Pending(); 
     } 

     [Then(@"The (.*)nd category is selected")] 
     public void ThenTheNdCategoryIsSelected(int p0) 
     { 
      ScenarioContext.Current.Pending(); 
     } 
    } 
} 

这是好的,我明白,这是在“步骤”将被调用的场景时,我的黄瓜文件写在小黄瓜要求他们。

但是,因为这是一个完全集成的UI测试,我需要能够使用Xamarin.UITest.Android来点击视图等。

所以我需要以某种方式获取代表被测试应用程序的对象,以便我可以对其执行UI操作。现在

,我可以看到,这个对象是在被称为“Tests.cs”另一个自动生成的测试夹具文件初始化:

using NUnit.Framework; 
using Xamarin.UITest; 
using Xamarin.UITest.Android; 

namespace UITest1 
{ 
    [TestFixture] 
    public class Tests 
    { 
     AndroidApp app; 

     [SetUp] 
     public void BeforeEachTest() 
     { 
      // TODO: If the Android app being tested is included in the solution then open 
      // the Unit Tests window, right click Test Apps, select Add App Project 
      // and select the app projects that should be tested. 
      app = ConfigureApp 
       .Android 
       // TODO: Update this path to point to your Android app and uncomment the 
       // code if the app is not included in the solution. 
       //.ApkFile ("../../../Android/bin/Debug/UITestsAndroid.apk") 
       .StartApp(); 
     } 

     [Test] 
     public void AppLaunches() 
     { 
      app.Screenshot("First screen."); 
     } 
    } 
} 

我可以看到属性AndroidApp app是我所需要的对象访问,但我如何从上面的CategoryPagerSteps代码访问该属性? Tests不是静态的,也不是任何方法或属性。我很紧张,只是自己实例化它,因为这应该可以由测试运行者完成,对吧?其他自动生成的文件之一包含一个testRunner属性,但它被标记为私有。

因此,我所下的每条大街都出现了堵塞,我觉得我错过了一些明显的东西。

+1

看看:http://arteksoftware.com/bdd-tests-with-xamarin-uitest-and-specflow/本文深入介绍了如何开始使用SpecFlow + Xamarin.UITest – Cheesebaron

回答

0

这里是我如何解决它,万一别人可能会发现它有用:

由@CheeseBaron从arteksoftware提供的link跟进,关键是要使用SpecFlow的FeatureContext.Current持有的价值。这是FeatureContext的预期用途之一。

从arteksoftware参考使用这种方法,如以下代码所示:

[SetUp] 
public void BeforeEachTest() 
{ 
    app = AppInitializer.StartApp (platform, iOSSimulator); 
    FeatureContext.Current.Add ("App", app); 

    //This next line is not relevant to this post. 
    AppInitializer.InitializeScreens (platform); 
} 

但是,它并没有立即对我来说,因为[Setup]结合就不叫为specflow测试的一部分工作。更改绑定到SpecFlow的绑定并使该方法静态解决问题。

[BeforeFeature] 
public static void Before() 
{ 
    AndroidApp app; 

    Console.WriteLine("** [BeforeFeature]"); 
    app = ConfigureApp 
     .Android 
     // TODO: Update this path to point to your Android app and uncomment the 
     // code if the app is not included in the solution. 
     .ApkFile(<Path to APK>) 
     .StartApp(); 
    FeatureContext.Current.Add("App", app); 
} 

然后,在特征代码本身,应用程序可以从FeatureContext字典中提取的,像这样:

[Binding] 
public class FeatureSteps 
{ 
    AndroidApp app; 

    public FeatureSteps() 
    { 
     app = FeatureContext.Current.Get<AndroidApp>("App"); 
    } 

    //Code for the rest of your feature steps. 
} 

我想,一个人的测试跑步者的选择是相关的,属于绑定使用,所以这是我的“App.config”。我正在使用NUnit和SpecFlow插件。我没有尝试与其他测试运行器配置。

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <configSections> 
    <section name="specFlow" type="TechTalk.SpecFlow.Configuration.ConfigurationSectionHandler, TechTalk.SpecFlow" /> 
    </configSections> 
    <specFlow> 
    <!-- For additional details on SpecFlow configuration options see http://go.specflow.org/doc-config --> 
    <!-- For additional details on SpecFlow configuration options see http://go.specflow.org/doc-config --> 
    <!-- use unit test provider SpecRun+NUnit or SpecRun+MsTest for being able to execute the tests with SpecRun and another provider --> 
    <unitTestProvider name="NUnit" /> 
    <plugins> 
     <add name="SpecRun" /> 
    </plugins> 
    </specFlow> 
</configuration>