Skip to content

Commit

Permalink
#79 Add ScrollTo method to Control`1
Browse files Browse the repository at this point in the history
  • Loading branch information
YevgeniyShunevych committed Sep 19, 2017
1 parent 072fbe5 commit 7d13c0b
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/Atata/Atata.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@
<Link>Properties\CommonAssemblyInfo.cs</Link>
</Compile>
<Compile Include="ActivatorEx.cs" />
<Compile Include="Attributes\Behaviors\ScrollBehaviorAttribute.cs" />
<Compile Include="Attributes\Behaviors\ScrollUsingMoveToElementAttribute.cs" />
<Compile Include="Attributes\Behaviors\ScrollUsingScrollIntoViewAttribute.cs" />
<Compile Include="Context\AtataContextMode.cs" />
<Compile Include="AtataMapper.cs" />
<Compile Include="AttributeLevels.cs" />
Expand Down Expand Up @@ -119,6 +122,7 @@
<Compile Include="Logging\Sections\DragAndDropToComponentLogSection.cs" />
<Compile Include="Logging\Sections\DragAndDropToOffsetLogSection.cs" />
<Compile Include="Logging\Sections\PressKeysLogSection.cs" />
<Compile Include="Logging\Sections\ScrollToComponentLogSection.cs" />
<Compile Include="PropertyBag.cs" />
<Compile Include="Attributes\ScopeDefinitionAttribute.cs" />
<Compile Include="Attributes\Triggers\HoverParentAttribute.cs" />
Expand Down
11 changes: 11 additions & 0 deletions src/Atata/Attributes/Behaviors/ScrollBehaviorAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Atata
{
/// <summary>
/// Represents the base behavior class for scrolling to control.
/// </summary>
public abstract class ScrollBehaviorAttribute : MulticastAttribute
{
public abstract void Execute<TOwner>(IControl<TOwner> control)
where TOwner : PageObject<TOwner>, IPageObject<TOwner>;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using OpenQA.Selenium;
using OpenQA.Selenium.Interactions;

namespace Atata
{
/// <summary>
/// Represents the behavior for scrolling to control using WebDriver's <see cref="Actions"/>.
/// Performs <see cref="Actions.MoveToElement(IWebElement)"/> action.
/// </summary>
public class ScrollUsingMoveToElementAttribute : ScrollBehaviorAttribute
{
public override void Execute<TOwner>(IControl<TOwner> control)
{
control.Owner.Driver.Perform(a => a.MoveToElement(control.Scope));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Atata
{
/// <summary>
/// Represents the behavior for scrolling to control using JavaScript.
/// Performs "element.scrollIntoView(true)" function.
/// </summary>
public class ScrollUsingScrollIntoViewAttribute : ScrollBehaviorAttribute
{
public override void Execute<TOwner>(IControl<TOwner> control)
{
control.Owner.Driver.ExecuteScript("arguments[0].scrollIntoView(true);", control.Scope);
}
}
}
27 changes: 27 additions & 0 deletions src/Atata/Components/Control`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ public TOwner DragAndDropTo(Func<TOwner, Control<TOwner>> targetSelector)

/// <summary>
/// Drags and drops the control to the target control. By default uses <see cref="DragAndDropUsingActionsAttribute"/>.
/// Also executes <see cref="TriggerEvents.BeforeClick" /> and <see cref="TriggerEvents.AfterClick" /> triggers.
/// </summary>
/// <param name="target">The target control.</param>
/// <returns>The instance of the owner page object.</returns>
Expand All @@ -177,6 +178,32 @@ protected virtual void OnDragAndDropTo(Control<TOwner> target)
behavior.Execute(this, target);
}

/// <summary>
/// Scrolls to the control. By default uses <see cref="ScrollUsingMoveToElementAttribute"/>.
/// Also executes <see cref="TriggerEvents.BeforeScroll" /> and <see cref="TriggerEvents.AfterScroll" /> triggers.
/// </summary>
/// <returns>The instance of the owner page object.</returns>
public TOwner ScrollTo()
{
ExecuteTriggers(TriggerEvents.BeforeScroll);
Log.Start(new ScrollToComponentLogSection(this));

OnScrollTo();

Log.EndSection();
ExecuteTriggers(TriggerEvents.AfterScroll);

return Owner;
}

protected virtual void OnScrollTo()
{
var behavior = Metadata.Get<ScrollBehaviorAttribute>(AttributeLevels.All)
?? new ScrollUsingMoveToElementAttribute();

behavior.Execute(this);
}

/// <summary>
/// Drags and drops the control to the specified offset.
/// </summary>
Expand Down
11 changes: 11 additions & 0 deletions src/Atata/Logging/Sections/ScrollToComponentLogSection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Atata
{
public class ScrollToComponentLogSection : UIComponentLogSection
{
public ScrollToComponentLogSection(UIComponent component)
: base(component)
{
Message = $"Scroll to {component.ComponentFullName}";
}
}
}
14 changes: 12 additions & 2 deletions src/Atata/TriggerEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,27 @@ public enum TriggerEvents
/// </summary>
AfterFocus = 1 << 13,

/// <summary>
/// Occurs before the scrolling to control.
/// </summary>
BeforeScroll = 1 << 14,

/// <summary>
/// Occurs after the scrolling to control.
/// </summary>
AfterScroll = 1 << 15,

BeforeGetOrSet = BeforeGet | BeforeSet,
BeforeClickOrHover = BeforeClick | BeforeHover,
BeforeClickOrFocus = BeforeClick | BeforeFocus,
BeforeClickOrHoverOrFocus = BeforeClick | BeforeHover | BeforeFocus,
BeforeAnyAction = BeforeClick | BeforeGet | BeforeSet | BeforeFocus,
BeforeAnyAction = BeforeClick | BeforeGet | BeforeSet | BeforeFocus | BeforeScroll,

AfterGetOrSet = AfterGet | AfterSet,
AfterClickOrHover = AfterClick | AfterHover,
AfterClickOrFocus = AfterClick | AfterFocus,
AfterClickOrHoverOrFocus = AfterClick | AfterHover | AfterFocus,
AfterAnyAction = AfterClick | AfterGet | AfterSet | AfterFocus,
AfterAnyAction = AfterClick | AfterGet | AfterSet | AfterFocus | AfterScroll,
AfterClickOrSet = AfterClick | AfterSet,

BeforeAndAfterClick = BeforeClick | AfterClick
Expand Down

0 comments on commit 7d13c0b

Please sign in to comment.