コード例 #1
0
        protected override void Append(LoggingEvent loggingEvent)
        {
            if (Bridge.Context.TestId != null)
            {
                var level = LogLevel.Info;
                if (_levelMap.ContainsKey(loggingEvent.Level))
                {
                    level = _levelMap[loggingEvent.Level];
                }

                var request = new AddLogItemRequest
                {
                    TestItemId = Bridge.Context.TestId,
                    Level      = level,
                    Time       = DateTime.UtcNow,
                    Text       = loggingEvent.RenderedMessage
                };

                try
                {
                    Bridge.Service.AddLogItem(request);
                }
                catch (Exception)
                {
                    // Ignoring
                }
            }
        }
コード例 #2
0
        public bool AddLogItem(string testFullName, string logMessage, LogLevel logLevel)
        {
            try
            {
                var test = _concurrentTree.FindNode(testFullName);
                if (test == null)
                {
                    throw new Exception($"{testFullName} is not found");
                }

                var addLogItemRequest = new AddLogItemRequest()
                {
                    Level = (Client.Models.LogLevel)logLevel,
                    Time  = DateTime.UtcNow,
                    Text  = logMessage
                };
                test.Value.Log(addLogItemRequest);

                ReportSuccess(nameof(AddLogItem));
                return(true);
            }
            catch (Exception ex)
            {
                ReportError(nameof(AddLogItem), ex);
                return(false);
            }
        }
コード例 #3
0
 public TestItemOutputEventArgs(Service service, AddLogItemRequest request, ITestReporter testReporter, string report)
 {
     Service           = service;
     AddLogItemRequest = request;
     TestReporter      = testReporter;
     Report            = report;
 }
コード例 #4
0
        /// <summary>
        /// Creates a new log item.
        /// </summary>
        /// <param name="model">Information about representation of log item.</param>
        /// <returns>Representation of just created log item.</returns>
        public async Task <LogItem> AddLogItemAsync(AddLogItemRequest model)
        {
            var uri = BaseUri.Append($"{Project}/log");

            if (model.Attach == null)
            {
                var body     = ModelSerializer.Serialize <AddLogItemRequest>(model);
                var response = await _httpClient.PostAsync(uri, new StringContent(body, Encoding.UTF8, "application/json")).ConfigureAwait(false);

                response.VerifySuccessStatusCode();
                return(ModelSerializer.Deserialize <LogItem>(await response.Content.ReadAsStringAsync().ConfigureAwait(false)));
            }
            else
            {
                var body = ModelSerializer.Serialize <List <AddLogItemRequest> >(new List <AddLogItemRequest> {
                    model
                });
                var multipartContent = new MultipartFormDataContent();
                multipartContent.Add(new StringContent(body, Encoding.UTF8, "application/json"), "json_request_part");
                multipartContent.Add(new ByteArrayContent(model.Attach.Data, 0, model.Attach.Data.Length), "file", model.Attach.Name);
                var response = await _httpClient.PostAsync(uri, multipartContent).ConfigureAwait(false);

                response.VerifySuccessStatusCode();
                var c = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                return(ModelSerializer.Deserialize <Responses>(c).LogItems[0]);
            }
        }
コード例 #5
0
ファイル: Bridge.cs プロジェクト: OlegYanush/commons-net
        public static void LogMessage(LogLevel level, string text)
        {
            var request = new AddLogItemRequest
            {
                Level = level,
                Time  = DateTime.UtcNow,
                Text  = text
            };

            var handled = false;

            foreach (var extension in Extensions)
            {
                extension.FormatLog(ref request);
                if (extension.Handled)
                {
                    handled = true;
                    break;
                }
            }

            if (!handled && Context.LaunchReporter?.LastTestNode != null)
            {
                var testNode = Context.LaunchReporter.TestNodes
                               .Select(t => FindNonFinishedTestReporter(t, Thread.CurrentThread.ManagedThreadId))
                               .FirstOrDefault(t => t != null) ?? Context.LaunchReporter.LastTestNode;

                testNode.Log(request);
            }
        }
