Skip to content

Commit

Permalink
Value conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
nvborisenko committed Aug 24, 2024
1 parent b86a81f commit 78d1a9c
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Yapoml.Selenium/Components/BaseComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,11 @@ public virtual bool IsFocused
}
}

/// <summary>
/// Returns a value of the component.
/// </summary>
public virtual string Value => RelocateOnStaleReference(() => _elementHandler.Locate().GetAttribute("value"));

public override bool Equals(object obj)
{
var str = obj as string;
Expand Down
11 changes: 11 additions & 0 deletions src/Yapoml.Selenium/Components/BaseComponentConditions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,17 @@ public virtual StylesCollectionConditions<TSelf> Styles
}
}

/// <summary>
/// Various expected conditions for value.
/// </summary>
public virtual ValueConditions<TSelf> Value
{
get
{
return new ValueConditions<TSelf>(_self, ElementHandler, Timeout, PollingInterval, $"value of the {ElementHandler.ComponentMetadata.Name}", Logger);
}
}

/// <summary>
/// Waits specified amount of time.
/// </summary>
Expand Down
100 changes: 100 additions & 0 deletions src/Yapoml.Selenium/Components/Conditions/ValueConditions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using OpenQA.Selenium;
using System;
using System.Text.RegularExpressions;
using Yapoml.Framework.Logging;
using Yapoml.Selenium.Components.Conditions.Generic;
using Yapoml.Selenium.Services.Locator;

namespace Yapoml.Selenium.Components.Conditions
{
public class ValueConditions<TConditions> : TextualConditions<TConditions>
{
private IElementHandler _elementHandler;

public ValueConditions(TConditions conditions, IElementHandler elementHandler, TimeSpan timeout, TimeSpan pollingInterval, string subject, ILogger logger)
: base(conditions, timeout, pollingInterval, subject, logger)
{
_elementHandler = elementHandler;
}

public override NumericConditions<TConditions, int> Length => new TextualLengthConditons<TConditions>(_conditions, _timeout, _pollingInterval, FetchValueFunc, $"value of {_elementHandler.ComponentMetadata.Name}", _logger);

protected override Func<string> FetchValueFunc => () => RelocateOnStaleReference(() => _elementHandler.Locate().GetAttribute("value"));

protected override string GetIsError(string latestValue, string expectedValue)
{
return $"Value of the {_elementHandler.ComponentMetadata.Name} is not '{expectedValue}',{GetDifference("it was:", expectedValue, latestValue)}";
}

protected override string GetIsNotError(string latestValue, string expectedValue)
{
return $"Value of the {_elementHandler.ComponentMetadata.Name} is '{latestValue}', when expected to be not.";
}

protected override string GetIsEmptyError(string latestValue)
{
return $"StValueyle '{latestValue}' of the {_elementHandler.ComponentMetadata.Name} is not empty, when expected to be empty.";
}

protected override string GetIsNotEmptyError(string latestValue)
{
return $"Value of the {_elementHandler.ComponentMetadata.Name} is empty, when expected to be not empty.";
}

protected override string GetStartsWithError(string latestValue, string expectedValue)
{
return $"Value '{latestValue}' of the {_elementHandler.ComponentMetadata.Name} component does not start with '{expectedValue}' yet.";
}

protected override string GetDoesNotStartWithError(string latestValue, string expectedValue)
{
return $"Value '{latestValue}' of the {_elementHandler.ComponentMetadata.Name} component starts with '{expectedValue}'.";
}

protected override string GetEndsWithError(string latestValue, string expectedValue)
{
return $"Value '{latestValue}' of the {_elementHandler.ComponentMetadata.Name} component doesn't end with '{expectedValue}'.";
}

protected override string GetDoesNotEndWithError(string latestValue, string expectedValue)
{
return $"Value '{latestValue}' of the {_elementHandler.ComponentMetadata.Name} component ends with '{expectedValue}'.";
}

protected override string GetContainsError(string latestValue, string expectedValue)
{
return $"Value '{latestValue}' of the {_elementHandler.ComponentMetadata.Name} component doesn't contain '{expectedValue}' yet.";
}

protected override string GetDoesNotContainError(string latestValue, string expectedValue)
{
return $"Value '{latestValue}' of the {_elementHandler.ComponentMetadata.Name} component contains '{expectedValue}'.";
}

protected override string GetMatchesError(string latestValue, Regex regex)
{
return $"Value '{latestValue}' of the {_elementHandler.ComponentMetadata.Name} component doesn't match '{regex}'.";
}

protected override string GetDoesNotMatchError(string latestValue, Regex regex)
{
return $"Value '{latestValue}' of the {_elementHandler.ComponentMetadata.Name} component matches '{regex}'.";
}

private T RelocateOnStaleReference<T>(Func<T> act)
{
try
{
return act();
}
catch (StaleElementReferenceException)
{
_elementHandler.Invalidate();

_elementHandler.Locate();

return act();
}
}
}
}

0 comments on commit 78d1a9c

Please sign in to comment.