-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
Copy pathCaptureSeleniumLogsAttribute.cs
50 lines (43 loc) · 1.72 KB
/
CaptureSeleniumLogsAttribute.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Linq;
using System.Reflection;
using OpenQA.Selenium;
using Xunit.Sdk;
namespace Microsoft.AspNetCore.E2ETesting;
// This has to use BeforeAfterTestAttribute because running the log capture
// in the BrowserFixture.Dispose method is too late, and we can't add logging
// to the test.
public class CaptureSeleniumLogsAttribute : BeforeAfterTestAttribute
{
public override void Before(MethodInfo methodUnderTest)
{
if (!typeof(BrowserTestBase).IsAssignableFrom(methodUnderTest.DeclaringType))
{
throw new InvalidOperationException("This should only be used with BrowserTestBase");
}
}
public override void After(MethodInfo methodUnderTest)
{
var browser = BrowserTestBase.BrowserAccessor;
var logs = BrowserTestBase.Logs;
var output = BrowserTestBase.Output;
if (logs != null && output != null)
{
// Put browser logs first, the test UI will truncate output after a certain length
// and the browser logs will include exceptions thrown by js in the browser.
foreach (var kind in logs.AvailableLogTypes.OrderBy(k => k == LogType.Browser ? 0 : 1))
{
output.WriteLine($"{kind} Logs from Selenium:");
var entries = logs.GetLog(kind);
foreach (LogEntry entry in entries)
{
output.WriteLine($"[{entry.Timestamp}] - {entry.Level} - {entry.Message}");
}
output.WriteLine("");
output.WriteLine("");
}
}
}
}