public Config Read(TextReader textReader) { var config = new Config(); XDocument doc = XDocument.Load(textReader); // Check for <root> XElement configElement = doc.Elements().FirstOrDefault(i => i.Name.LocalName == "Config"); if (configElement == null) throw new ConfigurationException("<Config> node is missing from the config file."); // Fill the known properties config.BaseUrl = XmlHelper.GetOptionalElementValue(configElement, "baseurl"); config.Proxy = XmlHelper.GetOptionalElementValue(configElement, "proxy"); config.Useragent = XmlHelper.GetOptionalElementValue(configElement, "useragent"); config.Httpauth = XmlHelper.GetOptionalElementValue(configElement, "httpauth"); config.Comment = XmlHelper.GetOptionalElementValue(configElement, "comment"); config.Timeout = XmlHelper.ElementAsInt(configElement, "timeout"); config.GlobalTimeout = XmlHelper.ElementAsInt(configElement, "globaltimeout"); var logType = LogType.None; string httpLog = XmlHelper.GetOptionalElementValue(configElement, "globalhttplog"); if (!string.IsNullOrEmpty(httpLog)) { Enum.TryParse(httpLog, true, out logType); } config.GlobalHttpLog = logType; // All elements get stored in the variables, for custom variables. foreach (XElement element in configElement.Elements().Where(x => x.Name.LocalName == "Variables").Descendants()) { config.Variables.Add(new Variable(element.Attribute("name").Value, element.Value)); } return config; }
public void Run_should_populate_StartTime_and_EndTime_and_TotalRunTime() { // Arrange var beforeStart = DateTime.UtcNow; var config = new Config(); var testCaseReader = new TestCaseReaderMock(); var stringReader = new StringReader(""); var response = new HttpResponse(); response.ResponseTime = TimeSpan.FromSeconds(5); HttpClientMock httpClient = new HttpClientMock(response); IResultWriter resultWriter = new ResultWriterStub(); var runner = new TestSessionRunner(config, httpClient, resultWriter); var caseCollection = CreateCaseCollection(new[] { new Case() { Url = "foo1" }, }); // Act TestCaseSession session = runner.Run(caseCollection); // Assert Assert.That(session.StartTime, Is.GreaterThanOrEqualTo(beforeStart)); Assert.That(session.EndTime, Is.GreaterThanOrEqualTo(session.StartTime)); Assert.That(session.TotalRunTime, Is.EqualTo(session.EndTime - session.StartTime)); }
public void AddGlobalVariables(Config config) { if (!string.IsNullOrEmpty(config.BaseUrl)) _currentVariables.Add("baseurl", config.BaseUrl); foreach (Variable variable in config.Variables) { _currentVariables.Add(variable.Name, variable.Value); } }
public void AddGlobalVariables_should_add_variables_from_config() { // Arrange var config = new Config(); config.Variables.Add(new Variable("eggs", "ham")); var sessionVariables = new SessionVariables(); // Act sessionVariables.AddGlobalVariables(config); // Assert Assert.That(sessionVariables.GetVariableValue("eggs"), Is.EqualTo("ham")); }
public void AddGlobalVariables_should_add_baseurl_as_variable() { // Arrange var config = new Config(); config.BaseUrl = "mybaseurl"; var sessionVariables = new SessionVariables(); // Act sessionVariables.AddGlobalVariables(config); // Assert Assert.That(sessionVariables.GetVariableValue("baseurl"), Is.EqualTo("mybaseurl")); }
public TestSessionRunner(Config config, IHttpClient httpClient, IResultWriter resultWriter) { if (config == null) throw new ArgumentNullException("config"); if (httpClient == null) throw new ArgumentNullException("httpClient"); if (resultWriter == null) throw new ArgumentNullException("resultWriter"); _config = config; _httpClient = httpClient; _resultWriter = resultWriter; _currentResults = new List<TestCaseResult>(); }
public void Run_should_log_request_and_responses_using_httpclient_when_logging_is_enabled() { // Arrange var config = new Config(); config.GlobalHttpLog = LogType.All; TestSessionRunner runner = CreateRunner(config); var caseCollection = CreateCaseCollection(new[] { new Case() { Url = "foo1" } }); // Act runner.Run(caseCollection); // Assert Assert.That(_httpClientMock.LogLastRequestCalled, Is.True); Assert.That(_httpClientMock.LogLastResponseCalled, Is.True); }
public void should_parse_responses() { // Arrange var stringBuilder = new StringBuilder(); var httpLogWriter = new HttpLogWriter(new StringWriter(stringBuilder)); var httpClient = new HttpClient(httpLogWriter, new RestClient()); var config = new Config(); string xml = TestHelpers.ReadEmbeddedFile("parseresponses.xml", XmlExamplesFolder); var stringReader = new StringReader(xml); var reader = new TestCaseReader(); var caseCollection = reader.Read(stringReader); var runner = new TestSessionRunner(config, httpClient, new ConsoleResultWriter()); // Act TestCaseSession result = runner.Run(caseCollection); // Assert DumpAsYaml(result); }
public void should_do_something() { // Arrange var stringBuilder = new StringBuilder(); var httpLogWriter = new HttpLogWriter(new StringWriter(stringBuilder)); var httpClient = new HttpClient(httpLogWriter, new RestClient()); var config = new Config(); string xml = TestHelpers.ReadEmbeddedFile("wikipedia-simple.xml", XmlExamplesFolder); var stringReader = new StringReader(xml); var reader = new LegacyTestCaseReader(); var caseCollection = reader.Read(stringReader); var runner = new TestSessionRunner(config, httpClient, new ConsoleResultWriter()); // Act runner.Run(caseCollection); // Assert Console.WriteLine(stringBuilder); }
public void Run_should_set_testresult_success_and_response_when_httpcode_passes() { // Arrange var config = new Config(); TestSessionRunner runner = CreateRunner(config); _httpClientMock.Response.StatusCode = HttpStatusCode.OK; var caseCollection = CreateCaseCollection(new[] { new Case() { Url = "foo1", VerifyResponseCode = HttpStatusCode.OK }, }); // Act TestCaseSession session = runner.Run(caseCollection); // Assert Assert.That(session.TestCaseResults[0].Success, Is.True); Assert.That(session.TestCaseResults[0].HttpResponse, Is.EqualTo(_httpClientMock.Response)); }
public void Run_should_set_parseresponsevariables_across_testcases() { // Arrange var config = new Config(); TestSessionRunner runner = CreateRunner(config); _httpClientMock.Responses = new List<HttpResponse>() { new HttpResponse() { StatusCode = HttpStatusCode.OK, Content = "1st content SECRET_KEY" }, new HttpResponse() { StatusCode = HttpStatusCode.OK, Content = "2nd content - SECRET_KEY in here to match" }, new HttpResponse() { StatusCode = HttpStatusCode.OK, Content = "3rd content - SECRET_KEY in here to match" } }; var caseCollection = CreateCaseCollection(new[] { new Case() { Url = "case1", VerifyResponseCode = HttpStatusCode.OK, ParseResponses = new List<ParseResponseItem>() { new ParseResponseItem("1", @"(SECRET_KEY)") }, }, new Case() { Url = "case2", VerifyResponseCode = HttpStatusCode.OK, ParseResponses = new List<ParseResponseItem>() { new ParseResponseItem("2", @"(SECRET_KEY)") }, VerifyPositives = new List<VerificationItem>() { // Test the parsedresponse variable from the 1st case new VerificationItem("positive-for-case-2", "{parsedresponse1}", VerifyType.Positive) }, }, new Case() { Url = "case3", VerifyResponseCode = HttpStatusCode.OK, VerifyPositives = new List<VerificationItem>() { // Test the parseresponse variable from the 1st case new VerificationItem("positive-for-case-3", "{parsedresponse2}", VerifyType.Positive) }, } }); // Act TestCaseSession session = runner.Run(caseCollection); // Assert Assert.That(session.TestCaseResults[1].VerifyPositiveResults[0].Success, Is.True); Assert.That(session.TestCaseResults[2].VerifyPositiveResults[0].Success, Is.True); }
/// <summary> /// Creates a new <see cref="TestSessionRunner"/> using the defaults. /// </summary> /// <returns></returns> public static TestSessionRunner CreateNew() { var config = new Config(); var logStringBuilder = new StringBuilder(); var httpLogWriter = new HttpLogWriter(new StringWriter(logStringBuilder)); var httpClient = new HttpClient(httpLogWriter, new RestClient()); return new TestSessionRunner(config, httpClient, new TextFileResultWriter()); }
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 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); }
public void Run_should_verify_positive_and_negative_items_when_httpcode_passes() { // Arrange var config = new Config(); TestSessionRunner runner = CreateRunner(config); _httpClientMock.Response.StatusCode = HttpStatusCode.OK; _httpClientMock.Response.Content = "some content"; var caseCollection = CreateCaseCollection(new[] { new Case() { Url = "foo1", VerifyResponseCode = HttpStatusCode.OK, VerifyPositives = new List<VerificationItem>() { new VerificationItem("positive-1", "some content", VerifyType.Positive) }, VerifyNegatives = new List<VerificationItem>() { new VerificationItem("negative-1", "no text like this", VerifyType.Negative) } }, }); // Act TestCaseSession session = runner.Run(caseCollection); // Assert Assert.That(session.TestCaseResults[0].Success, Is.True); Assert.That(session.TestCaseResults[0].VerifyPositiveResults.Count, Is.EqualTo(1)); Assert.That(session.TestCaseResults[0].VerifyPositiveResults[0].Success, Is.True); Assert.That(session.TestCaseResults[0].VerifyNegativeResults.Count, Is.EqualTo(1)); Assert.That(session.TestCaseResults[0].VerifyNegativeResults[0].Success, Is.True); }
/// <summary> /// Starts the test case XML file run. /// </summary> internal void StartSession(SessionRunnerTaskInfo item) { try { // TODO: this run could be for a user run only, not the entire team (read the XML from their folder?) string username = item.Username; string teamName = item.TeamName; // Read in the XML file from the team folder, e.g. "c:\testcases\myteam\test1.xml" string fullPath = Path.Combine(_appConfig.TestCasesBaseDirectory, teamName, item.Request.Filename); string xml = File.ReadAllText(fullPath); using (var stringReader = new StringReader(xml)) { var testCaseReader = new TestCaseReader(); CaseCollection caseCollection = testCaseReader.Read(stringReader); var config = new Config(); var logStringBuilder = new StringBuilder(); var httpLogWriter = new HttpLogWriter(new StringWriter(logStringBuilder)); var httpClient = new HttpClient(httpLogWriter, new RestClient()); var runner = new TestSessionRunner(config, httpClient, new TextFileResultWriter()); item.Runner = runner; runner.Run(caseCollection); } } catch (Exception e) { item.Errors = e.ToString(); } }
public void Run_should_sleep_thread_in_seconds_when_case_has_sleep_set() { // Arrange int seconds = 2; var now = DateTime.UtcNow; var config = new Config(); TestSessionRunner runner = CreateRunner(config); var caseCollection = CreateCaseCollection(new[] { new Case() { Url = "foo1", Sleep = seconds } }); // Act runner.Run(caseCollection); var timeAfterRun = DateTime.UtcNow; // Assert Assert.That(timeAfterRun, Is.GreaterThanOrEqualTo(now.AddSeconds(seconds))); }
public void should_post() { // Arrange //_streamWriter = new StreamWriter(new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write)); var stringBuilder = new StringBuilder(); var stringWriter = new StringWriter(stringBuilder); var config = new Config(); config.GlobalHttpLog = LogType.All; var httpClient = new HttpClient(new HttpLogWriter(stringWriter), new RestClient()); var resultWriter = new TextResultWriter(stringWriter); string xml = TestHelpers.ReadEmbeddedFile("roadkill-login.xml", XmlExamplesFolder); var stringReader = new StringReader(xml); var reader = new TestCaseReader(); var runner = new TestSessionRunner(config, httpClient, resultWriter); var caseCollection = reader.Read(stringReader); // Act TestCaseSession session = runner.Run(caseCollection); // Assert DumpAsXml(session); DumpAsYaml(session); Console.WriteLine(stringBuilder); }
public void Run_should_write_test_result_to_resultwriter() { // Arrange var config = new Config(); TestSessionRunner runner = CreateRunner(config); var caseCollection = CreateCaseCollection(new[] { new Case() { Url = "foo1"}, }); // Act runner.Run(caseCollection); // Assert Assert.That(_resultWriterStub.StringBuilder.ToString(), Is.Not.Empty.Or.Null); }
public void Run_should_replace_variables_in_url() { // Arrange var config = new Config(); config.BaseUrl = "http://www.yahoo.com"; TestSessionRunner runner = CreateRunner(config); var caseCollection = CreateCaseCollection(new[] { new Case() { Url = "{baseurl}/foo/test.html" }, }); // Act TestCaseSession session = runner.Run(caseCollection); // Assert Assert.That(session.TestCaseResults[0].ActualUrl, Is.EqualTo("http://www.yahoo.com/foo/test.html")); }
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 Run_should_set_message_from_case_errormessage_when_httpcode_fails() { // Arrange var config = new Config(); TestSessionRunner runner = CreateRunner(config); var caseCollection = CreateCaseCollection(new[] { new Case() { Url = "foo1", ErrorMessage = "It broke", VerifyResponseCode = HttpStatusCode.Ambiguous}, }); // Act TestCaseSession session = runner.Run(caseCollection); // Assert Assert.That(session.TestCaseResults[0].Message, Is.EqualTo("It broke")); }
private TestSessionRunner CreateRunner(Config config) { _httpResponse = new HttpResponse(); _httpClientMock = new HttpClientMock(_httpResponse); _resultWriterStub = new ResultWriterStub(); return new TestSessionRunner(config, _httpClientMock, _resultWriterStub); }
public void Run_should_set_MinResponseTime_and_MaxResponseTime_from_http_response_times() { // Arrange var config = new Config(); var stringReader = new StringReader(""); var testCaseReader = new TestCaseReaderMock(); var response = new HttpResponse(); response.ResponseTime = TimeSpan.FromSeconds(5); HttpClientMock httpClient = new HttpClientMock(response); httpClient.ResponseTimes = new List<TimeSpan>() { // Deliberately mixed up order TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(88), TimeSpan.FromSeconds(3), TimeSpan.FromSeconds(10) }; httpClient.Response = response; IResultWriter resultWriter = new ResultWriterStub(); var runner = new TestSessionRunner(config, httpClient, resultWriter); var caseCollection = CreateCaseCollection(new[] { new Case() { Url = "foo1" }, new Case() { Url = "foo2" }, new Case() { Url = "foo3" }, new Case() { Url = "foo4" }, }); // Act TestCaseSession session = runner.Run(caseCollection); // Assert Assert.That(session.MinResponseTime, Is.EqualTo(TimeSpan.FromSeconds(3))); Assert.That(session.MaxResponseTime, Is.EqualTo(TimeSpan.FromSeconds(88))); }
public void Run_should_repeat_testcases_from_repeat_property() { // Arrange var stringReader = new StringReader(""); var config = new Config(); TestSessionRunner runner = CreateRunner(config); var caseCollection = CreateCaseCollection(new[] { new Case() { Url = "foo1" }, }); caseCollection.Repeat = 10; // Act TestCaseSession session = runner.Run(caseCollection); // Assert Assert.That(session.TestCaseResults.Count, Is.EqualTo(10)); }
public void Run_should_set_parsed_variables() { // Arrange var config = new Config(); TestSessionRunner runner = CreateRunner(config); _httpClientMock.Response.Content = "some content"; _httpClientMock.Response.StatusCode = HttpStatusCode.OK; var caseCollection = CreateCaseCollection(new[] { new Case() { Url = "case1", VerifyResponseCode = HttpStatusCode.OK, ParseResponses = new List<ParseResponseItem>() { new ParseResponseItem("1", "some content") }, VerifyPositives = new List<VerificationItem>() { new VerificationItem("positive-1", "{parsedresponse1}", VerifyType.Positive) }, } }); // Act TestCaseSession session = runner.Run(caseCollection); // Assert Assert.That(session.TestCaseResults[0].VerifyPositiveResults[0].Success, Is.True); }