コード例 #1
0
        public IEnumerable <INativeElement> GetElementsWithQuerySelector(ICssSelector selector, DomContainer domContainer)
        {
            var container = "document";

            if (_element != null)
            {
                var elementTag = new ElementTag(_element.TagName);
                if (elementTag.Equals(new ElementTag("frame")) || elementTag.Equals(new ElementTag("iframe")))
                {
                    var frameHierarchy = _element.GetAttributeValue("data-watinFrameHierarchy");
                    container = frameHierarchy + ".document";
                }
                else
                {
                    var document = _element.AsHtmlElement.document;
                    var result   = new Expando(document).GetValue <string>("___WATINFRAMEHIERARCHY");

                    container = "";
                    if (result != null)
                    {
                        container = result;
                    }

                    if (!string.IsNullOrEmpty(container))
                    {
                        container += ".";
                    }

                    container += _element.GetJavaScriptElementReference();
                }
            }
            else
            {
                //container = CreateArray();
            }

            domContainer.RunScript(new ScriptLoader().GetSizzleInstallScript());

            var code = string.Format("document.___WATINRESULT = Sizzle('{0}', {1});", selector.Selector(true), container);

            domContainer.RunScript(code);

            return(new JScriptElementArrayEnumerator((IEDocument)domContainer.NativeDocument, "___WATINRESULT"));
        }
コード例 #2
0
        public void SetValueWhenOnKeyPress(string eventName, NameValueCollection eventProperties)
        {
            if (eventName != "onKeyPress" || eventProperties == null)
            {
                return;
            }

            var keys = eventProperties.GetValues("keyCode");

            if (keys == null || keys.Length <= 0)
            {
                return;
            }

            var addChar  = keys[0];
            var newValue = _ieElement.GetAttributeValue("value") + ((char)int.Parse(addChar));

            _ieElement.SetAttributeValue("value", newValue);
        }
コード例 #3
0
        /// <summary>
        /// Fires the given event on the given element.
        /// </summary>
        /// <param name="eventName">Name of the event to fire</param>
        /// <param name="eventProperties">The event object properties.</param>
        public void FireEvent(string eventName, NameValueCollection eventProperties)
        {
            if (eventName == "onKeyPress" && eventProperties != null)
            {
                var keys = eventProperties.GetValues("keyCode");
                if (keys != null && keys.Length > 0)
                {
                    var addChar  = keys[0];
                    var newValue = _ieElement.GetAttributeValue("value") + ((char)int.Parse(addChar));
                    _ieElement.SetAttributeValue("value", newValue);
                }
            }

            var scriptCode = CreateJavaScriptFireEventCode(eventProperties, eventName);

            try
            {
                var window = _ieElement.ParentWindow;
                IEUtils.RunScript(scriptCode, window);
            }
            catch (RunScriptException)
            {
                // In a cross domain automation scenario a System.UnauthorizedAccessException
                // is thrown.  This code does cause the registered client event handlers to be fired
                // but it does not deliver the event to the control itself.  Consequently the
                // control state may need to be updated directly (eg. as with key press event).

                object prototypeEvent = null;
                object eventObj       = ((IHTMLDocument4)_ieElement.AsHtmlElement.document).CreateEventObject(ref prototypeEvent);

                for (var index = 0; index < eventProperties.Count; index++)
                {
                    var property = eventProperties.GetKey(index);
                    var value    = eventProperties.GetValues(index)[0];

                    ((IHTMLEventObj2)eventObj).setAttribute(property, value, 0);
                }

                _ieElement.AsDispHTMLBaseElement.FireEvent(eventName, ref eventObj);
            }
        }