-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a unit test project and add a few small unit tests related to #99, which prove that this is not an issue in FlaUI.WebDriver.
- Loading branch information
1 parent
8c02962
commit e5d543b
Showing
7 changed files
with
180 additions
and
88 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
src/FlaUI.WebDriver.UnitTests/FlaUI.WebDriver.UnitTests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0-windows</TargetFramework> | ||
|
||
<IsPackable>false</IsPackable> | ||
<LangVersion>preview</LangVersion> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" /> | ||
<PackageReference Include="NUnit" Version="4.2.2" /> | ||
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" /> | ||
<PackageReference Include="coverlet.collector" Version="6.0.2"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\FlaUI.WebDriver\FlaUI.WebDriver.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
22 changes: 22 additions & 0 deletions
22
src/FlaUI.WebDriver.UnitTests/Services/ConditionParserTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using FlaUI.WebDriver.Services; | ||
using NUnit.Framework; | ||
|
||
namespace FlaUI.WebDriver.UnitTests.Services | ||
{ | ||
public class ConditionParserTests | ||
{ | ||
[TestCase("[name=\"2\"]")] | ||
[TestCase("*[name=\"2\"]")] | ||
[TestCase("*[name = \"2\"]")] | ||
public void ParseCondition_ByCssAttributeName_ReturnsCondition(string selector) | ||
{ | ||
var parser = new ConditionParser(); | ||
var uia3 = new UIA3.UIA3Automation(); | ||
|
||
var result = parser.ParseCondition(uia3.ConditionFactory, "css selector", selector); | ||
|
||
Assert.That(result.Property, Is.EqualTo(uia3.PropertyLibrary.Element.Name)); | ||
Assert.That(result.Value, Is.EqualTo("2")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
using FlaUI.Core.Conditions; | ||
using FlaUI.Core.Definitions; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace FlaUI.WebDriver.Services | ||
{ | ||
public class ConditionParser : IConditionParser | ||
{ | ||
/// <summary> | ||
/// Based on https://www.w3.org/TR/CSS21/grammar.html (see also https://www.w3.org/TR/CSS22/grammar.html) | ||
/// Limitations: | ||
/// - Unicode escape characters are not supported. | ||
/// - Multiple selectors are not supported. | ||
/// </summary> | ||
private static Regex SimpleCssIdSelectorRegex = new Regex(@"^#(?<name>(?<nmchar>[_a-z0-9-]|[\240-\377]|(?<escape>\\[^\r\n\f0-9a-f]))+)$", RegexOptions.Compiled | RegexOptions.IgnoreCase); | ||
|
||
/// <summary> | ||
/// Based on https://www.w3.org/TR/CSS21/grammar.html (see also https://www.w3.org/TR/CSS22/grammar.html) | ||
/// Limitations: | ||
/// - Unicode escape characters are not supported. | ||
/// - Multiple selectors are not supported. | ||
/// </summary> | ||
private static Regex SimpleCssClassSelectorRegex = new Regex(@"^\.(?<ident>-?(?<nmstart>[_a-z]|[\240-\377])(?<nmchar>[_a-z0-9-]|[\240-\377]|(?<escape>\\[^\r\n\f0-9a-f]))*)$", RegexOptions.Compiled | RegexOptions.IgnoreCase); | ||
|
||
/// <summary> | ||
/// Based on https://www.w3.org/TR/CSS21/grammar.html (see also https://www.w3.org/TR/CSS22/grammar.html) | ||
/// Limitations: | ||
/// - Unicode escape characters or escape characters in the attribute name are not supported. | ||
/// - Multiple selectors are not supported. | ||
/// - Attribute presence selector (e.g. `[name]`) not supported. | ||
/// - Attribute equals attribute (e.g. `[name=value]`) not supported. | ||
/// - ~= or |= not supported. | ||
/// </summary> | ||
private static Regex SimpleCssAttributeSelectorRegex = new Regex(@"^\*?\[\s*(?<ident>-?(?<nmstart>[_a-z]|[\240-\377])(?<nmchar>[_a-z0-9-]|[\240-\377])*)\s*=\s*(?<string>(?<string1>""(?<string1value>([^\n\r\f\\""]|(?<escape>\\[^\r\n\f0-9a-f]))*)"")|(?<string2>'(?<string2value>([^\n\r\f\\']|(?<escape>\\[^\r\n\f0-9a-f]))*)'))\s*\]$", RegexOptions.Compiled | RegexOptions.IgnoreCase); | ||
|
||
/// <summary> | ||
/// Based on https://www.w3.org/TR/CSS21/grammar.html (see also https://www.w3.org/TR/CSS22/grammar.html) | ||
/// Limitations: | ||
/// - Unicode escape characters are not supported. | ||
/// </summary> | ||
private static Regex SimpleCssEscapeCharacterRegex = new Regex(@"\\[^\r\n\f0-9a-f]", RegexOptions.Compiled | RegexOptions.IgnoreCase); | ||
|
||
public PropertyCondition ParseCondition(ConditionFactory conditionFactory, string @using, string value) | ||
{ | ||
switch (@using) | ||
{ | ||
case "accessibility id": | ||
return conditionFactory.ByAutomationId(value); | ||
case "name": | ||
return conditionFactory.ByName(value); | ||
case "class name": | ||
return conditionFactory.ByClassName(value); | ||
case "link text": | ||
return conditionFactory.ByText(value); | ||
case "partial link text": | ||
return conditionFactory.ByText(value, PropertyConditionFlags.MatchSubstring); | ||
case "tag name": | ||
return conditionFactory.ByControlType(Enum.Parse<ControlType>(value)); | ||
case "css selector": | ||
var cssIdSelectorMatch = SimpleCssIdSelectorRegex.Match(value); | ||
if (cssIdSelectorMatch.Success) | ||
{ | ||
return conditionFactory.ByAutomationId(ReplaceCssEscapedCharacters(value.Substring(1))); | ||
} | ||
var cssClassSelectorMatch = SimpleCssClassSelectorRegex.Match(value); | ||
if (cssClassSelectorMatch.Success) | ||
{ | ||
return conditionFactory.ByClassName(ReplaceCssEscapedCharacters(value.Substring(1))); | ||
} | ||
var cssAttributeSelectorMatch = SimpleCssAttributeSelectorRegex.Match(value); | ||
if (cssAttributeSelectorMatch.Success) | ||
{ | ||
var attributeValue = ReplaceCssEscapedCharacters(cssAttributeSelectorMatch.Groups["string1value"].Success ? | ||
cssAttributeSelectorMatch.Groups["string1value"].Value : | ||
cssAttributeSelectorMatch.Groups["string2value"].Value); | ||
if (cssAttributeSelectorMatch.Groups["ident"].Value == "name") | ||
{ | ||
return conditionFactory.ByName(attributeValue); | ||
} | ||
} | ||
throw WebDriverResponseException.UnsupportedOperation($"Selector strategy 'css selector' with value '{value}' is not supported"); | ||
default: | ||
throw WebDriverResponseException.UnsupportedOperation($"Selector strategy '{@using}' is not supported"); | ||
} | ||
} | ||
|
||
private static string ReplaceCssEscapedCharacters(string value) | ||
{ | ||
return SimpleCssEscapeCharacterRegex.Replace(value, match => match.Value.Substring(1)); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using FlaUI.Core.Conditions; | ||
|
||
namespace FlaUI.WebDriver.Services | ||
{ | ||
public interface IConditionParser | ||
{ | ||
PropertyCondition ParseCondition(ConditionFactory conditionFactory, string @using, string value); | ||
} | ||
} |