2017-02-27 75 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>