Skip to content

Instantly share code, notes, and snippets.

@YevgeniyShunevych
Created November 23, 2018 20:29
Show Gist options
  • Save YevgeniyShunevych/4744a14cdb36b509e3543bf4a066b5d0 to your computer and use it in GitHub Desktop.
Save YevgeniyShunevych/4744a14cdb36b509e3543bf4a066b5d0 to your computer and use it in GitHub Desktop.
Atata approach to get associated label container elements
using System;
namespace Atata
{
public class LabelContainerList<TOwner> : ControlList<Text<TOwner>, TOwner>
where TOwner : PageObject<TOwner>
{
public Text<TOwner> this[Func<TOwner, Control<TOwner>> controlSelector]
{
get { return For(controlSelector); }
}
public Text<TOwner> For(Func<TOwner, Control<TOwner>> controlSelector)
{
Control<TOwner> targetControl = controlSelector(Component.Owner);
return CreateAssociatedControl(targetControl);
}
private Text<TOwner> CreateAssociatedControl(Control<TOwner> control)
{
string id = control.Attributes.Id;
return Component.Controls.Create<Text<TOwner>>(
control.ComponentName,
new ControlDefinitionAttribute { ComponentTypeName = "container" },
new FindByXPathAttribute($"div[label[@for='{id}']]"));
}
}
}
<div class="form-group">
<label for="text-input">Text Input</label>
<input id="text-input" type="text" class="form-control" />
+ DIV content
</div>
using Atata;
namespace SomeApp.UITests
{
using _ = SamplePage;
public class SamplePage : Page<_>
{
[FindById("text-input")]
public TextInput<_> TextInput { get; private set; }
public LabelContainerList<_> LabelContainers { get; private set; }
}
}
using Atata;
using NUnit.Framework;
namespace SampleApp.UITests
{
public class LabelListTests : UITestFixture
{
[Test]
public void LabelList()
{
Go.To<SomePage>().
LabelContainers[x => x.TextInput].Should.ContainAll("Text Input", "+ DIV content");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment