Skip to content

Commit

Permalink
Added experimental appium driver support
Browse files Browse the repository at this point in the history
  • Loading branch information
teo-nikolov committed Jun 24, 2024
1 parent 83bb762 commit 738adc2
Show file tree
Hide file tree
Showing 38 changed files with 289 additions and 153 deletions.
2 changes: 1 addition & 1 deletion src/Bellatrix.Desktop/Bellatrix.Desktop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Import Project="..\..\shared\SharedAssemblyInfo.targets" />

<ItemGroup>
<PackageReference Include="Appium.WebDriver" Version="4.4.5" />
<PackageReference Include="Appium.WebDriver" Version="5.0.0" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
Expand Down
44 changes: 41 additions & 3 deletions src/Bellatrix.Desktop/components/ComboBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
// <site>https://bellatrix.solutions/</site>
using System;
using System.Diagnostics;
using System.Linq;
using Bellatrix.Desktop.Contracts;
using Bellatrix.Desktop.Events;
using Bellatrix.Desktop.Services;
using OpenQA.Selenium.Appium;

namespace Bellatrix.Desktop;

Expand All @@ -34,16 +37,51 @@ public virtual void SelectByText(string value)
{
Selecting?.Invoke(this, new ComponentActionEventArgs(this, value));

if (WrappedElement.Text != value)
if (ConfigurationService.GetSection<ExecutionSettings>().ExperimentalDesktopDriver)
{
WrappedElement.SendKeys(value);
var itemToSelect = this.CreateAllByTag<ListItem>("ListItem")
.FirstOrDefault(x => x.CreateByTag<Label>("Text").InnerText == value);

WrappedDriver.ExecuteScript("windows: select", itemToSelect);
}
else
{
if (WrappedElement.Text != value)
{
WrappedElement.SendKeys(value);
}
}

Selected?.Invoke(this, new ComponentActionEventArgs(this, value));
}

public virtual ListItem SelectedItem
{
get
{
if (!ConfigurationService.GetSection<ExecutionSettings>().ExperimentalDesktopDriver)
{
throw new InvalidOperationException("This option is supported only with ExperimentalDesktopDriver enabled");
}

return new ComponentsRepository().CreateComponentThatIsFound<ListItem>(null,
(AppiumElement)WrappedDriver.ExecuteScript("windows: selectedItem", WrappedElement));
}
}

