我如何获得对被测应用程序的引用?

How do I get a reference to the application under test?

我将 specflow 与 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();
        }
    }
}

这很好,我知道这些是 "Steps",当我的黄瓜文件和用 Gherkin 编写的场景调用它们时将被调用。

但是,由于这是一个完全集成的 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 属性,但它被标记为私有。

所以我走过的每条路都被堵住了,我觉得我错过了一些明显的东西。

这是我解决它的方法,以防其他人发现它有用:

跟进@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] 绑定并使方法静态化解决了问题。

[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>

上面提到的 repo https://github.com/RobGibbens/BddWithXamarinUITest 最后更新于 2015 年,并且已经转向 POP 架构,我会推荐。

在此处查看 - 使用 SpecFlow 的 POP 架构,https://github.com/xamarin-automation-service/uitest-specflow-example

您可以看到有一个名为 AppManager 的静态 class,它具有 App 属性

有很多关于这种风格的视频和博客post: