Skip to content

Commit 903aa86

Browse files
Merge pull request #108 from aquality-automation/enhancement/get-all-base-form-elements
[Visualization] Get all elements, including private from base forms, by default
2 parents 43787d2 + 239e8fe commit 903aa86

File tree

3 files changed

+71
-16
lines changed

3 files changed

+71
-16
lines changed

Aquality.Selenium.Core/src/Aquality.Selenium.Core/Aquality.Selenium.Core.xml

Lines changed: 15 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Aquality.Selenium.Core/src/Aquality.Selenium.Core/Forms/Form.cs

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,30 +41,60 @@ public abstract class Form<T> : IForm where T : IElement
4141

4242
/// <summary>
4343
/// List of pairs uniqueName-element to be used for dump saving and comparing.
44-
/// By default, only currently displayed elements to be used (<see cref="ElementsInitializedAsDisplayed"/>).
45-
/// You can override this property with defined <see cref="AllElements"/>, <see cref="DisplayedElements"/> or your own element set.
44+
/// By default, only currently displayed elements to be used (<see cref="DisplayedElements"/>).
45+
/// You can override this property with defined <see cref="AllElements"/>, <see cref="AllCurrentFormElements"/>, <see cref="ElementsInitializedAsDisplayed"/> or your own element set.
4646
/// </summary>
4747
protected virtual IDictionary<string, T> ElementsForVisualization => DisplayedElements;
4848

4949
/// <summary>
50-
/// List of pairs uniqueName-element from all fields and properties of type <typeparamref name="T"/>.
50+
/// List of pairs uniqueName-element from all fields and properties of type <typeparamref name="T"/> from the current form and it's parent forms.
5151
/// </summary>
5252
protected IDictionary<string, T> AllElements
5353
{
5454
get
5555
{
56-
const BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
57-
var elementProperties = GetType().GetProperties(bindingFlags).Where(property => typeof(T).IsAssignableFrom(property.PropertyType))
58-
.ToDictionary(property => property.Name, property => (T)property.GetValue(this));
59-
var elementFields = GetType().GetFields(bindingFlags).Where(field => typeof(T).IsAssignableFrom(field.FieldType))
60-
.ToDictionary(field => elementProperties.Keys.Any(
61-
key => key.Equals(field.Name, StringComparison.InvariantCultureIgnoreCase)) ? $"_{field.Name}" : field.Name,
62-
field => (T)field.GetValue(this));
63-
return elementFields.Concat(elementProperties)
64-
.ToDictionary(el => el.Key, el => el.Value);
56+
var elements = new Dictionary<string, T>();
57+
AddElementsToDictionary(elements, GetType());
58+
Type baseType = GetType().BaseType;
59+
while(baseType != null)
60+
{
61+
AddElementsToDictionary(elements, baseType);
62+
baseType = baseType.BaseType;
63+
}
64+
return elements;
6565
}
6666
}
6767

68+
/// <summary>
69+
/// List of pairs uniqueName-element from all fields and properties of type <typeparamref name="T"/> from the current form.
70+
/// </summary>
71+
protected IDictionary<string, T> AllCurrentFormElements
72+
{
73+
get
74+
{
75+
var elements = new Dictionary<string, T>();
76+
AddElementsToDictionary(elements, GetType());
77+
return elements;
78+
}
79+
}
80+
81+
/// <summary>
82+
/// Adds pairs uniqueName-element from the specified type to dictionary using the reflection.
83+
/// </summary>
84+
/// <param name="dictionary">Dictionary to save elements.</param>
85+
/// <param name="type">Type to extract element fields and properties from.</param>
86+
protected void AddElementsToDictionary(IDictionary<string, T> dictionary, Type type)
87+
{
88+
#pragma warning disable S3011 // Reflection should not be used to increase accessibility of classes, methods, or fields
89+
const BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
90+
#pragma warning restore S3011 // Reflection should not be used to increase accessibility of classes, methods, or fields
91+
type.GetProperties(bindingFlags).Where(property => typeof(T).IsAssignableFrom(property.PropertyType) && !dictionary.ContainsKey(property.Name))
92+
.ToList().ForEach(property => dictionary.Add(property.Name, (T)property.GetValue(this)));
93+
type.GetFields(bindingFlags).Where(field => typeof(T).IsAssignableFrom(field.FieldType) && (!dictionary.ContainsKey(field.Name) || !dictionary.ContainsKey($"_{field.Name}")))
94+
.ToList().ForEach(field => dictionary.Add(dictionary.Keys.Any(
95+
key => key.Equals(field.Name, StringComparison.InvariantCultureIgnoreCase)) ? $"_{field.Name}" : field.Name, (T)field.GetValue(this)));
96+
}
97+
6898
/// <summary>
6999
/// List of pairs uniqueName-element from all fields and properties of type <typeparamref name="T"/>,
70100
/// which were initialized as <see cref="ElementState.Displayed"/>.

Aquality.Selenium.Core/tests/Aquality.Selenium.Core.Tests/Visualization/FormDumpTests.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,15 @@ public void Should_BePossibleTo_SaveAndCompareWithDump_WithCustomName_WhenAllEle
9696
Assert.That(customForm.Dump.Compare("All elements"), Is.EqualTo(0), "Some elements should be failed to take image, but difference should be around zero");
9797
}
9898

99+
[Test]
100+
public void Should_BePossibleTo_SaveAndCompareWithDump_WithCustomName_WhenCurrentFormElementsSelected()
101+
{
102+
var customForm = new WebForm();
103+
customForm.SetElementsForDump(WebForm.ElementsFilter.CurrentFormElements);
104+
Assert.DoesNotThrow(() => customForm.Dump.Save("CurrentForm elements"));
105+
Assert.That(customForm.Dump.Compare("CurrentForm elements"), Is.EqualTo(0), "Some elements should be failed to take image, but difference should be around zero");
106+
}
107+
99108
[Test]
100109
public void Should_BePossibleTo_SaveAndCompareWithDump_WithOverlengthDumpName_WhenAllElementsSelected()
101110
{
@@ -240,6 +249,9 @@ public void SetElementsForDump(ElementsFilter filter)
240249
case ElementsFilter.DisplayedElements:
241250
elementsToCheck = DisplayedElements;
242251
break;
252+
case ElementsFilter.CurrentFormElements:
253+
elementsToCheck = AllCurrentFormElements;
254+
break;
243255
}
244256
}
245257

@@ -263,7 +275,8 @@ public enum ElementsFilter
263275
{
264276
ElementsInitializedAsDisplayed,
265277
AllElements,
266-
DisplayedElements
278+
DisplayedElements,
279+
CurrentFormElements
267280
}
268281
}
269282

0 commit comments

Comments
 (0)