Пример #1
0
 /// <summary>
 /// Clear the control cache
 /// </summary>
 public static void ClearCache()
 {
     ControlCache.ClearCache();
 }
Пример #2
0
        /// <summary>Instantiates a new TeamControlium Control as a child of the passed ParentControl. If no parent, Control is top-level.
        /// </summary>
        /// <param name="SeleniumDriver">Reference to TeamControlium Driver</param>
        /// <param name="ParentControl">Control the instantiated control is a child of. Can be null if new control is a top-level Control (IE. A function/pattern)</param>
        /// <param name="CustomRootElement">A dynamic Element the child control may be referenced from.  Can be null if child's find logic is from the parents root</param>
        /// <param name="NewControl">New control instance with find logic and friendly name set</param>
        /// <remarks>If control was sucessfully located, caching is not disabled and the control is cached, the cached reference is returned instead of the located.  If a cache miss the reference is
        /// added.</remarks>
        /// <returns>Reference to found control (or cached - see Remarks)</returns>
        public static T SetControl <T>(SeleniumDriver SeleniumDriver, ControlBase ParentControl, T NewControl) where T : ControlBase
        {
            Stopwatch stopWatch = Stopwatch.StartNew();

            try
            {
                Logger.WriteLine(Logger.LogLevels.TestInformation, "Setting on control [{0}] from parent [{1}]", NewControl == null ? "<No control>" : NewControl.Mapping?.FriendlyName ?? NewControl.Mapping?.FindLogic ?? "No find logic!!", (ParentControl == null) ? "<No parent - so top level>" : ParentControl.Mapping?.FriendlyName ?? ParentControl.Mapping?.FindLogic ?? "Paraent has no find logic!!");
                // Find the element - we can assume it will be good as any issue will have chucked an exception
                if ((ParentControl != null) && ParentControl.IsStale)
                {
                    Logger.WriteLine(Logger.LogLevels.TestInformation, "Parent control is stale. Refreshing");
                    ParentControl.RootElement = null;
                    ControlBase RefreshedParentControl = ControlBase.SetControl(ParentControl.SeleniumDriver, ParentControl.ParentControl, ParentControl);
                    ParentControl = RefreshedParentControl;
                }

                //
                // We may just be wrapping an Element in a Control that has already been found.  In which case, dont bother
                // to do a find for it....
                //
                if (NewControl?._RootElement.WebElement == null)
                {
                    Logger.WriteLine(Logger.LogLevels.TestDebug, $"New control Root is null (It has not yet been found), so finding (Find logic: [{NewControl?.Mapping?.FindLogic ?? "Null!!!"}]");
                    ControlFindElement finder = FindToUse(SeleniumDriver, ParentControl);

                    Element Element = FindControlRootElement(finder, NewControl.Mapping);

                    NewControl.RootElement = Element;
                }

                //
                // Populate new Control object.
                //
                NewControl.SeleniumDriver = SeleniumDriver;
                NewControl.ParentControl  = ParentControl; // This may be null.  So, new control is top level....

                // Put the control through the cache - either adds it to the cache (miss) or references the cache version (hit)
                // Inform the control we have performed a 'set' on it letting it know whether this is the first or subsequent times.  Note
                // that caching has been DISABLED for development of the framework purposes. Check will return false if the control EXISTS OR NOT!
                switch (ControlCache.Check(ref NewControl))
                {
                case ControlCache.ControlCacheStates.CacheHit:
                {
                    Logger.WriteLine(Logger.LogLevels.TestInformation, "Using cached control");
                    NewControl.ControlBeingSet(false);
                    break;
                }

                case ControlCache.ControlCacheStates.CacheMiss:
                {
                    Logger.WriteLine(Logger.LogLevels.TestInformation, "Control not cached - new control used");
                    NewControl.ControlBeingSet(true);
                    break;
                }

                case ControlCache.ControlCacheStates.CachedControlWasStale:
                {
                    Logger.WriteLine(Logger.LogLevels.TestInformation, "Control stale - all cached controls invalidated and new control used");
                    NewControl.ControlBeingSet(true);
                    break;
                }

                default:
                {
                    Logger.WriteLine(Logger.LogLevels.TestInformation, "Control caching disabled");
                    NewControl.ControlBeingSet(true);
                    break;
                }
                }
                return(NewControl);
            }
            catch (Exception ex)
            {
                // Be worth sticking a log in here
                throw ex;
            }
        }