private static void AddCosmosDbContainerForCleanup(
            SpecFlowContext context,
            Container container,
            Database?database = null)
        {
            if (!context.ContainsKey(CosmosDbContainersToDelete))
            {
                context.Set(new List <Container>(), CosmosDbContainersToDelete);
            }

            if (database is not null)
            {
                if (!context.ContainsKey(CosmosDbDatabasesToDelete))
                {
                    context.Set(new List <Database>(), CosmosDbDatabasesToDelete);
                }

                List <Database> databasesToDelete = context.Get <List <Database> >(CosmosDbDatabasesToDelete);
                databasesToDelete.Add(database);
            }

            List <Container> containers = context.Get <List <Container> >(CosmosDbContainersToDelete);

            containers.Add(container);
        }
        private static bool TryGetInstance <T>(SpecFlowContext context, string key, out T instance)
        {
            instance = default(T);
            try
            {
                instance = key == null
                               ? context.Get <T>()
                               : context.Get <T>(key);

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Ejemplo n.º 3
0
 private static void DisposeServiceProvider(SpecFlowContext context)
 {
     if (context.Get <IServiceProvider>(ServiceProviderKey) is IDisposable spDisposable)
     {
         spDisposable.Dispose();
     }
 }
Ejemplo n.º 4
0
        private static void CompleteContainerSetup(SpecFlowContext context)
        {
            context.Set(false, ContainerBindingPhaseKey);

            ServiceCollection serviceCollection = context.Get <ServiceCollection>(ServiceCollectionKey);

            IServiceProvider service = serviceCollection.BuildServiceProvider();

            context.Set(service, ServiceProviderKey);
        }
        protected static void LogTestSession(SpecFlowContext context)
        {
            var testSession = context.Get <TestSession>();

            Trace.WriteLine("Finished scenario with:\n" +
                            $"Ukprn: {testSession.Ukprn}\n" +
                            $"Job ID: {testSession.JobId}\n" +
                            $"Learners:\n" +
                            $"\t{string.Join("\n\t", testSession.Learners)}");
        }
Ejemplo n.º 6
0
        private static IServiceProvider GetServiceProvider(SpecFlowContext context, string messageForBuildInProgressError)
        {
            bool serviceBuildInProgress = VerifyBindingAvailable(context);

            if (serviceBuildInProgress)
            {
                throw new InvalidOperationException(messageForBuildInProgressError);
            }

            return(context.Get <IServiceProvider>(ServiceProviderKey));
        }
        protected static void CleanUpTestSession(SpecFlowContext context)
        {
            if (!context.ContainsKey("container_scope"))
            {
                return;
            }
            var scope = context.Get <ILifetimeScope>("container_scope");

            context.Remove("container_scope");
            scope.Dispose();
        }
 private static async Task TeardownCosmosDBDatabasesCoreAsync(
     SpecFlowContext context)
 {
     if (context.ContainsKey(CosmosDbDatabasesToDelete))
     {
         await Task.WhenAll(context.Get <List <Database> >(CosmosDbDatabasesToDelete)
                            .DistinctBy(database => database.Id)
                            .Select(async database => await database.DeleteAsync().ConfigureAwait(false)))
         .ConfigureAwait(false);
     }
 }
        /// <summary>
        /// Create a container against a database.
        /// </summary>
        /// <param name="partitionKeyPath">The partition key path to use when creating the container.</param>
        /// <param name="databaseContext">The context from which to get the <see cref="Database"/>.</param>
        /// <param name="containerContext">The context into which to store the <see cref="Container"/>, or null if you do not need to store it.</param>
        /// <param name="containerKey">The key at which to store the <see cref="Container"/>, or null if you do not need to store it.</param>
        /// <returns>A task that completes when the container has been created.</returns>
        internal static async Task <Container> CreateContainer(string partitionKeyPath, SpecFlowContext databaseContext, ScenarioContext?containerContext = null, string?containerKey = null)
        {
            Database  database  = databaseContext.Get <Database>(CosmosDbContextKeys.CosmosDbDatabase);
            Container container = await database.CreateContainerIfNotExistsAsync("client-" + Guid.NewGuid(), partitionKeyPath).ConfigureAwait(false);

            if (containerContext != null && containerKey != null)
            {
                containerContext.Set(container, containerKey);
                CosmosDbContextBindings.AddScenarioLevelCosmosDbContainerForCleanup(containerContext, container);
            }

            return(container);
        }
Ejemplo n.º 10
0
        //public static ScenarioStepContext GetStepContext(this SpecFlowContext context)
        //{
        //    var result = context.GetOrDefault<ScenarioStepContext>("__stepContext__", null);

        //    if (result != null) return result;

        //    // If this method was called with a step context then use it otherwise set to null.
        //    SetStepContext(context, context as ScenarioStepContext);
        //    return context.Get<ScenarioStepContext>("__stepContext__");
        //}

        //public static void SetStepContext(this SpecFlowContext context, ScenarioStepContext value)
        //    => context.Set(value, "__stepContext__");

        //public static ScenarioContext GetScenarioContext(this SpecFlowContext context)
        //{
        //    var result = context.GetOrDefault<ScenarioContext>("__scenarioContext__", null);

        //    if (result != null) return result;

        //    // If this method was called with a scenario context then use it otherwise set to null.
        //    SetScenarioContext(context, context as ScenarioContext);
        //    return context.Get<ScenarioContext>("__scenarioContext__");
        //}

        //public static void SetScenarioContext(this SpecFlowContext context, ScenarioContext value)
        //    => context.Set(value, "__scenarioContext__");

        public static FeatureContext GetFeatureContext(this SpecFlowContext context)
        {
            var result = context.GetOrDefault <FeatureContext>("__featureContext__", null);

            if (result != null)
            {
                return(result);
            }

            // If this method was called with a feature context then use it otherwise set to null.
            SetFeatureContext(context, context as FeatureContext);
            return(context.Get <FeatureContext>("__featureContext__"));
        }
Ejemplo n.º 11
0
        private static IWebDriver CreateSeleniumDriver(this SpecFlowContext specFlowContext, IConfiguration configuration)
        {
            System.Enum.TryParse(configuration[Constants.EnvironmentVariableKeys.Browser], true, out BrowserTypes browser);

            switch (browser)
            {
            case BrowserTypes.Edge:
                EdgeOptions egdeOptions = new EdgeOptions
                {
                    PageLoadStrategy = PageLoadStrategy.Normal,
                    UseChromium      = true
                };

                EdgeDriverService edgeDriverService = EdgeDriverService.CreateChromiumService(configuration[Constants.EnvironmentVariableKeys.EdgeWebDriver]);

                specFlowContext.Set((IWebDriver) new EdgeDriver(edgeDriverService, egdeOptions));
                break;

            case BrowserTypes.Firefox:
                specFlowContext.Set((IWebDriver) new FirefoxDriver());
                break;

            case BrowserTypes.InternetExplorer:

                InternetExplorerOptions internetExplorerOptions = new InternetExplorerOptions()
                {
                    BrowserVersion  = configuration[Constants.EnvironmentVariableKeys.InternetExplorerVersion],
                    IgnoreZoomLevel = true
                };

                specFlowContext.Set((IWebDriver) new InternetExplorerDriver(internetExplorerOptions));
                break;

            case BrowserTypes.Safari:
                SafariDriverService safariDriverService = SafariDriverService.CreateDefaultService();
                SafariOptions       safariOptions       = new SafariOptions();
                safariOptions.PageLoadStrategy = PageLoadStrategy.Normal;
                specFlowContext.Set((IWebDriver) new SafariDriver(safariDriverService));
                break;

            default:
            {
                ChromeDriverService chromeDriverService = ChromeDriverService.CreateDefaultService();
                chromeDriverService.SuppressInitialDiagnosticInformation = true;
                specFlowContext.Set((IWebDriver) new ChromeDriver(chromeDriverService));
            }
            break;
            }

            return(specFlowContext.Get <IWebDriver>());
        }
Ejemplo n.º 12
0
        private static void ConfigureServices(
            SpecFlowContext context,
            Action <IServiceCollection> configure,
            string exceptionMessageIfBuildNotInProgress)
        {
            bool serviceBuildInProgress = VerifyBindingAvailable(context);

            if (!serviceBuildInProgress)
            {
                throw new InvalidOperationException(exceptionMessageIfBuildNotInProgress);
            }

            ServiceCollection serviceCollection = context.Get <ServiceCollection>(ServiceCollectionKey);

            configure(serviceCollection);
        }
 private static async Task TeardownCosmosDBContainersCoreAsync(SpecFlowContext context)
 {
     if (context.ContainsKey(CosmosDbContainersToDelete))
     {
         // We run this way not so much because we want to delete everything at once,
         // but because this way we do actually attempt every deletion, and then report
         // any failures at the end in one big AggregateException.
         await Task.WhenAll(context.Get <List <Container> >(CosmosDbContainersToDelete)
                            .Select(async container =>
         {
             try
             {
                 await container.DeleteContainerAsync().ConfigureAwait(false);
             }
             catch (InvalidOperationException x)
                 when(x.Message == "Unable to use database throughput in a database that has already been created.")
                 {
                     // Swallow this because it's an expected exception in certain test scenarios.
                 }
         }))
         .ConfigureAwait(false);
     }
 }
 private static Container GetCosmosContainer(SpecFlowContext containerContext, string containerKey = CosmosDbContextKeys.CosmosDbContainer)
 {
     return(containerContext.Get <Container>(containerKey));
 }
Ejemplo n.º 15
0
 public static CalculatorProcess Process(this SpecFlowContext context)
 {
     return(context.Get <CalculatorProcess>("Process"));
 }
Ejemplo n.º 16
0
 public static int ProcessId(this SpecFlowContext context)
 {
     return(context.Get <int>("ProcessId"));
 }
 public static T Get <T>(SpecFlowContext context, string key)
 {
     return(context.Get <T>(key));
 }