Skip to content

Commit

Permalink
#160 Property exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Sekulski committed Mar 1, 2024
1 parent b7c577a commit cf5164d
Show file tree
Hide file tree
Showing 13 changed files with 232 additions and 8 deletions.
10 changes: 8 additions & 2 deletions TestApplications/TestApplications.sln
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30011.22
# Visual Studio Version 17
VisualStudioVersion = 17.9.34607.119
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WpfApplication", "src\WpfApplication\WpfApplication.csproj", "{C8276299-FA43-409B-A969-EF030AB56224}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Notifier", "src\Notifier\Notifier.csproj", "{483A4AB8-F697-46CB-B82E-6CAB3D525D87}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AvaloniaApplication", "src\AvaloniaApplication\AvaloniaApplication.csproj", "{6A3A5D94-EDCD-4330-8BB4-C9A472ACD874}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,6 +23,10 @@ Global
{483A4AB8-F697-46CB-B82E-6CAB3D525D87}.Debug|Any CPU.Build.0 = Debug|Any CPU
{483A4AB8-F697-46CB-B82E-6CAB3D525D87}.Release|Any CPU.ActiveCfg = Release|Any CPU
{483A4AB8-F697-46CB-B82E-6CAB3D525D87}.Release|Any CPU.Build.0 = Release|Any CPU
{6A3A5D94-EDCD-4330-8BB4-C9A472ACD874}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6A3A5D94-EDCD-4330-8BB4-C9A472ACD874}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6A3A5D94-EDCD-4330-8BB4-C9A472ACD874}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6A3A5D94-EDCD-4330-8BB4-C9A472ACD874}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
10 changes: 10 additions & 0 deletions TestApplications/src/AvaloniaApplication/App.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="AvaloniaApplication.App"
RequestedThemeVariant="Default">
<!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. -->

<Application.Styles>
<FluentTheme />
</Application.Styles>
</Application>
34 changes: 34 additions & 0 deletions TestApplications/src/AvaloniaApplication/App.axaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Data.Core;
using Avalonia.Data.Core.Plugins;
using Avalonia.Markup.Xaml;
using AvaloniaApplication.ViewModels;
using AvaloniaApplication.Views;

namespace AvaloniaApplication
{
public partial class App : Application
{
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
}

public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
// Line below is needed to remove Avalonia data validation.
// Without this line you will get duplicate validations from both Avalonia and CT
BindingPlugins.DataValidators.RemoveAt(0);
desktop.MainWindow = new MainWindow
{
DataContext = new MainWindowViewModel(),
};
}

base.OnFrameworkInitializationCompleted();
}
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
<ApplicationManifest>app.manifest</ApplicationManifest>
<AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>
</PropertyGroup>

<ItemGroup>
<Folder Include="Models\" />
<AvaloniaResource Include="Assets\**" />
</ItemGroup>


<ItemGroup>
<PackageReference Include="Avalonia" Version="11.0.9" />
<PackageReference Include="Avalonia.Desktop" Version="11.0.9" />
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.0.9" />
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.0.9" />
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.0.9" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
</ItemGroup>
</Project>
22 changes: 22 additions & 0 deletions TestApplications/src/AvaloniaApplication/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Avalonia;
using System;

