Skip to content

Commit

Permalink
Check/Uncheck methods with corresponding expectations
Browse files Browse the repository at this point in the history
  • Loading branch information
nvborisenko committed May 17, 2024
1 parent db9f700 commit 3454922
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 2 deletions.
52 changes: 52 additions & 0 deletions src/Yapoml.Playwright/Components/BaseComponent.Actions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,58 @@ public virtual TComponent DoubleClick(Action<TConditions> when)
return DoubleClick();
}

/// <summary>
/// Perform check operation for the component.
/// </summary>
/// <returns>The same instance of the component to continue interaction with it.</returns>
public virtual TComponent Check()
{
using (var scope = _logger.BeginLogScope($"Checking on {Metadata.Name}"))
{
scope.Execute(async () =>
{
await RelocateOnStaleReference(async () => await WrappedElement.CheckAsync());
});
}

return component;
}

/// <inheritdoc cref="Check()"/>
/// <param name="when">Condition to be satisfied before checking.</param>
public virtual TComponent Check(Action<TConditions> when)
{
when(conditions);

return Check();
}

/// <summary>
/// Perform uncheck operation for the component.
/// </summary>
/// <returns>The same instance of the component to continue interaction with it.</returns>
public virtual TComponent Uncheck()
{
using (var scope = _logger.BeginLogScope($"Unchecking on {Metadata.Name}"))
{
scope.Execute(async () =>
{
await RelocateOnStaleReference(async () => await WrappedElement.UncheckAsync());
});
}

return component;
}

/// <inheritdoc cref="Uncheck()"/>
/// <param name="when">Condition to be satisfied before unchecking.</param>
public virtual TComponent Uncheck(Action<TConditions> when)
{
when(conditions);

return Uncheck();
}

/// <summary>
/// Performs a drag and drop operation to another component.
/// </summary>
Expand Down
9 changes: 7 additions & 2 deletions src/Yapoml.Playwright/Components/BaseComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Yapoml.Playwright.Components.Metadata;
using Yapoml.Playwright.Options;
using Microsoft.Playwright;
using System.Threading.Tasks;

namespace Yapoml.Playwright.Components
{
Expand Down Expand Up @@ -159,7 +158,13 @@ public BaseComponent(BasePage page, BaseComponent parentComponent, IPage driver,
/// </summary>
public virtual bool IsEnabled => RelocateOnStaleReference(() => WrappedElement.IsEnabledAsync().GetAwaiter().GetResult());

public virtual bool IsSelected => RelocateOnStaleReference(() => WrappedElement.IsCheckedAsync().GetAwaiter().GetResult());
/// <summary>
/// Indicates whether a component currently is checked or not.
/// <para>
/// It returns a boolean value: <c>true</c> if the component is checked, and <c>false</c> if the component is unchecked.
/// </para>
/// </summary>
public virtual bool IsChecked => RelocateOnStaleReference(() => WrappedElement.IsCheckedAsync().GetAwaiter().GetResult());

/// <summary>
/// Indicates whether a component currently is partially visible within viewport or not.
Expand Down
66 changes: 66 additions & 0 deletions src/Yapoml.Playwright/Components/BaseComponentConditions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,72 @@ public TSelf IsNotEnabled(TimeSpan? timeout = null)
return IsDisabled(timeout);
}

/// <summary>
/// Waits until the component is checked.
/// </summary>
/// <param name="timeout">How long to wait until the component is checked.</param>
/// <returns></returns>
/// <exception cref="ExpectException"></exception>
public virtual TSelf IsChecked(TimeSpan? timeout = null)
{
timeout ??= Timeout;

try
{
using (var scope = Logger.BeginLogScope($"Expect {ElementHandler.ComponentMetadata.Name} is checked"))
{
scope.Execute(() =>
{
Assertions.Expect(ElementHandler.Locate()).ToBeCheckedAsync(new LocatorAssertionsToBeCheckedOptions { Timeout = (float)timeout.Value.TotalMilliseconds }).GetAwaiter().GetResult();
});
}
}
catch (TimeoutException ex)
{
throw new ExpectException($"{ElementHandler.ComponentMetadata.Name} is still not checked.", ex);
}

return _self;
}

/// <summary>
/// Waits until the component is unchecked.
/// </summary>
/// <param name="timeout">How long to wait until the component is unchecked.</param>
/// <returns></returns>
/// <exception cref="ExpectException"></exception>
public virtual TSelf IsNotChecked(TimeSpan? timeout = null)
{
timeout ??= Timeout;

try
{
using (var scope = Logger.BeginLogScope($"Expect {ElementHandler.ComponentMetadata.Name} is unchecked"))
{
scope.Execute(() =>
{
Assertions.Expect(ElementHandler.Locate()).ToBeCheckedAsync(new LocatorAssertionsToBeCheckedOptions { Checked = false, Timeout = (float)timeout.Value.TotalMilliseconds }).GetAwaiter().GetResult();
});
}
}
catch (TimeoutException ex)
{
throw new ExpectException($"{ElementHandler.ComponentMetadata.Name} is still checked.", ex);
}

return _self;
}

/// <summary>
/// Waits until the component is unchecked.
/// </summary>
/// <param name="timeout">How long to wait until the component is unchecked.</param>
/// <returns></returns>
public virtual TSelf IsUnchecked(TimeSpan? timeout = null)
{
return IsNotChecked(timeout);
}

/// <summary>
/// Waits until the component is in view.
/// </summary>
Expand Down

0 comments on commit 3454922

Please sign in to comment.