/// <summary> /// construct json payload for Api modules /// </summary> /// <param name="rawRequestPayload">WSE XmoduleAttributes</param> /// <param name="jObject">json payload</param> /// <returns>json payload</returns> public static JObject ConstructJsonStructure(XModuleAttribute rawRequestPayload, JObject jObject) { try { foreach (var attribute in rawRequestPayload.Attributes) { if (attribute.BusinessType == "JsonValue") { jObject.Add(attribute.Name, attribute.DefaultValue); } else if (attribute.BusinessType == "JsonObject") { JObject nestedObject = new JObject(); nestedObject = ConstructJsonStructure(attribute, nestedObject); jObject.Add(attribute.Name, nestedObject); } else if (attribute.BusinessType == "JsonArray") { JArray arrayAttributes = new JArray(); JObject arrayObject = new JObject(); arrayObject = ConstructJsonStructure(attribute, arrayObject); arrayAttributes.Add(arrayObject.Values()); jObject.Add(attribute.Name, arrayAttributes); } } } catch (Exception ex) { FileLogger.Instance.Error(ex.Message); } return(jObject); }
private XTestStepValue GetOrCreateApiXTestStepValue(XTestStep apiTestStep, KeyValuePair <string, string> wseTestStepValue) { XTestStepValue apiTeststepValue = (XTestStepValue)apiTestStep .Search(string.Format(TqlToGetApiTestStepValue, wseTestStepValue.Key)) .FirstOrDefault(); //if xTestStepValue not present,create it. if (apiTeststepValue == null) { //check if module attribute is present. var xModuleAttribute = (XModuleAttribute)apiTestStep .Module.Search( string.Format(TqlToGetModuleAttributeInApiModule, wseTestStepValue.Key)).FirstOrDefault(); //if not present, Create it. if (xModuleAttribute == null) { XModuleAttribute m = CreateModuleAttribute(apiTestStep.Module as ApiModule, wseTestStepValue); apiTeststepValue = apiTestStep.CreateXTestStepValue(m); } //if already present, use it. else { apiTeststepValue = apiTestStep.CreateXTestStepValue(xModuleAttribute); } } return(apiTeststepValue); }
/// <summary> /// Get Endpoint, Resource and Query Params from WSE Module /// </summary> /// <param name="wseModule"></param> /// <returns>AddressParserResult object which contains Endpoint, Resource and Query Params</returns> public AddressParserResult Parse(XModule wseModule) { var addressParserResult = new AddressParserResult(); try { TCObject addressValueTql = wseModule.Search(AddOnConstants.AddressValueTql).FirstOrDefault(); if (addressValueTql == null) { return(addressParserResult); } XModuleAttribute addressValue = addressValueTql as XModuleAttribute; if (string.IsNullOrEmpty(addressValue?.DefaultValue)) { return(addressParserResult); } ParseAddressInternal(addressParserResult, addressValue.DefaultValue); } catch (Exception ex) { FileLogger.Instance.Error(ex); } return(addressParserResult); }
/// <summary> /// Creates and set Body Param at request and response module and teststeps. /// </summary> /// <param name="apiTestStep">Teststep of ApiEngine</param> /// <param name="wseTestStepValues">Teststepvalues of WseEngine</param> public static void CreateBodyParameterForRequestAndResponse(XTestStep apiTestStep, List <XTestStepValue> wseTestStepValues) { if (!wseTestStepValues.Any()) { return; } XModuleAttribute bodyXModuleAttribute = (XModuleAttribute) apiTestStep.Module.Search( "=>SUBPARTS:XModuleAttribute[Name==\"Body\" and Path==\"Body\" and PathType==\"Body\"]") .FirstOrDefault(); if (bodyXModuleAttribute == null) { bodyXModuleAttribute = (apiTestStep.Module as ApiModule).CreateModuleAttribute("Body", string.Empty, string.Empty, "Body", "Body", XTestStepActionMode .Select, ModuleAttributeDataType .String); bodyXModuleAttribute.Cardinality = "0-N"; } CreateBodyParameterAtTeststepLevel(wseTestStepValues, bodyXModuleAttribute, apiTestStep); }
/// <summary> /// Creates Module Attribute /// </summary> /// <param name="apiModule">ApiModule</param> /// <param name="name"> Name of the Module Attribute</param> /// <param name="valueRange">ValueRange of the Module Attribute</param> /// <param name="value">Default value of the Module Attribute</param> /// <param name="pathType">pathType of the Module Attribute</param> /// <param name="path">path of the Module Attribute</param> /// <param name="actionMode">Default ActionMode of the Module Attribute</param> /// <param name="dataType">Data Type of the Module Attribute</param> /// <returns>Module Attribute</returns> public static XModuleAttribute CreateModuleAttribute(this ApiModule apiModule, string name, string valueRange, string value, string pathType, string path, XTestStepActionMode actionMode = XTestStepActionMode.Verify, ModuleAttributeDataType dataType = ModuleAttributeDataType.String) { XModuleAttribute xModuleAttribute = apiModule.CreateModuleAttribute(); xModuleAttribute.Name = name; xModuleAttribute.DefaultValue = value; if (!string.IsNullOrEmpty(valueRange)) { xModuleAttribute.ValueRange = valueRange; } xModuleAttribute.EnsureUniqueName(); xModuleAttribute.AddXParamToModuleAttribute("PathType", pathType, ParamTypeE.TechnicalID); xModuleAttribute.AddXParamToModuleAttribute("Path", path, ParamTypeE.TechnicalID); xModuleAttribute.AddXParamToModuleAttribute("ExplicitName", "True", ParamTypeE.Configuration); xModuleAttribute.DefaultActionMode = actionMode; xModuleAttribute.DefaultDataType = dataType; return(xModuleAttribute); }
private static List <XAttribute> GetXAttribute(XModuleAttribute xModuleAttribue) { List <XAttribute> lstXAttributes = null; var xMAttributes = xModuleAttribue.SubAttributes.Where(x => x.BusinessType == "XmlAttribute"); foreach (var mAttribute in xMAttributes) { if (!mAttribute.Name.Contains("xmlns")) { var regexItem = new Regex("^[a-zA-Z0-9]*$"); if (regexItem.IsMatch(mAttribute.Name)) { if (lstXAttributes == null) { lstXAttributes = new List <XAttribute> { new XAttribute(mAttribute.Name, mAttribute.DefaultValue) }; } else { lstXAttributes.Add(new XAttribute(mAttribute.Name, mAttribute.DefaultValue)); } } } } return(lstXAttributes); }
public XModuleAttribute SetupAttributes(XModule mainModule, HtmlElement iframe) { XModuleAttribute iframeAttribute = mainModule.CreateModuleAttribute(); iframeAttribute.Name = iframe.TagName; iframeAttribute.DefaultActionMode = XTestStepActionMode.Select; iframeAttribute.Cardinality = "0-1"; iframeAttribute.BusinessType = "HtmlFrame"; AddBusinessParam(iframeAttribute.CreateConfigurationParam(), "Engine", "Html"); AddBusinessParam(iframeAttribute.CreateConfigurationParam(), "BusinessAssociation", "Descendants"); AddBusinessParam(iframeAttribute.CreateTechnicalIDParam(), "Id", iframe.Id); AddBusinessParam(iframeAttribute.CreateTechnicalIDParam(), "Tag", iframe.TagName); var htmlDocumentAttr = iframeAttribute.CreateModuleAttribute(); htmlDocumentAttr.Name = "HtmlDocument"; htmlDocumentAttr.BusinessType = "HtmlDocument"; htmlDocumentAttr.Cardinality = "0-1"; htmlDocumentAttr.DefaultActionMode = XTestStepActionMode.Select; AddBusinessParam(htmlDocumentAttr.CreateConfigurationParam(), "BusinessAssociation", "Descendants"); AddBusinessParam(htmlDocumentAttr.CreateConfigurationParam(), "Engine", "Html"); return(htmlDocumentAttr); }
/// <summary> /// Parses WSE Module to get payload in string format /// </summary> /// <param name="wseModule">WSE Module</param> /// <param name="tql"> Not used</param> /// <returns>payload in string format</returns> public string Parse(XModule wseModule, string tql = "") { try { if (wseModule == null) { return(string.Empty); } XModuleAttribute jsonObject = wseModule .Search("=>SUBPARTS:XModuleAttribute").Cast <XModuleAttribute>() .FirstOrDefault(x => x.BusinessType == "JsonObject" || x.BusinessType == "JsonArray"); if (jsonObject != null) { JArray arrayObject = new JArray(); JObject plainObject = CommonUtilities.ConstructJsonStructure(jsonObject, new JObject()); if (jsonObject.BusinessType == "JsonObject") { return(Convert.ToString(plainObject)); } arrayObject.Add(plainObject.Values()); return(Convert.ToString(arrayObject)); } } catch (Exception e) { FileLogger.Instance.Error( $"Failed to create Json payload for request :{wseModule?.Name}", e); } return(string.Empty); }
public static XParam CreateTechnicalIDParam(this XModuleAttribute moduleAttribute) { var xp = XParam.Create(); xp.ParamType = XParam.ParamTypeE.TechnicalID; moduleAttribute.Properties.Add(xp); return(xp); }
public static XParam CreateConfigurationParam(this XModuleAttribute moduleAttribute) { var xp = XParam.Create(); xp.ParamType = XParam.ParamTypeE.Configuration; moduleAttribute.Properties.Add(xp); return(xp); }
public static XModuleAttribute CreateModuleAttribute(this XModuleAttribute moduleAttribute) { var ma = XModuleAttribute.Create(); moduleAttribute.Attributes.Add(ma); //ma.Module.Set(moduleAttribute.Module.Get()); return(ma); }
public static void AddXParamToModuleAttribute(this XModuleAttribute moduleAttribute, string name, string value, ParamTypeE xParamType) { XParam newXParam = moduleAttribute.CreateConfigurationParam(); newXParam.Name = name; newXParam.Value = value; newXParam.ParamType = xParamType; }
public void SetupCellAttributes(XModuleAttribute attribute, HtmlElement table) { attribute.Name = "<Cell>"; attribute.DefaultDataType = ModuleAttributeDataType.String; attribute.Cardinality = "0-N"; attribute.BusinessType = "Cell"; attribute.DefaultActionMode = XTestStepActionMode.Verify; AddBusinessParam(attribute.CreateConfigurationParam(), "Engine", "Html"); AddBusinessParam(attribute.CreateConfigurationParam(), "BusinessAssociation", "Cells"); AddBusinessParam(attribute.CreateConfigurationParam(), "ExplicitName", "#1;#<n>;#last"); }
private static void CreateBodyParameterAtTeststepLevel(List <XTestStepValue> tcObjects, XModuleAttribute bodyAttribute, XTestStep apiTestStep) { foreach (XTestStepValue wseTestStepValue in tcObjects) { var apiTestStepValue = apiTestStep.CreateXTestStepValue(bodyAttribute); if (wseTestStepValue.SpecializationModule.Name == "Web service request data in XML Resource" || wseTestStepValue.SpecializationModule.Name == "Web service request data in JSON Resource") { apiTestStepValue.DataType = wseTestStepValue.DataType; apiTestStepValue.ActionMode = XTestStepActionMode.Insert; if (!wseTestStepValue.Value.IsNullOrBlankInTosca()) { apiTestStepValue.Value = "{RES[" + wseTestStepValue.Value + "]}"; } break; } if (wseTestStepValue.SpecializationModule.Name == "Plain Text") { if (wseTestStepValue.SubValues != null && wseTestStepValue.SubValues.Any()) { apiTestStepValue.DataType = wseTestStepValue.SubValues.First().DataType; apiTestStepValue.ActionMode = wseTestStepValue.SubValues.First().ActionMode; //if (StringExtensions.IsNullOrBlank(wTestStepValue)) apiTestStepValue.Value = wseTestStepValue.SubValues.FirstOrDefault(x => x.Name == "Value")?.Value; } if (!string.IsNullOrEmpty(apiTestStepValue.Value)) { apiTestStepValue.Value = CommonUtilities.RemoveExtraDoubleQuotes(apiTestStepValue.Value); } break; } var wTestStepValue = wseTestStepValue.SubValues.FirstOrDefault(x => x.Name == "Filepath")?.Value; if (!wTestStepValue.IsNullOrBlankInTosca()) { apiTestStepValue.ActionProperty = $"File"; apiTestStepValue.Operator = Operator.Equals; apiTestStepValue.Value = wTestStepValue; apiTestStepValue.ActionMode = XTestStepActionMode.Select; } } }
private string Parse(XModuleAttribute root) { string payload = string.Empty; if (root == null) { return(payload); } XElement xmlStructure = XmlHelper.ConstructXmlStructure(root); xmlStructure = XmlHelper.RemoveNodeFromXml(xmlStructure, "Fault", "Body"); payload = Convert.ToString(xmlStructure); return(payload); }
//TODO: //When you run this you get an exception. Run in Tosca and fix it derp private IEnumerable <XModuleAttribute> CreateClassStructure(XModuleAttribute parent, Type type, XTestStepActionMode defaultMode, int currentDepth, bool isParameter) { currentDepth++; if (currentDepth > MAX_DEPTH) { yield break; } //Loop through properties var properties = type.GetProperties().Where(x => x.SetMethod.IsPublic); foreach (var property in properties) { var childAttribute = parent.CreateModuleAttribute(); BuildModuleAttribute(childAttribute, property.Name, property.PropertyType, currentDepth, isParameter, defaultMode); yield return(childAttribute); } }
private void BuildModuleAttribute(XModuleAttribute attribute, string name, Type type, int currentDepth, bool isParameter, XTestStepActionMode defaultMode = XTestStepActionMode.Input) { attribute.Name = name; if (isParameter) { var paramParam = attribute.CreateConfigurationParam(); paramParam.Name = "Parameter"; paramParam.Value = "True"; } var nameParam = attribute.CreateConfigurationParam(); nameParam.Name = "Name"; nameParam.Value = attribute.Name; if (type.IsSimple()) //This is a simple type { if (IsNumericType(type)) { attribute.DefaultDataType = ModuleAttributeDataType.Numeric; } else if (type == typeof(DateTime)) { attribute.DefaultDataType = ModuleAttributeDataType.Date; } else if (type == typeof(bool)) { attribute.DefaultDataType = ModuleAttributeDataType.Boolean; attribute.ValueRange = "true;false"; } else if (type.IsEnum) { attribute.DefaultDataType = ModuleAttributeDataType.String; attribute.ValueRange = Enum.GetNames(type).Aggregate((x, y) => $"{x};{y}"); } else { attribute.DefaultDataType = ModuleAttributeDataType.String; } attribute.DefaultActionMode = defaultMode; } else //class { attribute.DefaultActionMode = XTestStepActionMode.Select; var structureModules = CreateClassStructure(attribute, type, defaultMode, currentDepth, isParameter).ToArray(); } }
/// <summary> /// Extracts method from WSE Module /// </summary> /// <param name="xModule">WSE Module</param> /// <returns>HTTP Method</returns> public string Parse(XModule xModule) { try { XModuleAttribute method = (XModuleAttribute) xModule.Search(AddOnConstants.MethodTql).FirstOrDefault(); if (method == null) { return(string.Empty); } return(method.DefaultValue); } catch (Exception ex) { FileLogger.Instance.Error(ex); } return(string.Empty); }
/// <summary> /// Creates Xml structure from Wse XModuleAttributes for API Modules Payload section /// </summary> /// <param name="xModuleAttribute">Wse XModuleAttributes</param> /// <param name="xmlStructure">XElement used to create Payload Structure</param> /// <param name="parent">XElement used to identify and create parent nodes in xml structure </param> /// <returns>xml structure for API Modules Payload Section</returns> public static XElement ConstructXmlStructure(XModuleAttribute xModuleAttribute, XElement xmlStructure = null, XElement parent = null) { XNamespace xNamespace = GetNamespace(xModuleAttribute); List <XAttribute> xAttribute = GetXAttribute(xModuleAttribute); if (xmlStructure == null) { xmlStructure = new XElement(xNamespace != null ? xNamespace + xModuleAttribute.Name : xModuleAttribute.Name, xAttribute, xModuleAttribute.DefaultValue == "{NULL}" ? "" : xModuleAttribute.DefaultValue); parent = xmlStructure; } else if (xmlStructure == parent) { parent = new XElement( xNamespace != null ? xNamespace + xModuleAttribute.Name : xModuleAttribute.Name, xAttribute, xModuleAttribute.DefaultValue == "{NULL}" ? "" : xModuleAttribute.DefaultValue); xmlStructure.Add(parent); } else { var e = new XElement( xNamespace != null ? xNamespace + xModuleAttribute.Name : xModuleAttribute.Name, xAttribute, xModuleAttribute.DefaultValue == "{NULL}" ? "" : xModuleAttribute.DefaultValue); parent?.Add(e); parent = e; } IEnumerable <XModuleAttribute> childSubAttributes = xModuleAttribute.SubAttributes.Where(x => x.BusinessType == "XmlElement"); foreach (var childSubAttribute in childSubAttributes) { xmlStructure = ConstructXmlStructure(childSubAttribute, xmlStructure, parent); } return(xmlStructure); }
/// <summary> /// Gets Response Status Code from WSE Module /// </summary> /// <param name="xModule">WSE Module</param> /// <returns>Status code</returns> public string ParseResponseStatus(XModule xModule) { try { TCObject statusCodeModuleAttribute = xModule.Search(AddOnConstants.ResponseStatusTql) .FirstOrDefault(); if (statusCodeModuleAttribute == null) { return(string.Empty); } XModuleAttribute statusCodeXModuleAttribute = statusCodeModuleAttribute as XModuleAttribute; return(statusCodeXModuleAttribute?.DefaultValue); } catch (Exception ex) { FileLogger.Instance.Error("Failed to retrieve response Status Code :", ex); } return(string.Empty); }
/// <summary> /// Gets headers from WSE Module /// </summary> /// <param name="wseModule">WSE Module</param> /// <param name="tql">tql to get Header XModuleAttribute from WSE Module</param> /// <returns>All headers as a dictionary</returns> public Dictionary <string, string> Parse(XModule wseModule, string tql) { var headersDict = new Dictionary <string, string>(); try { List <TCObject> headers = wseModule.Search(tql); foreach (TCObject header in headers) { XModuleAttribute headerAttribute = (XModuleAttribute)header; if (string.IsNullOrEmpty(headerAttribute.DefaultValue)) { continue; } headersDict.Add(headerAttribute.Name, CommonUtilities.RemoveExtraDoubleQuotes(headerAttribute.DefaultValue)); } } catch (Exception) { // do nothing as this could happen possibly, just move on with the other attributes } return(headersDict); }
public void Execute(XTestStep apiTestStep, XTestStep wseTestStep) { try { //Get Raw Value XTestStepValue wseTestStepValue = (XTestStepValue)wseTestStep.Search(TqlToGetWseTestStepValue).FirstOrDefault(); if (wseTestStepValue == null) { return; } //Extract value need to be filled in api test step string wseValue = GetRefinedWseTestStepValue(wseTestStepValue.Value); //Note: we ignore this step in case of response. if (!IsResponseTestStepValue) { //get api value string apiValue = GetValueInApiModule(apiTestStep.Module as ApiModule); //if values match,do nothing. if (!Regex.IsMatch(wseValue, @"\{.*\}") && apiValue == wseValue) { return; } } //if values does not match , Search if there is already a API xTestStepValue present. XTestStepValue apiTeststepValue = (XTestStepValue)apiTestStep.Search(TqlToGetApiTestStepValue).FirstOrDefault(); //if xTestStepValue not present,create it. if (apiTeststepValue == null) { //check if module attribute is present. var xModuleAttribute = (XModuleAttribute)apiTestStep .Module.Search(TqlToGetModuleAttributeInApiModule).FirstOrDefault(); //if not present, Create it. if (xModuleAttribute == null) { XModuleAttribute m = CreateModuleAttribute(apiTestStep.Module as ApiModule, wseTestStepValue); apiTeststepValue = apiTestStep.CreateXTestStepValue(m); } //if already present, use it. else { apiTeststepValue = apiTestStep.CreateXTestStepValue(xModuleAttribute); } } //API xTestStepValue is present now. Assign Values to it now. apiTeststepValue.Disabled = wseTestStepValue.Disabled; apiTeststepValue.Value = wseValue; apiTeststepValue.ActionMode = wseTestStepValue.ActionMode; apiTeststepValue.Condition = wseTestStepValue.Condition; apiTeststepValue.Path = wseTestStepValue.Path; } catch (Exception e) { FileLogger.Instance.Error("Setting value failed : ", e); } }
/// <summary> /// Creates and set module attributes at module and teststep /// </summary> /// <param name="wseTestStepValue">TestStepValues of WSE Teststep</param> /// <param name="tcPath">XPath and JsonPath for xml and Json respectively</param> /// <param name="xModule">xModule and xModuleAttribute of ApiEngine</param> /// <param name="xModuleDemo">ApiModule</param> /// <param name="parameterPathType">Enum which define creation of xml and json moduleattributes</param> /// <param name="parentTest">TestStep and TestStepValues for ApiTestStep</param> /// <param name="cardinality">Cardinality of WSE XModule</param> /// <param name="isArray">True if Wse Xmodule attributes is array</param> /// <returns></returns> public static XTestStepValue CreateBusinessParameterInTosca(XTestStepValue wseTestStepValue, string tcPath, dynamic xModule, ApiModule xModuleDemo, BusinessParameterPathTypes parameterPathType, dynamic parentTest, string cardinality, bool isArray) { string businessParameterName = wseTestStepValue.Name; string businessParameterValue = wseTestStepValue.Value == "{NULL}" ? string.Empty : wseTestStepValue.Value; XTestStepActionMode xTestStepActionMode = wseTestStepValue.ActionModeToUse; ModuleAttributeDataType attributeDataType = wseTestStepValue.DataType; var mAttributes = xModuleDemo.Search("=>SUBPARTS:XModuleAttribute").Cast <XModuleAttribute>(); XModuleAttribute xModuleAttribute = GetExistingXModuleAttribute(mAttributes, tcPath, parameterPathType); if (xModuleAttribute != null) { if (!string.IsNullOrEmpty(xModuleAttribute.ValueRange)) { if (!xModuleAttribute.ValueRange.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries) .Contains(businessParameterValue)) { xModuleAttribute.ValueRange = $"{xModuleAttribute.ValueRange};{businessParameterValue}"; } } else { xModuleAttribute.ValueRange = $"{businessParameterValue}"; } } else { xModuleAttribute = xModule.CreateModuleAttribute(); xModuleAttribute.Name = businessParameterName; xModuleAttribute.AddXParamToModuleAttribute("PathType", parameterPathType == BusinessParameterPathTypes.XPATH ? "XPath" : parameterPathType == BusinessParameterPathTypes.JSONPATH ? "JsonPath" : "", ParamTypeE.TechnicalID); xModuleAttribute.AddXParamToModuleAttribute("Path", tcPath, ParamTypeE.TechnicalID); xModuleAttribute.AddXParamToModuleAttribute("ExplicitName", "True", ParamTypeE.Configuration); xModuleAttribute.DefaultActionMode = xTestStepActionMode; xModuleAttribute.DefaultDataType = wseTestStepValue.ModuleAttribute.DefaultDataType; xModuleAttribute.DefaultValue = wseTestStepValue.ModuleAttribute?.DefaultValue == "{NULL}" ? string.Empty : wseTestStepValue.ModuleAttribute?.DefaultValue; xModuleAttribute.ValueRange = $"{businessParameterValue}"; xModuleAttribute.Cardinality = isArray ? "0-N" : cardinality; xModuleAttribute.EnsureUniqueName(); } XTestStepValue testStepValue = parentTest.CreateXTestStepValue(xModuleAttribute); testStepValue.ActionProperty = wseTestStepValue.ActionProperty; testStepValue.Operator = wseTestStepValue.Operator; testStepValue.Value = wseTestStepValue.Value; testStepValue.ActionMode = xTestStepActionMode; testStepValue.DataType = attributeDataType; testStepValue.Disabled = wseTestStepValue.Disabled; SetConstraintIndexProperty(wseTestStepValue, testStepValue); return(testStepValue); }
private static XNamespace GetNamespace(XModuleAttribute xdoc) { XParam namespaceXparam = xdoc.XParams.FirstOrDefault(x => x.Name == "NamespaceURI"); return(namespaceXparam?.Value); }