コード例 #6
0
        public void TraceBindingError(BindingException ex)
        {
            if (CurrentScenario != null)
            {
                Status = Status.Failed;

                var request = new FinishTestItemRequest
                {
                    Status  = Status.Failed,
                    EndTime = DateTime.UtcNow.AddMilliseconds(1),
                    Issue   = new Issue
                    {
                        Type    = IssueType.AutomationBug,
                        Comment = ex.Message
                    }
                };

                var errorRequest = new AddLogItemRequest
                {
                    Level = LogLevel.Error,
                    Time  = DateTime.UtcNow.AddMilliseconds(1),
                    Text  = ex.ToString()
                };
                CurrentScenario.Log(errorRequest);

                if (BeforeStepFinished != null)
                {
                    BeforeStepFinished(this, new TestItemFinishedEventArgs(Bridge.Service, request, null));
                }
                if (AfterStepFinished != null)
                {
                    AfterStepFinished(this, new TestItemFinishedEventArgs(Bridge.Service, request, null));
                }
            }
        }
コード例 #7
0
 public override async Task <LogItem> AddLogItemAsync(AddLogItemRequest model)
 {
     lock (_addLogtemCounterLockObj)
     {
         AddLogItemCounter++;
     }
     //await Task.Delay(RequestsDelay);
     return(await Task.FromResult(new LogItem()));
 }
        private AddLogItemRequest CreateMetaDataLogItemRequest(RanorexRpLogItem logItem, LogLevel level)
        {
            var rq = new AddLogItemRequest();

            rq.Time  = DateTime.UtcNow;
            rq.Level = level;
            rq.Text  = ConstructMetaDataLogMessage(logItem);

            return(rq);
        }
コード例 #9
0
        private static AddLogItemRequest GetDefaultLogRequest(string text)
        {
            var logRequest = new AddLogItemRequest
            {
                Time = DateTime.UtcNow,
                Text = text
            };

            return(logRequest);
        }
コード例 #10
0
        public static void LogMessage(LogLevel level, string text)
        {
            var logRequest = new AddLogItemRequest
            {
                Level = level,
                Time  = DateTime.UtcNow,
                Text  = text
            };

            Log.Message(logRequest);
        }
コード例 #11
0
        public static void Message(AddLogItemRequest logRequest)
        {
            foreach (var handler in Bridge.LogHandlerExtensions)
            {
                var isHandled = handler.Handle(logRequest);

                if (isHandled)
                {
                    break;
                }
            }
        }
