Skip to content

Commit

Permalink
#29 OuterXPath functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
YevgeniyShunevych committed May 26, 2017
1 parent b677e86 commit fe7f8eb
Show file tree
Hide file tree
Showing 19 changed files with 49 additions and 35 deletions.
2 changes: 2 additions & 0 deletions src/Atata/ScopeSearch/ComponentScopeLocateOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public class ComponentScopeLocateOptions : ICloneable

public string[] Terms { get; set; }

public string OuterXPath { get; set; }

public string ElementXPath { get; set; }

public int? Index { get; set; }
Expand Down
5 changes: 5 additions & 0 deletions src/Atata/ScopeSearch/ComponentScopeXPathBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ public ComponentScopeXPathBuilder(ComponentScopeLocateOptions options)

public ComponentScopeLocateOptions Options { get; private set; }

public ComponentScopeXPathBuilder OuterXPath
{
get { return Options.OuterXPath != null ? _(Options.OuterXPath) : Descendant; }
}

public ComponentScopeXPathBuilder ComponentXPath
{
get { return _(Options.ElementXPath); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public FindByAttributeStrategy(string attributeName)
protected override string Build(ComponentScopeXPathBuilder builder, ComponentScopeLocateOptions options)
{
return builder.
WrapWithIndex(x => x.Descendant.ComponentXPath[y => y.TermsConditionOf(attributeName)]);
WrapWithIndex(x => x.OuterXPath.ComponentXPath[y => y.TermsConditionOf(attributeName)]);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public FindByChildContentStrategy(int childIndex)
protected override string Build(ComponentScopeXPathBuilder builder, ComponentScopeLocateOptions options)
{
return builder.
WrapWithIndex(x => x.Descendant.ComponentXPath[y => y.Any[childIndex + 1][z => z.TermsConditionOfContent]]);
WrapWithIndex(x => x.OuterXPath.ComponentXPath[y => y.Any[childIndex + 1][z => z.TermsConditionOfContent]]);
}
}
}
2 changes: 1 addition & 1 deletion src/Atata/ScopeSearch/Strategies/FindByClassStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ protected override string Build(ComponentScopeXPathBuilder builder, ComponentSco
string classCondition = GetClassCondition(options);

return builder.
WrapWithIndex(x => x.Descendant.Any[classCondition]).
WrapWithIndex(x => x.OuterXPath.Any[classCondition]).
DescendantOrSelf.ComponentXPath;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public FindByColumnIndexStrategy(int columnIndex)
protected override string Build(ComponentScopeXPathBuilder builder, ComponentScopeLocateOptions options)
{
return builder.
WrapWithIndex(x => x.Descendant._("td").WhereIndex(columnIndex).DescendantOrSelf.ComponentXPath);
WrapWithIndex(x => x.OuterXPath._("td").WhereIndex(columnIndex).DescendantOrSelf.ComponentXPath);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public class FindByContentOrValueStrategy : XPathComponentScopeLocateStrategy
protected override string Build(ComponentScopeXPathBuilder builder, ComponentScopeLocateOptions options)
{
return builder.
WrapWithIndex(x => x.Descendant.ComponentXPath[y => y.TermsConditionOfContent.Or.TermsConditionOf("value")]);
WrapWithIndex(x => x.OuterXPath.ComponentXPath[y => y.TermsConditionOfContent.Or.TermsConditionOf("value")]);
}
}
}
2 changes: 1 addition & 1 deletion src/Atata/ScopeSearch/Strategies/FindByContentStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public class FindByContentStrategy : XPathComponentScopeLocateStrategy
protected override string Build(ComponentScopeXPathBuilder builder, ComponentScopeLocateOptions options)
{
return builder.
WrapWithIndex(x => x.Descendant.ComponentXPath[y => y.TermsConditionOfContent]);
WrapWithIndex(x => x.OuterXPath.ComponentXPath[y => y.TermsConditionOfContent]);
}
}
}
28 changes: 16 additions & 12 deletions src/Atata/ScopeSearch/Strategies/FindByCssStrategy.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using OpenQA.Selenium;

