From 7363c450eae42484572f113123b65a00e737816e Mon Sep 17 00:00:00 2001 From: James Croft Date: Thu, 16 Feb 2023 15:55:34 +0000 Subject: [PATCH] Update element wrapper base implementation to be usable as standalone elements enabling advantages of Legerity features --- .../Elements/AndroidElementWrapper.cs | 163 ++++++--- .../Elements/IOSElementWrapper.cs | 163 ++++++--- .../Elements/WebElementWrapper.cs | 345 +++++++++--------- .../Elements/WindowsElementWrapper.cs | 163 ++++++--- 4 files changed, 488 insertions(+), 346 deletions(-) diff --git a/src/Legerity.Android/Elements/AndroidElementWrapper.cs b/src/Legerity.Android/Elements/AndroidElementWrapper.cs index a67f2837..2104e559 100644 --- a/src/Legerity.Android/Elements/AndroidElementWrapper.cs +++ b/src/Legerity.Android/Elements/AndroidElementWrapper.cs @@ -1,80 +1,123 @@ -namespace Legerity.Android.Elements +namespace Legerity.Android.Elements; + +using System; + +using Legerity.Exceptions; + +using OpenQA.Selenium; +using OpenQA.Selenium.Appium; +using OpenQA.Selenium.Appium.Android; +using OpenQA.Selenium.Remote; +using OpenQA.Selenium.Support.UI; + +/// +/// Defines an element wrapper for a . +/// +public class AndroidElementWrapper : ElementWrapper { - using System; + /// + /// Initializes a new instance of the class. + /// + /// + /// The reference. + /// + public AndroidElementWrapper(AndroidElement element) + : base(element) + { + } - using Legerity.Exceptions; + /// + /// Gets the instance of the Appium driver for the Android application. + /// + public AndroidDriver Driver => this.ElementDriver as AndroidDriver; - using OpenQA.Selenium; - using OpenQA.Selenium.Appium.Android; - using OpenQA.Selenium.Support.UI; + /// + /// Allows conversion of a to the without direct casting. + /// + /// + /// The . + /// + /// + /// The . + /// + public static implicit operator AndroidElementWrapper(AndroidElement element) + { + return new AndroidElementWrapper(element); + } /// - /// Defines an element wrapper for a . + /// Allows conversion of a to the without direct casting. /// - public abstract class AndroidElementWrapper : ElementWrapper + /// + /// The . + /// + /// + /// The . + /// + public static implicit operator AndroidElementWrapper(AppiumWebElement element) { - /// - /// Initializes a new instance of the class. - /// - /// - /// The reference. - /// - protected AndroidElementWrapper(AndroidElement element) - : base(element) - { - } + return new AndroidElementWrapper(element as AndroidElement); + } - /// - /// Gets the instance of the Appium driver for the Android application. - /// - public AndroidDriver Driver => this.ElementDriver as AndroidDriver; + /// + /// Allows conversion of a to the without direct casting. + /// + /// + /// The . + /// + /// + /// The . + /// + public static implicit operator AndroidElementWrapper(RemoteWebElement element) + { + return new AndroidElementWrapper(element as AndroidElement); + } - /// - /// Determines whether the specified element is shown with the specified timeout. - /// - /// The locator to find a specific element. - /// - /// The amount of time the driver should wait when searching for the if it is not immediately present. - /// - protected void VerifyDriverElementShown(By locator, TimeSpan? timeout) + /// + /// Determines whether the specified element is shown with the specified timeout. + /// + /// The locator to find a specific element. + /// + /// The amount of time the driver should wait when searching for the if it is not immediately present. + /// + protected void VerifyDriverElementShown(By locator, TimeSpan? timeout) + { + if (timeout == null) { - if (timeout == null) - { - if (this.Driver.FindElement(locator) == null) - { - throw new ElementNotShownException(locator.ToString()); - } - } - else + if (this.Driver.FindElement(locator) == null) { - var wait = new WebDriverWait(this.Driver, timeout.Value); - wait.Until(driver => driver.FindElement(locator) != null); + throw new ElementNotShownException(locator.ToString()); } } + else + { + var wait = new WebDriverWait(this.Driver, timeout.Value); + wait.Until(driver => driver.FindElement(locator) != null); + } + } - /// - /// Determines whether the specified elements are shown with the specified timeout. - /// - /// - /// The locator to find a collection of elements. - /// - /// - /// The amount of time the driver should wait when searching for the if it is not immediately present. - /// - protected void VerifyDriverElementsShown(By locator, TimeSpan? timeout) + /// + /// Determines whether the specified elements are shown with the specified timeout. + /// + /// + /// The locator to find a collection of elements. + /// + /// + /// The amount of time the driver should wait when searching for the if it is not immediately present. + /// + protected void VerifyDriverElementsShown(By locator, TimeSpan? timeout) + { + if (timeout == null) { - if (timeout == null) + if (this.Driver.FindElements(locator).Count == 0) { - if (this.Driver.FindElements(locator).Count == 0) - { - throw new ElementNotShownException(locator.ToString()); - } - } - else - { - var wait = new WebDriverWait(this.Driver, timeout.Value); - wait.Until(driver => driver.FindElements(locator).Count != 0); + throw new ElementNotShownException(locator.ToString()); } } + else + { + var wait = new WebDriverWait(this.Driver, timeout.Value); + wait.Until(driver => driver.FindElements(locator).Count != 0); + } } } \ No newline at end of file diff --git a/src/Legerity.IOS/Elements/IOSElementWrapper.cs b/src/Legerity.IOS/Elements/IOSElementWrapper.cs index 571155c7..34b25c86 100644 --- a/src/Legerity.IOS/Elements/IOSElementWrapper.cs +++ b/src/Legerity.IOS/Elements/IOSElementWrapper.cs @@ -1,80 +1,123 @@ -namespace Legerity.IOS.Elements +namespace Legerity.IOS.Elements; + +using System; + +using Legerity.Exceptions; + +using OpenQA.Selenium; +using OpenQA.Selenium.Appium; +using OpenQA.Selenium.Appium.iOS; +using OpenQA.Selenium.Remote; +using OpenQA.Selenium.Support.UI; + +/// +/// Defines an element wrapper for a . +/// +public class IOSElementWrapper : ElementWrapper { - using System; + /// + /// Initializes a new instance of the class. + /// + /// + /// The reference. + /// + public IOSElementWrapper(IOSElement element) + : base(element) + { + } - using Legerity.Exceptions; + /// + /// Gets the instance of the Appium driver for the iOS application. + /// + public IOSDriver Driver => this.ElementDriver as IOSDriver; - using OpenQA.Selenium; - using OpenQA.Selenium.Appium.iOS; - using OpenQA.Selenium.Support.UI; + /// + /// Allows conversion of a to the without direct casting. + /// + /// + /// The . + /// + /// + /// The . + /// + public static implicit operator IOSElementWrapper(IOSElement element) + { + return new IOSElementWrapper(element); + } /// - /// Defines an element wrapper for a . + /// Allows conversion of a to the without direct casting. /// - public abstract class IOSElementWrapper : ElementWrapper + /// + /// The . + /// + /// + /// The . + /// + public static implicit operator IOSElementWrapper(AppiumWebElement element) { - /// - /// Initializes a new instance of the class. - /// - /// - /// The reference. - /// - protected IOSElementWrapper(IOSElement element) - : base(element) - { - } + return new IOSElementWrapper(element as IOSElement); + } - /// - /// Gets the instance of the Appium driver for the iOS application. - /// - public IOSDriver Driver => this.ElementDriver as IOSDriver; + /// + /// Allows conversion of a to the without direct casting. + /// + /// + /// The . + /// + /// + /// The . + /// + public static implicit operator IOSElementWrapper(RemoteWebElement element) + { + return new IOSElementWrapper(element as IOSElement); + } - /// - /// Determines whether the specified element is shown with the specified timeout. - /// - /// The locator to find a specific element. - /// - /// The amount of time the driver should wait when searching for the if it is not immediately present. - /// - protected void VerifyDriverElementShown(By locator, TimeSpan? timeout) + /// + /// Determines whether the specified element is shown with the specified timeout. + /// + /// The locator to find a specific element. + /// + /// The amount of time the driver should wait when searching for the if it is not immediately present. + /// + protected void VerifyDriverElementShown(By locator, TimeSpan? timeout) + { + if (timeout == null) { - if (timeout == null) - { - if (this.Driver.FindElement(locator) == null) - { - throw new ElementNotShownException(locator.ToString()); - } - } - else + if (this.Driver.FindElement(locator) == null) { - var wait = new WebDriverWait(this.Driver, timeout.Value); - wait.Until(driver => driver.FindElement(locator) != null); + throw new ElementNotShownException(locator.ToString()); } } + else + { + var wait = new WebDriverWait(this.Driver, timeout.Value); + wait.Until(driver => driver.FindElement(locator) != null); + } + } - /// - /// Determines whether the specified elements are shown with the specified timeout. - /// - /// - /// The locator to find a collection of elements. - /// - /// - /// The amount of time the driver should wait when searching for the if it is not immediately present. - /// - protected void VerifyDriverElementsShown(By locator, TimeSpan? timeout) + /// + /// Determines whether the specified elements are shown with the specified timeout. + /// + /// + /// The locator to find a collection of elements. + /// + /// + /// The amount of time the driver should wait when searching for the if it is not immediately present. + /// + protected void VerifyDriverElementsShown(By locator, TimeSpan? timeout) + { + if (timeout == null) { - if (timeout == null) + if (this.Driver.FindElements(locator).Count == 0) { - if (this.Driver.FindElements(locator).Count == 0) - { - throw new ElementNotShownException(locator.ToString()); - } - } - else - { - var wait = new WebDriverWait(this.Driver, timeout.Value); - wait.Until(driver => driver.FindElements(locator).Count != 0); + throw new ElementNotShownException(locator.ToString()); } } + else + { + var wait = new WebDriverWait(this.Driver, timeout.Value); + wait.Until(driver => driver.FindElements(locator).Count != 0); + } } } \ No newline at end of file diff --git a/src/Legerity.Web/Elements/WebElementWrapper.cs b/src/Legerity.Web/Elements/WebElementWrapper.cs index c4172283..059cf298 100644 --- a/src/Legerity.Web/Elements/WebElementWrapper.cs +++ b/src/Legerity.Web/Elements/WebElementWrapper.cs @@ -1,195 +1,208 @@ -namespace Legerity.Web.Elements +namespace Legerity.Web.Elements; + +using System; +using System.Collections.ObjectModel; +using Legerity.Exceptions; +using Legerity.Extensions; +using OpenQA.Selenium; +using OpenQA.Selenium.Remote; +using OpenQA.Selenium.Support.UI; + +/// +/// Defines an element wrapper for a . +/// +public class WebElementWrapper : IElementWrapper { - using System; - using System.Collections.ObjectModel; - using Legerity.Exceptions; - using Legerity.Extensions; - using OpenQA.Selenium; - using OpenQA.Selenium.Remote; - using OpenQA.Selenium.Support.UI; + private readonly WeakReference elementReference; /// - /// Defines an element wrapper for a . + /// Initializes a new instance of the class. /// - public abstract class WebElementWrapper : IElementWrapper + /// + /// The reference. + /// + public WebElementWrapper(IWebElement element) + : this(element as RemoteWebElement) { - private readonly WeakReference elementReference; - - /// - /// Initializes a new instance of the class. - /// - /// - /// The reference. - /// - protected WebElementWrapper(IWebElement element) - : this(element as RemoteWebElement) - { - } + } - /// - /// Initializes a new instance of the class. - /// - /// - /// The reference. - /// - protected WebElementWrapper(RemoteWebElement element) - { - this.elementReference = new WeakReference(element); - } + /// + /// Initializes a new instance of the class. + /// + /// + /// The reference. + /// + public WebElementWrapper(RemoteWebElement element) + { + this.elementReference = new WeakReference(element); + } - /// Gets the original reference object. - public RemoteWebElement Element => - this.elementReference is { IsAlive: true } - ? this.elementReference.Target as RemoteWebElement - : null; - - /// - /// Gets the driver used to find this element. - /// - public IWebDriver ElementDriver => this.Element.WrappedDriver; - - /// - /// Gets the instance of the driver for the web application. - /// - public RemoteWebDriver Driver => this.ElementDriver as RemoteWebDriver; - - /// - /// Gets a value indicating whether the element is enabled. - /// - public virtual bool IsEnabled => this.Element.Enabled; - - /// - /// Gets a value indicating whether the element is visible. - /// - public virtual bool IsVisible => this.Element.Displayed; - - /// - /// Finds a child element by the specified locator. - /// - /// The locator to find a child element by. - /// The . - public RemoteWebElement FindElement(By locator) - { - return this.Element.FindWebElement(locator); - } + /// Gets the original reference object. + public RemoteWebElement Element => + this.elementReference is { IsAlive: true } + ? this.elementReference.Target as RemoteWebElement + : null; - /// - /// Finds a collection of child elements by the specified locator. - /// - /// The locator to find a child element by. - /// The readonly collection of . - public ReadOnlyCollection FindElements(By locator) - { - return this.Element.FindWebElements(locator); - } + /// + /// Gets the driver used to find this element. + /// + public IWebDriver ElementDriver => this.Element.WrappedDriver; - /// - /// Determines whether the given element is not shown. - /// - /// - /// The locator for the element to locate. - /// - public void VerifyElementNotShown(By locator) - { - try - { - this.VerifyElementShown(locator); - } - catch (ElementNotShownException) - { - } - } + /// + /// Gets the instance of the driver for the web application. + /// + public RemoteWebDriver Driver => this.ElementDriver as RemoteWebDriver; - /// - /// Clicks the element. - /// - public virtual void Click() - { - this.Element.Click(); - } + /// + /// Gets a value indicating whether the element is enabled. + /// + public virtual bool IsEnabled => this.Element.Enabled; + + /// + /// Gets a value indicating whether the element is visible. + /// + public virtual bool IsVisible => this.Element.Displayed; + + /// + /// Allows conversion of a to the without direct casting. + /// + /// + /// The . + /// + /// + /// The . + /// + public static implicit operator WebElementWrapper(RemoteWebElement element) + { + return new WebElementWrapper(element); + } + + /// + /// Finds a child element by the specified locator. + /// + /// The locator to find a child element by. + /// The . + public RemoteWebElement FindElement(By locator) + { + return this.Element.FindWebElement(locator); + } + + /// + /// Finds a collection of child elements by the specified locator. + /// + /// The locator to find a child element by. + /// The readonly collection of . + public ReadOnlyCollection FindElements(By locator) + { + return this.Element.FindWebElements(locator); + } - /// - /// Gets the value of the specified attribute for this element. - /// - /// The name of the attribute. - /// The attribute's current value if it exists; otherwise, null. - public string GetAttribute(string attributeName) + /// + /// Determines whether the given element is not shown. + /// + /// + /// The locator for the element to locate. + /// + public void VerifyElementNotShown(By locator) + { + try { - return this.Element.GetAttribute(attributeName); + this.VerifyElementShown(locator); } - - /// - /// Determines whether the given element is shown. - /// - /// - /// The locator for the element to find. - /// - /// Thrown if the element is not shown. - public void VerifyElementShown(By locator) + catch (ElementNotShownException) { - this.VerifyElementShown(locator, null); } + } + + /// + /// Clicks the element. + /// + public virtual void Click() + { + this.Element.Click(); + } + + /// + /// Gets the value of the specified attribute for this element. + /// + /// The name of the attribute. + /// The attribute's current value if it exists; otherwise, null. + public string GetAttribute(string attributeName) + { + return this.Element.GetAttribute(attributeName); + } + + /// + /// Determines whether the given element is shown. + /// + /// + /// The locator for the element to find. + /// + /// Thrown if the element is not shown. + public void VerifyElementShown(By locator) + { + this.VerifyElementShown(locator, null); + } - /// - /// Determines whether the specified element is shown with the specified timeout. - /// - /// The locator to find a specific element. - /// - /// The amount of time the driver should wait when searching for the if it is not immediately present. - /// - /// Thrown if the element is not shown. - public void VerifyElementShown(By locator, TimeSpan? timeout) + /// + /// Determines whether the specified element is shown with the specified timeout. + /// + /// The locator to find a specific element. + /// + /// The amount of time the driver should wait when searching for the if it is not immediately present. + /// + /// Thrown if the element is not shown. + public void VerifyElementShown(By locator, TimeSpan? timeout) + { + if (timeout == null) { - if (timeout == null) + if (this.Element.FindElement(locator) == null) { - if (this.Element.FindElement(locator) == null) - { - throw new ElementNotShownException(locator.ToString()); - } - } - else - { - var wait = new WebDriverWait(this.Driver, timeout.Value); - wait.Until(driver => driver.FindElement(locator) != null); + throw new ElementNotShownException(locator.ToString()); } } - - /// - /// Determines whether the specified elements are shown. - /// - /// - /// The locator for the element to find. - /// - /// Thrown if the elements are not shown. - public void VerifyElementsShown(By locator) + else { - this.VerifyElementsShown(locator, null); + var wait = new WebDriverWait(this.Driver, timeout.Value); + wait.Until(driver => driver.FindElement(locator) != null); } + } - /// - /// Determines whether the specified elements are shown with the specified timeout. - /// - /// - /// The locator to find a collection of elements. - /// - /// - /// The amount of time the driver should wait when searching for the if it is not immediately present. - /// - /// Thrown if the elements are not shown. - public void VerifyElementsShown(By locator, TimeSpan? timeout) + /// + /// Determines whether the specified elements are shown. + /// + /// + /// The locator for the element to find. + /// + /// Thrown if the elements are not shown. + public void VerifyElementsShown(By locator) + { + this.VerifyElementsShown(locator, null); + } + + /// + /// Determines whether the specified elements are shown with the specified timeout. + /// + /// + /// The locator to find a collection of elements. + /// + /// + /// The amount of time the driver should wait when searching for the if it is not immediately present. + /// + /// Thrown if the elements are not shown. + public void VerifyElementsShown(By locator, TimeSpan? timeout) + { + if (timeout == null) { - if (timeout == null) - { - if (this.Element.FindElements(locator).Count == 0) - { - throw new ElementNotShownException(locator.ToString()); - } - } - else + if (this.Element.FindElements(locator).Count == 0) { - var wait = new WebDriverWait(this.Driver, timeout.Value); - wait.Until(driver => driver.FindElements(locator).Count != 0); + throw new ElementNotShownException(locator.ToString()); } } + else + { + var wait = new WebDriverWait(this.Driver, timeout.Value); + wait.Until(driver => driver.FindElements(locator).Count != 0); + } } } \ No newline at end of file diff --git a/src/Legerity.Windows/Elements/WindowsElementWrapper.cs b/src/Legerity.Windows/Elements/WindowsElementWrapper.cs index 7d84801e..157e6fa7 100644 --- a/src/Legerity.Windows/Elements/WindowsElementWrapper.cs +++ b/src/Legerity.Windows/Elements/WindowsElementWrapper.cs @@ -1,79 +1,122 @@ -namespace Legerity.Windows.Elements +namespace Legerity.Windows.Elements; + +using System; +using Legerity.Exceptions; + +using OpenQA.Selenium; +using OpenQA.Selenium.Appium; +using OpenQA.Selenium.Appium.Windows; +using OpenQA.Selenium.Remote; +using OpenQA.Selenium.Support.UI; + +/// +/// Defines an element wrapper for a . +/// +public class WindowsElementWrapper : ElementWrapper { - using System; - using Legerity.Exceptions; + /// + /// Initializes a new instance of the class. + /// + /// + /// The reference. + /// + public WindowsElementWrapper(WindowsElement element) + : base(element) + { + } + + /// + /// Gets the instance of the Appium driver for the Windows application. + /// + public WindowsDriver Driver => this.ElementDriver as WindowsDriver; - using OpenQA.Selenium; - using OpenQA.Selenium.Appium.Windows; - using OpenQA.Selenium.Support.UI; + /// + /// Allows conversion of a to the without direct casting. + /// + /// + /// The . + /// + /// + /// The . + /// + public static implicit operator WindowsElementWrapper(WindowsElement element) + { + return new WindowsElementWrapper(element); + } /// - /// Defines an element wrapper for a . + /// Allows conversion of a to the without direct casting. /// - public abstract class WindowsElementWrapper : ElementWrapper + /// + /// The . + /// + /// + /// The . + /// + public static implicit operator WindowsElementWrapper(AppiumWebElement element) { - /// - /// Initializes a new instance of the class. - /// - /// - /// The reference. - /// - protected WindowsElementWrapper(WindowsElement element) - : base(element) - { - } + return new WindowsElementWrapper(element as WindowsElement); + } - /// - /// Gets the instance of the Appium driver for the Windows application. - /// - public WindowsDriver Driver => this.ElementDriver as WindowsDriver; + /// + /// Allows conversion of a to the without direct casting. + /// + /// + /// The . + /// + /// + /// The . + /// + public static implicit operator WindowsElementWrapper(RemoteWebElement element) + { + return new WindowsElementWrapper(element as WindowsElement); + } - /// - /// Determines whether the specified element is shown with the specified timeout. - /// - /// The locator to find a specific element. - /// - /// The amount of time the driver should wait when searching for the if it is not immediately present. - /// - protected void VerifyDriverElementShown(By locator, TimeSpan? timeout) + /// + /// Determines whether the specified element is shown with the specified timeout. + /// + /// The locator to find a specific element. + /// + /// The amount of time the driver should wait when searching for the if it is not immediately present. + /// + protected void VerifyDriverElementShown(By locator, TimeSpan? timeout) + { + if (timeout == null) { - if (timeout == null) - { - if (this.Driver.FindElement(locator) == null) - { - throw new ElementNotShownException(locator.ToString()); - } - } - else + if (this.Driver.FindElement(locator) == null) { - var wait = new WebDriverWait(this.Driver, timeout.Value); - wait.Until(driver => driver.FindElement(locator) != null); + throw new ElementNotShownException(locator.ToString()); } } + else + { + var wait = new WebDriverWait(this.Driver, timeout.Value); + wait.Until(driver => driver.FindElement(locator) != null); + } + } - /// - /// Determines whether the specified elements are shown with the specified timeout. - /// - /// - /// The locator to find a collection of elements. - /// - /// - /// The amount of time the driver should wait when searching for the if it is not immediately present. - /// - protected void VerifyDriverElementsShown(By locator, TimeSpan? timeout) + /// + /// Determines whether the specified elements are shown with the specified timeout. + /// + /// + /// The locator to find a collection of elements. + /// + /// + /// The amount of time the driver should wait when searching for the if it is not immediately present. + /// + protected void VerifyDriverElementsShown(By locator, TimeSpan? timeout) + { + if (timeout == null) { - if (timeout == null) + if (this.Driver.FindElements(locator).Count == 0) { - if (this.Driver.FindElements(locator).Count == 0) - { - throw new ElementNotShownException(locator.ToString()); - } - } - else - { - var wait = new WebDriverWait(this.Driver, timeout.Value); - wait.Until(driver => driver.FindElements(locator).Count != 0); + throw new ElementNotShownException(locator.ToString()); } } + else + { + var wait = new WebDriverWait(this.Driver, timeout.Value); + wait.Until(driver => driver.FindElements(locator).Count != 0); + } } } \ No newline at end of file