Skip to content

Commit

Permalink
Merge pull request #33 from grokys/fixes/sendkeys
Browse files Browse the repository at this point in the history
Implement Element Send Keys according to spec
  • Loading branch information
aristotelos authored May 8, 2024
2 parents 8ca04cc + 7188f38 commit 18a6075
Show file tree
Hide file tree
Showing 10 changed files with 1,154 additions and 300 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,14 @@ There is an interpretation to use the WebDriver specification to drive native au
| tag name | Control type in Windows |
| attribute | [UI automation element property](https://learn.microsoft.com/en-us/windows/win32/winauto/uiauto-automation-element-propids) in Windows |
### Deviations from W3C WebDriver Spec

https://www.w3.org/TR/webdriver2/#element-send-keys says:
> Set the text insertion caret using set selection range using current text length for both the start and end parameters.

This is impossible using UIA, as there is no API to set the caret position: text instead gets inserted at the beginning of a text box. This is also WinAppDriver's behavior.

### Element Attributes

Attributes are mapped to UI automation element properties. Attributes without a period (`.`) are mapped to [Automation Element Properties](https://learn.microsoft.com/en-us/windows/win32/winauto/uiauto-automation-element-propids). For example to read the `UIA_ClassNamePropertyId` using Selenium WebDriver:
Expand Down
50 changes: 47 additions & 3 deletions src/FlaUI.WebDriver.UITests/ElementTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using FlaUI.WebDriver.UITests.TestUtil;
using System.Threading;
using FlaUI.WebDriver.UITests.TestUtil;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Remote;
using System;

namespace FlaUI.WebDriver.UITests
{
Expand Down Expand Up @@ -134,7 +134,51 @@ public void SendKeys_Default_IsSupported()

element.SendKeys("Hello World!");

Assert.That(element.Text, Is.EqualTo("Hello World!"));
Assert.That(element.Text, Is.EqualTo("Hello World!Test TextBox"));
}

[Test]
public void SendKeys_ShiftedCharacter_ShiftIsReleased()
{
var driverOptions = FlaUIDriverOptions.TestApp();
using var driver = new RemoteWebDriver(WebDriverFixture.WebDriverUrl, driverOptions);
var element = driver.FindElement(ExtendedBy.AccessibilityId("TextBox"));

element.SendKeys("!");
element.SendKeys("1");

Assert.That(element.Text, Is.EqualTo("!1Test TextBox"));
}

[Test]
public void SendKeys_DownArrow_IsSupported()
{
var driverOptions = FlaUIDriverOptions.TestApp();
using var driver = new RemoteWebDriver(WebDriverFixture.WebDriverUrl, driverOptions);
var element = driver.FindElement(ExtendedBy.AccessibilityId("NonEditableCombo"));

element.SendKeys(Keys.Down);

Assert.That(element.Text, Is.EqualTo("Item 2"));
}

[Test, Ignore("Alt key combinations currently fail due to https://github.com/FlaUI/FlaUI/issues/320")]
public void SendKeys_AltDownArrowEscape_IsSupported()
{
var driverOptions = FlaUIDriverOptions.TestApp();
using var driver = new RemoteWebDriver(WebDriverFixture.WebDriverUrl, driverOptions);
var element = driver.FindElement(ExtendedBy.AccessibilityId("NonEditableCombo"));
var expandCollapseState = element.GetDomAttribute("ExpandCollapse.ExpandCollapseState");

Assert.That(expandCollapseState, Is.EqualTo("Collapsed"));

element.SendKeys(Keys.Alt + Keys.Down);

Assert.That(expandCollapseState, Is.EqualTo("Expanded"));

element.SendKeys(Keys.Escape);

Assert.That(expandCollapseState, Is.EqualTo("Collapsed"));
}

[Test]
Expand Down
3 changes: 3 additions & 0 deletions src/FlaUI.WebDriver/Action.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class Action
{
public Action(ActionSequence actionSequence, ActionItem actionItem)
{
Id = actionSequence.Id;
Type = actionSequence.Type;
SubType = actionItem.Type;
Button = actionItem.Button;
Expand All @@ -29,6 +30,7 @@ public Action(ActionSequence actionSequence, ActionItem actionItem)

public Action(Action action)
{
Id = action.Id;
Type = action.Type;
SubType = action.SubType;
Button = action.Button;
Expand All @@ -50,6 +52,7 @@ public Action(Action action)
Value = action.Value;
}

public string Id { get; set; }
public string Type { get; set; }
public string SubType { get; set; }
public int? Button { get; set; }
Expand Down
Loading

0 comments on commit 18a6075

Please sign in to comment.