using OpenQA.Selenium.Support.PageObjects;

namespace Atata
{
public class FindByCssStrategy : IComponentScopeLocateStrategy
Expand All @@ -10,17 +11,20 @@ public ComponentScopeLocateResult Find(IWebElement scope, ComponentScopeLocateOp
{
By by = By.CssSelector(string.Join(",", options.Terms));

if (options.Index.HasValue)
{
var elements = scope.GetAll(by.With(searchOptions));
if (elements.Count <= options.Index.Value)
throw ExceptionFactory.CreateForNoSuchElement(by: by, searchContext: scope);
else
return new SequalComponentScopeLocateResult(elements[options.Index.Value], sequalStrategy);
}
else
{
return new SequalComponentScopeLocateResult(by, sequalStrategy);
if (options.OuterXPath != null)
by = new ByChained(By.XPath(options.OuterXPath + "*"), by);

if (options.Index.HasValue)
{
var elements = scope.GetAll(by.With(searchOptions));
if (elements.Count <= options.Index.Value)
throw ExceptionFactory.CreateForNoSuchElement(by: by, searchContext: scope);
else
return new SequalComponentScopeLocateResult(elements[options.Index.Value], sequalStrategy);
}
else
{
return new SequalComponentScopeLocateResult(by, sequalStrategy);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public class FindByDescriptionTermStrategy : XPathComponentScopeLocateStrategy
protected override string Build(ComponentScopeXPathBuilder builder, ComponentScopeLocateOptions options)
{
return builder.
WrapWithIndex(x => x.Descendant._("dl/dt")[y => y.TermsConditionOfContent]).
WrapWithIndex(x => x.OuterXPath._("dl/dt")[y => y.TermsConditionOfContent]).
FollowingSibling._("dd").DescendantOrSelf.ComponentXPath;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Atata/ScopeSearch/Strategies/FindByFieldSetStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public class FindByFieldSetStrategy : XPathComponentScopeLocateStrategy
protected override string Build(ComponentScopeXPathBuilder builder, ComponentScopeLocateOptions options)
{
return builder.
WrapWithIndex(x => x.Descendant._("fieldset")[y => y._("legend")[z => z.TermsConditionOfContent]]).
WrapWithIndex(x => x.OuterXPath._("fieldset")[y => y._("legend")[z => z.TermsConditionOfContent]]).
DescendantOrSelf.ComponentXPath;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Atata/ScopeSearch/Strategies/FindByIdStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public class FindByIdStrategy : XPathComponentScopeLocateStrategy
protected override string Build(ComponentScopeXPathBuilder builder, ComponentScopeLocateOptions options)
{
return builder.
WrapWithIndex(x => x.Descendant.Any[y => y.TermsConditionOf("id")]).
WrapWithIndex(x => x.OuterXPath.Any[y => y.TermsConditionOf("id")]).
DescendantOrSelf.ComponentXPath;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Atata/ScopeSearch/Strategies/FindByIndexStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public class FindByIndexStrategy : XPathComponentScopeLocateStrategy
{
protected override string Build(ComponentScopeXPathBuilder builder, ComponentScopeLocateOptions options)
{
return builder.WrapWithIndex(options.Index.Value, x => x.Descendant.ComponentXPath);
return builder.WrapWithIndex(options.Index.Value, x => x.OuterXPath.ComponentXPath);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ protected override string Build(ComponentScopeXPathBuilder builder, ComponentSco
? options.Terms.Select(x => $"({x})").ToArray()
: options.Terms;

return builder.WrapWithIndex(x => x.Descendant.ComponentXPath[y => y.JoinOr(conditions)]);
return builder.WrapWithIndex(x => x.OuterXPath.ComponentXPath[y => y.JoinOr(conditions)]);
}
}
}
2 changes: 1 addition & 1 deletion src/Atata/ScopeSearch/Strategies/FindByLabelStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class FindByLabelStrategy : IComponentScopeLocateStrategy
public ComponentScopeLocateResult Find(IWebElement scope, ComponentScopeLocateOptions options, SearchOptions searchOptions)
{
string labelXPath = new ComponentScopeXPathBuilder(options).
WrapWithIndex(x => x.Descendant._("label")[y => y.TermsConditionOfContent]);
WrapWithIndex(x => x.OuterXPath._("label")[y => y.TermsConditionOfContent]);

IWebElement label = scope.Get(By.XPath(labelXPath).With(searchOptions).Label(options.GetTermsAsString()));

Expand Down
2 changes: 1 addition & 1 deletion src/Atata/ScopeSearch/Strategies/FindByNameStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public class FindByNameStrategy : XPathComponentScopeLocateStrategy
protected override string Build(ComponentScopeXPathBuilder builder, ComponentScopeLocateOptions options)
{
return builder.
WrapWithIndex(x => x.Descendant.Any[y => y.TermsConditionOf("name")]).
WrapWithIndex(x => x.OuterXPath.Any[y => y.TermsConditionOf("name")]).
DescendantOrSelf.ComponentXPath;
}
}
Expand Down
19 changes: 11 additions & 8 deletions src/Atata/ScopeSearch/Strategies/FindByXPathStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,25 @@ protected override string Build(ComponentScopeXPathBuilder builder, ComponentSco
}

string conditionalXPath = conditionalXPathSelectors.Any()
? builder.WrapWithIndex(x => x.Descendant.ComponentXPath[y => y.JoinOr(conditionalXPathSelectors)])
? builder.WrapWithIndex(x => x.OuterXPath.ComponentXPath[y => y.JoinOr(conditionalXPathSelectors)])
: null;

string[] outerXPathSelectors = builder.Options.Terms.
string[] completeXPathSelectors = builder.Options.Terms.
Except(conditionalXPathTerms).
Select(x => acceptableXPathPrefixValues.Any(prefix => x.StartsWith(prefix)) ? x : ".//" + x).
Select(x =>
acceptableXPathPrefixValues.Any(prefix => x.StartsWith(prefix))
? (options.OuterXPath?.Append(x) ?? x)
: ((options.OuterXPath ?? ".//") + x)).
ToArray();

string outerXPath = outerXPathSelectors.Any()
? builder.WrapWithIndex(x => x._($"({string.Join(" | ", outerXPathSelectors)})")).DescendantOrSelf.ComponentXPath
string completeXPath = completeXPathSelectors.Any()
? builder.WrapWithIndex(x => x._($"({string.Join(" | ", completeXPathSelectors)})")).DescendantOrSelf.ComponentXPath
: null;

if (conditionalXPath != null && outerXPath != null)
return $"(({outerXPath}) | ({conditionalXPath}))";
if (conditionalXPath != null && completeXPath != null)
return $"(({completeXPath}) | ({conditionalXPath}))";
else
return outerXPath ?? conditionalXPath;
return completeXPath ?? conditionalXPath;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public class FindFirstDescendantStrategy : XPathComponentScopeLocateStrategy
{
protected override string Build(ComponentScopeXPathBuilder builder, ComponentScopeLocateOptions options)
{
return builder.Descendant.ComponentXPath;
return builder.OuterXPath.ComponentXPath;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public class FindLastDescendantStrategy : XPathComponentScopeLocateStrategy
{
protected override string Build(ComponentScopeXPathBuilder builder, ComponentScopeLocateOptions options)
{
return builder.Wrap(x => x.Descendant.ComponentXPath)["last()"];
return builder.Wrap(x => x.OuterXPath.ComponentXPath)["last()"];
}
}
}

0 comments on commit fe7f8eb

Please sign in to comment.