Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SingleUIState and ManyUIState implement questions #81

Merged
merged 1 commit into from
Sep 2, 2018
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
Expand Up @@ -24,7 +24,7 @@ public Model.ToDoItem Convert(IWebElement source, CultureInfo culture)
{
var target = Target.The("To do item").LocatedByWebElement(source);
return new Model.ToDoItem(
_actor.AsksFor(TextContent.Of(ToDoPage.ToDoItemName.RelativeTo(target)).Value),
_actor.AsksFor(TextContent.Of(ToDoPage.ToDoItemName.RelativeTo(target))),
target.ResolveFor(_webDriver).GetAttribute("class").Contains("completed")
);
}
Expand Down
20 changes: 10 additions & 10 deletions src/Tranquire.Selenium.Tests/Actions/Actions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private void TestClick(string buttonId, Func<ITarget, IAction<Unit>> click)
//act
Fixture.Actor.When(click(clickTarget));
//assert
var actual = Answer(TextContent.Of(expectedClickContent).Value);
var actual = Answer(TextContent.Of(expectedClickContent));
Assert.Equal(expected, actual);
}

Expand All @@ -58,7 +58,7 @@ public void SelectByValueAction_ShouldSelectCorrectElement(string expected)
var action = Select.TheValue(expected).Into(SelectElementTarget);
//act
Fixture.Actor.When(action);
var actual = Answer(SelectedValue.Of(SelectElementTarget).Value);
var actual = Answer(SelectedValue.Of(SelectElementTarget));
//assert
Assert.Equal(expected, actual);
}
Expand All @@ -79,7 +79,7 @@ public void SelectByValuesAction_ShouldSelectCorrectElement(
var action = Select.TheValues(expected).Into(SelectManyElementTarget);
//act
Fixture.Actor.When(action);
var actual = Answer(SelectedValues.Of(SelectManyElementTarget).Value);
var actual = Answer(SelectedValues.Of(SelectManyElementTarget));
//assert
Assert.Equal(expected, actual);
}
Expand All @@ -95,7 +95,7 @@ public void SelectByIndexAction_ShouldSelectCorrectElement(int index, string exp
var action = Select.TheIndex(index).Into(SelectElementTarget);
//act
Fixture.Actor.When(action);
var actual = Answer(SelectedValue.Of(SelectElementTarget).Value);
var actual = Answer(SelectedValue.Of(SelectElementTarget));
//assert
Assert.Equal(expected, actual);
}
Expand All @@ -112,7 +112,7 @@ public void SelectByIndexesAction_ShouldSelectCorrectElement(int[] indexes, stri
var action = Select.TheIndexes(indexes).Into(SelectManyElementTarget);
//act
Fixture.Actor.When(action);
var actual = Answer(SelectedValues.Of(SelectManyElementTarget).Value);
var actual = Answer(SelectedValues.Of(SelectManyElementTarget));
//assert
Assert.Equal(expected, actual);
}
Expand All @@ -128,7 +128,7 @@ public void SelectByTextAction_ShouldSelectCorrectElement(string text, string ex
var action = Select.TheText(text).Into(SelectElementTarget);
//act
Fixture.Actor.When(action);
var actual = Answer(SelectedValue.Of(SelectElementTarget).Value);
var actual = Answer(SelectedValue.Of(SelectElementTarget));
//assert
Assert.Equal(expected, actual);
}
Expand All @@ -145,7 +145,7 @@ public void SelectByTextsAction_ShouldSelectCorrectElement(string[] texts, strin
var action = Select.TheTexts(texts).Into(SelectManyElementTarget);
//act
Fixture.Actor.When(action);
var actual = Answer(SelectedValues.Of(SelectManyElementTarget).Value);
var actual = Answer(SelectedValues.Of(SelectManyElementTarget));
//assert
Assert.Equal(expected, actual);
}
Expand All @@ -163,7 +163,7 @@ public void ClearValue_ShouldClearTheValue(string inputValue)
//act
Fixture.Actor.When(Clear.TheValueOf(target));
//assert
var actual = Answer(Value.Of(target).Value);
var actual = Answer(Value.Of(target));
Assert.Equal(string.Empty, actual);
}

