From b15a1789cc97edda4b5a8a199b148435d4f523b4 Mon Sep 17 00:00:00 2001 From: James Croft Date: Sat, 4 Jun 2022 08:10:30 +0100 Subject: [PATCH 1/2] Added new BasePage extensions to find elements --- .../Extensions/PageExtensions.cs | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/Legerity.Core/Extensions/PageExtensions.cs b/src/Legerity.Core/Extensions/PageExtensions.cs index e71268d0..10a8623c 100644 --- a/src/Legerity.Core/Extensions/PageExtensions.cs +++ b/src/Legerity.Core/Extensions/PageExtensions.cs @@ -1,8 +1,10 @@ namespace Legerity.Extensions { using System; + using System.Collections.ObjectModel; using Legerity.Pages; using OpenQA.Selenium; + using OpenQA.Selenium.Remote; using OpenQA.Selenium.Support.UI; /// @@ -10,6 +12,36 @@ namespace Legerity.Extensions /// public static class PageExtensions { + /// + /// Finds the first element in the given element that matches the locator. + /// + /// + /// The type of . + /// + /// The page object. + /// The locator to find the element. + /// A . + public static RemoteWebElement FindWebElement(this TPage page, By locator) + where TPage : BasePage + { + return page.App.FindWebElement(locator); + } + + /// + /// Finds all the elements in the given element that matches the locator. + /// + /// + /// The type of . + /// + /// The page object. + /// The locator to find the elements. + /// A readonly collection of . + public static ReadOnlyCollection FindWebElements(this TPage page, By locator) + where TPage : BasePage + { + return page.App.FindWebElements(locator); + } + /// /// Waits until a specified page condition is met, with an optional timeout. /// From 3a9d37cdcbfc99ae28e5ab29897c4fe2ad5abe46 Mon Sep 17 00:00:00 2001 From: James Croft Date: Sat, 4 Jun 2022 08:21:12 +0100 Subject: [PATCH 2/2] Updated page object with FindElement methods --- .../Extensions/PageExtensions.cs | 30 --------- src/Legerity.Core/Pages/BasePage.cs | 65 ++++++++++++++++++- 2 files changed, 63 insertions(+), 32 deletions(-) diff --git a/src/Legerity.Core/Extensions/PageExtensions.cs b/src/Legerity.Core/Extensions/PageExtensions.cs index 10a8623c..576d6bf7 100644 --- a/src/Legerity.Core/Extensions/PageExtensions.cs +++ b/src/Legerity.Core/Extensions/PageExtensions.cs @@ -12,36 +12,6 @@ namespace Legerity.Extensions /// public static class PageExtensions { - /// - /// Finds the first element in the given element that matches the locator. - /// - /// - /// The type of . - /// - /// The page object. - /// The locator to find the element. - /// A . - public static RemoteWebElement FindWebElement(this TPage page, By locator) - where TPage : BasePage - { - return page.App.FindWebElement(locator); - } - - /// - /// Finds all the elements in the given element that matches the locator. - /// - /// - /// The type of . - /// - /// The page object. - /// The locator to find the elements. - /// A readonly collection of . - public static ReadOnlyCollection FindWebElements(this TPage page, By locator) - where TPage : BasePage - { - return page.App.FindWebElements(locator); - } - /// /// Waits until a specified page condition is met, with an optional timeout. /// diff --git a/src/Legerity.Core/Pages/BasePage.cs b/src/Legerity.Core/Pages/BasePage.cs index 48d42236..74f0cb8b 100644 --- a/src/Legerity.Core/Pages/BasePage.cs +++ b/src/Legerity.Core/Pages/BasePage.cs @@ -1,9 +1,10 @@ namespace Legerity.Pages { using System; - + using System.Collections.ObjectModel; + using System.Linq; using Legerity.Exceptions; - + using Legerity.Extensions; using OpenQA.Selenium; using OpenQA.Selenium.Appium.Android; using OpenQA.Selenium.Appium.iOS; @@ -75,6 +76,66 @@ protected BasePage(TimeSpan? traitTimeout) /// protected abstract By Trait { get; } + /// + /// Finds the first element in the page that matches the locator. + /// + /// The locator to find the element. + /// A . + public RemoteWebElement FindElement(By locator) + { + return this.App.FindWebElement(locator); + } + + /// + /// Finds all the elements in the page that matches the locator. + /// + /// The locator to find the elements. + /// A readonly collection of . + public ReadOnlyCollection FindElements(By locator) + { + return this.App.FindWebElements(locator); + } + + /// + /// Finds the first element in the page that matches the specified XPath. + /// + /// The XPath to find the element. + /// A . + public RemoteWebElement FindElementByXPath(string xpath) + { + return this.App.FindElementByXPath(xpath) as RemoteWebElement; + } + + /// + /// Finds all the elements in the page that matches the specified XPath. + /// + /// The XPath to find the elements. + /// A readonly collection of . + public ReadOnlyCollection FindElementsByXPath(string xpath) + { + return this.App.FindElementsByXPath(xpath).Cast().ToList().AsReadOnly(); + } + + /// + /// Finds the first element in the page that matches the specified ID. + /// + /// The ID of the element. + /// A . + public RemoteWebElement FindElementById(string id) + { + return this.App.FindElementById(id) as RemoteWebElement; + } + + /// + /// Finds the first of element in the page that matches the specified name. + /// + /// The name of the element. + /// A . + public RemoteWebElement FindElementByName(string name) + { + return this.App.FindElementByName(name) as RemoteWebElement; + } + /// /// Determines whether the current page is shown immediately. ///