Skip to content

Commit d748c00

Browse files
authored
Merge pull request #102 from quantori/bindings-refactoring
Updated project template wrappers
2 parents a5bd3ba + 361c0e3 commit d748c00

File tree

7 files changed

+89
-73
lines changed

7 files changed

+89
-73
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: 63 additions & 47 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
{
1113
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 not 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 or 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;
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,34 +117,22 @@ 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);
94-
}
95-
96-
private IEnumerable<IWebElementWrapper> ElementsToWrappers(IEnumerable<IWebElement> elements, string caption)
97-
{
98-
foreach (var element in elements)
120+
try
99121
{
100-
var wrapper = new WebElementWrapper(() => element, caption, _driverService);
101-
yield return wrapper;
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;
102128
}
103129
}
104130

105-
protected IDriverService Driver => _driverService;
106-
107-
private bool AriaEnabled
131+
private IEnumerable<IWebElementWrapper> ElementsToWrappers(IEnumerable<IWebElement> elements, string caption)
108132
{
109-
get
110-
{
111-
switch (Element.GetAttribute("aria-disabled"))
112-
{
113-
case null:
114-
case "false":
115-
return true;
116-
}
117-
118-
return false;
119-
}
133+
return elements.Select(element => new WebElementWrapper(() => element, caption, Driver));
120134
}
135+
136+
protected IDriverService Driver { get; }
121137
}
122138
}

CHANGELOG.md

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,5 @@ 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.6] - 2022-01-25
8-
### Fixed
9-
- Error when calling Aggregate() to generate message for DropdownWrapper
10-
11-
[1.8.5] - 2022-01-27
12-
### Changed
13-
- Bindings code refactoring
14-
- Updated Specflow package version
7+
[1.8.7] - 2022-02-25
8+
- Moved tooltip attribute to separate interface

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.6</PackageVersion>
19+
<PackageVersion>1.8.7</PackageVersion>
2020
<RepositoryUrl>https://github.com/quantori/Behavioral.Automation</RepositoryUrl>
2121
<PublishRepositoryUrl>true</PublishRepositoryUrl>
2222
<IncludeSymbols>true</IncludeSymbols>

src/Behavioral.Automation/Bindings/LabelBinding.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
namespace Behavioral.Automation.Bindings
1010
{
11-
/// <summary>
12-
/// Bindings for labels or other text elements testing
11+
/// <summary>
12+
/// Bindings for labels or other text elements testing
1313
/// </summary>
1414
[Binding]
1515
public sealed class LabelBinding
@@ -23,18 +23,18 @@ public LabelBinding([NotNull] RunnerService runnerService, [NotNull] ScenarioCon
2323
_scenarioContext = scenarioContext;
2424
}
2525

26-
/// <summary>
27-
/// Check that element's text is equal to the expected one
28-
/// </summary>
29-
/// <param name="element">Tested web element wrapper</param>
30-
/// <param name="behavior">Assertion behavior (instant or continuous)</param>
26+
/// <summary>
27+
/// Check that element's text is equal to the expected one
28+
/// </summary>
29+
/// <param name="element">Tested web element wrapper</param>
30+
/// <param name="behavior">Assertion behavior (instant or continuous)</param>
3131
/// <param name="value">Expected value</param>
3232
/// <example>Then "Test" element text should be "expected text"</example>
3333
[Given("the (.*?) text (is|is not|become|become not) \"(.*)\"")]
3434
[Then("the (.*?) text should (be|be not|become|become not) \"(.*)\"")]
3535
public void CheckSelectedText(
36-
[NotNull] IWebElementWrapper element,
37-
[NotNull] AssertionBehavior behavior,
36+
[NotNull] IWebElementWrapper element,
37+
[NotNull] AssertionBehavior behavior,
3838
[NotNull] string value)
3939
{
4040
Assert.ShouldBecome(() => StringExtensions.GetElementTextOrValue(element), value, behavior,
@@ -68,7 +68,7 @@ public void CheckSelectedTextContain(
6868
/// <example>Then the "Test" element should have tooltip with text "expected string"</example>
6969
[Then("the (.*?) should (have|not have) tooltip with text \"(.*)\"")]
7070
public void CheckElementTooltip(
71-
[NotNull] IWebElementWrapper element,
71+
[NotNull] ITooltipElementWrapper element,
7272
[NotNull] AssertionBehavior behavior,
7373
[NotNull] string value)
7474
{
@@ -88,8 +88,8 @@ public void CheckElementIsEmpty([NotNull] IWebElementWrapper element, AssertionB
8888
{
8989
Assert.ShouldBecome(() => StringExtensions.GetElementTextOrValue(element), string.Empty, behavior,
9090
$"{element.Caption} text is \"{StringExtensions.GetElementTextOrValue(element)}\"");
91-
}
92-
91+
}
92+
9393
/// <summary>
9494
/// Check that multiple elements' texts are empty
9595
/// </summary>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Behavioral.Automation.Elements
2+
{
3+
public interface ITooltipElementWrapper : IWebElementWrapper
4+
{
5+
/// <summary>
6+
/// Message that appears when mouse is hovered over element
7+
/// </summary>
8+
public string Tooltip { get; }
9+
}
10+
}

src/Behavioral.Automation/Elements/IWebElementWrapper.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,6 @@ public interface IWebElementWrapper
6565
/// </summary>
6666
public bool Enabled { get; }
6767

68-
/// <summary>
69-
/// Message that appears when mouse is hovered over element
70-
/// </summary>
71-
public string Tooltip { get; }
72-
7368
/// <summary>
7469
/// Element staleness. See <seealso cref="StaleElementReferenceException"/>
7570
/// </summary>

0 commit comments

Comments
 (0)