/
UITestBase.cs
131 lines (114 loc) · 3.83 KB
/
UITestBase.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System.Reflection;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using UITest.Core;
namespace UITest.Appium.NUnit
{
//#if ANDROID
// [TestFixture(TestDevice.Android)]
//#elif IOSUITEST
// [TestFixture(TestDevice.iOS)]
//#elif MACUITEST
// [TestFixture(TestDevice.Mac)]
//#elif WINTEST
// [TestFixture(TestDevice.Windows)]
//#else
// [TestFixture(TestDevice.iOS)]
// [TestFixture(TestDevice.Mac)]
// [TestFixture(TestDevice.Windows)]
// [TestFixture(TestDevice.Android)]
//#endif
public abstract class UITestBase : UITestContextBase
{
public UITestBase(TestDevice testDevice)
: base(testDevice)
{
}
[SetUp]
public void RecordTestSetup()
{
var name = TestContext.CurrentContext.Test.MethodName ?? TestContext.CurrentContext.Test.Name;
TestContext.Progress.WriteLine($">>>>> {DateTime.Now} {name} Start");
}
[TearDown]
public void RecordTestTeardown()
{
var name = TestContext.CurrentContext.Test.MethodName ?? TestContext.CurrentContext.Test.Name;
TestContext.Progress.WriteLine($">>>>> {DateTime.Now} {name} Stop");
}
protected virtual void FixtureSetup()
{
var name = TestContext.CurrentContext.Test.MethodName ?? TestContext.CurrentContext.Test.Name;
TestContext.Progress.WriteLine($">>>>> {DateTime.Now} {nameof(FixtureSetup)} for {name}");
}
protected virtual void FixtureTeardown()
{
var name = TestContext.CurrentContext.Test.MethodName ?? TestContext.CurrentContext.Test.Name;
TestContext.Progress.WriteLine($">>>>> {DateTime.Now} {nameof(FixtureTeardown)} for {name}");
}
[TearDown]
public void UITestBaseTearDown()
{
if (App.AppState == ApplicationState.Not_Running)
{
// Assert.Fail will immediately exit the test which is desirable as the app is not
// running anymore so we don't want to log diagnostic data as there is nothing to collect from
Reset();
FixtureSetup();
Assert.Fail("The app was expected to be running still, investigate as possible crash");
}
var testOutcome = TestContext.CurrentContext.Result.Outcome;
if (testOutcome == ResultState.Error ||
testOutcome == ResultState.Failure)
{
SaveDiagnosticLogs("UITestBaseTearDown");
}
}
[OneTimeSetUp]
public void OneTimeSetup()
{
InitialSetup(UITestContextSetupFixture.ServerContext);
try
{
//SaveDiagnosticLogs("BeforeFixtureSetup");
FixtureSetup();
}
catch
{
SaveDiagnosticLogs("FixtureSetup");
throw;
}
}
[OneTimeTearDown]
public void OneTimeTearDown()
{
var outcome = TestContext.CurrentContext.Result.Outcome;
// We only care about setup failures as regular test failures will already do logging
if (outcome.Status == ResultState.SetUpFailure.Status &&
outcome.Site == ResultState.SetUpFailure.Site)
{
SaveDiagnosticLogs("OneTimeTearDown");
}
FixtureTeardown();
}
void SaveDiagnosticLogs(string? note = null)
{
if (string.IsNullOrEmpty(note))
note = "-";
else
note = $"-{note}-";
var logDir = (Path.GetDirectoryName(Environment.GetEnvironmentVariable("APPIUM_LOG_FILE")) ?? Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location))!;
// App could be null if UITestContext was not able to connect to the test process (e.g. port already in use etc...)
if (UITestContext is not null)
{
string name = TestContext.CurrentContext.Test.MethodName ?? TestContext.CurrentContext.Test.Name;
var screenshotPath = Path.Combine(logDir, $"{name}-{_testDevice}{note}ScreenShot.png");
_ = App.Screenshot(screenshotPath);
TestContext.AddTestAttachment(screenshotPath, Path.GetFileName(screenshotPath));
var pageSourcePath = Path.Combine(logDir, $"{name}-{_testDevice}{note}PageSource.txt");
File.WriteAllText(pageSourcePath, App.ElementTree);
TestContext.AddTestAttachment(pageSourcePath, Path.GetFileName(pageSourcePath));
}
}
}
}