Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Behavioral.Automation.Services;
using Behavioral.Automation.Services.Mapping.Contract;
using JetBrains.Annotations;
using System;
using TechTalk.SpecFlow;

namespace Behavioral.Automation.Hooks.BeforeStep
{
/// <summary>
/// Allows to automatically switch PageContext, depending on current Driver url
/// </summary>
[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 SwitchContextToCurrentUrl()
{
_scopeContextManager.SwitchToCurrentUrl(new Uri(_driverService.CurrentUrl));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ public interface IScopeContextManager
void SwitchPage(Uri uri);
IScopeContextRuntime UseControlScopeContextRuntime(ControlScopeSelector controlScopeSelector);
void SwitchPage(string pageName);
void SwitchToCurrentUrl(Uri currentUrl);
}
}
11 changes: 11 additions & 0 deletions src/Behavioral.Automation/Services/Mapping/ScopeContextManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public sealed class ScopeContextManager : IScopeContextManager
{
private readonly IScopeContextRuntime _scopeContextRuntime;
private readonly IUriToPageScopeMapper _uriToPageScopeMapper;
private Uri LastVisitedUrl = default;
private readonly Uri EmptyPageUrl = new Uri("data:,");

public ScopeContextManager(IScopeContextRuntime scopeContextRuntime, IUriToPageScopeMapper uriToPageScopeMapper)
{
Expand Down Expand Up @@ -41,5 +43,14 @@ public void SwitchPage(string pageName)
var pageScopeContext = _uriToPageScopeMapper.GetPageScopeContext(pageName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably, call of SwitchPage method is not needed anymore from NavigationBindings.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, I think we still need that, in case if step is complex and we call driverService.Navigate() multiple times inside of it, Of course it may lead to extra context switches, but Andrey agreed that they are not that time consuming.

_scopeContextRuntime.SwitchToPageScope(pageScopeContext);
}

public void SwitchToCurrentUrl(Uri currentUrl)
{
if (currentUrl == EmptyPageUrl || currentUrl == LastVisitedUrl)
return;

SwitchPage(currentUrl);
LastVisitedUrl = currentUrl;
}
}
}