public void SetTestMethodCategories(TechTalk.SpecFlow.Generator.TestClassGenerationContext generationContext, System.CodeDom.CodeMemberMethod testMethod, IEnumerable<string> scenarioCategories) { this.codeDomHelper.AddAttributeForEachValue(testMethod, CATEGORY_ATTR, scenarioCategories.Where(cat => !cat.StartsWith("Browser:"))); var sauceLabConfigTag = scenarioCategories.SingleOrDefault(x => x == "SauceLabConfig"); if (sauceLabConfigTag != null) { this.sauceLabSettings = (SauceLabSettingsSection)ConfigurationManager.GetSection("sauceLabSettings"); this.sauce = true; } if (this.sauce) { var settings = this.sauceLabSettings; foreach (var config in settings.ConfigurationElementCollection) { var sauceLabConfig = (SauceLabTestConfigurationElement) config; testMethod.UserData.Add("Browser:" + sauceLabConfig.Browser, sauceLabConfig.Browser); var testName = string.Format("{0} on {1} version {2} on {3}", testMethod.Name, sauceLabConfig.Browser, sauceLabConfig.Version, sauceLabConfig.Platform); var withBrowserArgs = new[] { new CodeAttributeArgument(new CodePrimitiveExpression(sauceLabConfig.Browser)), new CodeAttributeArgument(new CodePrimitiveExpression(sauceLabConfig.Version)), new CodeAttributeArgument(new CodePrimitiveExpression(sauceLabConfig.Platform)), new CodeAttributeArgument(new CodePrimitiveExpression(settings.Credentials.Url)), new CodeAttributeArgument(new CodePrimitiveExpression(testName)) } .Concat(new[] { new CodeAttributeArgument("Category", new CodePrimitiveExpression(sauceLabConfig.Browser)), new CodeAttributeArgument("TestName", new CodePrimitiveExpression(testName)) }) .ToArray(); this.codeDomHelper.AddAttribute(testMethod, ROW_ATTR, withBrowserArgs); } if (!scenarioSetupMethodsAdded) { generationContext.ScenarioInitializeMethod.Statements.Add(new CodeSnippetStatement(" if(this.driver != null)")); generationContext.ScenarioInitializeMethod.Statements.Add(new CodeSnippetStatement(" ScenarioContext.Current.Add(\"Driver\", this.driver);")); generationContext.ScenarioInitializeMethod.Statements.Add(new CodeSnippetStatement(" if(this.container != null)")); generationContext.ScenarioInitializeMethod.Statements.Add(new CodeSnippetStatement(" ScenarioContext.Current.Add(\"Container\", this.container);")); scenarioSetupMethodsAdded = true; } testMethod.Statements.Insert(0, new CodeSnippetStatement(" InitializeSeleniumSauce(browser, version, platform, name, url);")); testMethod.Parameters.Insert(0, new System.CodeDom.CodeParameterDeclarationExpression("System.string", "browser")); testMethod.Parameters.Insert(1, new System.CodeDom.CodeParameterDeclarationExpression("System.string", "version")); testMethod.Parameters.Insert(2, new System.CodeDom.CodeParameterDeclarationExpression("System.string", "platform")); testMethod.Parameters.Insert(3, new System.CodeDom.CodeParameterDeclarationExpression("System.string", "url")); testMethod.Parameters.Insert(4, new System.CodeDom.CodeParameterDeclarationExpression("System.string", "testName")); return; } bool hasBrowser = false; foreach(var browser in scenarioCategories.Where(cat => cat.StartsWith("Browser:")).Select(cat => cat.Replace("Browser:", ""))) { testMethod.UserData.Add("Browser:" + browser, browser); var withBrowserArgs = new[] { new CodeAttributeArgument(new CodePrimitiveExpression(browser)) } .Concat(new[] { new CodeAttributeArgument("Category", new CodePrimitiveExpression(browser)), new CodeAttributeArgument("TestName", new CodePrimitiveExpression(string.Format("{0} on {1}", testMethod.Name, browser))) }) .ToArray(); this.codeDomHelper.AddAttribute(testMethod, ROW_ATTR, withBrowserArgs); hasBrowser = true; } if (hasBrowser) { if (!scenarioSetupMethodsAdded) { generationContext.ScenarioInitializeMethod.Statements.Add(new CodeSnippetStatement(" if(this.driver != null)")); generationContext.ScenarioInitializeMethod.Statements.Add(new CodeSnippetStatement(" ScenarioContext.Current.Add(\"Driver\", this.driver);")); generationContext.ScenarioInitializeMethod.Statements.Add(new CodeSnippetStatement(" if(this.container != null)")); generationContext.ScenarioInitializeMethod.Statements.Add(new CodeSnippetStatement(" ScenarioContext.Current.Add(\"Container\", this.container);")); scenarioSetupMethodsAdded = true; } testMethod.Statements.Insert(0, new CodeSnippetStatement(" InitializeSelenium(browser);")); testMethod.Parameters.Insert(0, new System.CodeDom.CodeParameterDeclarationExpression("System.string" , "browser")); } }
public void SetTestClassCategories(TechTalk.SpecFlow.Generator.TestClassGenerationContext generationContext, IEnumerable<string> featureCategories) { this.codeDomHelper.AddAttributeForEachValue(generationContext.TestClass, CATEGORY_ATTR, featureCategories); var sauceLabConfigTag = featureCategories.SingleOrDefault(x => x == "SauceLabConfig"); if (sauceLabConfigTag != null) { this.sauceLabSettings = (SauceLabSettingsSection) ConfigurationManager.GetSection("sauceLabSettings"); this.sauce = true; } }