Skip to content

Commit

Permalink
Merge pull request #30 from grokys/fixes/get-element-text
Browse files Browse the repository at this point in the history
Make Get Element Text match WinAppDriver more closely.
  • Loading branch information
aristotelos authored May 2, 2024
2 parents d5fc010 + 41dd3ea commit 88b51ae
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 20 deletions.
54 changes: 42 additions & 12 deletions src/FlaUI.WebDriver.UITests/ElementTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using FlaUI.WebDriver.UITests.TestUtil;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Remote;
using System;

Expand All @@ -9,40 +10,69 @@ namespace FlaUI.WebDriver.UITests
[TestFixture]
public class ElementTests
{
[Test]
public void GetText_Label_ReturnsRenderedText()
[TestCase("TextBox", "Test TextBox")]
[TestCase("PasswordBox", "●●●●●●●●●●")]
[TestCase("EditableCombo", "Item 1")]
[TestCase("NonEditableCombo", "Item 1")]
[TestCase("ListBox", "ListBox Item #1")]
[TestCase("SimpleCheckBox", "Test Checkbox")]
[TestCase("ThreeStateCheckBox", "3-Way Test Checkbox")]
[TestCase("RadioButton1", "RadioButton1")]
[TestCase("RadioButton2", "RadioButton2")]
[TestCase("Slider", "5")]
[TestCase("InvokableButton", "Invoke me!")]
[TestCase("PopupToggleButton1", "Popup Toggle 1")]
[TestCase("Label", "Menu Item Checked")]
public void GetText_Returns_Correct_Text(string elementAccessibilityId, string expectedValue)
{
var driverOptions = FlaUIDriverOptions.TestApp();
using var driver = new RemoteWebDriver(WebDriverFixture.WebDriverUrl, driverOptions);
var element = driver.FindElement(ExtendedBy.AccessibilityId("Label"));

var element = driver.FindElement(ExtendedBy.AccessibilityId(elementAccessibilityId));
var text = element.Text;

Assert.That(text, Is.EqualTo("Menu Item Checked"));
Assert.That(text, Is.EqualTo(expectedValue));
}

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

new Actions(driver)
.MoveToElement(element)
.Click()
.KeyDown(Keys.Control)
.KeyDown("a")
.KeyUp("a")
.KeyUp(Keys.Control)
.Perform();

var text = element.Text;

Assert.That(text, Is.EqualTo("Test TextBox"));

// Seems that the order in which the selected items are returned is not guaranteed.
Assert.That(text, Is.EqualTo("ListBox Item #1, ListBox Item #2").Or.EqualTo("ListBox Item #2, ListBox Item #1"));
}

[Test]
public void GetText_Button_ReturnsButtonText()
public void GetText_Returns_Empty_String_For_No_Selection()
{
var driverOptions = FlaUIDriverOptions.TestApp();
using var driver = new RemoteWebDriver(WebDriverFixture.WebDriverUrl, driverOptions);
var element = driver.FindElement(ExtendedBy.AccessibilityId("InvokableButton"));
var element = driver.FindElement(ExtendedBy.AccessibilityId("ListBox"));
var item = driver.FindElement(ExtendedBy.Name("ListBox Item #1"));

new Actions(driver)
.MoveToElement(item)
.KeyDown(Keys.Control)
.Click()
.KeyUp(Keys.Control)
.Perform();

var text = element.Text;

Assert.That(text, Is.EqualTo("Invoke me!"));
Assert.That(text, Is.Empty);
}

[Test]
Expand Down
34 changes: 29 additions & 5 deletions src/FlaUI.WebDriver/Controllers/ElementController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using FlaUI.Core.AutomationElements;
using FlaUI.WebDriver.Models;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using System.Text;

namespace FlaUI.WebDriver.Controllers
Expand Down Expand Up @@ -85,18 +86,41 @@ public async Task<ActionResult> GetElementText([FromRoute] string sessionId, [Fr
{
var session = GetActiveSession(sessionId);
var element = GetElement(session, elementId);
var text = GetElementText(element);

string text;
return await Task.FromResult(WebDriverResult.Success(text));
}

private static string GetElementText(AutomationElement element)
{
// https://www.w3.org/TR/webdriver2/#get-element-text says about this:
//
// > Let rendered text be the result of performing implementation-specific steps whose result is exactly
// > the same as the result of a Function.[[Call]](null, element) with bot.dom.getVisibleText as the this value.
//
// Because it's implementation-defined, this method tries to follow WinAppDriver's implementation as closely as
// possible.
if (element.Patterns.Text.IsSupported)
{
text = element.Patterns.Text.Pattern.DocumentRange.GetText(int.MaxValue);
return element.Patterns.Text.Pattern.DocumentRange.GetText(int.MaxValue);
}
else if (element.Patterns.Value.IsSupported)
{
return element.Patterns.Value.Pattern.Value.ToString();
}
else if (element.Patterns.RangeValue.IsSupported)
{
return element.Patterns.RangeValue.Pattern.Value.ToString();
}
else if (element.Patterns.Selection.IsSupported)
{
var selected = element.Patterns.Selection.Pattern.Selection.Value;
return string.Join(", ", selected.Select(GetElementText));
}
else
{
text = GetRenderedText(element);
return GetRenderedText(element);
}

return await Task.FromResult(WebDriverResult.Success(text));
}

private static string GetRenderedText(AutomationElement element)
Expand Down
6 changes: 3 additions & 3 deletions src/TestApplications/WpfApplication/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@
<Label Content="Test Label" Foreground="Blue" />
<TextBox AutomationProperties.AutomationId="TextBox" Text="Test TextBox" Foreground="Green" x:Name="textBox"/>
<PasswordBox AutomationProperties.AutomationId="PasswordBox" Password="MyPassword" x:Name="passwordBox" />
<ComboBox Width="120" IsEditable="True" AutomationProperties.AutomationId="EditableCombo" x:Name="editableCombo">
<ComboBox Width="120" IsEditable="True" SelectedIndex="0" AutomationProperties.AutomationId="EditableCombo" x:Name="editableCombo">
<TextBlock Text="Item 1" AutomationProperties.AutomationId="EditableComboItem1" />
<TextBlock Text="Item 2" AutomationProperties.AutomationId="EditableComboItem2" />
<TextBlock Text="Item 3" AutomationProperties.AutomationId="EditableComboItem3" />
</ComboBox>
<ComboBox Width="120" AutomationProperties.AutomationId="NonEditableCombo" IsEditable="False"
<ComboBox Width="120" SelectedIndex="0" AutomationProperties.AutomationId="NonEditableCombo" IsEditable="False"
SelectionChanged="Selector_OnSelectionChanged" x:Name="nonEditableCombo">
<TextBlock Text="Item 1" AutomationProperties.AutomationId="EditableComboItem1" />
<TextBlock Text="Item 2" AutomationProperties.AutomationId="EditableComboItem2" />
<TextBlock Text="Item 3" AutomationProperties.AutomationId="EditableComboItem3" />
<TextBlock Text="Item 4" AutomationProperties.AutomationId="EditableComboItem4" />
</ComboBox>
<ListBox AutomationProperties.AutomationId="ListBox" x:Name="listBox">
<ListBox SelectedIndex="0" SelectionMode="Extended" AutomationProperties.AutomationId="ListBox" x:Name="listBox">
<ListBoxItem>ListBox Item #1</ListBoxItem>
<ListBoxItem>ListBox Item #2</ListBoxItem>
</ListBox>
Expand Down

0 comments on commit 88b51ae

Please sign in to comment.