Expand All @@ -178,7 +178,7 @@ public void ClearValue_WhenCursorIsNotAtTheEnd_ShouldClearTheValue()
//act
Fixture.Actor.When(Clear.TheValueOf(target));
//assert
var actual = Answer(Value.Of(target).Value);
var actual = Answer(Value.Of(target));
Assert.Equal(string.Empty, actual);
}

Expand All @@ -193,7 +193,7 @@ public void ClearValue_WhenCursorIsAtTheBeginning_ShouldClearTheValue()
//act
Fixture.Actor.When(Clear.TheValueOf(target));
//assert
var actual = Answer(Value.Of(target).Value);
var actual = Answer(Value.Of(target));
Assert.Equal(string.Empty, actual);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private void TestExecute(string expected, System.Action<Tranquire.IAction<Unit>>
//act
execute(Click.On(target).AllowRetry());
//assert
var actual = Answer(Value.Of(Target.The("expected value").LocatedBy(By.Id("ExpectedValue"))).Value);
var actual = Answer(Value.Of(Target.The("expected value").LocatedBy(By.Id("ExpectedValue"))));
Assert.Equal(expected, actual);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Tranquire.Selenium.Tests/Actions/EnterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void EnterNewValue_ShouldReturnCorrectValue(string expected)
var action = Enter.TheNewValue(expected).Into(target);
//act
Fixture.Actor.When(action);
var actual = Answer(Value.Of(target).Value);
var actual = Answer(Value.Of(target));
//assert
Assert.Equal(expected, actual);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void Execute_ShouldOpenContextMenu(string expected)
//act
Fixture.Actor.When(action);
//assert
var actual = Answer(Value.Of(expectedClickContent).Value);
var actual = Answer(Value.Of(expectedClickContent));
Assert.Equal(expected, actual);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Tranquire.Selenium.Tests/Actions/WaitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void Execute_ShouldWait(string id)
//act
Fixture.Actor.When(Wait.UntilTargetIsPresent(target));
//assert
var actual = Answer(Presence.Of(target).Value);
var actual = Answer(Presence.Of(target));
Assert.True(actual);
}

