ElementExtensions.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using OpenQA.Selenium.Appium;
  4. using OpenQA.Selenium.Appium.MultiTouch;
  5. using OpenQA.Selenium.Interactions;
  6. namespace Avalonia.IntegrationTests.Appium
  7. {
  8. internal static class ElementExtensions
  9. {
  10. public static string GetName(this AppiumWebElement element) => GetAttribute(element, "Name", "title");
  11. public static bool? GetIsChecked(this AppiumWebElement element) =>
  12. GetAttribute(element, "Toggle.ToggleState", "value") switch
  13. {
  14. "0" => false,
  15. "1" => true,
  16. "2" => null,
  17. _ => throw new ArgumentOutOfRangeException($"Unexpected IsChecked value.")
  18. };
  19. public static void SendClick(this AppiumWebElement element)
  20. {
  21. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  22. {
  23. element.Click();
  24. }
  25. else
  26. {
  27. // The Click() method seems to correspond to accessibilityPerformPress on macOS but certain controls
  28. // such as list items don't support this action, so instead simulate a physical click as VoiceOver
  29. // does.
  30. var action = new Actions(element.WrappedDriver);
  31. action.MoveToElement(element);
  32. action.Click();
  33. action.Perform();
  34. }
  35. }
  36. public static string GetAttribute(AppiumWebElement element, string windows, string macOS)
  37. {
  38. return element.GetAttribute(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? windows : macOS);
  39. }
  40. }
  41. }