Skip to content

Commit

Permalink
Merge pull request #194 from obstar/master
Browse files Browse the repository at this point in the history
Added the ability to pass in separate options to the Select.From method
  • Loading branch information
obstar authored Dec 8, 2018
2 parents 87747bb + 73f6e68 commit f9d771b
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 43 deletions.
27 changes: 25 additions & 2 deletions src/Coypu.AcceptanceTests/Examples/SelectFromExamples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Coypu.AcceptanceTests.Examples
public class SelectFromExamples : WaitAndRetryExamples
{
[Test]
public void SelectFrom_element_example()
public void SelectFrom_element()
{
var field = Browser.FindField("containerLabeledSelectFieldId");
Assert.That(field.SelectedOption, Is.EqualTo("select two option one"));
Expand All @@ -16,7 +16,7 @@ public void SelectFrom_element_example()
}

[Test]
public void SelectFrom_example()
public void SelectFrom()
{
var textField = Browser.FindField("containerLabeledSelectFieldId");
Assert.That(textField.SelectedOption, Is.EqualTo("select two option one"));
Expand All @@ -26,5 +26,28 @@ public void SelectFrom_example()
textField = Browser.FindField("containerLabeledSelectFieldId");
Assert.That(textField.SelectedOption, Is.EqualTo("select two option two"));
}

[Test]
public void SelectFromWithOptions()
{
var textField = Browser.FindField("selectField", Options.First);
Assert.That(textField.SelectedOption, Is.EqualTo("option"));

Browser.Select("value3")
.From("selectField", Options.First);
textField = Browser.FindField("selectField", Options.First);
Assert.That(textField.SelectedOption, Is.EqualTo("option option"));
}
[Test]
public void SelectWithOptionsFromWithOptions()
{
var textField = Browser.FindField("selectField", Options.First);
Assert.That(textField.SelectedOption, Is.EqualTo("option"));

Browser.Select("four", Options.Single)
.From("selectField", Options.First);
textField = Browser.FindField("selectField", Options.First);
Assert.That(textField.Value, Is.EqualTo("value4"));
}
}
}
16 changes: 14 additions & 2 deletions src/Coypu.Drivers.Tests/html/InteractionTestsPage.htm
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,20 @@ <h2>These are unique so should be found</h2>
<input type="radio" name="chooseRadio" id="chooseRadio1" value="Radio buttons - 1st value" />
<input type="radio" name="chooseRadio" id="chooseRadio2" value="Radio buttons - 2nd value" onclick="this.value += ' - clicked';" />
<input type="radio" name="chooseRadio" id="chooseRadio3" value="Radio buttons - 3rd value" />


<br/>
select field 1:
<select id="selectField" name="selectField" onchange="this.name += ' - changed'">
<option value="value1">option</option>
<option value="value2">option</option>
<option value="value3">option option</option>
<option value="value4">four</option>
</select>
<br/>
select field 2:
<select id="selectField" name="selectField" onchange="this.name += ' - changed'">
<option value="value1">test</option>
<option value="value2">test</option>
</select>
</fieldset>

