Skip to content

Commit 205c5eb

Browse files
committed
Updated project template wrappers
1 parent 9afd55c commit 205c5eb

File tree

4 files changed

+73
-44
lines changed

4 files changed

+73
-44
lines changed

Behavioral.Automation.Template/Behavioral.Automation.Template.Bindings/ElementWrappers/TextElementWrapper.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public void ClearInput()
2727
new AssertionBehavior(AssertionType.Continuous, false),
2828
$"{Caption} is not enabled");
2929

30+
Element.Clear();
3031
while (Element.GetAttribute("value").Length > 0)
3132
{
3233
Element.SendKeys(Keys.Backspace);

Behavioral.Automation.Template/Behavioral.Automation.Template.Bindings/ElementWrappers/WebElementWrapper.cs

Lines changed: 69 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,72 @@
55
using System;
66
using System.Collections.Generic;
77
using System.Diagnostics.CodeAnalysis;
8+
using System.Linq;
9+
using System.Text.RegularExpressions;
810

911
namespace Behavioral.Automation.Template.Bindings.ElementWrappers
1012
{
11-
public class WebElementWrapper : IWebElementWrapper
13+
public class WebElementWrapper : IWebElementWrapper
1214
{
1315
private readonly Func<IWebElement> _elementSelector;
14-
private readonly IDriverService _driverService;
1516

16-
public WebElementWrapper([NotNull] Func<IWebElement> elementSelector, [NotNull] string caption, [NotNull] IDriverService driverService)
17+
public WebElementWrapper([NotNull] Func<IWebElement> elementSelector, [NotNull] string caption,
18+
[NotNull] IDriverService driverService)
1719
{
1820
_elementSelector = elementSelector;
19-
_driverService = driverService;
21+
Driver = driverService;
2022
Caption = caption;
2123
}
2224

2325
public string Caption { get; }
2426

2527
public IWebElement Element => _elementSelector();
2628

27-
public string Text => Element.Text;
29+
public string Text
30+
{
31+
get
32+
{
33+
try
34+
{
35+
return Regex.Replace(Element.Text, @"\t|\n|\r", " ").Replace(" ", " ");
36+
}
37+
catch (Exception e) when (e is NullReferenceException or StaleElementReferenceException)
38+
{
39+
return string.Empty;
40+
}
41+
}
42+
}
2843

29-
public string GetAttribute(string attribute) => Element.GetAttribute(attribute);
44+
public string GetAttribute(string attribute)
45+
{
46+
try
47+
{
48+
return Element.GetAttribute(attribute);
49+
}
50+
catch (Exception e) when (e is NullReferenceException or StaleElementReferenceException)
51+
{
52+
return null;
53+
}
54+
}
3055

3156
public void Click()
3257
{
33-
MouseHover();
34-
Assert.ShouldGet(() => Enabled);
35-
_driverService.MouseClick();
58+
Assert.ShouldBecome(() => Enabled, true, $"Unable to click on {Caption}. The element was disabled");
59+
try
60+
{
61+
Element.Click();
62+
}
63+
catch (ElementClickInterceptedException)
64+
{
65+
MouseHover();
66+
Driver.MouseClick();
67+
}
3668
}
3769

3870
public void MouseHover()
3971
{
40-
Assert.ShouldBecome(() => Enabled, true, $"{Caption} is disabled");
41-
_driverService.ScrollTo(Element);
72+
Assert.ShouldBecome(() => Displayed, true, $"{Caption} is not visible");
73+
Driver.ScrollTo(Element);
4274
}
4375

4476
public void SendKeys(string text)
@@ -47,29 +79,25 @@ public void SendKeys(string text)
4779
Element.SendKeys(text);
4880
}
4981

50-
public bool Displayed => Element != null && Element.Displayed;
51-
52-
public bool Enabled => Displayed && Element.Enabled && AriaEnabled;
53-
54-
public string Tooltip
82+
public bool Displayed
5583
{
5684
get
5785
{
58-
var matTooltip = GetAttribute("matTooltip");
59-
if (matTooltip != null)
86+
try
6087
{
61-
return matTooltip;
88+
return !(Element is null) && Element.Displayed;
6289
}
63-
var ngReflectTip = GetAttribute("ng-reflect-message"); //some elements have their tooltips' texts stored inside 'ng-reflect-message' attribute
64-
if (ngReflectTip != null)
90+
catch (Exception e) when (e is NullReferenceException || e is StaleElementReferenceException)
6591
{
66-
return ngReflectTip;
92+
return false;
6793
}
68-
69-
return GetAttribute("aria-label"); //some elements have their tooltips' texts stored inside 'aria-label' attribute
7094
}
7195
}
7296

97+
public bool Enabled => Displayed && (Element.Enabled || AriaEnabled);
98+
99+
public string Tooltip => GetAttribute("data-test-tooltip-text");
100+
73101
public bool Stale
74102
{
75103
get
@@ -80,7 +108,7 @@ public bool Stale
80108
var elementEnabled = Element.Enabled;
81109
return false;
82110
}
83-
catch (StaleElementReferenceException)
111+
catch (Exception e) when (e is NullReferenceException or StaleElementReferenceException)
84112
{
85113
return true;
86114
}
@@ -89,33 +117,34 @@ public bool Stale
89117

90118
public IEnumerable<IWebElementWrapper> FindSubElements(By locator, string caption)
91119
{
92-
var elements = Assert.ShouldGet(() => Element.FindElements(locator));
93-
return ElementsToWrappers(elements, caption);
120+
try
121+
{
122+
return ElementsToWrappers(Assert.ShouldGet(() => Element.FindElements(locator)), caption);
123+
}
124+
catch (Exception e) when (e is NullReferenceException or InvalidOperationException)
125+
{
126+
NUnit.Framework.Assert.Fail($"Couldn't find elements with {caption}");
127+
return null;
128+
}
94129
}
95130

96131
private IEnumerable<IWebElementWrapper> ElementsToWrappers(IEnumerable<IWebElement> elements, string caption)
97132
{
98-
foreach (var element in elements)
99-
{
100-
var wrapper = new WebElementWrapper(() => element, caption, _driverService);
101-
yield return wrapper;
102-
}
133+
return elements.Select(element => new WebElementWrapper(() => element, caption, Driver));
103134
}
104135

105-
protected IDriverService Driver => _driverService;
136+
protected IDriverService Driver { get; }
106137

107138
private bool AriaEnabled
108139
{
109140
get
110141
{
111-
switch (Element.GetAttribute("aria-disabled"))
142+
return Element.GetAttribute("aria-disabled") switch
112143
{
113-
case null:
114-
case "false":
115-
return true;
116-
}
117-
118-
return false;
144+
null => true,
145+
"false" => true,
146+
_ => false
147+
};
119148
}
120149
}
121150
}

CHANGELOG.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7-
[1.8.5] - 2022-01-27
7+
[1.8.6] - 2022-02-14
88
### Changed
9-
- Bindings code refactoring
10-
- Updated Specflow package version
9+
- Updated project template with the new version of web element wrapper

src/Behavioral.Automation/Behavioral.Automation.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ The whole automation code is divided into the following parts:
1616
- UI structure descriptive code
1717
- Supportive code</Description>
1818
<Copyright>Quantori Inc.</Copyright>
19-
<PackageVersion>1.8.5</PackageVersion>
19+
<PackageVersion>1.8.6</PackageVersion>
2020
<RepositoryUrl>https://github.com/quantori/Behavioral.Automation</RepositoryUrl>
2121
<PublishRepositoryUrl>true</PublishRepositoryUrl>
2222
<IncludeSymbols>true</IncludeSymbols>

0 commit comments

Comments
 (0)