namespace AvaloniaApplication
{
internal class Program
{
// Initialization code. Don't use any Avalonia, third-party APIs or any
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
// yet and stuff might break.
[STAThread]
public static void Main(string[] args) => BuildAvaloniaApp()
.StartWithClassicDesktopLifetime(args);

// Avalonia configuration, don't remove; also used by visual designer.
public static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>()
.UsePlatformDetect()
.WithInterFont()
.LogToTrace();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project>
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<PublishDir>bin\Release\net8.0\publish\win-x86\</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
<_TargetId>Folder</_TargetId>
<TargetFramework>net8.0</TargetFramework>
<SelfContained>false</SelfContained>
<RuntimeIdentifier>win-x86</RuntimeIdentifier>
<PublishSingleFile>true</PublishSingleFile>
<PublishReadyToRun>false</PublishReadyToRun>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace AvaloniaApplication.ViewModels
{
public partial class MainWindowViewModel : ViewModelBase
{
public string Greeting => "Welcome to Avalonia!";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using CommunityToolkit.Mvvm.ComponentModel;

namespace AvaloniaApplication.ViewModels
{
public class ViewModelBase : ObservableObject
{
}
}
23 changes: 23 additions & 0 deletions TestApplications/src/AvaloniaApplication/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:AvaloniaApplication.ViewModels"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="AvaloniaApplication.Views.MainWindow"
x:DataType="vm:MainWindowViewModel"
Icon="/Assets/avalonia-logo.ico"
Title="AvaloniaApplication">

<Design.DataContext>
<!-- This only sets the DataContext for the previewer in an IDE,
to set the actual DataContext for runtime, set the DataContext property in code (look at App.axaml.cs) -->
<vm:MainWindowViewModel/>
</Design.DataContext>

<TextBlock Text="{Binding Greeting}"
AutomationProperties.AutomationId="Greetings"
HorizontalAlignment="Center"
VerticalAlignment="Center"/>

</Window>
12 changes: 12 additions & 0 deletions TestApplications/src/AvaloniaApplication/Views/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Avalonia.Controls;

namespace AvaloniaApplication.Views
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
18 changes: 18 additions & 0 deletions TestApplications/src/AvaloniaApplication/app.manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<!-- This manifest is used on Windows only.
Don't remove it as it might cause problems with window transparency and embeded controls.
For more details visit https://learn.microsoft.com/en-us/windows/win32/sbscs/application-manifests -->
<assemblyIdentity version="1.0.0.0" name="AvaloniaApplication.Desktop"/>

<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- A list of the Windows versions that this application has been tested on
and is designed to work with. Uncomment the appropriate elements
and Windows will automatically select the most compatible environment. -->

<!-- Windows 10 -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
</application>
</compatibility>
</assembly>
52 changes: 46 additions & 6 deletions src/FlaUILibrary/flaui/module/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from enum import Enum
from typing import Optional, Any
from System import Exception as CSharpException # pylint: disable=import-error
from FlaUI.Core import Debug as FlaUIDebug # pylint: disable=import-error
from FlaUI.Core import Debug as FlaUIDebug # pylint: disable=import-error
from FlaUI.Core.Exceptions import PropertyNotSupportedException # pylint: disable=import-error
from FlaUILibrary.flaui.util.converter import Converter
from FlaUILibrary.flaui.exception import FlaUiError
from FlaUILibrary.flaui.interface import (ModuleInterface, ValueContainer)
Expand Down Expand Up @@ -61,7 +62,7 @@ def __init__(self, automation: Any, timeout: int = 1000):
self._timeout = timeout

@staticmethod
def create_value_container(name=None, xpath=None, retries=None, use_exception=None, msg=None):
def create_value_container(name=None, xpath=None, retries=None, use_exception=None, msg=None):
"""
Helper to create container object.
Expand Down Expand Up @@ -98,7 +99,7 @@ def execute_action(self, action: Action, values: Container):
lambda: self._get_name_from_element(values["xpath"]),
self.Action.GET_ELEMENT_RECTANGLE_BOUNDING:
lambda: self._get_rectangle_bounding_from_element(
values["xpath"]),
values["xpath"]),
self.Action.IS_ELEMENT_ENABLED:
lambda: self._get_element(values["xpath"]).IsEnabled,
self.Action.NAME_SHOULD_BE:
Expand Down Expand Up @@ -229,9 +230,9 @@ def _find_all_elements(self, xpath: str):
elements = self._get_all_elements_by_xpath(xpath)
for element in elements:
values.append(AutomationElement(
element.AutomationId,
element.Name,
element.ClassName,
self._try_get_automation_id_property(element),
self._try_get_name_property(element),
self._try_get_classname_property(element),
FlaUIDebug.GetXPathToElement(element)
))

Expand Down Expand Up @@ -456,3 +457,42 @@ def _set_timeout(self, timeout: int):
timeout (Number): Timeout value in seconds
"""
self._timeout = timeout

@staticmethod
def _try_get_automation_id_property(element):
"""
Try to get automation id property from element. Return empty string if failed.
Args:
element (UIA): AutomationElement.
"""
try:
return element.AutomationId
except PropertyNotSupportedException:
return ""

@staticmethod
def _try_get_name_property(element):
"""
Try to get name property from element. Return empty string if failed.
Args:
element (UIA): AutomationElement.
"""
try:
return element.Name
except PropertyNotSupportedException:
return ""

@staticmethod
def _try_get_classname_property(element):
"""
Try to get class name property from element. Return empty string if failed.
Args:
element (UIA): AutomationElement.
"""
try:
return element.ClassName
except PropertyNotSupportedException:
return ""

0 comments on commit cf5164d

Please sign in to comment.