<fieldset>
Expand Down
Binary file not shown.
68 changes: 44 additions & 24 deletions src/Coypu/Actions/Select.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,66 @@ namespace Coypu.Actions
{
internal class Select : DriverAction
{
private readonly string locator;
private readonly string optionToSelect;
private readonly Options options;
private readonly DisambiguationStrategy disambiguationStrategy;
private ElementScope selectElement;
private readonly DisambiguationStrategy _disambiguationStrategy;
private Options _fromOptions;
private readonly string _locator;
private readonly Options _options;
private readonly string _optionToSelect;
private ElementScope _selectElement;

internal Select(IDriver driver, DriverScope scope, string locator, string optionToSelect, DisambiguationStrategy disambiguationStrategy, Options options)
: base(driver, scope, options)
internal Select(IDriver driver,
DriverScope scope,
string locator,
string optionToSelect,
DisambiguationStrategy disambiguationStrategy,
Options options) : base(driver, scope, options)
{
this.locator = locator;
this.optionToSelect = optionToSelect;
this.options = options;
this.disambiguationStrategy = disambiguationStrategy;
_locator = locator;
_optionToSelect = optionToSelect;
_options = options;
_disambiguationStrategy = disambiguationStrategy;
}

internal Select(IDriver driver, ElementScope selectElement, string optionToSelect, DisambiguationStrategy disambiguationStrategy, Options options)
: base(driver, selectElement, options)
internal Select(IDriver driver,
DriverScope scope,
string locator,
string optionToSelect,
DisambiguationStrategy disambiguationStrategy,
Options options,
Options fromOptions) : this(driver, scope, locator, optionToSelect, disambiguationStrategy, options)
{
this.selectElement = selectElement;
this.optionToSelect = optionToSelect;
this.options = options;
this.disambiguationStrategy = disambiguationStrategy;
_fromOptions = fromOptions;
}

internal Select(IDriver driver,
ElementScope selectElement,
string optionToSelect,
DisambiguationStrategy disambiguationStrategy,
Options options) : base(driver, selectElement, options)
{
_selectElement = selectElement;
_optionToSelect = optionToSelect;
_options = options;
_disambiguationStrategy = disambiguationStrategy;
}

public override void Act()
{
selectElement = selectElement ?? FindSelectElement();
SelectOption(selectElement);
_selectElement = _selectElement ?? FindSelectElement();
SelectOption(_selectElement);
}

private SnapshotElementScope FindSelectElement()
{
var selectElementFound = disambiguationStrategy.ResolveQuery(new SelectFinder(Driver, locator, Scope, options));
return new SnapshotElementScope(selectElementFound, Scope, options);
if (_fromOptions == null) _fromOptions = _options;
var selectElementFound = _disambiguationStrategy.ResolveQuery(new SelectFinder(Driver, _locator, Scope, _fromOptions));
return new SnapshotElementScope(selectElementFound, Scope, _fromOptions);
}

void SelectOption(ElementScope selectElementScope)
private void SelectOption(ElementScope selectElementScope)
{
var option = disambiguationStrategy.ResolveQuery(new OptionFinder(Driver, optionToSelect, selectElementScope, options));
var option = _disambiguationStrategy.ResolveQuery(new OptionFinder(Driver, _optionToSelect, selectElementScope, _options));
Driver.Click(option);
}
}
}
}
48 changes: 33 additions & 15 deletions src/Coypu/SelectFrom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,50 @@ namespace Coypu
{
public class SelectFrom
{
private readonly string option;
private readonly IDriver driver;
private readonly TimingStrategy timingStrategy;
private readonly DriverScope scope;
private readonly Options options;
private readonly DisambiguationStrategy disambiguationStrategy;
private readonly DisambiguationStrategy _disambiguationStrategy;
private readonly IDriver _driver;
private readonly string _option;
private readonly Options _options;
private readonly DriverScope _scope;
private readonly TimingStrategy _timingStrategy;

internal SelectFrom(string option, IDriver driver, TimingStrategy timingStrategy, DriverScope scope, Options options, DisambiguationStrategy disambiguationStrategy)
internal SelectFrom(string option,
IDriver driver,
TimingStrategy timingStrategy,
DriverScope scope,
Options options,
DisambiguationStrategy disambiguationStrategy)
{
this.option = option;
this.driver = driver;
this.timingStrategy = timingStrategy;
this.scope = scope;
this.options = options;
this.disambiguationStrategy = disambiguationStrategy;
_option = option;
_driver = driver;
_timingStrategy = timingStrategy;
_scope = scope;
_options = options;
_disambiguationStrategy = disambiguationStrategy;
}

/// <summary>
/// Find the first matching select to appear within the SessionConfiguration.Timeout from which to select this option.
/// Find the first matching select to appear within the SessionConfiguration.Timeout from which to select this option.
/// </summary>
/// <param name="locator">The text of the associated label element, the id or name</param>
/// <exception cref="T:Coypu.MissingHtmlException">Thrown if the element cannot be found</exception>
public void From(string locator)
{
timingStrategy.Synchronise(new Select(driver, scope, locator, option, disambiguationStrategy, options));
From(locator, null);
}

/// <summary>
/// Find the first matching select to appear within the SessionConfiguration.Timeout from which to select this option.
/// </summary>
/// <param name="locator">The text of the associated label element, the id or name</param>
/// <param name="fromOptions">
/// <para>Override the way Coypu is configured to find elements for this call only.</para>
/// <para>E.g. A longer wait</para></param>
/// <exception cref="T:Coypu.MissingHtmlException">Thrown if the element cannot be found</exception>
public void From(string locator,
Options fromOptions)
{
_timingStrategy.Synchronise(new Select(_driver, _scope, locator, _option, _disambiguationStrategy, _options, fromOptions));
}
}
}

0 comments on commit f9d771b

Please sign in to comment.