-
Notifications
You must be signed in to change notification settings - Fork 130
/
ProjectTestBase.cs
154 lines (138 loc) · 5.34 KB
/
ProjectTestBase.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// <copyright file="ProjectTestBase.cs" company="Objectivity Bespoke Software Specialists">
// Copyright (c) Objectivity Bespoke Software Specialists. All rights reserved.
// </copyright>
// <license>
// The MIT License (MIT)
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// </license>
using System.IO;
namespace Ocaramba.Tests.Angular
{
using System;
using NLog;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using OpenQA.Selenium;
using Logger;
/// <summary>
/// The base class for all tests <see href="https://github.com/ObjectivityLtd/Ocaramba/wiki/ProjectTestBase-class">More details on wiki</see>
/// </summary>
public class ProjectTestBase : TestBase
{
private readonly DriverContext driverContext = new DriverContext();
#if net47
private static readonly NLog.Logger Logger = LogManager.GetCurrentClassLogger();
#endif
#if net8_0
private static readonly NLog.Logger Logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
#endif
public ProjectTestBase()
{
this.driverContext.DriverOptionsSet += this.DriverContext_DriverOptionsSet;
}
/// <summary>
/// Gets or sets logger instance for driver
/// </summary>
public TestLogger LogTest
{
get
{
return this.DriverContext.LogTest;
}
set
{
this.DriverContext.LogTest = value;
}
}
/// <summary>
/// Gets or Sets the driver context
/// </summary>
protected DriverContext DriverContext
{
get
{
return this.driverContext;
}
}
/// <summary>
/// Before the class.
/// </summary>
[OneTimeSetUp]
public void BeforeClass()
{
#if net8_0
this.DriverContext.CurrentDirectory = Directory.GetCurrentDirectory();
#endif
#if net47
this.DriverContext.CurrentDirectory = TestContext.CurrentContext.TestDirectory;
#endif
this.DriverContext.Start();
}
/// <summary>
/// After the class.
/// </summary>
[OneTimeTearDown]
public void AfterClass()
{
this.DriverContext.Stop();
}
/// <summary>
/// Before the test.
/// </summary>
[SetUp]
public void BeforeTest()
{
this.DriverContext.TestTitle = TestContext.CurrentContext.Test.Name;
this.LogTest.LogTestStarting(this.driverContext);
}
/// <summary>
/// After the test.
/// </summary>
[TearDown]
public void AfterTest()
{
this.DriverContext.IsTestFailed = TestContext.CurrentContext.Result.Outcome.Status == TestStatus.Failed || !this.driverContext.VerifyMessages.Count.Equals(0);
this.SaveTestDetailsIfTestFailed(this.driverContext);
this.LogTest.LogTestEnding(this.driverContext);
this.LogTest.LogTestEnding(this.driverContext);
var logs = this.driverContext.Driver.Manage().Logs;
if (BaseConfiguration.TestBrowser == BrowserType.Chrome)
{
///// It doesn't work due to https://github.com/SeleniumHQ/selenium/issues/7900
//// and https://github.com/SeleniumHQ/selenium/issues/7335
////var perfLogs = logs.GetLog("performance");
////foreach (var perfLog in perfLogs)
////{
//// Logger.Info(perfLog.ToString);
////}
}
if (this.IsVerifyFailedAndClearMessages(this.driverContext) && TestContext.CurrentContext.Result.Outcome.Status != TestStatus.Failed)
{
Assert.Fail();
}
}
private void DriverContext_DriverOptionsSet(object sender, DriverOptionsSetEventArgs args)
{
if (args == null || args.DriverOptions == null)
{
throw new ArgumentNullException();
}
args.DriverOptions.SetLoggingPreference("performance", OpenQA.Selenium.LogLevel.All);
args.DriverOptions.SetLoggingPreference(LogType.Browser, OpenQA.Selenium.LogLevel.All);
}
}
}