Skip to content

Commit

Permalink
Merge pull request #37 from grokys/fixes/dpi-scaling
Browse files Browse the repository at this point in the history
Make WebDriver DPI scaling-aware.
  • Loading branch information
aristotelos authored May 8, 2024
2 parents 4857093 + c0f876f commit 19553d2
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 9 deletions.
9 changes: 5 additions & 4 deletions src/FlaUI.WebDriver.UITests/ElementTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,15 @@ public void GetElementRect_Default_IsSupported()
using var driver = new RemoteWebDriver(WebDriverFixture.WebDriverUrl, driverOptions);
var element = driver.FindElement(ExtendedBy.AccessibilityId("EditableCombo"));

var scaling = TestApplication.GetScaling(driver);
var location = element.Location;
var size = element.Size;

var windowLocation = driver.Manage().Window.Position;
Assert.That(location.X, Is.InRange(windowLocation.X + 253, windowLocation.X + 257));
Assert.That(location.Y, Is.InRange(windowLocation.Y + 132, windowLocation.Y + 136));
Assert.That(size.Width, Is.EqualTo(120));
Assert.That(size.Height, Is.EqualTo(22));
Assert.That(location.X, Is.InRange(windowLocation.X + (253 * scaling), windowLocation.X + (257 * scaling)));
Assert.That(location.Y, Is.InRange(windowLocation.Y + (132 * scaling), windowLocation.Y + (136 * scaling)));
Assert.That(size.Width, Is.EqualTo(120 * scaling));
Assert.That(size.Height, Is.EqualTo(22 * scaling));
}

[TestCase("TextBox")]
Expand Down
6 changes: 6 additions & 0 deletions src/FlaUI.WebDriver.UITests/TestUtil/TestApplication.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.IO;
using System.Linq;
using OpenQA.Selenium.Remote;

namespace FlaUI.WebDriver.UITests.TestUtil
{
Expand All @@ -9,6 +10,11 @@ public static class TestApplication
private static readonly string s_currentDirectory = Directory.GetCurrentDirectory();
private static readonly string s_solutionDirectory = FindSolutionDirectory(s_currentDirectory);

public static double GetScaling(RemoteWebDriver driver)
{
return double.Parse(driver.FindElement(ExtendedBy.AccessibilityId("DpiScaling")).Text.ToString());
}

private static string FindSolutionDirectory(string currentDirectory)
{
while (!Directory.GetFiles(currentDirectory, "*.sln").Any())
Expand Down
5 changes: 3 additions & 2 deletions src/FlaUI.WebDriver.UITests/WindowTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ public void GetWindowRect_Default_IsSupported()
var driverOptions = FlaUIDriverOptions.TestApp();
using var driver = new RemoteWebDriver(WebDriverFixture.WebDriverUrl, driverOptions);

var scaling = TestApplication.GetScaling(driver);
var position = driver.Manage().Window.Position;
var size = driver.Manage().Window.Size;

Assert.That(position.X, Is.GreaterThanOrEqualTo(0));
Assert.That(position.Y, Is.GreaterThanOrEqualTo(0));
Assert.That(size.Width, Is.InRange(629, 630));
Assert.That(size.Height, Is.InRange(515, 516));
Assert.That(size.Width, Is.InRange(629 * scaling, 630 * scaling));
Assert.That(size.Height, Is.InRange(515 * scaling, 516 * scaling));
}

[Test]
Expand Down
1 change: 1 addition & 0 deletions src/FlaUI.WebDriver/FlaUI.WebDriver.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<RuntimeIdentifiers>win-x64</RuntimeIdentifiers>
<PublishSingleFile>true</PublishSingleFile>
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
<ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup>

<ItemGroup>
Expand Down
9 changes: 9 additions & 0 deletions src/FlaUI.WebDriver/app.manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
<asmv3:application>
<asmv3:windowsSettings>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2</dpiAwareness>
</asmv3:windowsSettings>
</asmv3:application>
</assembly>
4 changes: 3 additions & 1 deletion src/TestApplications/WpfApplication/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
</MenuItem>
</Menu>
<StatusBar DockPanel.Dock="Bottom">
<StatusBarItem />
<StatusBarItem>
<TextBlock x:Name="DpiScaling"/>
</StatusBarItem>
</StatusBar>
<TabControl>
<TabItem Header="Simple Controls">
Expand Down
7 changes: 7 additions & 0 deletions src/TestApplications/WpfApplication/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace WpfApplication
{
Expand Down Expand Up @@ -27,6 +28,12 @@ private void Selector_OnSelectionChanged(object sender, SelectionChangedEventArg
}
}

protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
{
DpiScaling.Text = VisualTreeHelper.GetDpi(this).DpiScaleX.ToString();
base.OnRenderSizeChanged(sizeInfo);
}

private void OnShowLabel(object sender, RoutedEventArgs e)
{
MenuItem menuitem = sender as MenuItem;
Expand Down
5 changes: 3 additions & 2 deletions src/TestApplications/WpfApplication/WpfApplication.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>disable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UseWPF>true</UseWPF>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup>

</Project>
9 changes: 9 additions & 0 deletions src/TestApplications/WpfApplication/app.manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
<asmv3:application>
<asmv3:windowsSettings>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2</dpiAwareness>
</asmv3:windowsSettings>
</asmv3:application>
</assembly>

0 comments on commit 19553d2

Please sign in to comment.