Monday, 5 August 2019

Automation Selenium Control Actions

using Itron.FDM.SQA.VSAutomation.Framework.Reporting;
using OpenQA.Selenium;
using OpenQA.Selenium.Appium.Windows;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Support.UI;
using System;
using System.Threading;

namespace Itron.FDM.SQA.VSAutomation.Framework.SeleniumUtilities
{
    public class ControlActions
    {
        private static ExtentReporter TestReport = SessionFactory.GetReport();

        public static bool IsElementExists(IWebElement element)
        {
            bool exists = false;

            try
            {
                exists = element.Displayed;
                return exists;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// <param name="element"></param>
        /// <param name="intervalToWaitInMilliSecs"></param>
        /// <param name="loopCount - The total wait time will be loopcount * intervalToWaitInMilliSecs"></param>
        /// <returns></returns>
        /// </summary>
        public static bool IsElementExistsWithWait(IWebElement element, int loopCount = 20, int intervalToWaitInMilliSecs = 500)
        {
            int attempt = 0;
            bool exists = false;

            if (element != null)
            {
                do
                {
                    try
                    {
                        exists = element.Displayed;

                        if (exists)
                        {
                            break;
                        }
                        else
                        {
                            attempt++;
                            Thread.Sleep(intervalToWaitInMilliSecs);
                        }
                    }
                    catch (Exception ex)
                    {
                        var s = ex.Message;
                        attempt++;
                        Thread.Sleep(intervalToWaitInMilliSecs);
                    }
                }
                while (attempt < loopCount);
            }

            return exists;
        }

        /// <summary>
        /// <param name="element"></param>
        /// <param name="intervalToWaitInMilliSecs"></param>
        /// <param name="loopCount - The total wait time will be loopcount * intervalToWaitInMilliSecs"></param>
        /// <returns></returns>
        /// </summary>
        public static bool WaitForElementNotExists(IWebElement element, int loopCount = 20, int intervalToWaitInMilliSecs = 500)
        {
            int attempt = 0;
            bool exists = true;

            if (element != null)
            {
                do
                {
                    try
                    {
                        exists = element.Displayed;

                        if (!exists)
                        {
                            break;
                        }
                        else
                        {
                            attempt++;
                            Thread.Sleep(intervalToWaitInMilliSecs);
                        }
                    }
                    catch (Exception ex)
                    {
                        var s = ex.Message;
                        attempt++;
                        Thread.Sleep(intervalToWaitInMilliSecs);
                    }
                }
                while (attempt < loopCount);
            }

            return exists;
        }

        /// <summary>
        /// Waits for the element to be enabled
        /// </summary>
        /// <param name="element"></param>
        /// <param name="maxTimeoutInSeconds"></param>
        public static void WaitForElementEnabled(WindowsDriver<IWebElement> session, IWebElement element, int loopCount = 1, int maxTimeIntervalInSeconds = 30)
        {
            int attempt = 0;
            bool t = false;

            if (element != null)
            {
                do
                {
                    try
                    {
                        var wait = new DefaultWait<WindowsDriver<IWebElement>>(session)
                        {
                            Timeout = TimeSpan.FromSeconds(maxTimeIntervalInSeconds),
                            PollingInterval = TimeSpan.FromSeconds(2)
                        };
                        wait.IgnoreExceptionTypes(typeof(InvalidOperationException));
                        wait.IgnoreExceptionTypes(typeof(WebDriverTimeoutException));
                        wait.IgnoreExceptionTypes(typeof(WebDriverException));

                        wait.Until(driver =>
                        {
                            t = element.Enabled;
                            return t != false;
                        });

                        if (t)
                        {
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        var s = ex.Message;
                        attempt++;
                    }
                }
                while (attempt < loopCount);
            }
        }

        public static void Click(IWebElement element, int loopCount = 20, int intervalToWaitMillSecs = 500)
        {
            try
            {
                IsElementExistsWithWait(element, loopCount, intervalToWaitMillSecs);
                TestReport.LogSuccess(string.Format("{0}{1}", "Click element ", element.GetAttribute("Name")));
                element.Click();
            }
            catch (Exception ex)
            {
                TestReport.LogFailure(string.Format("{0}{1}", "Failed Clicking element ", ex.Message));
            }
        }

        public static void Clear(IWebElement element, int loopCount = 20, int intervalToWaitMillSecs = 500)
        {
            try
            {
                IsElementExistsWithWait(element, loopCount, intervalToWaitMillSecs);
                TestReport.LogSuccess(string.Format("{0}{1}", "Clearing contents of element ", element.GetAttribute("Name")));
                element.Clear();
            }
            catch (Exception ex)
            {
                TestReport.LogFailure(string.Format("{0}{1}", "Failed Clearing contents of element ", ex.Message));
            }
        }

        public static void SendKeyBoardPressKey(WindowsDriver<IWebElement> session, string key)
        {
            try
            {
                TestReport.LogSuccess("Entering value '" + key + "' through keyboard ");
                GetActionsInstance(session).SendKeys(key);
            }
            catch (Exception ex)
            {
                TestReport.LogFailure(string.Format("{0}{1}", "Failed entering value through keyboard", ex.Message));
            }
        }

        public static void SelectAllKeyboard(WindowsDriver<IWebElement> session)
        {
            try
            {
                TestReport.LogSuccess("Performing Select All operation");
                GetActionsInstance(session).SendKeys(Keys.Control + "a" + Keys.Control);
            }
            catch (Exception ex)
            {
                TestReport.LogFailure(string.Format("{0}{1}", "Failed performing Select All operation", ex.Message));
            }
        }

        public static void SendKeysToElement(IWebElement element, string textToEnter, int loopCount = 20, int intervalToWaitMillSecs = 500)
        {
            try
            {
                IsElementExistsWithWait(element, loopCount, intervalToWaitMillSecs);
                TestReport.LogSuccess(string.Format("{0},{1}", "Entering value '" + textToEnter + "' to element ", element.GetAttribute("Name")));
                element.SendKeys(textToEnter);
            }
            catch (Exception ex)
            {
                TestReport.LogFailure(string.Format("{0},{1}", "Failed entering value to element ", ex.Message));
            }
        }

        public static void SendKeysToElementClearFirst(IWebElement element, string textToEnter, int loopCount = 20, int intervalToWaitMillSecs = 500)
        {
            try
            {
                IsElementExistsWithWait(element, loopCount, intervalToWaitMillSecs);
                TestReport.LogSuccess(string.Format("{0},{1}", "Clearing and Entering value '" + textToEnter + "' to element ", element.GetAttribute("Name")));
                element.Clear();
                element.SendKeys(textToEnter);
            }
            catch (Exception ex)
            {
                TestReport.LogFailure(string.Format("{0},{1}", "Failed clearing and entering value to element ", ex.Message));
            }
        }

        public static void ClickByMoveByOffset(WindowsDriver<IWebElement> driver, int x, int y)
        {
            try
            {
                TestReport.LogSuccess("Performing click moving by offset using coordinates " + x + " , " + y + "");
                GetActionsInstance(driver).MoveByOffset(x, y).Click().Build().Perform();
            }
            catch (Exception ex)
            {
                TestReport.LogFailure(string.Format("{0}{1}", "Performing click moving by offset using coordinates", ex.Message));
            }
        }

        public static void MovetoElement(WindowsDriver<IWebElement> driver, IWebElement element, int loopCount = 20, int intervalToWaitMillSecs = 500)
        {
            try
            {
                IsElementExistsWithWait(element, loopCount, intervalToWaitMillSecs);
                TestReport.LogSuccess(string.Format("{0}{1}", "Moving the cursor to the element ", element.GetAttribute("Name")));
                GetActionsInstance(driver).MoveToElement(element).Build().Perform();
            }
            catch (Exception ex)
            {
                TestReport.LogFailure(string.Format("{0}{1}", "Failed moving the cursor to the element", ex.Message));
            }
        }

        public static void MovetoElement(WindowsDriver<IWebElement> driver, IWebElement element, int x, int y, int loopCount = 20,
            int intervalToWaitMillSecs = 500)
        {
            try
            {
                IsElementExistsWithWait(element, loopCount, intervalToWaitMillSecs);
                TestReport.LogSuccess(string.Format("{0}{1}{2}", "Moving the cursor to the element '" + element.GetAttribute("Name") + "' using coordinates", x, y));
                GetActionsInstance(driver).MoveToElement(element, x, y).Build().Perform();
            }
            catch (Exception ex)
            {
                TestReport.LogFailure(string.Format("{0}{1}", "Failed moving the cursor to the element using coordinates", ex.Message));
            }
        }

        public static void ClickByMovetoElement(WindowsDriver<IWebElement> driver, IWebElement element, int x, int y, int loopCount = 20,
            int intervalToWaitMillSecs = 500)
        {
            try
            {
                IsElementExistsWithWait(element, loopCount, intervalToWaitMillSecs);
                TestReport.LogSuccess(string.Format("{0}{1}{2}", "performing click by moving the cursor to the element '" + element.GetAttribute("Name") + "' using coordinates", x, y));
                GetActionsInstance(driver).MoveToElement(element, x, y).Click().Build().Perform();
            }
            catch (Exception ex)
            {
                TestReport.LogFailure(string.Format("{0}{1}", "Failed performing click by moving the cursor to the element using coordinates", ex.Message));
            }
        }

        public static void ClickByMovetoElement(WindowsDriver<IWebElement> driver, IWebElement element, int loopCount = 20,
            int intervalToWaitMillSecs = 500)
        {
            try
            {
                IsElementExistsWithWait(element, loopCount, intervalToWaitMillSecs);
                TestReport.LogSuccess("performing click by moving the cursor to the element '" + element.GetAttribute("Name") + "'");
                GetActionsInstance(driver).MoveToElement(element).Click().Build().Perform();
            }
            catch (Exception ex)
            {
                TestReport.LogFailure(string.Format("{0}{1}", "Failed performing click by moving the cursor to the element '" + element.GetAttribute("Name") + "'", ex.Message));
            }
        }

        public static void SelectCheckBox(IWebElement element, int loopCount = 20, int intervalToWaitMillSecs = 500)
        {
            try
            {
                IsElementExistsWithWait(element, loopCount, intervalToWaitMillSecs);
                TestReport.LogSuccess("Selecting checkbox '" + element.GetAttribute("Name") + "'");
                if (!(element.Selected))
                    element.Click();
            }
            catch (Exception ex)
            {
                TestReport.LogFailure(string.Format("{0}{1}", "Failed selecting checkbox '" + element.GetAttribute("Name") + "'", ex.Message));
            }
        }

        public static void UnSelectCheckBox(IWebElement element, int loopCount = 20, int intervalToWaitMillSecs = 500)
        {
            try
            {
                IsElementExistsWithWait(element, loopCount, intervalToWaitMillSecs);
                TestReport.LogSuccess("UnSelecting checkbox '" + element.GetAttribute("Name") + "'");
                if (element.Selected)
                    element.Click();
            }
            catch (Exception ex)
            {
                TestReport.LogFailure(string.Format("{0}{1}", "Failed Unselecting checkbox '" + element.GetAttribute("Name") + "'", ex.Message));
            }
        }

        public static bool IsEnabled(IWebElement element, int loopCount = 20, int intervalToWaitMillSecs = 500)
        {
            bool status = false;
            try
            {
                IsElementExistsWithWait(element, loopCount, intervalToWaitMillSecs);
                TestReport.LogSuccess("Enabled status for '" + element.GetAttribute("Name") + "'");
                status = element.Enabled;
            }

            catch (Exception ex)
            {
                TestReport.LogFailure(string.Format("{0}{1}", "Failed to find element '" + element.GetAttribute("Name") + "'", ex.Message));
            }

            return status;
        }

        public static bool IsSelected(IWebElement element, int loopCount = 20, int intervalToWaitMillSecs = 500)
        {
            bool status = false;
            try
            {
                IsElementExistsWithWait(element, loopCount, intervalToWaitMillSecs);
                TestReport.LogSuccess("Selected status for '" + element.GetAttribute("Name") + "'");
                status = element.Selected;
            }

            catch (Exception ex)
            {
                TestReport.LogFailure(string.Format("{0}{1}", "Failed to find element '" + element.GetAttribute("Name") + "'", ex.Message));
            }

            return status;
        }

        public static void SelectValueInCombobox(IWebElement element, IWebElement elementToSelect, int loopCount = 20, int intervalToWaitMillSecs = 500)
        {
            try
            {
                IsElementExistsWithWait(element, loopCount, intervalToWaitMillSecs);
                TestReport.LogSuccess(string.Format("{0}{1}", "Selecting value ", elementToSelect.GetAttribute("Name"), element.GetAttribute("Name")));
                element.Click();
                elementToSelect.Click();
            }

            catch (Exception ex)
            {
                TestReport.LogSuccess(string.Format("{0}{1}{2}", "Failed selecting value ", elementToSelect.GetAttribute("Name"), element.GetAttribute("Name"), ex.Message));
            }
        }

        public static void ClickByActions(WindowsDriver<IWebElement> driver, IWebElement element, int loopCount = 20, int intervalToWaitMillSecs = 500)
        {
            try
            {
                IsElementExistsWithWait(element, loopCount, intervalToWaitMillSecs);
                TestReport.LogSuccess("Click element " + element.GetAttribute("Name") + " using action builder");
                GetActionsInstance(driver).Click(element).Build().Perform();
            }
            catch (Exception ex)
            {
                TestReport.LogFailure(string.Format("{0}{1}", "Failed to click element using action builder", ex.Message));
            }
        }

        public static void ClickByActions(WindowsDriver<IWebElement> driver)
        {
            try
            {
                TestReport.LogSuccess("perform click using action builder");
                GetActionsInstance(driver).Click().Build().Perform();
            }
            catch (Exception ex)
            {
                TestReport.LogFailure(string.Format("{0}{1}", "Failed to perform click using action builder", ex.Message));
            }
        }

        public static void DoubleClick(WindowsDriver<IWebElement> driver, IWebElement element, int loopCount = 20, int intervalToWaitMillSecs = 500)
        {
            try
            {
                IsElementExistsWithWait(element, loopCount, intervalToWaitMillSecs);
                TestReport.LogSuccess("Double Click element " + element.GetAttribute("Name") + "");
                GetActionsInstance(driver).DoubleClick(element).Build().Perform();
            }
            catch (Exception ex)
            {
                TestReport.LogFailure(string.Format("{0}{1}", "Failed to double click element", ex.Message));
            }
        }

        public static void RightClick(WindowsDriver<IWebElement> driver, IWebElement element, int loopCount = 20, int intervalToWaitMillSecs = 500)
        {
            try
            {
                IsElementExistsWithWait(element, loopCount, intervalToWaitMillSecs);
                TestReport.LogSuccess("Right Click element " + element.GetAttribute("Name") + "");
                GetActionsInstance(driver).ContextClick(element).Build().Perform();
            }
            catch (Exception ex)
            {
                TestReport.LogFailure(string.Format("{0}{1}", "Failed to right click element", ex.Message));
            }
        }

        private static Actions GetActionsInstance(WindowsDriver<IWebElement> driver) { return new Actions(driver); }
    }
}

No comments:

Post a Comment