コード例 #12
0
        public void TestMessage(XmlDocument xmlDoc)
        {
            try
            {
                var testId  = xmlDoc.SelectSingleNode("/test-message/@testid").Value;
                var message = xmlDoc.SelectSingleNode("/test-message").InnerText;

                if (_flowItems.ContainsKey(testId))
                {
                    AddLogItemRequest logRequest = null;
                    try
                    {
                        var sharedMessage = ModelSerializer.Deserialize <SharedLogMessage>(message);

                        logRequest = new AddLogItemRequest
                        {
                            Level      = sharedMessage.Level,
                            Time       = sharedMessage.Time,
                            TestItemId = sharedMessage.TestItemId,
                            Text       = sharedMessage.Text
                        };
                        if (sharedMessage.Attach != null)
                        {
                            logRequest.Attach = new Client.Models.Attach
                            {
                                Name     = sharedMessage.Attach.Name,
                                MimeType = sharedMessage.Attach.MimeType,
                                Data     = sharedMessage.Attach.Data
                            };
                        }
                    }
                    catch (Exception)
                    {
                    }

                    if (logRequest != null)
                    {
                        _flowItems[testId].TestReporter.Log(logRequest);
                    }
                    else
                    {
                        _flowItems[testId].TestReporter.Log(new AddLogItemRequest {
                            Level = LogLevel.Info, Time = DateTime.UtcNow, Text = message
                        });
                    }
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine("ReportPortal exception was thrown." + Environment.NewLine + exception);
            }
        }
コード例 #13
0
        protected virtual void Execution_TestOutputEvent(MessageHandlerArgs <ITestOutput> args)
        {
            lock (Logger.LockObject)
            {
                try
                {
                    var    testEvent = args.Message;
                    string key       = testEvent.Test.TestCase.UniqueID;

                    ITestReporter testReporter = TestReporterDictionary[key];

                    bool isInternalSharedMessage = false;

                    try
                    {
                        var sharedLogMessage = Client.Converters.ModelSerializer.Deserialize <SharedLogMessage>(testEvent.Output);

                        isInternalSharedMessage = true;

                        var logItemRequest = new AddLogItemRequest
                        {
                            Time  = sharedLogMessage.Time,
                            Level = sharedLogMessage.Level,
                            Text  = sharedLogMessage.Text
                        };

                        if (sharedLogMessage.Attach != null)
                        {
                            logItemRequest.Attach = new Client.Models.Attach(sharedLogMessage.Attach.Name, sharedLogMessage.Attach.MimeType, sharedLogMessage.Attach.Data);
                        }

                        testReporter.Log(logItemRequest);
                    }
                    catch (Exception) { }

                    if (!isInternalSharedMessage)
                    {
                        testReporter.Log(new AddLogItemRequest
                        {
                            Level = LogLevel.Info,
                            Time  = DateTime.UtcNow,
                            Text  = testEvent.Output
                        });
                    }
                }
                catch (Exception exp)
                {
                    Logger.LogError(exp.ToString());
                }
            }
        }
コード例 #14
0
        public void TraceStep(StepInstance stepInstance, bool showAdditionalArguments)
        {
            if (CurrentScenario != null)
            {
                CurrentScenarioDescription += Environment.NewLine + stepInstance.Keyword + " " + stepInstance.Text;

                if (stepInstance.MultilineTextArgument != null)
                {
                    CurrentScenarioDescription += Environment.NewLine + stepInstance.MultilineTextArgument;
                }

                var tableDescription = string.Empty;
                if (stepInstance.TableArgument != null)
                {
                    tableDescription = string.Empty;
                    foreach (var header in stepInstance.TableArgument.Header)
                    {
                        tableDescription += "| " + header + "\t";
                    }
                    tableDescription += "|\n";
                    foreach (var row in stepInstance.TableArgument.Rows)
                    {
                        foreach (var value in row.Values)
                        {
                            tableDescription += "| " + value + "\t";
                        }
                        tableDescription += "|\n";
                    }
                }
                if (!string.IsNullOrEmpty(tableDescription))
                {
                    CurrentScenarioDescription += Environment.NewLine + tableDescription;
                }

                var updateScenarioRequest = new UpdateTestItemRequest
                {
                    Description = CurrentScenarioDescription
                };
                CurrentScenario.Update(updateScenarioRequest);

                var stepInfoRequest = new AddLogItemRequest
                {
                    Level = LogLevel.Info,
                    //TODO log time should be greater than test time
                    Time = DateTime.UtcNow.AddMilliseconds(1),
                    Text = string.Format("{0}\r{1}", stepInstance.Keyword + " " + stepInstance.Text, tableDescription)
                };
                CurrentScenario.Log(stepInfoRequest);
            }
        }
コード例 #15
0
        public void TraceWarning(string text)
        {
            if (CurrentScenario != null)
            {
                var request = new AddLogItemRequest
                {
                    Level = LogLevel.Warning,
                    Time  = DateTime.UtcNow.AddMilliseconds(1),
                    Text  = text
                };

                CurrentScenario.Log(request);
            }
        }
コード例 #16
0
        private AddLogItemRequest CreateLogItemRequest(RanorexRpLogItem logItem, LogLevel level)
        {
            var rq = new AddLogItemRequest();

            rq.Time  = DateTime.UtcNow;
            rq.Level = level;
            rq.Text  = logItem.Category + " - " + logItem.Message;

            if (logItem.AttachData != null)
            {
                rq.Attach = new Attach("Attachment", "image/jpeg", logItem.AttachData);
            }
            return(rq);
        }
コード例 #17
0
        public void FormatLog(ref AddLogItemRequest logRequest)
        {
            var regex = new Regex("{rp#base64#(.*)#(.*)}");
            var match = regex.Match(logRequest.Text);

            if (match.Success)
            {
                logRequest.Text = logRequest.Text.Replace(match.Value, "");

                var mimeType = match.Groups[1].Value;
                var bytes    = Convert.FromBase64String(match.Groups[2].Value);

                logRequest.Attach = new Client.Models.Attach("file", mimeType, bytes);
            }
        }
コード例 #18
0
        public void TraceWarning(string text)
        {
            if (CurrentStepId != null)
            {
                var request = new AddLogItemRequest
                {
                    TestItemId = CurrentStepId,
                    Level      = LogLevel.Warning,
                    Time       = DateTime.UtcNow,
                    Text       = text
                };

                Bridge.Service.AddLogItem(request);
            }
        }
コード例 #19
0
        public void TraceError(Exception ex)
        {
            if (CurrentStepId != null)
            {
                var request = new FinishTestItemRequest
                {
                    Status  = Status.Failed,
                    EndTime = DateTime.UtcNow,
                    Issue   = new Issue
                    {
                        Type    = IssueType.ToInvestigate,
                        Comment = ex.Message
                    }
                };

                if (ex.GetType().Name == "IgnoreException")
                {
                    request.Status     = Status.Skipped;
                    request.Issue.Type = IssueType.NoDefect;
                }

                var errorRequest = new AddLogItemRequest
                {
                    TestItemId = CurrentStepId,
                    Level      = LogLevel.Error,
                    Time       = DateTime.UtcNow,
                    Text       = ex.ToString()
                };
                Bridge.Service.AddLogItem(errorRequest);

                if (BeforeStepFinished != null)
                {
                    BeforeStepFinished(this, new TestItemFinishedEventArgs(Bridge.Service, request));
                }
                var message = Bridge.Service.FinishTestItem(CurrentStepId, request).Info;
                if (AfterStepFinished != null)
                {
                    AfterStepFinished(this, new TestItemFinishedEventArgs(Bridge.Service, request, message));
                }

                CurrentStepId         = null;
                Bridge.Context.TestId = null;
            }
        }
コード例 #20
0
        public void Log(AddLogItemRequest request)
        {
            if (StartTask == null)
            {
                throw new InsufficientExecutionStackException("The test item wasn't scheduled for starting to add log messages.");
            }

            if (StartTask.IsFaulted || StartTask.IsCanceled)
            {
                return;
            }

            if (FinishTask == null || !FinishTask.IsCompleted)
            {
                var parentTask = _additionalTasks?.Last() ?? StartTask;

                var task = parentTask.ContinueWith(async(pt) =>
                {
                    if (!StartTask.IsFaulted)
                    {
                        if (request.Time < TestInfo.StartTime)
                        {
                            request.Time = TestInfo.StartTime;
                        }

                        request.TestItemId = TestInfo.Id;

                        foreach (var formatter in Bridge.LogFormatterExtensions)
                        {
                            formatter.FormatLog(ref request);
                        }

                        await _service.AddLogItemAsync(request);
                    }
                }).Unwrap();

                if (_additionalTasks == null)
                {
                    _additionalTasks = new ConcurrentBag <Task>();
                }

                _additionalTasks.Add(task);
            }
        }
コード例 #21
0
        public void FormatLog(ref AddLogItemRequest logRequest)
        {
            var regex = new Regex("{rp#file#(.*)}");
            var match = regex.Match(logRequest.Text);

            if (match.Success)
            {
                logRequest.Text = logRequest.Text.Replace(match.Value, "");

                var filePath = match.Groups[1].Value;

                var fileExtension = Path.GetExtension(filePath);

                var mimeType = "application/octet-stream";
                _mimeTypes.TryGetValue(fileExtension, out mimeType);

                logRequest.Attach = new Client.Models.Attach(Path.GetFileName(filePath), mimeType, File.ReadAllBytes(filePath));
            }
        }
コード例 #22
0
        public bool Handle(AddLogItemRequest logRequest)
        {
            var handled = false;

            if ((Bridge.Context.LaunchReporter as LaunchReporter)?.LastTestNode != null)
            {
                var testNode = Bridge.Context.LaunchReporter.ChildTestReporters?
                               .Select(t => FindNonFinishedTestReporter(t, Thread.CurrentThread.ManagedThreadId))
                               .FirstOrDefault(t => t != null) ?? (Bridge.Context.LaunchReporter as LaunchReporter).LastTestNode;

                if (testNode != null)
                {
                    testNode.Log(logRequest);

                    handled = true;
                }
            }

            return(handled);
        }
コード例 #23
0
        public async Task GetLogItem()
        {
            var addLogItemRequest = new AddLogItemRequest
            {
                TestItemId = _fixture.TestId,
                Text       = "Log1",
                Time       = DateTime.UtcNow,
                Level      = LogLevel.Info
            };

            var log = await Service.AddLogItemAsync(addLogItemRequest);

            Assert.NotNull(log.Id);

            var gotLogItem = await Service.GetLogItemAsync(log.Id);

            Assert.Equal(addLogItemRequest.Text, gotLogItem.Text);
            Assert.Equal(addLogItemRequest.Level, gotLogItem.Level);
            Assert.Equal(addLogItemRequest.Time, gotLogItem.Time);
        }
コード例 #24
0
        public void Log(AddLogItemRequest request)
        {
            if (FinishTask == null || !FinishTask.IsCompleted)
            {
                var task = Task.WhenAll(AdditionalTasks).ContinueWith(async(t) =>
                {
                    StartTask.Wait();

                    if (request.Time < StartTime)
                    {
                        request.Time = StartTime.AddMilliseconds(1);
                    }

                    request.TestItemId = TestId;
                    Debug.WriteLine($"Log message: '{request.Text}'");
                    await _service.AddLogItemAsync(request);
                });

                AdditionalTasks.Add(task);
            }
        }
コード例 #25
0
        public void TraceBindingError(BindingException ex)
        {
            if (CurrentStepId != null)
            {
                var request = new FinishTestItemRequest
                {
                    Status  = Status.Failed,
                    EndTime = DateTime.UtcNow,
                    Issue   = new Issue
                    {
                        Type    = IssueType.AutomationBug,
                        Comment = ex.Message
                    }
                };

                var errorRequest = new AddLogItemRequest
                {
                    TestItemId = CurrentStepId,
                    Level      = LogLevel.Error,
                    Time       = DateTime.UtcNow,
                    Text       = ex.ToString()
                };
                Bridge.Service.AddLogItem(errorRequest);

                if (BeforeStepFinished != null)
                {
                    BeforeStepFinished(this, new TestItemFinishedEventArgs(Bridge.Service, request));
                }
                var message = Bridge.Service.FinishTestItem(CurrentStepId, request).Info;
                if (AfterStepFinished != null)
                {
                    AfterStepFinished(this, new TestItemFinishedEventArgs(Bridge.Service, request, message));
                }

                CurrentStepId         = null;
                Bridge.Context.TestId = null;
            }
        }
コード例 #26
0
        public bool FormatLog(ref AddLogItemRequest logRequest)
        {
            var regex = new Regex("{rp#file#(.*)}");
            var match = regex.Match(logRequest.Text);

            if (match.Success)
            {
                logRequest.Text = logRequest.Text.Replace(match.Value, "");

                var filePath = match.Groups[1].Value;

                if (!Path.GetInvalidPathChars().Any(i => filePath.Contains(i)))
                {
                    var mimeType = MimeTypes.MimeTypeMap.GetMimeType(Path.GetExtension(filePath));

                    logRequest.Attach = new Client.Models.Attach(Path.GetFileName(filePath), mimeType, File.ReadAllBytes(filePath));

                    return(true);
                }
            }

            return(false);
        }
コード例 #27
0
        public void BeforeStep()
        {
            var currentScenario = ReportPortalAddin.GetScenarioTestReporter(this.ScenarioContext);

            if (currentScenario != null)
            {
                var stepInfoRequest = new AddLogItemRequest
                {
                    Level = LogLevel.Info,
                    Time  = DateTime.UtcNow,
                    Text  = this.StepContext.StepInfo.GetFullText()
                };

                var eventArg = new StepStartedEventArgs(Bridge.Service, stepInfoRequest, null);
                ReportPortalAddin.OnBeforeStepStarted(this, eventArg);

                if (!eventArg.Canceled)
                {
                    currentScenario.Log(stepInfoRequest);
                    ReportPortalAddin.OnAfterStepStarted(this, eventArg);
                }
            }
        }
コード例 #28
0
        public void FormatLog(ref AddLogItemRequest logRequest)
        {
            var sharedMessage = new SharedLogMessage()
            {
                TestItemId = logRequest.TestItemId,
                Time       = logRequest.Time,
                Text       = logRequest.Text,
                Level      = logRequest.Level
            };

            if (logRequest.Attach != null)
            {
                sharedMessage.Attach = new Attach
                {
                    Name     = logRequest.Attach.Name,
                    MimeType = logRequest.Attach.MimeType,
                    Data     = logRequest.Attach.Data
                };
            }

            NUnit.Framework.TestContext.Progress.WriteLine(ModelSerializer.Serialize <SharedLogMessage>(sharedMessage));
            Handled = true;
        }
コード例 #29
0
        public bool Handle(AddLogItemRequest logRequest)
        {
            if (_outputHelper != null)
            {
                var sharedLogMessage = new SharedLogMessage
                {
                    Level = logRequest.Level,
                    Time  = logRequest.Time,
                    Text  = logRequest.Text
                };

                if (logRequest.Attach != null)
                {
                    sharedLogMessage.Attach = new Attach(logRequest.Attach.Name, logRequest.Attach.MimeType, logRequest.Attach.Data);
                }

                _outputHelper.WriteLine(Client.Converters.ModelSerializer.Serialize <SharedLogMessage>(sharedLogMessage));

                return(true);
            }

            return(false);
        }
コード例 #30
0
        public void TraceStepPending(BindingMatch match, object[] arguments)
        {
            if (CurrentScenario != null)
            {
                Status = Status.Failed;

                var errorRequest = new AddLogItemRequest
                {
                    Level = LogLevel.Error,
                    Time  = DateTime.UtcNow.AddMilliseconds(1),
                    Text  = "One or more step definitions are not implemented yet.\r" + match.StepBinding.Method.Name + "(" + ")"
                };

                CurrentScenario.Log(errorRequest);

                var request = new FinishTestItemRequest
                {
                    Status  = Status.Failed,
                    EndTime = DateTime.UtcNow,
                    Issue   = new Issue
                    {
                        Type    = IssueType.ToInvestigate,
                        Comment = "Pending"
                    }
                };

                if (BeforeStepFinished != null)
                {
                    BeforeStepFinished(this, new TestItemFinishedEventArgs(Bridge.Service, request, null));
                }
                if (AfterStepFinished != null)
                {
                    AfterStepFinished(this, new TestItemFinishedEventArgs(Bridge.Service, request, null));
                }
            }
        }