Exemplo n.º 1
0
        private string GetCurrentElementXPath()
        {
            string jsScriptText =
                @"function createXPathFromElement(elm) {
                    var allNodes = document.getElementsByTagName('*');
                    for (var segs = []; elm && elm.nodeType === 1; elm = elm.parentNode) {
                        if (elm.hasAttribute('id')) {
                            var uniqueIdCount = 0;
                            for (var n = 0; n < allNodes.length; n++) {
                                if (allNodes[n].hasAttribute('id') && allNodes[n].id === elm.id) uniqueIdCount++;
                                if (uniqueIdCount > 1) break;
                            };
                            if (uniqueIdCount === 1) {
                                segs.unshift('//*[@id=\'' + elm.getAttribute('id') + '\']');
                                return segs.join('/');
                            }
                            else {
                                segs.unshift('//' + elm.localName.toLowerCase() + '[@id=\'' + elm.getAttribute('id') + '\']');
                            }
                        }
                        else {
                            for (i = 1, sib = elm.previousSibling; sib; sib = sib.previousSibling) {
                                if (sib.localName === elm.localName) i++;
                            };
                            segs.unshift(elm.localName.toLowerCase() + '[' + i + ']');
                        };
                    };
                    return segs.length ? '/' + segs.join('/') : null;
                };
                return createXPathFromElement(arguments[0])";

            return(JavaScriptService.Execute(jsScriptText, this));
        }
        internal void Hover(EventHandler <ComponentActionEventArgs> hovering, EventHandler <ComponentActionEventArgs> hovered)
        {
            hovering?.Invoke(this, new ComponentActionEventArgs(this));

            JavaScriptService.Execute("arguments[0].onmouseover();", this);

            hovered?.Invoke(this, new ComponentActionEventArgs(this));
        }
        protected virtual void DefaultSetAttribute(string attributeName, string attributeValue)
        {
            SettingAttribute?.Invoke(this, new ComponentActionEventArgs(this));

            JavaScriptService.Execute(
                $"arguments[0].setAttribute('{attributeName}', '{attributeValue}');", this);

            AttributeSet?.Invoke(this, new ComponentActionEventArgs(this));
        }
Exemplo n.º 4
0
        internal void Click(EventHandler <ElementActionEventArgs> clicking, EventHandler <ElementActionEventArgs> clicked)
        {
            clicking?.Invoke(this, new ElementActionEventArgs(this));

            this.ToExists().ToBeClickable().WaitToBe();
            JavaScriptService.Execute("arguments[0].focus();arguments[0].click();", this);

            clicked?.Invoke(this, new ElementActionEventArgs(this));
        }
        internal void Click(EventHandler <ComponentActionEventArgs> clicking, EventHandler <ComponentActionEventArgs> clicked)
        {
            clicking?.Invoke(this, new ComponentActionEventArgs(this));

            var sleepInterval         = ConfigurationService.GetSection <WebSettings>().TimeoutSettings.SleepInterval;
            var timeout               = ConfigurationService.GetSection <WebSettings>().TimeoutSettings.ElementToBeClickableTimeout;
            var timeoutTimeSpan       = TimeSpan.FromSeconds(timeout);
            var sleepIntervalTimeSpan = TimeSpan.FromSeconds(sleepInterval);
            var wait = new WebDriverWait(new SystemClock(), WrappedDriver, timeoutTimeSpan, sleepIntervalTimeSpan);

            wait.IgnoreExceptionTypes(typeof(NoSuchElementException));

            try
            {
                wait.Until((s) =>
                {
                    try
                    {
                        this.ToExists().ToBeClickable().WaitToBe();
                        WrappedElement.Click();
                        return(true);
                    }
                    catch (ElementNotInteractableException e)
                    {
                        return(false);
                    }
                    catch (WebDriverTimeoutException e)
                    {
                        return(false);
                    }
                    catch (Exception e)
                    {
                        return(false);
                    }
                });
            }
            catch (Exception e)
            {
                JavaScriptService.Execute("arguments[0].focus();arguments[0].click();", this);
            }

            clicked?.Invoke(this, new ComponentActionEventArgs(this));
        }
        private void ScrollToVisible(bool shouldWait = true)
        {
            ScrollingToVisible?.Invoke(this, new ComponentActionEventArgs(this));
            try
            {
                var wrappedElement = _wrappedElement ?? WrappedElement;
                JavaScriptService.Execute("arguments[0].scrollIntoView(true);", wrappedElement);
                if (shouldWait)
                {
                    Thread.Sleep(500);
                    this.ToExists().WaitToBe();
                }
            }
            catch (ElementNotInteractableException)
            {
                // ignore
            }

            ScrolledToVisible?.Invoke(this, new ComponentActionEventArgs(this));
        }
Exemplo n.º 7
0
        private string InvokeScript(string scriptToInvoke)
        {
            JavaScriptService javaScriptService = new JavaScriptService(WrappedDriver);

            return(javaScriptService.Execute(scriptToInvoke)?.ToString());
        }
Exemplo n.º 8
0
 private void PerformJsClick() => JavaScriptService.Execute("arguments[0].focus();arguments[0].click();", this);