Expand Down Expand Up @@ -59,7 +59,7 @@ public void UntilQuestionIsAnswered_ShouldWait(string expected)
//arrange
var target = Target.The("element to wait for").LocatedBy(By.Id("ChangeTextElement"));
ChangeText(expected);
var question = TextContent.Of(target).Value;
var question = TextContent.Of(target);
//act
Fixture.Actor.When(Wait.UntilQuestionIsAnswered(question, t => t == expected));
//arrange
Expand All @@ -73,7 +73,7 @@ public void UntilQuestionIsAnswered_WhenTimeout_ShouldThrow(string expected)
//arrange
var target = Target.The("element to wait for").LocatedBy(By.Id("ChangeTextElement"));
ChangeText(expected);
var question = TextContent.Of(target).Value;
var question = TextContent.Of(target);
//act
Assert.Throws<TimeoutException>(() =>
Fixture.Actor.When(Wait.UntilQuestionIsAnswered(question, t => t == expected)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public void ElementWithAttribute_ShouldReturnCorrectValue(string attribute, stri
{
//arrange
var target = Target.The("element with attribute").LocatedBy(By.Id("ElementWithAttribute"));
var question = HtmlAttribute.Of(target).Named(attribute).Value;
var question = HtmlAttribute.Of(target).Named(attribute);
//act
var actual = Answer(question);
//assert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void Classes_ShouldReturnCorrectValue(string id, string[] expected)
{
//arrange
var target = Target.The("Element with classes").LocatedBy(By.Id(id));
var question = Classes.Of(target).Value;
var question = Classes.Of(target);
//act
var actual = Answer(question);
//assert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void CssValueElement_ShouldReturnCorrectValue(string id, string cssProper
{
//arrange
var target = Target.The("css value element").LocatedBy(By.Id(id));
var question = CssValue.Of(target).AndTheProperty(cssProperty).Value;
var question = CssValue.Of(target).AndTheProperty(cssProperty);
//act
var actual = Answer(question);
//assert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void EnabledElement_ShouldReturnCorrectValue(string id, bool expected)
{
//arrange
var target = Target.The("enabled element").LocatedBy(By.Id(id));
var question = Enabled.Of(target).Value;
var question = Enabled.Of(target);
//act
var actual = Answer(question);
//assert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public void Presence_ShouldReturnCorrectValue(string id, bool expected)
{
//arrange
var target = Target.The("presence element").LocatedBy(By.Id(id));
var question = Presence.Of(target).Value;
var question = Presence.Of(target);
//act
var actual = Answer(question);
//assert
Expand Down
10 changes: 5 additions & 5 deletions src/Tranquire.Selenium.Tests/Questions/Questions.Selected.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void SelectedElement_ShouldReturnCorrectValue(string id, bool expected)
{
//arrange
var target = Target.The("selected element").LocatedBy(By.Id(id));
var question = Selected.Of(target).Value;
var question = Selected.Of(target);
//act
var actual = Answer(question);
//assert
Expand All @@ -31,7 +31,7 @@ public void SelectedValueElement_ShouldReturnCorrectValue(string id, string expe
{
//arrange
var target = Target.The("selected element").LocatedBy(By.Id(id));
var question = SelectedValue.Of(target).Value;
var question = SelectedValue.Of(target);
//act
var actual = Answer(question);
//assert
Expand All @@ -43,7 +43,7 @@ public void SelectedValueElement_WhenTargetIsNotASelectElement_ShouldThrow()
{
//arrange
var target = Target.The("selected element").LocatedBy(By.Id("NotASelectElement"));
var question = SelectedValue.Of(target).Value;
var question = SelectedValue.Of(target);
//act
Assert.Throws<UnexpectedTagNameException>(() => Answer(question));
}
Expand All @@ -59,7 +59,7 @@ public void SelectedValuesElement_ShouldReturnCorrectValue(string id, string[] e
{
//arrange
var target = Target.The("selected element").LocatedBy(By.Id(id));
var question = SelectedValues.Of(target).Value;
var question = SelectedValues.Of(target);
//act
var actual = Answer(question);
//assert
Expand All @@ -71,7 +71,7 @@ public void SelectedValuesElement_WhenTargetIsNotASelectElement_ShouldThrow()
{
//arrange
var target = Target.The("selected element").LocatedBy(By.Id("NotASelectElement"));
var question = SelectedValues.Of(target).Value;
var question = SelectedValues.Of(target);
//act
Assert.Throws<UnexpectedTagNameException>(() => Answer(question));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Tranquire.Selenium.Tests/Questions/Questions.Value.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void ValueElement_ShouldReturnCorrectValue(string id, string expected)
{
//arrange
var target = Target.The("value element").LocatedBy(By.Id(id));
var question = Value.Of(target).Value;
var question = Value.Of(target);
//act
var actual = Answer(question);
//assert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void VisibleElement_ShouldReturnCorrectValue(string id, bool expected)
{
//arrange
var target = Target.The("visible element").LocatedBy(By.Id(id));
var question = Visibility.Of(target).Value;
var question = Visibility.Of(target);
//act
var actual = Answer(question);
//assert
Expand Down
15 changes: 11 additions & 4 deletions src/Tranquire.Selenium/Questions/ManyUIState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ namespace Tranquire.Selenium.Questions
/// Represent the UI state for a list of elements in the page
/// </summary>
/// <typeparam name="T"></typeparam>
public class ManyUIState<T> : UIState<T>
public class ManyUIState<T> : UIState<T>, IQuestion<ImmutableArray<T>>
{
private readonly Func<IWebElement, T> Resolve;
private readonly Lazy<IQuestion<ImmutableArray<T>>> _question;

/// <summary>
/// Creates a new instance of <see cref="ManyUIState{T}"/>
Expand All @@ -25,13 +26,15 @@ public ManyUIState(ITarget target, Func<IWebElement, T> resolve, CultureInfo cul
: base(target, culture)
{
Resolve = resolve;
_question = new Lazy<IQuestion<ImmutableArray<T>>>(() => CreateQuestion(new GenericConverter<T, T>(t => t)));
}

/// <summary>
/// Gets a question which returns the state
/// </summary>
public IQuestion<ImmutableArray<T>> Value => CreateQuestion<T>(new GenericConverter<T, T>(t => t));

[Obsolete("This property will be removed in the future. Cast this class as a IQuestion<ImmutableArray<T>> in order to get a question")]
public IQuestion<ImmutableArray<T>> Value => _question.Value;

/// <summary>
/// Creates a question
/// </summary>
Expand Down Expand Up @@ -86,5 +89,9 @@ protected override ImmutableArray<TConverted> Answer(IActor actor, WebBrowser ab
/// </summary>
/// <returns></returns>
public override string ToString() => $"What are the state of the elements identified by {Target.ToString()} ?";

string INamed.Name => _question.Value.Name;

ImmutableArray<T> IQuestion<ImmutableArray<T>>.AnsweredBy(IActor actor) => _question.Value.AnsweredBy(actor);
}
}
18 changes: 15 additions & 3 deletions src/Tranquire.Selenium/Questions/SingleUIState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,26 @@ namespace Tranquire.Selenium.Questions
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="TState"></typeparam>
public abstract class SingleUIState<T, TState> : UIState<T> where TState : SingleUIState<T, TState>
public abstract class SingleUIState<T, TState> : UIState<T>, IQuestion<T>
where TState : SingleUIState<T, TState>
{
private readonly Lazy<IQuestion<T>> _question;

/// <summary>
/// Creates a new instance of <see cref="SingleUIState{T, TState}"/> with a culture
/// </summary>
/// <param name="target"></param>
/// <param name="culture"></param>
protected SingleUIState(ITarget target, CultureInfo culture) : base(target, culture)
{
_question = new Lazy<IQuestion<T>>(() => CreateQuestion(new GenericConverter<T, T>(t => t)));
}

/// <summary>
/// Gets a question returning the state
/// </summary>
public IQuestion<T> Value => CreateQuestion<T>(new GenericConverter<T, T>(t => t));
[Obsolete("This property will be removed in the future. Cast this class as a IQuestion<T> in order to get a question")]
public IQuestion<T> Value => _question.Value;

/// <summary>
/// Creates a question
Expand Down Expand Up @@ -94,5 +99,12 @@ protected override TConverted Answer(IActor actor, WebBrowser ability)
/// </summary>
/// <returns></returns>
public override string ToString() => $"What is the state of the element identified by {Target.ToString()} ?";
}

string INamed.Name => _question.Value.Name;

T IQuestion<T>.AnsweredBy(IActor actor)
{
return _question.Value.AnsweredBy(actor);
}
}
}
47 changes: 47 additions & 0 deletions src/Tranquire.Tests/QuestionClassWrapperTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using AutoFixture.Idioms;
using AutoFixture.Xunit2;
using Moq;
using System;
using Tranquire;
using Xunit;

namespace Tranquire.Tests
{
public class QuestionClassWrapperTests
{
[Theory, DomainAutoData]
public void Sut_VerifyGuardClauses(GuardClauseAssertion assertion)
{
assertion.Verify(typeof(QuestionClassWrapper<object>).GetConstructors());
}

[Theory, DomainAutoData]
public void Name_ShouldReturnCorrectValue(
string expected,
[Frozen]IQuestion<object> innerQuestion,
QuestionClassWrapper<object> sut)
{
// arrange
Mock.Get(innerQuestion).Setup(q => q.Name).Returns(expected);
// act
var actual = sut.Name;
// assert
Assert.Equal(expected, actual);
}

[Theory, DomainAutoData]
public void AnsweredBy_ShouldReturnCorrectValue(
object expected,
[Frozen]IQuestion<object> innerQuestion,
QuestionClassWrapper<object> sut,
IActor actor)
{
// arrange
Mock.Get(innerQuestion).Setup(q => q.AnsweredBy(actor)).Returns(expected);
// act
var actual = sut.AnsweredBy(actor);
// assert
Assert.Equal(expected, actual);
}
}
}
Loading