private XElement GetCaseElement(Case testCase) { XElement element = new XElement("case"); element.Add(new XAttribute("id", testCase.Id)); element.Add(new XAttribute("shortdescription", testCase.ShortDescription ?? "")); element.Add(new XAttribute("longdescription", testCase.LongDescription ?? "")); element.Add(new XAttribute("url", testCase.Url ?? "")); element.Add(new XAttribute("method", testCase.Method ?? "")); element.Add(new XAttribute("posttype", testCase.PostType ?? "")); element.Add(new XAttribute("verifyresponsecode", (int)testCase.VerifyResponseCode)); element.Add(new XAttribute("errormessage", testCase.ErrorMessage ?? "")); element.Add(new XAttribute("logrequest", testCase.LogRequest)); element.Add(new XAttribute("logresponse", testCase.LogResponse)); element.Add(new XAttribute("sleep", testCase.Sleep)); return element; }
public TestCaseViewModel BuildTestCase(Case testCase) { if (testCase == null) { throw new ArgumentNullException("testCase"); } var verifications = new List<Models.VerificationItem>(); IEnumerable<Models.VerificationItem> verifyPositives = GetVerificationItems(testCase.VerifyPositives); IEnumerable<Models.VerificationItem> verifyNegatives = GetVerificationItems(testCase.VerifyNegatives); verifications.AddRange(verifyPositives); verifications.AddRange(verifyNegatives); var headerList = new List<Models.HeaderItem>(testCase.Headers.Select(x => new Models.HeaderItem { Key = x.Key, Value = x.Value })); var parsedResponses = new List<Models.ParseResponseItem>(testCase.ParseResponses.Select(x => new Models.ParseResponseItem { Description = x.Description, Regex = x.Regex })); var model = new TestCaseViewModel { Id = testCase.Id, ErrorMessage = testCase.ErrorMessage, Headers = headerList, LogRequest = testCase.LogRequest, LogResponse = testCase.LogResponse, LongDescription = testCase.LongDescription, Method = testCase.Method, ParseResponses = parsedResponses, PostBody = testCase.PostBody, PostType = testCase.PostType == PostType.GET.ToString() ? PostType.GET : PostType.POST, VerifyResponseCode = testCase.VerifyResponseCode, ShortDescription = testCase.ShortDescription, Sleep = testCase.Sleep, Url = testCase.Url, Verifications = verifications, ParentFilename = testCase.ParentFilename }; return model; }
private CaseCollection CreateCaseCollection(Case[] cases) { var testCases = new List<Case>(); testCases.AddRange(cases); var collection = new CaseCollection(); collection.TestCases = testCases; return collection; }
public void ShouldLogResponse_should_return_true_when_testcase_logresponse_is_true() { // Arrange var config = new Config(); config.GlobalHttpLog = LogType.None; TestSessionRunner runner = CreateRunner(config); var testResult = new TestCaseResult(); var testCase = new Case() { LogResponse = true }; // Act bool shouldLog = runner.ShouldLogResponse(testResult, testCase); // Assert Assert.That(shouldLog, Is.True); }
public void ShouldLogResponse_should_return_true_when_case_fails_and_onfail() { // Arrange var config = new Config(); config.GlobalHttpLog = LogType.OnFail; TestSessionRunner runner = CreateRunner(config); var testResult = new TestCaseResult() { ResponseCodeSuccess = false }; var testCase = new Case(); // Act bool shouldLog = runner.ShouldLogResponse(testResult, testCase); // Assert Assert.That(shouldLog, Is.True); }
public void ShouldLogRequest_should_return_true_when_logtype_is_all() { // Arrange var config = new Config(); config.GlobalHttpLog = LogType.All; TestSessionRunner runner = CreateRunner(config); var testResult = new TestCaseResult() {ResponseCodeSuccess = true }; var testCase = new Case(); // Act bool shouldLog = runner.ShouldLogRequest(testResult, testCase); // Assert Assert.That(shouldLog, Is.True); }
private Case GetTestCase(XElement element) { var testCase = new Case(); // Required Properties testCase.Id = XmlHelper.AttributeAsInt(element, "id"); testCase.Url = XmlHelper.GetOptionalAttribute(element, "url"); if (string.IsNullOrEmpty(testCase.Url)) throw new TestCaseException("The url parameter is missing for test case {0}", testCase.Id); // Optionals testCase.Method = XmlHelper.GetOptionalAttribute(element, "method", "get"); testCase.PostBody = XmlHelper.GetOptionalAttribute(element, "postbody"); testCase.ErrorMessage = XmlHelper.GetOptionalAttribute(element, "errormessage"); testCase.PostType = XmlHelper.GetOptionalAttribute(element, "posttype", "application/x-www-form-urlencoded"); testCase.VerifyResponseCode = GetVerifyResponseCode(element); testCase.LogRequest = YesToBool(element, "logrequest"); testCase.LogResponse = YesToBool(element, "logresponse"); testCase.Sleep = XmlHelper.AttributeAsInt(element, "sleep"); testCase.Headers = ParseAddHeader(element); // Descriptions - support either description,description1 or description1/description2 testCase.ShortDescription = XmlHelper.GetOptionalAttribute(element, "description1"); testCase.LongDescription = XmlHelper.GetOptionalAttribute(element, "description2"); if (string.IsNullOrEmpty(testCase.ShortDescription)) { testCase.ShortDescription = XmlHelper.GetOptionalAttribute(element, "description"); if (string.IsNullOrEmpty(testCase.LongDescription)) { testCase.LongDescription = XmlHelper.GetOptionalAttribute(element, "description1"); } } // Numbered attributes List<ParseResponseItem> parseResponses = GetParseResponseItems(element, "parseresponse"); testCase.ParseResponses = ConvertParseResponsesToRegexes(parseResponses); testCase.VerifyPositives = GetVerificationItems(element, "verifypositive", VerifyType.Positive); testCase.VerifyNegatives = GetVerificationItems(element, "verifynegative", VerifyType.Negative); return testCase; }
public bool SaveTestCase(Case testCase, string teamName) { if (testCase == null) { throw new ArgumentNullException("testCase"); } string fullPath = Path.Combine(_appConfig.TestCasesBaseDirectory, teamName, testCase.ParentFilename); if (!File.Exists(fullPath)) throw new FileNotFoundException("The test case cannot be found", testCase.ParentFilename); CaseCollection collection; string xml = File.ReadAllText(fullPath); using (var stringReader = new StringReader(xml)) { collection = _testCaseReader.Read(stringReader); foreach (var item in collection.TestCases.Where(x => x.Id == testCase.Id)) { item.Id = testCase.Id; item.ShortDescription = testCase.ShortDescription; item.ErrorMessage = testCase.ErrorMessage; item.Headers = testCase.Headers.Select(x => new HeaderItem(x.Key, x.Value)).ToList(); item.LogRequest = testCase.LogRequest; item.LogResponse = testCase.LogResponse; item.LongDescription = testCase.LongDescription; item.Method = testCase.Method; item.ParentFilename = testCase.ParentFilename; item.ParseResponses = testCase.ParseResponses; item.PostBody = testCase.PostBody; item.VerifyPositives = testCase.VerifyPositives; item.VerifyNegatives = testCase.VerifyNegatives; item.ShortDescription = testCase.ShortDescription; item.Url = testCase.Url; item.Sleep = testCase.Sleep; item.PostType = testCase.PostType; item.VerifyResponseCode = testCase.VerifyResponseCode; } } string contents = _testCaseWriter.Write(collection); try { File.WriteAllText(fullPath, contents); return true; } catch (Exception exception) { //todo log error Log.Error(exception, exception.Message); } return false; }
private XElement GetHeadersElement(Case testCase) { XElement headerElement = new XElement("headers"); foreach (HeaderItem keyValuePair in testCase.Headers) { if (!string.IsNullOrEmpty(keyValuePair.Key)) { XElement element = new XElement("header"); element.Add(new XAttribute("name", keyValuePair.Key)); AddCDataToElementValue(keyValuePair.Value, element); headerElement.Add(element); } } return headerElement; }
private XElement GetPostBodyElement(Case testCase) { XElement postBodyElement = new XElement("postbody"); if (!string.IsNullOrEmpty(testCase.PostBody)) postBodyElement.Add(new XCData(testCase.PostBody)); return postBodyElement; }
private XElement GetVerificationElement(Case testCase) { XElement headerElement = new XElement("verifications"); foreach (VerificationItem verifyItem in testCase.VerifyPositives.Union(testCase.VerifyNegatives)) { if (!string.IsNullOrEmpty(verifyItem.Description)) { XElement element = new XElement("verify"); element.Add(new XAttribute("description", verifyItem.Description)); element.Add(new XAttribute("type", verifyItem.VerifyType.ToString().ToLower())); AddCDataToElementValue(verifyItem.Regex, element); headerElement.Add(element); } } return headerElement; }
private XElement GetParseResponsesElement(Case testCase) { XElement parseresponsesElement = new XElement("parseresponses"); foreach (ParseResponseItem item in testCase.ParseResponses) { if (!string.IsNullOrEmpty(item.Regex)) { XElement element = new XElement("parseresponse"); element.Add(new XAttribute("description", item.Description)); element.Value = item.Regex; parseresponsesElement.Add(element); } } return parseresponsesElement; }
public void BuildTestCase_should_return_correct_model_values_from_case() { // given var testCaseViewModelBuilder = new TestCaseViewModelBuilder(); var testCase = new Case { Id = 1, ShortDescription = "Short Description", Url = "http://www.google.com", ErrorMessage = "Error", LogRequest = true, LogResponse = true, LongDescription = "Long Description", Method = "Method", PostBody = "PostBody", PostType = PostType.GET.ToString(), VerifyResponseCode = HttpStatusCode.Accepted, Sleep = 10, Headers = new List<HeaderItem>(), ParseResponses = new List<ParseResponseItem> { new ParseResponseItem() }, VerifyNegatives = new List<VerificationItem> { new VerificationItem() }, VerifyPositives = new List<VerificationItem> { new VerificationItem() }, ParentFilename = "test.xml" }; // when var testCaseViewModel = testCaseViewModelBuilder.BuildTestCase(testCase); // then Assert.NotNull(testCaseViewModel); Assert.AreEqual(testCase.Id, testCaseViewModel.Id); Assert.AreEqual(testCase.ShortDescription, testCaseViewModel.ShortDescription); Assert.AreEqual(testCase.Url, testCaseViewModel.Url); Assert.AreEqual(testCase.ErrorMessage, testCaseViewModel.ErrorMessage); Assert.AreEqual(testCase.LogRequest, testCaseViewModel.LogRequest); Assert.AreEqual(testCase.LogResponse, testCaseViewModel.LogResponse); Assert.AreEqual(testCase.LongDescription, testCaseViewModel.LongDescription); Assert.AreEqual(testCase.Method, testCaseViewModel.Method); Assert.AreEqual(testCase.PostBody, testCaseViewModel.PostBody); Assert.AreEqual(PostType.GET, testCaseViewModel.PostType); Assert.AreEqual(testCase.VerifyResponseCode, testCaseViewModel.VerifyResponseCode); Assert.AreEqual(testCase.ParentFilename, testCaseViewModel.ParentFilename); Assert.AreEqual(1, testCaseViewModel.ParseResponses.Count); Assert.AreEqual(2, testCaseViewModel.Verifications.Count); }
private Case GetTestCase(XElement element) { var testCase = new Case(); // Required Properties testCase.Id = XmlHelper.AttributeAsInt(element, "id"); testCase.Url = XmlHelper.GetOptionalAttribute(element, "url"); if (string.IsNullOrEmpty(testCase.Url)) throw new TestCaseException("The url parameter is missing for test case {0}", testCase.Id); // Optionals testCase.Method = XmlHelper.GetOptionalAttribute(element, "method", "get"); testCase.PostBody = XmlHelper.GetOptionalElementValue(element, "postbody").Trim(); testCase.ErrorMessage = XmlHelper.GetOptionalAttribute(element, "errormessage"); testCase.PostType = XmlHelper.GetOptionalAttribute(element, "posttype", "application/x-www-form-urlencoded"); testCase.VerifyResponseCode = GetVerifyResponseCode(element); testCase.LogRequest = GetBoolValue(element, "logrequest"); testCase.LogResponse = GetBoolValue(element, "logresponse"); testCase.Sleep = XmlHelper.AttributeAsInt(element, "sleep"); testCase.Headers = GetHeader(element); // Numbered attributes testCase.ShortDescription = XmlHelper.GetOptionalAttribute(element, "shortdescription"); testCase.LongDescription = XmlHelper.GetOptionalAttribute(element, "longdescription"); testCase.ParseResponses = GetParseResponseCollection(element); List<VerificationItem> verifications = GetVerificationCollection(element); testCase.VerifyPositives = verifications.Where(x => x.VerifyType == VerifyType.Positive).ToList(); testCase.VerifyNegatives = verifications.Where(x => x.VerifyType == VerifyType.Negative).ToList(); return testCase; }
internal bool ShouldLogResponse(TestCaseResult testResult, Case testCase) { return (testResult.ResponseCodeSuccess == false && _config.GlobalHttpLog == LogType.OnFail) || _config.GlobalHttpLog == LogType.All || testCase.LogResponse; }
internal TestCaseResult RunCase(Case testCase, SessionVariables variables, VerificationsMatcher verificationMatcher) { var testResult = new TestCaseResult(); testResult.TestCase = testCase; try { string resolvedUrl = variables.ReplacePlainTextVariablesIn(testCase.Url); testResult.ActualUrl = resolvedUrl; HttpResponse response = _httpClient.ExecuteRequest(testCase.Method, resolvedUrl, testCase.PostType, testCase.PostBody, testCase.Headers); testResult.ResponseTime = response.ResponseTime; testResult.HttpResponse = response; if (response.StatusCode == testCase.VerifyResponseCode) { testResult.ResponseCodeSuccess = true; string content = response.ToString(); // Put the parseresponse regex values in the current variable set Dictionary<string, string> parsedVariables = ParseResponseMatcher.MatchParseResponses(testCase.ParseResponses, content); variables.AddOrUpdateVariables(parsedVariables); // Verify positives testResult.VerifyPositiveResults = verificationMatcher.MatchPositive(testCase.VerifyPositives, content); // Verify Negatives testResult.VerifyNegativeResults = verificationMatcher.MatchNegative(testCase.VerifyNegatives, content); } else { testResult.ResponseCodeSuccess = false; } if (testResult.Success == false) { testResult.Message = testCase.ErrorMessage; } if (ShouldLogRequest(testResult, testCase)) { _httpClient.LogLastRequest(); } if (ShouldLogResponse(testResult, testCase)) { _httpClient.LogLastResponse(); } _resultWriter.Write(testResult); if (testCase.Sleep > 0) Thread.Sleep(testCase.Sleep * 1000); } catch (Exception ex) { testResult.ResponseCodeSuccess = false; testResult.ExceptionMessage = ex.Message; } return testResult; }