From d935189cd9fd334bf5c69e6b82fe094fb8df0980 Mon Sep 17 00:00:00 2001 From: Igor Lastovka Date: Mon, 1 Nov 2021 12:46:21 +0400 Subject: [PATCH 1/3] Added auto PageContext switch on BeforeStep binding --- .../Bindings/PageScopeObserverBinding.cs | 29 +++++++++++++++++++ .../Mapping/Contract/IScopeContextManager.cs | 1 + .../Services/Mapping/ScopeContextManager.cs | 11 +++++++ 3 files changed, 41 insertions(+) create mode 100644 src/Behavioral.Automation/Bindings/PageScopeObserverBinding.cs diff --git a/src/Behavioral.Automation/Bindings/PageScopeObserverBinding.cs b/src/Behavioral.Automation/Bindings/PageScopeObserverBinding.cs new file mode 100644 index 00000000..455abe58 --- /dev/null +++ b/src/Behavioral.Automation/Bindings/PageScopeObserverBinding.cs @@ -0,0 +1,29 @@ +using Behavioral.Automation.Services; +using Behavioral.Automation.Services.Mapping.Contract; +using JetBrains.Annotations; +using TechTalk.SpecFlow; + +namespace Behavioral.Automation.Bindings +{ + /// + /// Allows to automatically switch PageContext, depending on current Driver url + /// + [Binding] + public class PageScopeObserverBinding + { + private readonly IScopeContextManager _scopeContextManager; + private readonly IDriverService _driverService; + + public PageScopeObserverBinding([NotNull] IScopeContextManager scopeContextManager, [NotNull] IDriverService driverService) + { + _scopeContextManager = scopeContextManager; + _driverService = driverService; + } + + [BeforeStep] + public void CheckScopeSwitch() + { + _scopeContextManager.SwitchToCurrentUrl(_driverService.CurrentUrl); + } + } +} diff --git a/src/Behavioral.Automation/Services/Mapping/Contract/IScopeContextManager.cs b/src/Behavioral.Automation/Services/Mapping/Contract/IScopeContextManager.cs index 6309639c..7e1ac9ee 100644 --- a/src/Behavioral.Automation/Services/Mapping/Contract/IScopeContextManager.cs +++ b/src/Behavioral.Automation/Services/Mapping/Contract/IScopeContextManager.cs @@ -7,5 +7,6 @@ public interface IScopeContextManager void SwitchPage(Uri uri); IScopeContextRuntime UseControlScopeContextRuntime(ControlScopeSelector controlScopeSelector); void SwitchPage(string pageName); + void SwitchToCurrentUrl(string currentUrl); } } \ No newline at end of file diff --git a/src/Behavioral.Automation/Services/Mapping/ScopeContextManager.cs b/src/Behavioral.Automation/Services/Mapping/ScopeContextManager.cs index c12fba51..c3e8b603 100644 --- a/src/Behavioral.Automation/Services/Mapping/ScopeContextManager.cs +++ b/src/Behavioral.Automation/Services/Mapping/ScopeContextManager.cs @@ -7,6 +7,8 @@ public sealed class ScopeContextManager : IScopeContextManager { private readonly IScopeContextRuntime _scopeContextRuntime; private readonly IUriToPageScopeMapper _uriToPageScopeMapper; + private string LastVisitedUrl = string.Empty; + private const string EmptyPageUrl = "data:,"; public ScopeContextManager(IScopeContextRuntime scopeContextRuntime, IUriToPageScopeMapper uriToPageScopeMapper) { @@ -41,5 +43,14 @@ public void SwitchPage(string pageName) var pageScopeContext = _uriToPageScopeMapper.GetPageScopeContext(pageName); _scopeContextRuntime.SwitchToPageScope(pageScopeContext); } + + public void SwitchToCurrentUrl(string currentUrl) + { + if (currentUrl == EmptyPageUrl || currentUrl == LastVisitedUrl) + return; + + SwitchPage(new Uri(currentUrl)); + LastVisitedUrl = currentUrl; + } } } \ No newline at end of file From a28ad82285370b28f2b645df90ecd08221350815 Mon Sep 17 00:00:00 2001 From: Igor Lastovka Date: Mon, 1 Nov 2021 13:28:41 +0400 Subject: [PATCH 2/3] Code review --- .../Bindings/PageScopeObserverBinding.cs | 3 ++- .../Services/Mapping/Contract/IScopeContextManager.cs | 2 +- .../Services/Mapping/ScopeContextManager.cs | 8 ++++---- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Behavioral.Automation/Bindings/PageScopeObserverBinding.cs b/src/Behavioral.Automation/Bindings/PageScopeObserverBinding.cs index 455abe58..1da1373a 100644 --- a/src/Behavioral.Automation/Bindings/PageScopeObserverBinding.cs +++ b/src/Behavioral.Automation/Bindings/PageScopeObserverBinding.cs @@ -1,6 +1,7 @@ using Behavioral.Automation.Services; using Behavioral.Automation.Services.Mapping.Contract; using JetBrains.Annotations; +using System; using TechTalk.SpecFlow; namespace Behavioral.Automation.Bindings @@ -23,7 +24,7 @@ public PageScopeObserverBinding([NotNull] IScopeContextManager scopeContextManag [BeforeStep] public void CheckScopeSwitch() { - _scopeContextManager.SwitchToCurrentUrl(_driverService.CurrentUrl); + _scopeContextManager.SwitchToCurrentUrl(new Uri(_driverService.CurrentUrl)); } } } diff --git a/src/Behavioral.Automation/Services/Mapping/Contract/IScopeContextManager.cs b/src/Behavioral.Automation/Services/Mapping/Contract/IScopeContextManager.cs index 7e1ac9ee..3b8b769c 100644 --- a/src/Behavioral.Automation/Services/Mapping/Contract/IScopeContextManager.cs +++ b/src/Behavioral.Automation/Services/Mapping/Contract/IScopeContextManager.cs @@ -7,6 +7,6 @@ public interface IScopeContextManager void SwitchPage(Uri uri); IScopeContextRuntime UseControlScopeContextRuntime(ControlScopeSelector controlScopeSelector); void SwitchPage(string pageName); - void SwitchToCurrentUrl(string currentUrl); + void SwitchToCurrentUrl(Uri currentUrl); } } \ No newline at end of file diff --git a/src/Behavioral.Automation/Services/Mapping/ScopeContextManager.cs b/src/Behavioral.Automation/Services/Mapping/ScopeContextManager.cs index c3e8b603..43fe39ca 100644 --- a/src/Behavioral.Automation/Services/Mapping/ScopeContextManager.cs +++ b/src/Behavioral.Automation/Services/Mapping/ScopeContextManager.cs @@ -7,8 +7,8 @@ public sealed class ScopeContextManager : IScopeContextManager { private readonly IScopeContextRuntime _scopeContextRuntime; private readonly IUriToPageScopeMapper _uriToPageScopeMapper; - private string LastVisitedUrl = string.Empty; - private const string EmptyPageUrl = "data:,"; + private Uri LastVisitedUrl = default; + private readonly Uri EmptyPageUrl = new Uri("data:,"); public ScopeContextManager(IScopeContextRuntime scopeContextRuntime, IUriToPageScopeMapper uriToPageScopeMapper) { @@ -44,12 +44,12 @@ public void SwitchPage(string pageName) _scopeContextRuntime.SwitchToPageScope(pageScopeContext); } - public void SwitchToCurrentUrl(string currentUrl) + public void SwitchToCurrentUrl(Uri currentUrl) { if (currentUrl == EmptyPageUrl || currentUrl == LastVisitedUrl) return; - SwitchPage(new Uri(currentUrl)); + SwitchPage(currentUrl); LastVisitedUrl = currentUrl; } } From 438a6598a73ec484107660407a147e50e89c1bfe Mon Sep 17 00:00:00 2001 From: Igor Lastovka Date: Mon, 1 Nov 2021 14:53:28 +0400 Subject: [PATCH 3/3] Code review --- .../BeforeStep}/PageScopeObserverBinding.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename src/Behavioral.Automation/{Bindings => Hooks/BeforeStep}/PageScopeObserverBinding.cs (89%) diff --git a/src/Behavioral.Automation/Bindings/PageScopeObserverBinding.cs b/src/Behavioral.Automation/Hooks/BeforeStep/PageScopeObserverBinding.cs similarity index 89% rename from src/Behavioral.Automation/Bindings/PageScopeObserverBinding.cs rename to src/Behavioral.Automation/Hooks/BeforeStep/PageScopeObserverBinding.cs index 1da1373a..cc9dd80c 100644 --- a/src/Behavioral.Automation/Bindings/PageScopeObserverBinding.cs +++ b/src/Behavioral.Automation/Hooks/BeforeStep/PageScopeObserverBinding.cs @@ -4,7 +4,7 @@ using System; using TechTalk.SpecFlow; -namespace Behavioral.Automation.Bindings +namespace Behavioral.Automation.Hooks.BeforeStep { /// /// Allows to automatically switch PageContext, depending on current Driver url @@ -22,7 +22,7 @@ public PageScopeObserverBinding([NotNull] IScopeContextManager scopeContextManag } [BeforeStep] - public void CheckScopeSwitch() + public void SwitchContextToCurrentUrl() { _scopeContextManager.SwitchToCurrentUrl(new Uri(_driverService.CurrentUrl)); }