Skip to content

Commit 8cf9a07

Browse files
#94 Add support of page snapshots configuration
1 parent 7fa3cc8 commit 8cf9a07

File tree

6 files changed

+76
-0
lines changed

6 files changed

+76
-0
lines changed

src/Atata.Configuration.Json/JsonConfig`1.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ public double? RetryInterval
197197

198198
public string TakeScreenshotOnNUnitErrorTitle { get; set; }
199199

200+
public bool TakePageSnapshotOnNUnitError { get; set; }
201+
202+
public string TakePageSnapshotOnNUnitErrorTitle { get; set; }
203+
200204
public bool OnCleanUpAddArtifactsToNUnitTestContext { get; set; }
201205

202206
public string OnCleanUpAddDirectoryFilesToNUnitTestContext { get; set; }
@@ -250,5 +254,10 @@ public double? RetryInterval
250254
/// Gets or sets the event subscriptions.
251255
/// </summary>
252256
public List<EventSubscriptionJsonSection> EventSubscriptions { get; set; }
257+
258+
/// <summary>
259+
/// Gets or sets the page snapshots configuration.
260+
/// </summary>
261+
public PageSnapshotsJsonSection PageSnapshots { get; set; }
253262
}
254263
}

src/Atata.Configuration.Json/Mapping/JsonConfigMapper.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,14 @@ public static AtataContextBuilder Map<TConfig>(TConfig config, AtataContextBuild
112112
builder.TakeScreenshotOnNUnitError();
113113
}
114114

115+
if (config.TakePageSnapshotOnNUnitError)
116+
{
117+
if (config.TakePageSnapshotOnNUnitErrorTitle != null)
118+
builder.TakePageSnapshotOnNUnitError(config.TakePageSnapshotOnNUnitErrorTitle);
119+
else
120+
builder.TakePageSnapshotOnNUnitError();
121+
}
122+
115123
if (config.OnCleanUpAddArtifactsToNUnitTestContext)
116124
builder.OnCleanUpAddArtifactsToNUnitTestContext();
117125

@@ -151,6 +159,9 @@ public static AtataContextBuilder Map<TConfig>(TConfig config, AtataContextBuild
151159
if (config.EventSubscriptions != null)
152160
MapEventSubscriptions(config.EventSubscriptions, builder);
153161

162+
if (config.PageSnapshots != null)
163+
MapPageSnapshots(config.PageSnapshots, builder);
164+
154165
return builder;
155166
}
156167

@@ -275,5 +286,36 @@ private static void MapEventSubscriptions(List<EventSubscriptionJsonSection> sec
275286
builder.BuildingContext.EventSubscriptions.AddRange(
276287
sections.Select(x => eventSubscriptionMapper.Map(x)));
277288
}
289+
290+
private static void MapPageSnapshots(PageSnapshotsJsonSection section, AtataContextBuilder builder)
291+
{
292+
if (!string.IsNullOrEmpty(section.FileNameTemplate))
293+
builder.PageSnapshots.UseFileNameTemplate(section.FileNameTemplate);
294+
295+
if (section?.Strategy?.Type != null)
296+
{
297+
if (!PageSnapshotStrategyAliases.TryResolve(section.Strategy.Type, out IPageSnapshotStrategy strategy))
298+
strategy = (IPageSnapshotStrategy)CreateObject(
299+
section.Strategy.Type,
300+
section.Strategy.ExtraPropertiesMap,
301+
builder.BuildingContext.DefaultAssemblyNamePatternToFindTypes);
302+
303+
builder.PageSnapshots.UseStrategy(strategy);
304+
}
305+
}
306+
307+
private static object CreateObject(string typeName, Dictionary<string, object> valuesMap, string assemblyNamePatternToFindType)
308+
{
309+
IObjectConverter objectConverter = new ObjectConverter
310+
{
311+
AssemblyNamePatternToFindTypes = assemblyNamePatternToFindType
312+
};
313+
IObjectMapper objectMapper = new ObjectMapper(objectConverter);
314+
IObjectCreator objectCreator = new ObjectCreator(objectConverter, objectMapper);
315+
316+
var assembliesToFindTypes = AssemblyFinder.FindAllByPattern(assemblyNamePatternToFindType);
317+
Type strategyType = TypeFinder.FindInAssemblies(typeName, assembliesToFindTypes);
318+
return objectCreator.Create(strategyType, valuesMap);
319+
}
278320
}
279321
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Atata.Configuration.Json
2+
{
3+
public sealed class PageSnapshotStrategyJsonSection : JsonSection
4+
{
5+
public string Type { get; set; }
6+
}
7+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Atata.Configuration.Json
2+
{
3+
public sealed class PageSnapshotsJsonSection
4+
{
5+
public string FileNameTemplate { get; set; }
6+
7+
public PageSnapshotStrategyJsonSection Strategy { get; set; }
8+
}
9+
}

test/Atata.Configuration.Json.Tests/Configs/Chrome+NUnit.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
"sectionFinish": true
2424
}
2525
],
26+
"pageSnapshots": {
27+
"fileNameTemplate": "{snapshot-number:D2}!",
28+
"strategy": {
29+
"type": "pageSource"
30+
}
31+
},
2632
"assertionExceptionType": "NUnit.Framework.AssertionException, nunit.framework",
2733
"baseRetryTimeout": 7,
2834
"baseRetryInterval": 0.7,

test/Atata.Configuration.Json.Tests/GeneralSettingsTests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ public void GeneralAndNUnit()
4141
context.DefaultAssemblyNamePatternToFindTypes.Should().Be("def");
4242
context.AssemblyNamePatternToFindComponentTypes.Should().Be("comp");
4343
context.AssemblyNamePatternToFindAttributeTypes.Should().Be("attr");
44+
45+
context.PageSnapshots.FileNameTemplate.Should().Be("{snapshot-number:D2}!");
46+
context.PageSnapshots.Strategy.Should().BeOfType<PageSourcePageSnapshotStrategy>();
4447
}
4548
}
4649
}

0 commit comments

Comments
 (0)