2017-02-27 8 views
1

私はNUnitテストランナーで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(); 
     } 
    } 
} 

これは結構です、と私はこれらのシナリオを持つとき、私のキュウリのファイルと呼ばれる「ステップ」であることを理解:私は私の機能ファイルを書いて、ステップを生成するspecflowを頼むときは、次のコードを出力しガーキンに書かれているのは彼らのためです。

しかし、これは完全に統合されたUIテストであるため、Xamarin.UITest.Androidを使用してビューなどをクリックする必要があります。

だから私は何とかテスト中のアプリケーションを表すオブジェクトをつかむ必要があるので、UI操作を実行できます。私はプロパティAndroidApp appは私が必要なオブジェクトであることがわかります

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."); 
     } 
    } 
} 

今、私は、このオブジェクトが「Tests.cs」と呼ばれる別の自動生成テスト・フィクスチャファイルで初期化されていることがわかりますアクセスできますが、上記のCategoryPagerStepsコードからそのプロパティにアクセスするにはどうすればよいですか? Testsは静的でも、メソッドやプロパティでもありません。おそらくテストランナーによって行われるはずなので、私はそれを簡単にインスタンス化するのが気になりますよね?他の自動生成ファイルの1つにはtestRunnerプロパティが含まれていますが、privateとマークされています。

私が行ったすべてのアベニューはブロックされたように見え、私は何かが明らかでないと感じています。ここで

+1

を見てみましょう:この記事は深さで説明しhttp://arteksoftware.com/bdd-tests-with-xamarin-uitest-and-specflow/ SpecFlow + Xamarin.UITestで始める方法 – Cheesebaron

答えて

0

は場合に誰がそれを有用見つけるかもしれない、私はそれを解決方法は次のとおりです。

がarteksoftwareから@CheeseBaronによって提供さlinkフォローアップ、トリックは値を保持するためにSpecFlowのFeatureContext.Currentを使用することです。これはFeatureContextの用途の1つです。このコードに示すように

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]へのバインディングを変更し、メソッドを静的にすることで問題は解決しました。私は1つのテストランナーの選択があるバインディングに関連していることを想像

[Binding] 
public class FeatureSteps 
{ 
    AndroidApp app; 

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

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

:そして、特徴コード自体に、アプリはそのようFeatureContext辞書から抽出することができ

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

私の "App.config"はここにあります。私はSpecFlowプラグインでNUnitを使用しています。他のテストランナーの設定で試してみませんでした。

<?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> 
関連する問題