[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public virtual string InnerText => GetInnerText();
public virtual string InnerText
{
get
{
if (ConfigurationService.GetSection<ExecutionSettings>().ExperimentalDesktopDriver)
{
return SelectedItem.CreateByTag<Label>("Text").InnerText;
}

return GetInnerText();
}
}

[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public virtual bool IsDisabled => GetIsDisabled();
Expand Down
18 changes: 13 additions & 5 deletions src/Bellatrix.Desktop/components/Core/Component.DefaultActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Bellatrix.Desktop.Contracts;
using Bellatrix.Desktop.Events;
using Bellatrix.Layout;
using OpenQA.Selenium.Interactions;

namespace Bellatrix.Desktop;

Expand All @@ -40,12 +41,19 @@ internal virtual void Hover(EventHandler<ComponentActionEventArgs> hovering, Eve
{
hovering?.Invoke(this, new ComponentActionEventArgs(this));

WrappedDriver.ExecuteScript("windows: hover", new Dictionary<string, object>
if (ConfigurationService.GetSection<ExecutionSettings>().ExperimentalDesktopDriver)
{
{ "startElementId", WrappedElement.Id },
{ "endElementId", WrappedElement.Id },
{ "durationMs", 0 }
});
new Actions(WrappedDriver).MoveToElement(WrappedElement).Perform();
}
else
{
WrappedDriver.ExecuteScript("windows: hover", new Dictionary<string, object>
{
{ "startElementId", WrappedElement.Id },
{ "endElementId", WrappedElement.Id },
{ "durationMs", 0 }
});
}

hovered?.Invoke(this, new ComponentActionEventArgs(this));
}
Expand Down
32 changes: 20 additions & 12 deletions src/Bellatrix.Desktop/components/Core/Component.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
using Bellatrix.Desktop.Untils;
using Bellatrix.Plugins.Screenshots;
using OpenQA.Selenium;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Windows;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Remote;

namespace Bellatrix.Desktop;
Expand All @@ -36,12 +38,12 @@ public partial class Component
{
private readonly ComponentWaitService _elementWait;
private readonly List<WaitStrategy> _untils;
private WindowsElement _wrappedElement;
private AppiumElement _wrappedElement;

public Component()
{
_elementWait = new ComponentWaitService();
WrappedDriver = ServicesCollection.Current.Resolve<WindowsDriver<WindowsElement>>();
WrappedDriver = ServicesCollection.Current.Resolve<WindowsDriver>();
_untils = new List<WaitStrategy>();
}

Expand All @@ -53,9 +55,9 @@ public Component()
public static event EventHandler<ComponentActionEventArgs> CreatedComponents;
public static event EventHandler<NativeElementActionEventArgs> ReturningWrappedElement;

public WindowsDriver<WindowsElement> WrappedDriver { get; }
public WindowsDriver WrappedDriver { get; }

public WindowsElement WrappedElement
public AppiumElement WrappedElement
{
get
{
Expand All @@ -66,9 +68,9 @@ public WindowsElement WrappedElement
internal set => _wrappedElement = value;
}

public WindowsElement ParentWrappedElement { get; set; }
public AppiumElement ParentWrappedElement { get; set; }

public WindowsElement FoundWrappedElement { get; set; }
public AppiumElement FoundWrappedElement { get; set; }

public int ElementIndex { get; set; }

Expand Down Expand Up @@ -179,11 +181,17 @@ public virtual bool IsVisible

public virtual void ScrollToVisible()
{
if (ConfigurationService.GetSection<ExecutionSettings>().ExperimentalDesktopDriver)
{
WrappedDriver.ExecuteScript("windows: scrollToVisible", WrappedElement);
return;
}

ScrollingToVisible?.Invoke(this, new ComponentActionEventArgs(this));

var touchActions = new RemoteTouchScreen(WrappedDriver);
var touchActions = new Actions(WrappedDriver);
System.Threading.Thread.Sleep(2000);
touchActions.Scroll(WrappedElement.Coordinates, 0, 0);
touchActions.ScrollToElement(WrappedElement);
this.ToBeVisible().ToExists().WaitToBe();
ScrolledToVisible?.Invoke(this, new ComponentActionEventArgs(this));
}
Expand Down Expand Up @@ -212,7 +220,7 @@ public override string ToString()
return sb.ToString();
}

protected WindowsElement GetAndWaitWebDriverElement()
protected AppiumElement GetAndWaitWebDriverElement()
{
if (_wrappedElement == null)
{
Expand All @@ -230,7 +238,7 @@ protected WindowsElement GetAndWaitWebDriverElement()
_elementWait.Wait(this, until);
}

if (until.GetType().Equals(typeof(WaitNotExistStrategy)))
if (until is WaitNotExistStrategy)
{
return _wrappedElement;
}
Expand All @@ -249,9 +257,9 @@ protected WindowsElement GetAndWaitWebDriverElement()
return _wrappedElement;
}

private WindowsElement GetWebDriverElement()
private AppiumElement GetWebDriverElement()
{
WindowsElement result = _wrappedElement;
var result = _wrappedElement;
if (FoundWrappedElement != null)
{
result = FoundWrappedElement;
Expand Down
17 changes: 9 additions & 8 deletions src/Bellatrix.Desktop/components/Core/ComponentsList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using Bellatrix.Desktop.Locators;
using Bellatrix.Desktop.Services;
using Bellatrix.Desktop.Untils;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Windows;

namespace Bellatrix.Desktop.Controls.Core;
Expand All @@ -27,14 +28,14 @@ public class ComponentsList<TComponent> : IEnumerable<TComponent>
where TComponent : Component
{
private readonly FindStrategy _by;
private readonly WindowsElement _parenTComponent;
private readonly AppiumElement _parenTComponent;
private readonly List<TComponent> _foundElements;
private readonly bool _shouldCacheFoundElements;
private List<TComponent> _cachedElements;

public ComponentsList(
FindStrategy by,
WindowsElement parenTComponent,
AppiumElement parenTComponent,
bool shouldCacheFoundElements)
: this(by, parenTComponent)
{
Expand All @@ -43,27 +44,27 @@ public ComponentsList(

public ComponentsList(
FindStrategy by,
WindowsElement parenTComponent)
AppiumElement parenTComponent)
{
_by = by;
_parenTComponent = parenTComponent;
_foundElements = new List<TComponent>();
WrappedDriver = ServicesCollection.Current.Resolve<WindowsDriver<WindowsElement>>();
WrappedDriver = ServicesCollection.Current.Resolve<WindowsDriver>();
}

public ComponentsList()
{
_foundElements = new List<TComponent>();
WrappedDriver = ServicesCollection.Current.Resolve<WindowsDriver<WindowsElement>>();
WrappedDriver = ServicesCollection.Current.Resolve<WindowsDriver>();
}

public ComponentsList(IEnumerable<TComponent> nativeElementList)
{
_foundElements = new List<TComponent>(nativeElementList);
WrappedDriver = ServicesCollection.Current.Resolve<WindowsDriver<WindowsElement>>();
WrappedDriver = ServicesCollection.Current.Resolve<WindowsDriver>();
}

public WindowsDriver<WindowsElement> WrappedDriver { get; }
public WindowsDriver WrappedDriver { get; }

public TComponent this[int i] => GetAndWaitWebDriverElements().ElementAt(i);

Expand Down Expand Up @@ -130,7 +131,7 @@ IEnumerable<TComponent> GetAndWaitNativeElements()
foreach (var nativeElement in _by?.FindAllElements(_parenTComponent))
{
var element =
elementRepository.CreateComponentThatIsFound<TComponent>(_by, (WindowsElement)nativeElement);
elementRepository.CreateComponentThatIsFound<TComponent>(_by, nativeElement);
yield return element;
}
}
Expand Down
62 changes: 62 additions & 0 deletions src/Bellatrix.Desktop/components/ListItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// <copyright file="ListBox.cs" company="Automate The Planet Ltd.">
// Copyright 2022 Automate The Planet Ltd.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
// <author>Anton Angelov</author>
// <site>https://bellatrix.solutions/</site>
using System;
using System.Diagnostics;
using Bellatrix.Desktop.Events;

namespace Bellatrix.Desktop;

public class ListItem : Component
{
public static event EventHandler<ComponentActionEventArgs> Hovering;
public static event EventHandler<ComponentActionEventArgs> Hovered;

public virtual void Hover()
{
Hover(Hovering, Hovered);
}

[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public virtual bool IsDisabled => GetIsDisabled();

public virtual void Select()
{
if (!ConfigurationService.GetSection<ExecutionSettings>().ExperimentalDesktopDriver)
{
throw new InvalidOperationException("This option is supported only with ExperimentalDesktopDriver enabled");
}

WrappedDriver.ExecuteScript("windows: select", WrappedElement);
}

public virtual void AddToSelection()
{
if (!ConfigurationService.GetSection<ExecutionSettings>().ExperimentalDesktopDriver)
{
throw new InvalidOperationException("This option is supported only with ExperimentalDesktopDriver enabled");
}

WrappedDriver.ExecuteScript("windows: addToSelection", WrappedElement);
}

public virtual void RemoveFromSelection()
{
if (!ConfigurationService.GetSection<ExecutionSettings>().ExperimentalDesktopDriver)
{
throw new InvalidOperationException("This option is supported only with ExperimentalDesktopDriver enabled");
}

WrappedDriver.ExecuteScript("windows: removeFromSelection", WrappedElement);
}
}
4 changes: 2 additions & 2 deletions src/Bellatrix.Desktop/components/Window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public virtual void Attach()
var driver = WrappedWebDriverCreateService.Create(currentAppConfiguration, ServicesCollection.Current);

WrappedDriver.Quit();
ServicesCollection.Current.UnregisterSingleInstance<WindowsDriver<WindowsElement>>();
ServicesCollection.Current.UnregisterSingleInstance<WindowsDriver>();
ServicesCollection.Current.RegisterInstance(driver);

Attached?.Invoke(this, new ComponentActionEventArgs(this));
Expand All @@ -86,7 +86,7 @@ public virtual void Detach()
var driver = WrappedWebDriverCreateService.Create(currentAppConfiguration, ServicesCollection.Current);

WrappedDriver.Quit();
ServicesCollection.Current.UnregisterSingleInstance<WindowsDriver<WindowsElement>>();
ServicesCollection.Current.UnregisterSingleInstance<WindowsDriver>();
ServicesCollection.Current.RegisterInstance(driver);

Attached?.Invoke(this, new ComponentActionEventArgs(this));
Expand Down
5 changes: 3 additions & 2 deletions src/Bellatrix.Desktop/contracts/IComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
// </copyright>
// <author>Anton Angelov</author>
// <site>https://bellatrix.solutions/</site>
using OpenQA.Selenium.Appium.Windows;

using OpenQA.Selenium.Appium;

namespace Bellatrix.Desktop.Contracts;

Expand All @@ -21,5 +22,5 @@ public interface IComponent

string PageName { get; }

WindowsElement WrappedElement { get; }
AppiumElement WrappedElement { get; }
}
6 changes: 4 additions & 2 deletions src/Bellatrix.Desktop/events/NativeElementActionEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
// </copyright>
// <author>Anton Angelov</author>
// <site>https://bellatrix.solutions/</site>

using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Windows;

namespace Bellatrix.Desktop.Events;

public class NativeElementActionEventArgs
{
public NativeElementActionEventArgs(WindowsElement element) => Element = element;
public NativeElementActionEventArgs(AppiumElement element) => Element = element;

public WindowsElement Element { get; }
public AppiumElement Element { get; }
}
Loading

0 comments on commit 738adc2

Please sign in to comment.